ServiceWorkerScriptCache.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 "ServiceWorkerScriptCache.h"
  6. #include "mozilla/Unused.h"
  7. #include "mozilla/dom/CacheBinding.h"
  8. #include "mozilla/dom/cache/CacheStorage.h"
  9. #include "mozilla/dom/cache/Cache.h"
  10. #include "mozilla/dom/Promise.h"
  11. #include "mozilla/dom/PromiseWorkerProxy.h"
  12. #include "mozilla/dom/ScriptLoader.h"
  13. #include "mozilla/ipc/BackgroundUtils.h"
  14. #include "mozilla/ipc/PBackgroundSharedTypes.h"
  15. #include "nsICacheInfoChannel.h"
  16. #include "nsIHttpChannelInternal.h"
  17. #include "nsIStreamLoader.h"
  18. #include "nsIThreadRetargetableRequest.h"
  19. #include "nsIInputStreamPump.h"
  20. #include "nsIPrincipal.h"
  21. #include "nsIScriptError.h"
  22. #include "nsContentUtils.h"
  23. #include "nsNetUtil.h"
  24. #include "ServiceWorkerManager.h"
  25. #include "Workers.h"
  26. #include "nsStringStream.h"
  27. using mozilla::dom::cache::Cache;
  28. using mozilla::dom::cache::CacheStorage;
  29. BEGIN_WORKERS_NAMESPACE
  30. namespace serviceWorkerScriptCache {
  31. namespace {
  32. // XXX A sandbox nsIGlobalObject does not preserve its reflector, so |aSandbox|
  33. // must be kept alive as long as the CacheStorage if you want to ensure that
  34. // the CacheStorage will continue to work. Failures will manifest as errors
  35. // like "JavaScript error: , line 0: TypeError: The expression cannot be
  36. // converted to return the specified type."
  37. already_AddRefed<CacheStorage>
  38. CreateCacheStorage(JSContext* aCx, nsIPrincipal* aPrincipal, ErrorResult& aRv,
  39. JS::MutableHandle<JSObject*> aSandbox)
  40. {
  41. AssertIsOnMainThread();
  42. MOZ_ASSERT(aPrincipal);
  43. nsIXPConnect* xpc = nsContentUtils::XPConnect();
  44. MOZ_ASSERT(xpc, "This should never be null!");
  45. aRv = xpc->CreateSandbox(aCx, aPrincipal, aSandbox.address());
  46. if (NS_WARN_IF(aRv.Failed())) {
  47. return nullptr;
  48. }
  49. nsCOMPtr<nsIGlobalObject> sandboxGlobalObject = xpc::NativeGlobal(aSandbox);
  50. if (!sandboxGlobalObject) {
  51. aRv.Throw(NS_ERROR_FAILURE);
  52. return nullptr;
  53. }
  54. // We assume private browsing is not enabled here. The ScriptLoader
  55. // explicitly fails for private browsing so there should never be
  56. // a service worker running in private browsing mode. Therefore if
  57. // we are purging scripts or running a comparison algorithm we cannot
  58. // be in private browing.
  59. //
  60. // Also, bypass the CacheStorage trusted origin checks. The ServiceWorker
  61. // has validated the origin prior to this point. All the information
  62. // to revalidate is not available now.
  63. return CacheStorage::CreateOnMainThread(cache::CHROME_ONLY_NAMESPACE,
  64. sandboxGlobalObject, aPrincipal,
  65. false /* private browsing */,
  66. true /* force trusted origin */,
  67. aRv);
  68. }
  69. class CompareManager;
  70. // This class downloads a URL from the network and then it calls
  71. // NetworkFinished() in the CompareManager.
  72. class CompareNetwork final : public nsIStreamLoaderObserver,
  73. public nsIRequestObserver
  74. {
  75. public:
  76. NS_DECL_ISUPPORTS
  77. NS_DECL_NSISTREAMLOADEROBSERVER
  78. NS_DECL_NSIREQUESTOBSERVER
  79. explicit CompareNetwork(CompareManager* aManager)
  80. : mManager(aManager)
  81. {
  82. MOZ_ASSERT(aManager);
  83. AssertIsOnMainThread();
  84. }
  85. nsresult
  86. Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL, nsILoadGroup* aLoadGroup);
  87. void
  88. Abort()
  89. {
  90. AssertIsOnMainThread();
  91. MOZ_ASSERT(mChannel);
  92. mChannel->Cancel(NS_BINDING_ABORTED);
  93. mChannel = nullptr;
  94. }
  95. const nsString& Buffer() const
  96. {
  97. AssertIsOnMainThread();
  98. return mBuffer;
  99. }
  100. private:
  101. ~CompareNetwork()
  102. {
  103. AssertIsOnMainThread();
  104. }
  105. RefPtr<CompareManager> mManager;
  106. nsCOMPtr<nsIChannel> mChannel;
  107. nsString mBuffer;
  108. };
  109. NS_IMPL_ISUPPORTS(CompareNetwork, nsIStreamLoaderObserver,
  110. nsIRequestObserver)
  111. // This class gets a cached Response from the CacheStorage and then it calls
  112. // CacheFinished() in the CompareManager.
  113. class CompareCache final : public PromiseNativeHandler
  114. , public nsIStreamLoaderObserver
  115. {
  116. public:
  117. NS_DECL_ISUPPORTS
  118. NS_DECL_NSISTREAMLOADEROBSERVER
  119. explicit CompareCache(CompareManager* aManager)
  120. : mManager(aManager)
  121. , mState(WaitingForCache)
  122. , mAborted(false)
  123. {
  124. MOZ_ASSERT(aManager);
  125. AssertIsOnMainThread();
  126. }
  127. nsresult
  128. Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL,
  129. const nsAString& aCacheName);
  130. void
  131. Abort()
  132. {
  133. AssertIsOnMainThread();
  134. MOZ_ASSERT(!mAborted);
  135. mAborted = true;
  136. if (mPump) {
  137. mPump->Cancel(NS_BINDING_ABORTED);
  138. mPump = nullptr;
  139. }
  140. }
  141. // This class manages 2 promises: 1 is to retrieve cache object, and 2 is for
  142. // the value from the cache. For this reason we have mState to know what
  143. // reject/resolve callback we are handling.
  144. virtual void
  145. ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
  146. {
  147. AssertIsOnMainThread();
  148. if (mAborted) {
  149. return;
  150. }
  151. if (mState == WaitingForCache) {
  152. ManageCacheResult(aCx, aValue);
  153. return;
  154. }
  155. MOZ_ASSERT(mState == WaitingForValue);
  156. ManageValueResult(aCx, aValue);
  157. }
  158. virtual void
  159. RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
  160. const nsString& Buffer() const
  161. {
  162. AssertIsOnMainThread();
  163. return mBuffer;
  164. }
  165. const nsString& URL() const
  166. {
  167. AssertIsOnMainThread();
  168. return mURL;
  169. }
  170. private:
  171. ~CompareCache()
  172. {
  173. AssertIsOnMainThread();
  174. }
  175. void
  176. ManageCacheResult(JSContext* aCx, JS::Handle<JS::Value> aValue);
  177. void
  178. ManageValueResult(JSContext* aCx, JS::Handle<JS::Value> aValue);
  179. RefPtr<CompareManager> mManager;
  180. nsCOMPtr<nsIInputStreamPump> mPump;
  181. nsString mURL;
  182. nsString mBuffer;
  183. enum {
  184. WaitingForCache,
  185. WaitingForValue
  186. } mState;
  187. bool mAborted;
  188. };
  189. NS_IMPL_ISUPPORTS(CompareCache, nsIStreamLoaderObserver)
  190. class CompareManager final : public PromiseNativeHandler
  191. {
  192. public:
  193. NS_DECL_ISUPPORTS
  194. explicit CompareManager(ServiceWorkerRegistrationInfo* aRegistration,
  195. CompareCallback* aCallback)
  196. : mRegistration(aRegistration)
  197. , mCallback(aCallback)
  198. , mState(WaitingForOpen)
  199. , mNetworkFinished(false)
  200. , mCacheFinished(false)
  201. , mInCache(false)
  202. {
  203. AssertIsOnMainThread();
  204. MOZ_ASSERT(aRegistration);
  205. }
  206. nsresult
  207. Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL,
  208. const nsAString& aCacheName, nsILoadGroup* aLoadGroup)
  209. {
  210. AssertIsOnMainThread();
  211. MOZ_ASSERT(aPrincipal);
  212. mURL = aURL;
  213. // Always create a CacheStorage since we want to write the network entry to
  214. // the cache even if there isn't an existing one.
  215. AutoJSAPI jsapi;
  216. jsapi.Init();
  217. ErrorResult result;
  218. mSandbox.init(jsapi.cx());
  219. mCacheStorage = CreateCacheStorage(jsapi.cx(), aPrincipal, result, &mSandbox);
  220. if (NS_WARN_IF(result.Failed())) {
  221. MOZ_ASSERT(!result.IsErrorWithMessage());
  222. Cleanup();
  223. return result.StealNSResult();
  224. }
  225. mCN = new CompareNetwork(this);
  226. nsresult rv = mCN->Initialize(aPrincipal, aURL, aLoadGroup);
  227. if (NS_WARN_IF(NS_FAILED(rv))) {
  228. Cleanup();
  229. return rv;
  230. }
  231. if (!aCacheName.IsEmpty()) {
  232. mCC = new CompareCache(this);
  233. rv = mCC->Initialize(aPrincipal, aURL, aCacheName);
  234. if (NS_WARN_IF(NS_FAILED(rv))) {
  235. mCN->Abort();
  236. Cleanup();
  237. return rv;
  238. }
  239. }
  240. return NS_OK;
  241. }
  242. const nsString&
  243. URL() const
  244. {
  245. AssertIsOnMainThread();
  246. return mURL;
  247. }
  248. void
  249. SetMaxScope(const nsACString& aMaxScope)
  250. {
  251. MOZ_ASSERT(!mNetworkFinished);
  252. mMaxScope = aMaxScope;
  253. }
  254. already_AddRefed<ServiceWorkerRegistrationInfo>
  255. GetRegistration()
  256. {
  257. RefPtr<ServiceWorkerRegistrationInfo> copy = mRegistration.get();
  258. return copy.forget();
  259. }
  260. void
  261. NetworkFinished(nsresult aStatus)
  262. {
  263. AssertIsOnMainThread();
  264. mNetworkFinished = true;
  265. if (NS_WARN_IF(NS_FAILED(aStatus))) {
  266. if (mCC) {
  267. mCC->Abort();
  268. }
  269. ComparisonFinished(aStatus, false);
  270. return;
  271. }
  272. MaybeCompare();
  273. }
  274. void
  275. CacheFinished(nsresult aStatus, bool aInCache)
  276. {
  277. AssertIsOnMainThread();
  278. mCacheFinished = true;
  279. mInCache = aInCache;
  280. if (NS_WARN_IF(NS_FAILED(aStatus))) {
  281. if (mCN) {
  282. mCN->Abort();
  283. }
  284. ComparisonFinished(aStatus, false);
  285. return;
  286. }
  287. MaybeCompare();
  288. }
  289. void
  290. MaybeCompare()
  291. {
  292. AssertIsOnMainThread();
  293. if (!mNetworkFinished || (mCC && !mCacheFinished)) {
  294. return;
  295. }
  296. if (!mCC || !mInCache) {
  297. ComparisonFinished(NS_OK, false);
  298. return;
  299. }
  300. ComparisonFinished(NS_OK, mCC->Buffer().Equals(mCN->Buffer()));
  301. }
  302. // This class manages 2 promises: 1 is to retrieve Cache object, and 2 is to
  303. // Put the value in the cache. For this reason we have mState to know what
  304. // callback we are handling.
  305. void
  306. ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
  307. {
  308. AssertIsOnMainThread();
  309. MOZ_ASSERT(mCallback);
  310. if (mState == WaitingForOpen) {
  311. if (NS_WARN_IF(!aValue.isObject())) {
  312. Fail(NS_ERROR_FAILURE);
  313. return;
  314. }
  315. JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
  316. if (NS_WARN_IF(!obj)) {
  317. Fail(NS_ERROR_FAILURE);
  318. return;
  319. }
  320. RefPtr<Cache> cache;
  321. nsresult rv = UNWRAP_OBJECT(Cache, obj, cache);
  322. if (NS_WARN_IF(NS_FAILED(rv))) {
  323. Fail(rv);
  324. return;
  325. }
  326. WriteToCache(cache);
  327. return;
  328. }
  329. MOZ_ASSERT(mState == WaitingForPut);
  330. mCallback->ComparisonResult(NS_OK, false /* aIsEqual */,
  331. mNewCacheName, mMaxScope);
  332. Cleanup();
  333. }
  334. void
  335. RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
  336. {
  337. AssertIsOnMainThread();
  338. if (mState == WaitingForOpen) {
  339. NS_WARNING("Could not open cache.");
  340. } else {
  341. NS_WARNING("Could not write to cache.");
  342. }
  343. Fail(NS_ERROR_FAILURE);
  344. }
  345. CacheStorage*
  346. CacheStorage_()
  347. {
  348. AssertIsOnMainThread();
  349. MOZ_ASSERT(mCacheStorage);
  350. return mCacheStorage;
  351. }
  352. void
  353. InitChannelInfo(nsIChannel* aChannel)
  354. {
  355. mChannelInfo.InitFromChannel(aChannel);
  356. }
  357. nsresult
  358. SetPrincipalInfo(nsIChannel* aChannel)
  359. {
  360. nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
  361. NS_ASSERTION(ssm, "Should never be null!");
  362. nsCOMPtr<nsIPrincipal> channelPrincipal;
  363. nsresult rv = ssm->GetChannelResultPrincipal(aChannel, getter_AddRefs(channelPrincipal));
  364. if (NS_WARN_IF(NS_FAILED(rv))) {
  365. return rv;
  366. }
  367. UniquePtr<mozilla::ipc::PrincipalInfo> principalInfo(new mozilla::ipc::PrincipalInfo());
  368. rv = PrincipalToPrincipalInfo(channelPrincipal, principalInfo.get());
  369. if (NS_WARN_IF(NS_FAILED(rv))) {
  370. return rv;
  371. }
  372. mPrincipalInfo = Move(principalInfo);
  373. return NS_OK;
  374. }
  375. private:
  376. ~CompareManager()
  377. {
  378. AssertIsOnMainThread();
  379. MOZ_ASSERT(!mCC);
  380. MOZ_ASSERT(!mCN);
  381. }
  382. void
  383. Fail(nsresult aStatus)
  384. {
  385. AssertIsOnMainThread();
  386. mCallback->ComparisonResult(aStatus, false /* aIsEqual */,
  387. EmptyString(), EmptyCString());
  388. Cleanup();
  389. }
  390. void
  391. Cleanup()
  392. {
  393. AssertIsOnMainThread();
  394. MOZ_ASSERT(mCallback);
  395. mCallback = nullptr;
  396. mCN = nullptr;
  397. mCC = nullptr;
  398. }
  399. void
  400. ComparisonFinished(nsresult aStatus, bool aIsEqual)
  401. {
  402. AssertIsOnMainThread();
  403. MOZ_ASSERT(mCallback);
  404. if (NS_WARN_IF(NS_FAILED(aStatus))) {
  405. Fail(aStatus);
  406. return;
  407. }
  408. if (aIsEqual) {
  409. mCallback->ComparisonResult(aStatus, aIsEqual, EmptyString(), mMaxScope);
  410. Cleanup();
  411. return;
  412. }
  413. // Write to Cache so ScriptLoader reads succeed.
  414. WriteNetworkBufferToNewCache();
  415. }
  416. void
  417. WriteNetworkBufferToNewCache()
  418. {
  419. AssertIsOnMainThread();
  420. MOZ_ASSERT(mCN);
  421. MOZ_ASSERT(mCacheStorage);
  422. MOZ_ASSERT(mNewCacheName.IsEmpty());
  423. ErrorResult result;
  424. result = serviceWorkerScriptCache::GenerateCacheName(mNewCacheName);
  425. if (NS_WARN_IF(result.Failed())) {
  426. MOZ_ASSERT(!result.IsErrorWithMessage());
  427. Fail(result.StealNSResult());
  428. return;
  429. }
  430. RefPtr<Promise> cacheOpenPromise = mCacheStorage->Open(mNewCacheName, result);
  431. if (NS_WARN_IF(result.Failed())) {
  432. MOZ_ASSERT(!result.IsErrorWithMessage());
  433. Fail(result.StealNSResult());
  434. return;
  435. }
  436. cacheOpenPromise->AppendNativeHandler(this);
  437. }
  438. void
  439. WriteToCache(Cache* aCache)
  440. {
  441. AssertIsOnMainThread();
  442. MOZ_ASSERT(aCache);
  443. MOZ_ASSERT(mState == WaitingForOpen);
  444. ErrorResult result;
  445. nsCOMPtr<nsIInputStream> body;
  446. result = NS_NewCStringInputStream(getter_AddRefs(body),
  447. NS_ConvertUTF16toUTF8(mCN->Buffer()));
  448. if (NS_WARN_IF(result.Failed())) {
  449. MOZ_ASSERT(!result.IsErrorWithMessage());
  450. Fail(result.StealNSResult());
  451. return;
  452. }
  453. RefPtr<InternalResponse> ir =
  454. new InternalResponse(200, NS_LITERAL_CSTRING("OK"));
  455. ir->SetBody(body, mCN->Buffer().Length());
  456. ir->InitChannelInfo(mChannelInfo);
  457. if (mPrincipalInfo) {
  458. ir->SetPrincipalInfo(Move(mPrincipalInfo));
  459. }
  460. RefPtr<Response> response = new Response(aCache->GetGlobalObject(), ir, nullptr);
  461. RequestOrUSVString request;
  462. request.SetAsUSVString().Rebind(URL().Data(), URL().Length());
  463. // For now we have to wait until the Put Promise is fulfilled before we can
  464. // continue since Cache does not yet support starting a read that is being
  465. // written to.
  466. RefPtr<Promise> cachePromise = aCache->Put(request, *response, result);
  467. if (NS_WARN_IF(result.Failed())) {
  468. MOZ_ASSERT(!result.IsErrorWithMessage());
  469. Fail(result.StealNSResult());
  470. return;
  471. }
  472. mState = WaitingForPut;
  473. cachePromise->AppendNativeHandler(this);
  474. }
  475. RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
  476. RefPtr<CompareCallback> mCallback;
  477. JS::PersistentRooted<JSObject*> mSandbox;
  478. RefPtr<CacheStorage> mCacheStorage;
  479. RefPtr<CompareNetwork> mCN;
  480. RefPtr<CompareCache> mCC;
  481. nsString mURL;
  482. // Only used if the network script has changed and needs to be cached.
  483. nsString mNewCacheName;
  484. ChannelInfo mChannelInfo;
  485. UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
  486. nsCString mMaxScope;
  487. enum {
  488. WaitingForOpen,
  489. WaitingForPut
  490. } mState;
  491. bool mNetworkFinished;
  492. bool mCacheFinished;
  493. bool mInCache;
  494. };
  495. NS_IMPL_ISUPPORTS0(CompareManager)
  496. nsresult
  497. CompareNetwork::Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL, nsILoadGroup* aLoadGroup)
  498. {
  499. MOZ_ASSERT(aPrincipal);
  500. AssertIsOnMainThread();
  501. nsCOMPtr<nsIURI> uri;
  502. nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, nullptr);
  503. if (NS_WARN_IF(NS_FAILED(rv))) {
  504. return rv;
  505. }
  506. nsCOMPtr<nsILoadGroup> loadGroup;
  507. rv = NS_NewLoadGroup(getter_AddRefs(loadGroup), aPrincipal);
  508. if (NS_WARN_IF(NS_FAILED(rv))) {
  509. return rv;
  510. }
  511. nsLoadFlags flags = nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
  512. RefPtr<ServiceWorkerRegistrationInfo> registration =
  513. mManager->GetRegistration();
  514. if (registration->IsLastUpdateCheckTimeOverOneDay()) {
  515. flags |= nsIRequest::LOAD_BYPASS_CACHE;
  516. }
  517. // Note that because there is no "serviceworker" RequestContext type, we can
  518. // use the TYPE_INTERNAL_SCRIPT content policy types when loading a service
  519. // worker.
  520. rv = NS_NewChannel(getter_AddRefs(mChannel),
  521. uri, aPrincipal,
  522. nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
  523. nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER,
  524. loadGroup,
  525. nullptr, // aCallbacks
  526. flags);
  527. if (NS_WARN_IF(NS_FAILED(rv))) {
  528. return rv;
  529. }
  530. nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
  531. if (httpChannel) {
  532. // Spec says no redirects allowed for SW scripts.
  533. httpChannel->SetRedirectionLimit(0);
  534. httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Service-Worker"),
  535. NS_LITERAL_CSTRING("script"),
  536. /* merge */ false);
  537. }
  538. nsCOMPtr<nsIStreamLoader> loader;
  539. rv = NS_NewStreamLoader(getter_AddRefs(loader), this, this);
  540. if (NS_WARN_IF(NS_FAILED(rv))) {
  541. return rv;
  542. }
  543. rv = mChannel->AsyncOpen2(loader);
  544. if (NS_WARN_IF(NS_FAILED(rv))) {
  545. return rv;
  546. }
  547. return NS_OK;
  548. }
  549. NS_IMETHODIMP
  550. CompareNetwork::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
  551. {
  552. AssertIsOnMainThread();
  553. // If no channel, Abort() has been called.
  554. if (!mChannel) {
  555. return NS_OK;
  556. }
  557. #ifdef DEBUG
  558. nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
  559. MOZ_ASSERT(channel == mChannel);
  560. #endif
  561. mManager->InitChannelInfo(mChannel);
  562. nsresult rv = mManager->SetPrincipalInfo(mChannel);
  563. if (NS_WARN_IF(NS_FAILED(rv))) {
  564. return rv;
  565. }
  566. return NS_OK;
  567. }
  568. NS_IMETHODIMP
  569. CompareNetwork::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
  570. nsresult aStatusCode)
  571. {
  572. // Nothing to do here!
  573. return NS_OK;
  574. }
  575. NS_IMETHODIMP
  576. CompareNetwork::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
  577. nsresult aStatus, uint32_t aLen,
  578. const uint8_t* aString)
  579. {
  580. AssertIsOnMainThread();
  581. // If no channel, Abort() has been called.
  582. if (!mChannel) {
  583. return NS_OK;
  584. }
  585. if (NS_WARN_IF(NS_FAILED(aStatus))) {
  586. if (aStatus == NS_ERROR_REDIRECT_LOOP) {
  587. mManager->NetworkFinished(NS_ERROR_DOM_SECURITY_ERR);
  588. } else {
  589. mManager->NetworkFinished(aStatus);
  590. }
  591. return NS_OK;
  592. }
  593. nsCOMPtr<nsIRequest> request;
  594. nsresult rv = aLoader->GetRequest(getter_AddRefs(request));
  595. if (NS_WARN_IF(NS_FAILED(rv))) {
  596. mManager->NetworkFinished(rv);
  597. return NS_OK;
  598. }
  599. nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
  600. MOZ_ASSERT(httpChannel, "How come we don't have an HTTP channel?");
  601. bool requestSucceeded;
  602. rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
  603. if (NS_WARN_IF(NS_FAILED(rv))) {
  604. mManager->NetworkFinished(rv);
  605. return NS_OK;
  606. }
  607. if (NS_WARN_IF(!requestSucceeded)) {
  608. // Get the stringified numeric status code, not statusText which could be
  609. // something misleading like OK for a 404.
  610. uint32_t status = 0;
  611. httpChannel->GetResponseStatus(&status); // don't care if this fails, use 0.
  612. nsAutoString statusAsText;
  613. statusAsText.AppendInt(status);
  614. RefPtr<ServiceWorkerRegistrationInfo> registration = mManager->GetRegistration();
  615. ServiceWorkerManager::LocalizeAndReportToAllClients(
  616. registration->mScope, "ServiceWorkerRegisterNetworkError",
  617. nsTArray<nsString> { NS_ConvertUTF8toUTF16(registration->mScope),
  618. statusAsText, mManager->URL() });
  619. mManager->NetworkFinished(NS_ERROR_FAILURE);
  620. return NS_OK;
  621. }
  622. nsAutoCString maxScope;
  623. // Note: we explicitly don't check for the return value here, because the
  624. // absence of the header is not an error condition.
  625. Unused << httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Service-Worker-Allowed"),
  626. maxScope);
  627. mManager->SetMaxScope(maxScope);
  628. bool isFromCache = false;
  629. nsCOMPtr<nsICacheInfoChannel> cacheChannel(do_QueryInterface(httpChannel));
  630. if (cacheChannel) {
  631. cacheChannel->IsFromCache(&isFromCache);
  632. }
  633. // [9.2 Update]4.13, If response's cache state is not "local",
  634. // set registration's last update check time to the current time
  635. if (!isFromCache) {
  636. RefPtr<ServiceWorkerRegistrationInfo> registration =
  637. mManager->GetRegistration();
  638. registration->RefreshLastUpdateCheckTime();
  639. }
  640. nsAutoCString mimeType;
  641. rv = httpChannel->GetContentType(mimeType);
  642. if (NS_WARN_IF(NS_FAILED(rv))) {
  643. // We should only end up here if !mResponseHead in the channel. If headers
  644. // were received but no content type was specified, we'll be given
  645. // UNKNOWN_CONTENT_TYPE "application/x-unknown-content-type" and so fall
  646. // into the next case with its better error message.
  647. mManager->NetworkFinished(NS_ERROR_DOM_SECURITY_ERR);
  648. return rv;
  649. }
  650. if (!mimeType.LowerCaseEqualsLiteral("text/javascript") &&
  651. !mimeType.LowerCaseEqualsLiteral("application/x-javascript") &&
  652. !mimeType.LowerCaseEqualsLiteral("application/javascript")) {
  653. RefPtr<ServiceWorkerRegistrationInfo> registration = mManager->GetRegistration();
  654. ServiceWorkerManager::LocalizeAndReportToAllClients(
  655. registration->mScope, "ServiceWorkerRegisterMimeTypeError",
  656. nsTArray<nsString> { NS_ConvertUTF8toUTF16(registration->mScope),
  657. NS_ConvertUTF8toUTF16(mimeType), mManager->URL() });
  658. mManager->NetworkFinished(NS_ERROR_DOM_SECURITY_ERR);
  659. return rv;
  660. }
  661. char16_t* buffer = nullptr;
  662. size_t len = 0;
  663. rv = ScriptLoader::ConvertToUTF16(httpChannel, aString, aLen,
  664. NS_LITERAL_STRING("UTF-8"), nullptr,
  665. buffer, len);
  666. if (NS_WARN_IF(NS_FAILED(rv))) {
  667. mManager->NetworkFinished(rv);
  668. return rv;
  669. }
  670. mBuffer.Adopt(buffer, len);
  671. mManager->NetworkFinished(NS_OK);
  672. return NS_OK;
  673. }
  674. nsresult
  675. CompareCache::Initialize(nsIPrincipal* aPrincipal, const nsAString& aURL,
  676. const nsAString& aCacheName)
  677. {
  678. MOZ_ASSERT(aPrincipal);
  679. AssertIsOnMainThread();
  680. mURL = aURL;
  681. ErrorResult rv;
  682. RefPtr<Promise> promise = mManager->CacheStorage_()->Open(aCacheName, rv);
  683. if (NS_WARN_IF(rv.Failed())) {
  684. MOZ_ASSERT(!rv.IsErrorWithMessage());
  685. return rv.StealNSResult();
  686. }
  687. promise->AppendNativeHandler(this);
  688. return NS_OK;
  689. }
  690. NS_IMETHODIMP
  691. CompareCache::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
  692. nsresult aStatus, uint32_t aLen,
  693. const uint8_t* aString)
  694. {
  695. AssertIsOnMainThread();
  696. if (mAborted) {
  697. return aStatus;
  698. }
  699. if (NS_WARN_IF(NS_FAILED(aStatus))) {
  700. mManager->CacheFinished(aStatus, false);
  701. return aStatus;
  702. }
  703. char16_t* buffer = nullptr;
  704. size_t len = 0;
  705. nsresult rv = ScriptLoader::ConvertToUTF16(nullptr, aString, aLen,
  706. NS_LITERAL_STRING("UTF-8"),
  707. nullptr, buffer, len);
  708. if (NS_WARN_IF(NS_FAILED(rv))) {
  709. mManager->CacheFinished(rv, false);
  710. return rv;
  711. }
  712. mBuffer.Adopt(buffer, len);
  713. mManager->CacheFinished(NS_OK, true);
  714. return NS_OK;
  715. }
  716. void
  717. CompareCache::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
  718. {
  719. AssertIsOnMainThread();
  720. if (mAborted) {
  721. return;
  722. }
  723. mManager->CacheFinished(NS_ERROR_FAILURE, false);
  724. }
  725. void
  726. CompareCache::ManageCacheResult(JSContext* aCx, JS::Handle<JS::Value> aValue)
  727. {
  728. AssertIsOnMainThread();
  729. if (NS_WARN_IF(!aValue.isObject())) {
  730. mManager->CacheFinished(NS_ERROR_FAILURE, false);
  731. return;
  732. }
  733. JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
  734. Cache* cache = nullptr;
  735. nsresult rv = UNWRAP_OBJECT(Cache, &obj, cache);
  736. if (NS_WARN_IF(NS_FAILED(rv))) {
  737. mManager->CacheFinished(rv, false);
  738. return;
  739. }
  740. RequestOrUSVString request;
  741. request.SetAsUSVString().Rebind(mURL.Data(), mURL.Length());
  742. ErrorResult error;
  743. CacheQueryOptions params;
  744. RefPtr<Promise> promise = cache->Match(request, params, error);
  745. if (NS_WARN_IF(error.Failed())) {
  746. mManager->CacheFinished(error.StealNSResult(), false);
  747. return;
  748. }
  749. promise->AppendNativeHandler(this);
  750. mState = WaitingForValue;
  751. }
  752. void
  753. CompareCache::ManageValueResult(JSContext* aCx, JS::Handle<JS::Value> aValue)
  754. {
  755. AssertIsOnMainThread();
  756. // The cache returns undefined if the object is not stored.
  757. if (aValue.isUndefined()) {
  758. mManager->CacheFinished(NS_OK, false);
  759. return;
  760. }
  761. MOZ_ASSERT(aValue.isObject());
  762. JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
  763. Response* response = nullptr;
  764. nsresult rv = UNWRAP_OBJECT(Response, &obj, response);
  765. if (NS_WARN_IF(NS_FAILED(rv))) {
  766. mManager->CacheFinished(rv, false);
  767. return;
  768. }
  769. MOZ_ASSERT(response->Ok());
  770. nsCOMPtr<nsIInputStream> inputStream;
  771. response->GetBody(getter_AddRefs(inputStream));
  772. MOZ_ASSERT(inputStream);
  773. MOZ_ASSERT(!mPump);
  774. rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream);
  775. if (NS_WARN_IF(NS_FAILED(rv))) {
  776. mManager->CacheFinished(rv, false);
  777. return;
  778. }
  779. nsCOMPtr<nsIStreamLoader> loader;
  780. rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
  781. if (NS_WARN_IF(NS_FAILED(rv))) {
  782. mManager->CacheFinished(rv, false);
  783. return;
  784. }
  785. rv = mPump->AsyncRead(loader, nullptr);
  786. if (NS_WARN_IF(NS_FAILED(rv))) {
  787. mPump = nullptr;
  788. mManager->CacheFinished(rv, false);
  789. return;
  790. }
  791. nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(mPump);
  792. if (rr) {
  793. nsCOMPtr<nsIEventTarget> sts =
  794. do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
  795. rv = rr->RetargetDeliveryTo(sts);
  796. if (NS_WARN_IF(NS_FAILED(rv))) {
  797. mPump = nullptr;
  798. mManager->CacheFinished(rv, false);
  799. return;
  800. }
  801. }
  802. }
  803. } // namespace
  804. nsresult
  805. PurgeCache(nsIPrincipal* aPrincipal, const nsAString& aCacheName)
  806. {
  807. AssertIsOnMainThread();
  808. MOZ_ASSERT(aPrincipal);
  809. if (aCacheName.IsEmpty()) {
  810. return NS_OK;
  811. }
  812. AutoJSAPI jsapi;
  813. jsapi.Init();
  814. ErrorResult rv;
  815. JS::Rooted<JSObject*> sandboxObject(jsapi.cx());
  816. RefPtr<CacheStorage> cacheStorage = CreateCacheStorage(jsapi.cx(), aPrincipal, rv, &sandboxObject);
  817. if (NS_WARN_IF(rv.Failed())) {
  818. return rv.StealNSResult();
  819. }
  820. // We use the ServiceWorker scope as key for the cacheStorage.
  821. RefPtr<Promise> promise =
  822. cacheStorage->Delete(aCacheName, rv);
  823. if (NS_WARN_IF(rv.Failed())) {
  824. return rv.StealNSResult();
  825. }
  826. // We don't actually care about the result of the delete operation.
  827. return NS_OK;
  828. }
  829. nsresult
  830. GenerateCacheName(nsAString& aName)
  831. {
  832. nsresult rv;
  833. nsCOMPtr<nsIUUIDGenerator> uuidGenerator =
  834. do_GetService("@mozilla.org/uuid-generator;1", &rv);
  835. if (NS_WARN_IF(NS_FAILED(rv))) {
  836. return rv;
  837. }
  838. nsID id;
  839. rv = uuidGenerator->GenerateUUIDInPlace(&id);
  840. if (NS_WARN_IF(NS_FAILED(rv))) {
  841. return rv;
  842. }
  843. char chars[NSID_LENGTH];
  844. id.ToProvidedString(chars);
  845. // NSID_LENGTH counts the null terminator.
  846. aName.AssignASCII(chars, NSID_LENGTH - 1);
  847. return NS_OK;
  848. }
  849. nsresult
  850. Compare(ServiceWorkerRegistrationInfo* aRegistration,
  851. nsIPrincipal* aPrincipal, const nsAString& aCacheName,
  852. const nsAString& aURL, CompareCallback* aCallback,
  853. nsILoadGroup* aLoadGroup)
  854. {
  855. AssertIsOnMainThread();
  856. MOZ_ASSERT(aRegistration);
  857. MOZ_ASSERT(aPrincipal);
  858. MOZ_ASSERT(!aURL.IsEmpty());
  859. MOZ_ASSERT(aCallback);
  860. RefPtr<CompareManager> cm = new CompareManager(aRegistration, aCallback);
  861. nsresult rv = cm->Initialize(aPrincipal, aURL, aCacheName, aLoadGroup);
  862. if (NS_WARN_IF(NS_FAILED(rv))) {
  863. return rv;
  864. }
  865. return NS_OK;
  866. }
  867. } // namespace serviceWorkerScriptCache
  868. END_WORKERS_NAMESPACE