CacheStorageParent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/CacheStorageParent.h"
  6. #include "mozilla/Unused.h"
  7. #include "mozilla/dom/ContentParent.h"
  8. #include "mozilla/dom/cache/ActorUtils.h"
  9. #include "mozilla/dom/cache/CacheOpParent.h"
  10. #include "mozilla/dom/cache/ManagerId.h"
  11. #include "mozilla/ipc/PBackgroundParent.h"
  12. namespace mozilla {
  13. namespace dom {
  14. namespace cache {
  15. using mozilla::ipc::PBackgroundParent;
  16. using mozilla::ipc::PrincipalInfo;
  17. // declared in ActorUtils.h
  18. PCacheStorageParent*
  19. AllocPCacheStorageParent(PBackgroundParent* aManagingActor,
  20. Namespace aNamespace,
  21. const mozilla::ipc::PrincipalInfo& aPrincipalInfo)
  22. {
  23. return new CacheStorageParent(aManagingActor, aNamespace, aPrincipalInfo);
  24. }
  25. // declared in ActorUtils.h
  26. void
  27. DeallocPCacheStorageParent(PCacheStorageParent* aActor)
  28. {
  29. delete aActor;
  30. }
  31. CacheStorageParent::CacheStorageParent(PBackgroundParent* aManagingActor,
  32. Namespace aNamespace,
  33. const PrincipalInfo& aPrincipalInfo)
  34. : mNamespace(aNamespace)
  35. , mVerifiedStatus(NS_OK)
  36. {
  37. MOZ_COUNT_CTOR(cache::CacheStorageParent);
  38. MOZ_DIAGNOSTIC_ASSERT(aManagingActor);
  39. // Start the async principal verification process immediately.
  40. mVerifier = PrincipalVerifier::CreateAndDispatch(this, aManagingActor,
  41. aPrincipalInfo);
  42. MOZ_DIAGNOSTIC_ASSERT(mVerifier);
  43. }
  44. CacheStorageParent::~CacheStorageParent()
  45. {
  46. MOZ_COUNT_DTOR(cache::CacheStorageParent);
  47. MOZ_DIAGNOSTIC_ASSERT(!mVerifier);
  48. }
  49. void
  50. CacheStorageParent::ActorDestroy(ActorDestroyReason aReason)
  51. {
  52. if (mVerifier) {
  53. mVerifier->RemoveListener(this);
  54. mVerifier = nullptr;
  55. }
  56. }
  57. PCacheOpParent*
  58. CacheStorageParent::AllocPCacheOpParent(const CacheOpArgs& aOpArgs)
  59. {
  60. if (aOpArgs.type() != CacheOpArgs::TStorageMatchArgs &&
  61. aOpArgs.type() != CacheOpArgs::TStorageHasArgs &&
  62. aOpArgs.type() != CacheOpArgs::TStorageOpenArgs &&
  63. aOpArgs.type() != CacheOpArgs::TStorageDeleteArgs &&
  64. aOpArgs.type() != CacheOpArgs::TStorageKeysArgs)
  65. {
  66. MOZ_CRASH("Invalid operation sent to CacheStorage actor!");
  67. }
  68. return new CacheOpParent(Manager(), mNamespace, aOpArgs);
  69. }
  70. bool
  71. CacheStorageParent::DeallocPCacheOpParent(PCacheOpParent* aActor)
  72. {
  73. delete aActor;
  74. return true;
  75. }
  76. bool
  77. CacheStorageParent::RecvPCacheOpConstructor(PCacheOpParent* aActor,
  78. const CacheOpArgs& aOpArgs)
  79. {
  80. auto actor = static_cast<CacheOpParent*>(aActor);
  81. if (mVerifier) {
  82. MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
  83. actor->WaitForVerification(mVerifier);
  84. return true;
  85. }
  86. if (NS_WARN_IF(NS_FAILED(mVerifiedStatus))) {
  87. ErrorResult result(mVerifiedStatus);
  88. Unused << CacheOpParent::Send__delete__(actor, result, void_t());
  89. result.SuppressException();
  90. return true;
  91. }
  92. MOZ_DIAGNOSTIC_ASSERT(mManagerId);
  93. actor->Execute(mManagerId);
  94. return true;
  95. }
  96. bool
  97. CacheStorageParent::RecvTeardown()
  98. {
  99. if (!Send__delete__(this)) {
  100. // child process is gone, warn and allow actor to clean up normally
  101. NS_WARNING("CacheStorage failed to delete actor.");
  102. }
  103. return true;
  104. }
  105. void
  106. CacheStorageParent::OnPrincipalVerified(nsresult aRv, ManagerId* aManagerId)
  107. {
  108. MOZ_DIAGNOSTIC_ASSERT(mVerifier);
  109. MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
  110. MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mVerifiedStatus));
  111. if (NS_WARN_IF(NS_FAILED(aRv))) {
  112. mVerifiedStatus = aRv;
  113. }
  114. mManagerId = aManagerId;
  115. mVerifier->RemoveListener(this);
  116. mVerifier = nullptr;
  117. }
  118. } // namespace cache
  119. } // namespace dom
  120. } // namespace mozilla