ServiceWorkerContainer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "ServiceWorkerContainer.h"
  6. #include "nsContentUtils.h"
  7. #include "nsIDocument.h"
  8. #include "nsIServiceWorkerManager.h"
  9. #include "nsIURL.h"
  10. #include "nsNetUtil.h"
  11. #include "nsPIDOMWindow.h"
  12. #include "mozilla/Preferences.h"
  13. #include "mozilla/Services.h"
  14. #include "nsCycleCollectionParticipant.h"
  15. #include "nsServiceManagerUtils.h"
  16. #include "mozilla/dom/Navigator.h"
  17. #include "mozilla/dom/Promise.h"
  18. #include "mozilla/dom/ServiceWorkerContainerBinding.h"
  19. #include "mozilla/dom/workers/bindings/ServiceWorker.h"
  20. #include "ServiceWorker.h"
  21. namespace mozilla {
  22. namespace dom {
  23. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorkerContainer)
  24. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  25. NS_IMPL_ADDREF_INHERITED(ServiceWorkerContainer, DOMEventTargetHelper)
  26. NS_IMPL_RELEASE_INHERITED(ServiceWorkerContainer, DOMEventTargetHelper)
  27. NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerContainer, DOMEventTargetHelper,
  28. mControllerWorker, mReadyPromise)
  29. /* static */ bool
  30. ServiceWorkerContainer::IsEnabled(JSContext* aCx, JSObject* aGlobal)
  31. {
  32. MOZ_ASSERT(NS_IsMainThread());
  33. JS::Rooted<JSObject*> global(aCx, aGlobal);
  34. nsCOMPtr<nsPIDOMWindowInner> window = Navigator::GetWindowFromGlobal(global);
  35. if (!window) {
  36. return false;
  37. }
  38. nsIDocument* doc = window->GetExtantDoc();
  39. if (!doc || nsContentUtils::IsInPrivateBrowsing(doc)) {
  40. return false;
  41. }
  42. return Preferences::GetBool("dom.serviceWorkers.enabled", false);
  43. }
  44. ServiceWorkerContainer::ServiceWorkerContainer(nsPIDOMWindowInner* aWindow)
  45. : DOMEventTargetHelper(aWindow)
  46. {
  47. }
  48. ServiceWorkerContainer::~ServiceWorkerContainer()
  49. {
  50. RemoveReadyPromise();
  51. }
  52. void
  53. ServiceWorkerContainer::DisconnectFromOwner()
  54. {
  55. mControllerWorker = nullptr;
  56. RemoveReadyPromise();
  57. DOMEventTargetHelper::DisconnectFromOwner();
  58. }
  59. void
  60. ServiceWorkerContainer::ControllerChanged(ErrorResult& aRv)
  61. {
  62. mControllerWorker = nullptr;
  63. aRv = DispatchTrustedEvent(NS_LITERAL_STRING("controllerchange"));
  64. }
  65. void
  66. ServiceWorkerContainer::RemoveReadyPromise()
  67. {
  68. if (nsCOMPtr<nsPIDOMWindowInner> window = GetOwner()) {
  69. nsCOMPtr<nsIServiceWorkerManager> swm =
  70. mozilla::services::GetServiceWorkerManager();
  71. if (!swm) {
  72. // If the browser is shutting down, we don't need to remove the promise.
  73. return;
  74. }
  75. swm->RemoveReadyPromise(window);
  76. }
  77. }
  78. JSObject*
  79. ServiceWorkerContainer::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  80. {
  81. return ServiceWorkerContainerBinding::Wrap(aCx, this, aGivenProto);
  82. }
  83. static nsresult
  84. CheckForSlashEscapedCharsInPath(nsIURI* aURI)
  85. {
  86. MOZ_ASSERT(aURI);
  87. // A URL that can't be downcast to a standard URL is an invalid URL and should
  88. // be treated as such and fail with SecurityError.
  89. nsCOMPtr<nsIURL> url(do_QueryInterface(aURI));
  90. if (NS_WARN_IF(!url)) {
  91. return NS_ERROR_DOM_SECURITY_ERR;
  92. }
  93. nsAutoCString path;
  94. nsresult rv = url->GetFilePath(path);
  95. if (NS_WARN_IF(NS_FAILED(rv))) {
  96. return rv;
  97. }
  98. ToLowerCase(path);
  99. if (path.Find("%2f") != kNotFound ||
  100. path.Find("%5c") != kNotFound) {
  101. return NS_ERROR_DOM_TYPE_ERR;
  102. }
  103. return NS_OK;
  104. }
  105. already_AddRefed<Promise>
  106. ServiceWorkerContainer::Register(const nsAString& aScriptURL,
  107. const RegistrationOptions& aOptions,
  108. ErrorResult& aRv)
  109. {
  110. nsCOMPtr<nsISupports> promise;
  111. nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager();
  112. if (!swm) {
  113. aRv.Throw(NS_ERROR_FAILURE);
  114. return nullptr;
  115. }
  116. nsCOMPtr<nsIURI> baseURI;
  117. nsIDocument* doc = GetEntryDocument();
  118. if (doc) {
  119. baseURI = doc->GetBaseURI();
  120. } else {
  121. // XXXnsm. One of our devtools browser test calls register() from a content
  122. // script where there is no valid entry document. Use the window to resolve
  123. // the uri in that case.
  124. nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  125. nsCOMPtr<nsPIDOMWindowOuter> outerWindow;
  126. if (window && (outerWindow = window->GetOuterWindow()) &&
  127. outerWindow->GetServiceWorkersTestingEnabled()) {
  128. baseURI = window->GetDocBaseURI();
  129. }
  130. }
  131. nsresult rv;
  132. nsCOMPtr<nsIURI> scriptURI;
  133. rv = NS_NewURI(getter_AddRefs(scriptURI), aScriptURL, nullptr, baseURI);
  134. if (NS_WARN_IF(NS_FAILED(rv))) {
  135. aRv.ThrowTypeError<MSG_INVALID_URL>(aScriptURL);
  136. return nullptr;
  137. }
  138. aRv = CheckForSlashEscapedCharsInPath(scriptURI);
  139. if (NS_WARN_IF(aRv.Failed())) {
  140. return nullptr;
  141. }
  142. // In ServiceWorkerContainer.register() the scope argument is parsed against
  143. // different base URLs depending on whether it was passed or not.
  144. nsCOMPtr<nsIURI> scopeURI;
  145. // Step 4. If none passed, parse against script's URL
  146. if (!aOptions.mScope.WasPassed()) {
  147. NS_NAMED_LITERAL_STRING(defaultScope, "./");
  148. rv = NS_NewURI(getter_AddRefs(scopeURI), defaultScope,
  149. nullptr, scriptURI);
  150. if (NS_WARN_IF(NS_FAILED(rv))) {
  151. nsAutoCString spec;
  152. scriptURI->GetSpec(spec);
  153. NS_ConvertUTF8toUTF16 wSpec(spec);
  154. aRv.ThrowTypeError<MSG_INVALID_SCOPE>(defaultScope, wSpec);
  155. return nullptr;
  156. }
  157. } else {
  158. // Step 5. Parse against entry settings object's base URL.
  159. rv = NS_NewURI(getter_AddRefs(scopeURI), aOptions.mScope.Value(),
  160. nullptr, baseURI);
  161. if (NS_WARN_IF(NS_FAILED(rv))) {
  162. nsIURI* uri = baseURI ? baseURI : scriptURI;
  163. nsAutoCString spec;
  164. uri->GetSpec(spec);
  165. NS_ConvertUTF8toUTF16 wSpec(spec);
  166. aRv.ThrowTypeError<MSG_INVALID_SCOPE>(aOptions.mScope.Value(), wSpec);
  167. return nullptr;
  168. }
  169. aRv = CheckForSlashEscapedCharsInPath(scopeURI);
  170. if (NS_WARN_IF(aRv.Failed())) {
  171. return nullptr;
  172. }
  173. }
  174. // The spec says that the "client" passed to Register() must be the global
  175. // where the ServiceWorkerContainer was retrieved from.
  176. aRv = swm->Register(GetOwner(), scopeURI, scriptURI, getter_AddRefs(promise));
  177. if (NS_WARN_IF(aRv.Failed())) {
  178. return nullptr;
  179. }
  180. RefPtr<Promise> ret = static_cast<Promise*>(promise.get());
  181. MOZ_ASSERT(ret);
  182. return ret.forget();
  183. }
  184. already_AddRefed<workers::ServiceWorker>
  185. ServiceWorkerContainer::GetController()
  186. {
  187. if (!mControllerWorker) {
  188. nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager();
  189. if (!swm) {
  190. return nullptr;
  191. }
  192. // TODO: What should we do here if the ServiceWorker script fails to load?
  193. // In theory the DOM ServiceWorker object can exist without the worker
  194. // thread running, but it seems our design does not expect that.
  195. nsCOMPtr<nsISupports> serviceWorker;
  196. nsresult rv = swm->GetDocumentController(GetOwner(),
  197. getter_AddRefs(serviceWorker));
  198. if (NS_WARN_IF(NS_FAILED(rv))) {
  199. return nullptr;
  200. }
  201. mControllerWorker =
  202. static_cast<workers::ServiceWorker*>(serviceWorker.get());
  203. }
  204. RefPtr<workers::ServiceWorker> ref = mControllerWorker;
  205. return ref.forget();
  206. }
  207. already_AddRefed<Promise>
  208. ServiceWorkerContainer::GetRegistrations(ErrorResult& aRv)
  209. {
  210. nsresult rv;
  211. nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
  212. if (NS_WARN_IF(NS_FAILED(rv))) {
  213. aRv.Throw(rv);
  214. return nullptr;
  215. }
  216. nsCOMPtr<nsISupports> promise;
  217. aRv = swm->GetRegistrations(GetOwner(), getter_AddRefs(promise));
  218. if (aRv.Failed()) {
  219. return nullptr;
  220. }
  221. RefPtr<Promise> ret = static_cast<Promise*>(promise.get());
  222. MOZ_ASSERT(ret);
  223. return ret.forget();
  224. }
  225. already_AddRefed<Promise>
  226. ServiceWorkerContainer::GetRegistration(const nsAString& aDocumentURL,
  227. ErrorResult& aRv)
  228. {
  229. nsresult rv;
  230. nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
  231. if (NS_WARN_IF(NS_FAILED(rv))) {
  232. aRv.Throw(rv);
  233. return nullptr;
  234. }
  235. nsCOMPtr<nsISupports> promise;
  236. aRv = swm->GetRegistration(GetOwner(), aDocumentURL, getter_AddRefs(promise));
  237. if (aRv.Failed()) {
  238. return nullptr;
  239. }
  240. RefPtr<Promise> ret = static_cast<Promise*>(promise.get());
  241. MOZ_ASSERT(ret);
  242. return ret.forget();
  243. }
  244. Promise*
  245. ServiceWorkerContainer::GetReady(ErrorResult& aRv)
  246. {
  247. if (mReadyPromise) {
  248. return mReadyPromise;
  249. }
  250. nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager();
  251. if (!swm) {
  252. aRv.Throw(NS_ERROR_FAILURE);
  253. return nullptr;
  254. }
  255. nsCOMPtr<nsISupports> promise;
  256. aRv = swm->GetReadyPromise(GetOwner(), getter_AddRefs(promise));
  257. mReadyPromise = static_cast<Promise*>(promise.get());
  258. return mReadyPromise;
  259. }
  260. // Testing only.
  261. void
  262. ServiceWorkerContainer::GetScopeForUrl(const nsAString& aUrl,
  263. nsString& aScope,
  264. ErrorResult& aRv)
  265. {
  266. nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager();
  267. if (!swm) {
  268. aRv.Throw(NS_ERROR_FAILURE);
  269. return;
  270. }
  271. nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  272. if (NS_WARN_IF(!window)) {
  273. aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
  274. return;
  275. }
  276. nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
  277. if (NS_WARN_IF(!doc)) {
  278. aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
  279. return;
  280. }
  281. aRv = swm->GetScopeForUrl(doc->NodePrincipal(),
  282. aUrl, aScope);
  283. }
  284. } // namespace dom
  285. } // namespace mozilla