ServiceWorkerManagerParent.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "ServiceWorkerManagerParent.h"
  6. #include "ServiceWorkerManagerService.h"
  7. #include "mozilla/AppProcessChecker.h"
  8. #include "mozilla/dom/ContentParent.h"
  9. #include "mozilla/dom/ServiceWorkerRegistrar.h"
  10. #include "mozilla/ipc/BackgroundParent.h"
  11. #include "mozilla/ipc/BackgroundUtils.h"
  12. #include "mozilla/Unused.h"
  13. #include "nsThreadUtils.h"
  14. namespace mozilla {
  15. using namespace ipc;
  16. namespace dom {
  17. namespace workers {
  18. namespace {
  19. uint64_t sServiceWorkerManagerParentID = 0;
  20. class RegisterServiceWorkerCallback final : public Runnable
  21. {
  22. public:
  23. RegisterServiceWorkerCallback(const ServiceWorkerRegistrationData& aData,
  24. uint64_t aParentID)
  25. : mData(aData)
  26. , mParentID(aParentID)
  27. {
  28. AssertIsInMainProcess();
  29. AssertIsOnBackgroundThread();
  30. }
  31. NS_IMETHOD
  32. Run() override
  33. {
  34. AssertIsInMainProcess();
  35. AssertIsOnBackgroundThread();
  36. RefPtr<dom::ServiceWorkerRegistrar> service =
  37. dom::ServiceWorkerRegistrar::Get();
  38. // Shutdown during the process of trying to update the registrar. Give
  39. // up on this modification.
  40. if (!service) {
  41. return NS_OK;
  42. }
  43. service->RegisterServiceWorker(mData);
  44. RefPtr<ServiceWorkerManagerService> managerService =
  45. ServiceWorkerManagerService::Get();
  46. if (managerService) {
  47. managerService->PropagateRegistration(mParentID, mData);
  48. }
  49. return NS_OK;
  50. }
  51. private:
  52. ServiceWorkerRegistrationData mData;
  53. const uint64_t mParentID;
  54. };
  55. class UnregisterServiceWorkerCallback final : public Runnable
  56. {
  57. public:
  58. UnregisterServiceWorkerCallback(const PrincipalInfo& aPrincipalInfo,
  59. const nsString& aScope,
  60. uint64_t aParentID)
  61. : mPrincipalInfo(aPrincipalInfo)
  62. , mScope(aScope)
  63. , mParentID(aParentID)
  64. {
  65. AssertIsInMainProcess();
  66. AssertIsOnBackgroundThread();
  67. }
  68. NS_IMETHOD
  69. Run() override
  70. {
  71. AssertIsInMainProcess();
  72. AssertIsOnBackgroundThread();
  73. RefPtr<dom::ServiceWorkerRegistrar> service =
  74. dom::ServiceWorkerRegistrar::Get();
  75. // Shutdown during the process of trying to update the registrar. Give
  76. // up on this modification.
  77. if (!service) {
  78. return NS_OK;
  79. }
  80. service->UnregisterServiceWorker(mPrincipalInfo,
  81. NS_ConvertUTF16toUTF8(mScope));
  82. RefPtr<ServiceWorkerManagerService> managerService =
  83. ServiceWorkerManagerService::Get();
  84. if (managerService) {
  85. managerService->PropagateUnregister(mParentID, mPrincipalInfo,
  86. mScope);
  87. }
  88. return NS_OK;
  89. }
  90. private:
  91. const PrincipalInfo mPrincipalInfo;
  92. nsString mScope;
  93. uint64_t mParentID;
  94. };
  95. class CheckPrincipalWithCallbackRunnable final : public Runnable
  96. {
  97. public:
  98. CheckPrincipalWithCallbackRunnable(already_AddRefed<ContentParent> aParent,
  99. const PrincipalInfo& aPrincipalInfo,
  100. Runnable* aCallback)
  101. : mContentParent(aParent)
  102. , mPrincipalInfo(aPrincipalInfo)
  103. , mCallback(aCallback)
  104. , mBackgroundThread(NS_GetCurrentThread())
  105. {
  106. AssertIsInMainProcess();
  107. AssertIsOnBackgroundThread();
  108. MOZ_ASSERT(mContentParent);
  109. MOZ_ASSERT(mCallback);
  110. MOZ_ASSERT(mBackgroundThread);
  111. }
  112. NS_IMETHOD Run() override
  113. {
  114. if (NS_IsMainThread()) {
  115. nsCOMPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(mPrincipalInfo);
  116. AssertAppPrincipal(mContentParent, principal);
  117. mContentParent = nullptr;
  118. mBackgroundThread->Dispatch(this, NS_DISPATCH_NORMAL);
  119. return NS_OK;
  120. }
  121. AssertIsOnBackgroundThread();
  122. mCallback->Run();
  123. mCallback = nullptr;
  124. return NS_OK;
  125. }
  126. private:
  127. RefPtr<ContentParent> mContentParent;
  128. PrincipalInfo mPrincipalInfo;
  129. RefPtr<Runnable> mCallback;
  130. nsCOMPtr<nsIThread> mBackgroundThread;
  131. };
  132. } // namespace
  133. ServiceWorkerManagerParent::ServiceWorkerManagerParent()
  134. : mService(ServiceWorkerManagerService::GetOrCreate())
  135. , mID(++sServiceWorkerManagerParentID)
  136. {
  137. AssertIsOnBackgroundThread();
  138. mService->RegisterActor(this);
  139. }
  140. ServiceWorkerManagerParent::~ServiceWorkerManagerParent()
  141. {
  142. AssertIsOnBackgroundThread();
  143. }
  144. bool
  145. ServiceWorkerManagerParent::RecvRegister(
  146. const ServiceWorkerRegistrationData& aData)
  147. {
  148. AssertIsInMainProcess();
  149. AssertIsOnBackgroundThread();
  150. // Basic validation.
  151. if (aData.scope().IsEmpty() ||
  152. aData.principal().type() == PrincipalInfo::TNullPrincipalInfo ||
  153. aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) {
  154. return false;
  155. }
  156. RefPtr<RegisterServiceWorkerCallback> callback =
  157. new RegisterServiceWorkerCallback(aData, mID);
  158. RefPtr<ContentParent> parent =
  159. BackgroundParent::GetContentParent(Manager());
  160. // If the ContentParent is null we are dealing with a same-process actor.
  161. if (!parent) {
  162. callback->Run();
  163. return true;
  164. }
  165. RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
  166. new CheckPrincipalWithCallbackRunnable(parent.forget(), aData.principal(),
  167. callback);
  168. MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
  169. return true;
  170. }
  171. bool
  172. ServiceWorkerManagerParent::RecvUnregister(const PrincipalInfo& aPrincipalInfo,
  173. const nsString& aScope)
  174. {
  175. AssertIsInMainProcess();
  176. AssertIsOnBackgroundThread();
  177. // Basic validation.
  178. if (aScope.IsEmpty() ||
  179. aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo ||
  180. aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
  181. return false;
  182. }
  183. RefPtr<UnregisterServiceWorkerCallback> callback =
  184. new UnregisterServiceWorkerCallback(aPrincipalInfo, aScope, mID);
  185. RefPtr<ContentParent> parent =
  186. BackgroundParent::GetContentParent(Manager());
  187. // If the ContentParent is null we are dealing with a same-process actor.
  188. if (!parent) {
  189. callback->Run();
  190. return true;
  191. }
  192. RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
  193. new CheckPrincipalWithCallbackRunnable(parent.forget(), aPrincipalInfo,
  194. callback);
  195. MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
  196. return true;
  197. }
  198. bool
  199. ServiceWorkerManagerParent::RecvPropagateSoftUpdate(const PrincipalOriginAttributes& aOriginAttributes,
  200. const nsString& aScope)
  201. {
  202. AssertIsOnBackgroundThread();
  203. if (NS_WARN_IF(!mService)) {
  204. return false;
  205. }
  206. mService->PropagateSoftUpdate(mID, aOriginAttributes, aScope);
  207. return true;
  208. }
  209. bool
  210. ServiceWorkerManagerParent::RecvPropagateUnregister(const PrincipalInfo& aPrincipalInfo,
  211. const nsString& aScope)
  212. {
  213. AssertIsOnBackgroundThread();
  214. if (NS_WARN_IF(!mService)) {
  215. return false;
  216. }
  217. mService->PropagateUnregister(mID, aPrincipalInfo, aScope);
  218. return true;
  219. }
  220. bool
  221. ServiceWorkerManagerParent::RecvPropagateRemove(const nsCString& aHost)
  222. {
  223. AssertIsOnBackgroundThread();
  224. if (NS_WARN_IF(!mService)) {
  225. return false;
  226. }
  227. mService->PropagateRemove(mID, aHost);
  228. return true;
  229. }
  230. bool
  231. ServiceWorkerManagerParent::RecvPropagateRemoveAll()
  232. {
  233. AssertIsOnBackgroundThread();
  234. if (NS_WARN_IF(!mService)) {
  235. return false;
  236. }
  237. mService->PropagateRemoveAll(mID);
  238. return true;
  239. }
  240. bool
  241. ServiceWorkerManagerParent::RecvShutdown()
  242. {
  243. AssertIsOnBackgroundThread();
  244. if (NS_WARN_IF(!mService)) {
  245. return false;
  246. }
  247. mService->UnregisterActor(this);
  248. mService = nullptr;
  249. Unused << Send__delete__(this);
  250. return true;
  251. }
  252. void
  253. ServiceWorkerManagerParent::ActorDestroy(ActorDestroyReason aWhy)
  254. {
  255. AssertIsOnBackgroundThread();
  256. if (mService) {
  257. // This object is about to be released and with it, also mService will be
  258. // released too.
  259. mService->UnregisterActor(this);
  260. }
  261. }
  262. } // namespace workers
  263. } // namespace dom
  264. } // namespace mozilla