CacheOpChild.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "mozilla/dom/cache/CacheOpChild.h"
  6. #include "mozilla/dom/Promise.h"
  7. #include "mozilla/dom/Request.h"
  8. #include "mozilla/dom/Response.h"
  9. #include "mozilla/dom/cache/Cache.h"
  10. #include "mozilla/dom/cache/CacheChild.h"
  11. #include "mozilla/dom/cache/CacheStreamControlChild.h"
  12. namespace mozilla {
  13. namespace dom {
  14. namespace cache {
  15. using mozilla::ipc::PBackgroundChild;
  16. namespace {
  17. void
  18. AddWorkerHolderToStreamChild(const CacheReadStream& aReadStream,
  19. CacheWorkerHolder* aWorkerHolder)
  20. {
  21. MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
  22. CacheStreamControlChild* cacheControl =
  23. static_cast<CacheStreamControlChild*>(aReadStream.controlChild());
  24. if (cacheControl) {
  25. cacheControl->SetWorkerHolder(aWorkerHolder);
  26. }
  27. }
  28. void
  29. AddWorkerHolderToStreamChild(const CacheResponse& aResponse,
  30. CacheWorkerHolder* aWorkerHolder)
  31. {
  32. MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
  33. if (aResponse.body().type() == CacheReadStreamOrVoid::Tvoid_t) {
  34. return;
  35. }
  36. AddWorkerHolderToStreamChild(aResponse.body().get_CacheReadStream(),
  37. aWorkerHolder);
  38. }
  39. void
  40. AddWorkerHolderToStreamChild(const CacheRequest& aRequest,
  41. CacheWorkerHolder* aWorkerHolder)
  42. {
  43. MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
  44. if (aRequest.body().type() == CacheReadStreamOrVoid::Tvoid_t) {
  45. return;
  46. }
  47. AddWorkerHolderToStreamChild(aRequest.body().get_CacheReadStream(),
  48. aWorkerHolder);
  49. }
  50. } // namespace
  51. CacheOpChild::CacheOpChild(CacheWorkerHolder* aWorkerHolder,
  52. nsIGlobalObject* aGlobal,
  53. nsISupports* aParent, Promise* aPromise)
  54. : mGlobal(aGlobal)
  55. , mParent(aParent)
  56. , mPromise(aPromise)
  57. {
  58. MOZ_DIAGNOSTIC_ASSERT(mGlobal);
  59. MOZ_DIAGNOSTIC_ASSERT(mParent);
  60. MOZ_DIAGNOSTIC_ASSERT(mPromise);
  61. MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
  62. SetWorkerHolder(aWorkerHolder);
  63. }
  64. CacheOpChild::~CacheOpChild()
  65. {
  66. NS_ASSERT_OWNINGTHREAD(CacheOpChild);
  67. MOZ_DIAGNOSTIC_ASSERT(!mPromise);
  68. }
  69. void
  70. CacheOpChild::ActorDestroy(ActorDestroyReason aReason)
  71. {
  72. NS_ASSERT_OWNINGTHREAD(CacheOpChild);
  73. // If the actor was terminated for some unknown reason, then indicate the
  74. // operation is dead.
  75. if (mPromise) {
  76. mPromise->MaybeReject(NS_ERROR_FAILURE);
  77. mPromise = nullptr;
  78. }
  79. RemoveWorkerHolder();
  80. }
  81. bool
  82. CacheOpChild::Recv__delete__(const ErrorResult& aRv,
  83. const CacheOpResult& aResult)
  84. {
  85. NS_ASSERT_OWNINGTHREAD(CacheOpChild);
  86. if (NS_WARN_IF(aRv.Failed())) {
  87. MOZ_DIAGNOSTIC_ASSERT(aResult.type() == CacheOpResult::Tvoid_t);
  88. // TODO: Remove this const_cast (bug 1152078).
  89. // It is safe for now since this ErrorResult is handed off to us by IPDL
  90. // and is thrown into the trash afterwards.
  91. mPromise->MaybeReject(const_cast<ErrorResult&>(aRv));
  92. mPromise = nullptr;
  93. return true;
  94. }
  95. switch (aResult.type()) {
  96. case CacheOpResult::TCacheMatchResult:
  97. {
  98. HandleResponse(aResult.get_CacheMatchResult().responseOrVoid());
  99. break;
  100. }
  101. case CacheOpResult::TCacheMatchAllResult:
  102. {
  103. HandleResponseList(aResult.get_CacheMatchAllResult().responseList());
  104. break;
  105. }
  106. case CacheOpResult::TCachePutAllResult:
  107. {
  108. mPromise->MaybeResolveWithUndefined();
  109. break;
  110. }
  111. case CacheOpResult::TCacheDeleteResult:
  112. {
  113. mPromise->MaybeResolve(aResult.get_CacheDeleteResult().success());
  114. break;
  115. }
  116. case CacheOpResult::TCacheKeysResult:
  117. {
  118. HandleRequestList(aResult.get_CacheKeysResult().requestList());
  119. break;
  120. }
  121. case CacheOpResult::TStorageMatchResult:
  122. {
  123. HandleResponse(aResult.get_StorageMatchResult().responseOrVoid());
  124. break;
  125. }
  126. case CacheOpResult::TStorageHasResult:
  127. {
  128. mPromise->MaybeResolve(aResult.get_StorageHasResult().success());
  129. break;
  130. }
  131. case CacheOpResult::TStorageOpenResult:
  132. {
  133. auto actor = static_cast<CacheChild*>(
  134. aResult.get_StorageOpenResult().actorChild());
  135. // If we have a success status then we should have an actor. Gracefully
  136. // reject instead of crashing, though, if we get a nullptr here.
  137. MOZ_DIAGNOSTIC_ASSERT(actor);
  138. if (!actor) {
  139. ErrorResult status;
  140. status.ThrowTypeError<MSG_CACHE_OPEN_FAILED>();
  141. mPromise->MaybeReject(status);
  142. break;
  143. }
  144. actor->SetWorkerHolder(GetWorkerHolder());
  145. RefPtr<Cache> cache = new Cache(mGlobal, actor);
  146. mPromise->MaybeResolve(cache);
  147. break;
  148. }
  149. case CacheOpResult::TStorageDeleteResult:
  150. {
  151. mPromise->MaybeResolve(aResult.get_StorageDeleteResult().success());
  152. break;
  153. }
  154. case CacheOpResult::TStorageKeysResult:
  155. {
  156. mPromise->MaybeResolve(aResult.get_StorageKeysResult().keyList());
  157. break;
  158. }
  159. default:
  160. MOZ_CRASH("Unknown Cache op result type!");
  161. }
  162. mPromise = nullptr;
  163. return true;
  164. }
  165. void
  166. CacheOpChild::StartDestroy()
  167. {
  168. NS_ASSERT_OWNINGTHREAD(CacheOpChild);
  169. // Do not cancel on-going operations when WorkerHolder calls this. Instead,
  170. // keep the Worker alive until we are done.
  171. }
  172. nsIGlobalObject*
  173. CacheOpChild::GetGlobalObject() const
  174. {
  175. return mGlobal;
  176. }
  177. #ifdef DEBUG
  178. void
  179. CacheOpChild::AssertOwningThread() const
  180. {
  181. NS_ASSERT_OWNINGTHREAD(CacheOpChild);
  182. }
  183. #endif
  184. PBackgroundChild*
  185. CacheOpChild::GetIPCManager()
  186. {
  187. MOZ_CRASH("CacheOpChild does not implement TypeUtils::GetIPCManager()");
  188. }
  189. void
  190. CacheOpChild::HandleResponse(const CacheResponseOrVoid& aResponseOrVoid)
  191. {
  192. if (aResponseOrVoid.type() == CacheResponseOrVoid::Tvoid_t) {
  193. mPromise->MaybeResolveWithUndefined();
  194. return;
  195. }
  196. const CacheResponse& cacheResponse = aResponseOrVoid.get_CacheResponse();
  197. AddWorkerHolderToStreamChild(cacheResponse, GetWorkerHolder());
  198. RefPtr<Response> response = ToResponse(cacheResponse);
  199. mPromise->MaybeResolve(response);
  200. }
  201. void
  202. CacheOpChild::HandleResponseList(const nsTArray<CacheResponse>& aResponseList)
  203. {
  204. AutoTArray<RefPtr<Response>, 256> responses;
  205. responses.SetCapacity(aResponseList.Length());
  206. for (uint32_t i = 0; i < aResponseList.Length(); ++i) {
  207. AddWorkerHolderToStreamChild(aResponseList[i], GetWorkerHolder());
  208. responses.AppendElement(ToResponse(aResponseList[i]));
  209. }
  210. mPromise->MaybeResolve(responses);
  211. }
  212. void
  213. CacheOpChild::HandleRequestList(const nsTArray<CacheRequest>& aRequestList)
  214. {
  215. AutoTArray<RefPtr<Request>, 256> requests;
  216. requests.SetCapacity(aRequestList.Length());
  217. for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
  218. AddWorkerHolderToStreamChild(aRequestList[i], GetWorkerHolder());
  219. requests.AppendElement(ToRequest(aRequestList[i]));
  220. }
  221. mPromise->MaybeResolve(requests);
  222. }
  223. } // namespace cache
  224. } // namespace dom
  225. } // namespace mozilla