ServiceWorkerManagerParent.cpp (3757B)
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 "ServiceWorkerManagerParent.h" 8 9 #include "ServiceWorkerUtils.h" 10 #include "mozilla/dom/ContentParent.h" 11 #include "mozilla/dom/ServiceWorkerRegistrar.h" 12 #include "mozilla/ipc/BackgroundParent.h" 13 #include "mozilla/ipc/BackgroundUtils.h" 14 #include "nsThreadUtils.h" 15 16 namespace mozilla { 17 18 using namespace ipc; 19 20 namespace dom { 21 22 ServiceWorkerManagerParent::ServiceWorkerManagerParent() { 23 AssertIsOnBackgroundThread(); 24 } 25 26 ServiceWorkerManagerParent::~ServiceWorkerManagerParent() { 27 AssertIsOnBackgroundThread(); 28 } 29 30 mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvRegister( 31 const ServiceWorkerRegistrationData& aData) { 32 AssertIsInMainProcess(); 33 AssertIsOnBackgroundThread(); 34 MOZ_ASSERT(!BackgroundParent::IsOtherProcessActor(Manager())); 35 36 // Basic validation. 37 if (aData.scope().IsEmpty() || 38 aData.principal().type() == PrincipalInfo::TNullPrincipalInfo || 39 aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) { 40 return IPC_FAIL_NO_REASON(this); 41 } 42 43 // If false then we have shutdown during the process of trying to update the 44 // registrar. We can give up on this modification. 45 if (const RefPtr<dom::ServiceWorkerRegistrar> service = 46 dom::ServiceWorkerRegistrar::Get()) { 47 service->RegisterServiceWorker(aData); 48 } 49 50 return IPC_OK(); 51 } 52 53 mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvUnregister( 54 const PrincipalInfo& aPrincipalInfo, const nsString& aScope) { 55 AssertIsInMainProcess(); 56 AssertIsOnBackgroundThread(); 57 MOZ_ASSERT(!BackgroundParent::IsOtherProcessActor(Manager())); 58 59 // Basic validation. 60 if (aScope.IsEmpty() || 61 aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo || 62 aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) { 63 return IPC_FAIL_NO_REASON(this); 64 } 65 66 // If false then we have shutdown during the process of trying to update the 67 // registrar. We can give up on this modification. 68 if (const RefPtr<dom::ServiceWorkerRegistrar> service = 69 dom::ServiceWorkerRegistrar::Get()) { 70 service->UnregisterServiceWorker(aPrincipalInfo, 71 NS_ConvertUTF16toUTF8(aScope)); 72 } 73 74 return IPC_OK(); 75 } 76 77 mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateUnregister( 78 const PrincipalInfo& aPrincipalInfo, const nsString& aScope) { 79 AssertIsOnBackgroundThread(); 80 81 RefPtr<dom::ServiceWorkerRegistrar> service = 82 dom::ServiceWorkerRegistrar::Get(); 83 MOZ_ASSERT(service); 84 85 // It's possible that we don't have any ServiceWorkerManager managing this 86 // scope but we still need to unregister it from the ServiceWorkerRegistrar. 87 service->UnregisterServiceWorker(aPrincipalInfo, 88 NS_ConvertUTF16toUTF8(aScope)); 89 90 // There is no longer any point to propagating because the only sender is the 91 // one and only ServiceWorkerManager, but it is necessary for us to have run 92 // the unregister call above because until Bug 1183245 is fixed, 93 // nsIServiceWorkerManager.propagateUnregister() is a de facto API for 94 // clearing ServiceWorker registrations by Sanitizer.sys.mjs via 95 // ServiceWorkerCleanUp.sys.mjs, as well as devtools "unregister" affordance 96 // and the no-longer-relevant about:serviceworkers UI. 97 98 return IPC_OK(); 99 } 100 101 void ServiceWorkerManagerParent::ActorDestroy(ActorDestroyReason aWhy) { 102 AssertIsOnBackgroundThread(); 103 } 104 105 } // namespace dom 106 } // namespace mozilla