ServiceWorkerUtils.h (4530B)
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 #ifndef _mozilla_dom_ServiceWorkerUtils_h 7 #define _mozilla_dom_ServiceWorkerUtils_h 8 9 #include "mozilla/MozPromise.h" 10 #include "mozilla/dom/DOMTypes.h" 11 #include "mozilla/dom/IPCNavigationPreloadState.h" 12 #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h" 13 #include "nsTArray.h" 14 15 class nsIGlobalObject; 16 class nsIURI; 17 18 namespace mozilla { 19 20 class CopyableErrorResult; 21 class ErrorResult; 22 23 namespace dom { 24 25 class Client; 26 class ClientInfo; 27 class ClientInfoAndState; 28 class ServiceWorkerRegistrationData; 29 class ServiceWorkerRegistrationDescriptor; 30 struct NavigationPreloadState; 31 32 using ServiceWorkerRegistrationPromise = 33 MozPromise<ServiceWorkerRegistrationDescriptor, CopyableErrorResult, false>; 34 35 using ServiceWorkerRegistrationListPromise = 36 MozPromise<CopyableTArray<ServiceWorkerRegistrationDescriptor>, 37 CopyableErrorResult, false>; 38 39 using NavigationPreloadStatePromise = 40 MozPromise<IPCNavigationPreloadState, CopyableErrorResult, false>; 41 42 using ServiceWorkerRegistrationCallback = 43 std::function<void(const ServiceWorkerRegistrationDescriptor&)>; 44 45 using ServiceWorkerRegistrationListCallback = 46 std::function<void(const nsTArray<ServiceWorkerRegistrationDescriptor>&)>; 47 48 using ServiceWorkerBoolCallback = std::function<void(bool)>; 49 50 using ServiceWorkerFailureCallback = std::function<void(ErrorResult&&)>; 51 52 using NavigationPreloadGetStateCallback = 53 std::function<void(NavigationPreloadState&&)>; 54 55 bool ServiceWorkerRegistrationDataIsValid( 56 const ServiceWorkerRegistrationData& aData); 57 58 // Performs key spec validation steps of 59 // https://w3c.github.io/ServiceWorker/#start-register-algorithm and 60 // https://w3c.github.io/ServiceWorker/#register-algorithm as well as CSP 61 // validation corresponding to 62 // https://w3c.github.io/webappsec-csp/#directive-worker-src. 63 // 64 // This is extracted out of ServiceWorkerContainer::Register because we validate 65 // both in the content process as the site of the call, as well as in the parent 66 // process in the ServiceWorkerManager. 67 // 68 // On worker threads, this will involve use of a syncloop until Bug 1901387 is 69 // addressed, allowing us to call CheckMayLoad off main-thread (OMT). 70 // 71 // A global may be optionally provided for reporting purposes; this is desired 72 // when this is used by ServiceWorkerContainer::Register but not necessary in 73 // the parent process. 74 void ServiceWorkerScopeAndScriptAreValid( 75 const ClientInfo& aClientInfo, nsIURI* aScopeURI, nsIURI* aScriptURI, 76 ErrorResult& aRv, nsIGlobalObject* aGlobalForReporting = nullptr); 77 78 // WebIDL enabling function; this does *not* consider the StorageAccess value 79 // for the global, just interface exposure. 80 bool ServiceWorkersEnabled(JSContext* aCx, JSObject* aGlobal); 81 82 // Perform a StorageAccess policy check for whether ServiceWorkers should work 83 // in this global / be able to communicate with ServiceWorkers from this global. 84 // 85 // Note that this check should not directly be used for assertions; callers need 86 // to ensure that about:blank and Blob URL globals that are defined to inherit 87 // controllers pass the assertion check. This is to handle situations like 88 // those bug 1441133 where a global is controlled when storage access is 89 // granted to the origin, then the storage access is revoked, and then a global 90 // is created that would inherit the controller. 91 // 92 // Also note that StorageAccess.h defines a function 93 // `StorageAllowedForServiceWorker` which is a lower level function akin to 94 // `StorageAllowedForWindow` that helps determine the approprioate 95 // `StorageAccess` value for a new global that has a principal but not a channel 96 // or window available. This method is downstream of those calls and depends 97 // on the `StorageAccess` value they compute. 98 bool ServiceWorkersStorageAllowedForGlobal(nsIGlobalObject* aGlobal); 99 100 // Perform a StorageAccess policy check for whether the given Client has 101 // appropriate StorageAccess to be exposed to the Clients API. 102 // 103 // Note that Window Clients lose storage access when they become not fully 104 // active. 105 bool ServiceWorkersStorageAllowedForClient( 106 const ClientInfoAndState& aInfoAndState); 107 108 } // namespace dom 109 } // namespace mozilla 110 111 #endif // _mozilla_dom_ServiceWorkerUtils_h