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/StaticPrefs_dom.h"
11 #include "mozilla/StorageAccess.h"
12 #include "nsCOMPtr.h"
13 #include "nsContentUtils.h"
14 #include "nsIChannel.h"
15 #include "ServiceWorkerManager.h"
16 #include "nsIPrincipal.h"
17 
18 namespace mozilla {
19 namespace dom {
20 
NS_IMPL_ISUPPORTS(ServiceWorkerInterceptController,nsINetworkInterceptController)21 NS_IMPL_ISUPPORTS(ServiceWorkerInterceptController,
22                   nsINetworkInterceptController)
23 
24 NS_IMETHODIMP
25 ServiceWorkerInterceptController::ShouldPrepareForIntercept(
26     nsIURI* aURI, nsIChannel* aChannel, bool* aShouldIntercept) {
27   *aShouldIntercept = false;
28 
29   nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
30 
31   RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
32 
33   // For subresource requests we base our decision solely on the client's
34   // controller value.  Any settings that would have blocked service worker
35   // access should have been set before the initial navigation created the
36   // window.
37   if (!nsContentUtils::IsNonSubresourceRequest(aChannel)) {
38     const Maybe<ServiceWorkerDescriptor>& controller =
39         loadInfo->GetController();
40     // If the controller doesn't handle fetch events, return false
41     if (controller.isSome()) {
42       *aShouldIntercept = controller.ref().HandlesFetch();
43 
44       // The service worker has no fetch event handler, try to schedule a
45       // soft-update through ServiceWorkerRegistrationInfo.
46       // Get ServiceWorkerRegistrationInfo by the ServiceWorkerInfo's principal
47       // and scope
48       if (!*aShouldIntercept && swm) {
49         nsCOMPtr<nsIPrincipal> principal =
50             controller.ref().GetPrincipal().unwrap();
51         RefPtr<ServiceWorkerRegistrationInfo> registration =
52             swm->GetRegistration(principal, controller.ref().Scope());
53         // Could not get ServiceWorkerRegistration here if unregister is
54         // executed before getting here.
55         if (NS_WARN_IF(!registration)) {
56           return NS_OK;
57         }
58         registration->MaybeScheduleTimeCheckAndUpdate();
59       }
60     } else {
61       *aShouldIntercept = false;
62     }
63     return NS_OK;
64   }
65 
66   nsCOMPtr<nsIPrincipal> principal = BasePrincipal::CreateContentPrincipal(
67       aURI, loadInfo->GetOriginAttributes());
68 
69   // First check with the ServiceWorkerManager for a matching service worker.
70   if (!swm || !swm->IsAvailable(principal, aURI, aChannel)) {
71     return NS_OK;
72   }
73 
74   // Check if we're in a secure context, unless service worker testing is
75   // enabled.
76   if (!nsContentUtils::ComputeIsSecureContext(aChannel) &&
77       !StaticPrefs::dom_serviceWorkers_testing_enabled()) {
78     return NS_OK;
79   }
80 
81   // Then check to see if we are allowed to control the window.
82   // It is important to check for the availability of the service worker first
83   // to avoid showing warnings about the use of third-party cookies in the UI
84   // unnecessarily when no service worker is being accessed.
85   if (StorageAllowedForChannel(aChannel) != StorageAccess::eAllow) {
86     return NS_OK;
87   }
88 
89   *aShouldIntercept = true;
90   return NS_OK;
91 }
92 
93 NS_IMETHODIMP
ChannelIntercepted(nsIInterceptedChannel * aChannel)94 ServiceWorkerInterceptController::ChannelIntercepted(
95     nsIInterceptedChannel* aChannel) {
96   // Note, do not cancel the interception here.  The caller will try to
97   // ResetInterception() on error.
98 
99   RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
100   if (!swm) {
101     return NS_ERROR_FAILURE;
102   }
103 
104   ErrorResult error;
105   swm->DispatchFetchEvent(aChannel, error);
106   if (NS_WARN_IF(error.Failed())) {
107     return error.StealNSResult();
108   }
109 
110   return NS_OK;
111 }
112 
113 }  // namespace dom
114 }  // namespace mozilla
115