Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ServiceWorkerInterceptController.h"
8
9
#include "mozilla/BasePrincipal.h"
10
#include "mozilla/StorageAccess.h"
11
#include "nsContentUtils.h"
12
#include "nsIChannel.h"
13
#include "ServiceWorkerManager.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
NS_IMPL_ISUPPORTS(ServiceWorkerInterceptController,
19
nsINetworkInterceptController)
20
21
NS_IMETHODIMP
22
ServiceWorkerInterceptController::ShouldPrepareForIntercept(
23
nsIURI* aURI, nsIChannel* aChannel, bool* aShouldIntercept) {
24
*aShouldIntercept = false;
25
26
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
27
28
// For subresource requests we base our decision solely on the client's
29
// controller value. Any settings that would have blocked service worker
30
// access should have been set before the initial navigation created the
31
// window.
32
if (!nsContentUtils::IsNonSubresourceRequest(aChannel)) {
33
const Maybe<ServiceWorkerDescriptor>& controller =
34
loadInfo->GetController();
35
*aShouldIntercept = controller.isSome();
36
return NS_OK;
37
}
38
39
nsCOMPtr<nsIPrincipal> principal = BasePrincipal::CreateContentPrincipal(
40
aURI, loadInfo->GetOriginAttributes());
41
42
// First check with the ServiceWorkerManager for a matching service worker.
43
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
44
if (!swm || !swm->IsAvailable(principal, aURI)) {
45
return NS_OK;
46
}
47
48
// Then check to see if we are allowed to control the window.
49
// It is important to check for the availability of the service worker first
50
// to avoid showing warnings about the use of third-party cookies in the UI
51
// unnecessarily when no service worker is being accessed.
52
if (StorageAllowedForChannel(aChannel) != StorageAccess::eAllow) {
53
return NS_OK;
54
}
55
56
*aShouldIntercept = true;
57
return NS_OK;
58
}
59
60
NS_IMETHODIMP
61
ServiceWorkerInterceptController::ChannelIntercepted(
62
nsIInterceptedChannel* aChannel) {
63
// Note, do not cancel the interception here. The caller will try to
64
// ResetInterception() on error.
65
66
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
67
if (!swm) {
68
return NS_ERROR_FAILURE;
69
}
70
71
ErrorResult error;
72
swm->DispatchFetchEvent(aChannel, error);
73
if (NS_WARN_IF(error.Failed())) {
74
return error.StealNSResult();
75
}
76
77
return NS_OK;
78
}
79
80
} // namespace dom
81
} // namespace mozilla