ActorsChild.h (8018B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_localstorage_ActorsChild_h 8 #define mozilla_dom_localstorage_ActorsChild_h 9 10 #include <cstdint> 11 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/dom/PBackgroundLSDatabaseChild.h" 14 #include "mozilla/dom/PBackgroundLSObserverChild.h" 15 #include "mozilla/dom/PBackgroundLSRequest.h" 16 #include "mozilla/dom/PBackgroundLSRequestChild.h" 17 #include "mozilla/dom/PBackgroundLSSimpleRequest.h" 18 #include "mozilla/dom/PBackgroundLSSimpleRequestChild.h" 19 #include "mozilla/dom/PBackgroundLSSnapshotChild.h" 20 #include "mozilla/ipc/ProtocolUtils.h" 21 #include "nsISupports.h" 22 #include "nsStringFwd.h" 23 #include "nscore.h" 24 25 namespace mozilla { 26 27 namespace ipc { 28 29 class BackgroundChildImpl; 30 31 } // namespace ipc 32 33 namespace dom { 34 35 class LocalStorageManager2; 36 class LSDatabase; 37 class LSObject; 38 class LSObserver; 39 class LSRequestChildCallback; 40 class LSSimpleRequestChildCallback; 41 class LSSnapshot; 42 43 /** 44 * Minimal glue actor with standard IPC-managed new/delete existence that exists 45 * primarily to track the continued existence of the LSDatabase in the child. 46 * Most of the interesting bits happen via PBackgroundLSSnapshot. 47 * 48 * Mutual raw pointers are maintained between LSDatabase and this class that are 49 * cleared at either (expected) when the child starts the shutdown process 50 * (Shutdown) or unexpected actor death (ActorDestroy). 51 * 52 * See `PBackgroundLSDatabase.ipdl` for more information. 53 * 54 * 55 * ## Low-Level Lifecycle ## 56 * - Created by LSObject::EnsureDatabase if it had to create a database. 57 * - Shutdown begun by LSDatabase's destructor invoking Shutdown which destroys 58 * the actor. 59 */ 60 class LSDatabaseChild final : public PBackgroundLSDatabaseChild { 61 friend class mozilla::ipc::BackgroundChildImpl; 62 friend class LSDatabase; 63 friend class LSObject; 64 65 LSDatabase* mDatabase; 66 67 NS_INLINE_DECL_REFCOUNTING(LSDatabaseChild, override) 68 69 public: 70 void AssertIsOnOwningThread() const { 71 NS_ASSERT_OWNINGTHREAD(LSDatabaseChild); 72 } 73 74 private: 75 // Only created by LSObject. 76 explicit LSDatabaseChild(LSDatabase* aDatabase); 77 78 ~LSDatabaseChild(); 79 80 void Shutdown(); 81 82 // IPDL methods are only called by IPDL. 83 void ActorDestroy(ActorDestroyReason aWhy) override; 84 85 mozilla::ipc::IPCResult RecvRequestAllowToClose() override; 86 87 PBackgroundLSSnapshotChild* AllocPBackgroundLSSnapshotChild( 88 const nsAString& aDocumentURI, const nsAString& aKey, 89 const bool& aIncreasePeakUsage, const int64_t& aMinSize, 90 LSSnapshotInitInfo* aInitInfo) override; 91 92 bool DeallocPBackgroundLSSnapshotChild( 93 PBackgroundLSSnapshotChild* aActor) override; 94 }; 95 96 /** 97 * Minimal IPC-managed (new/delete) actor that exists to receive and relay 98 * "storage" events from changes to LocalStorage that take place in other 99 * processes as their Snapshots are checkpointed to the canonical Datastore in 100 * the parent process. 101 * 102 * See `PBackgroundLSObserver.ipdl` for more info. 103 */ 104 class LSObserverChild final : public PBackgroundLSObserverChild { 105 friend class mozilla::ipc::BackgroundChildImpl; 106 friend class LSObserver; 107 friend class LSObject; 108 109 LSObserver* mObserver; 110 111 NS_DECL_OWNINGTHREAD 112 113 public: 114 void AssertIsOnOwningThread() const { 115 NS_ASSERT_OWNINGTHREAD(LSObserverChild); 116 } 117 118 private: 119 // Only created by LSObject. 120 explicit LSObserverChild(LSObserver* aObserver); 121 122 // Only destroyed by mozilla::ipc::BackgroundChildImpl. 123 ~LSObserverChild(); 124 125 void SendDeleteMeInternal(); 126 127 // IPDL methods are only called by IPDL. 128 void ActorDestroy(ActorDestroyReason aWhy) override; 129 130 mozilla::ipc::IPCResult RecvObserve(const PrincipalInfo& aPrinciplaInfo, 131 const uint32_t& aPrivateBrowsingId, 132 const nsAString& aDocumentURI, 133 const nsAString& aKey, 134 const LSValue& aOldValue, 135 const LSValue& aNewValue) override; 136 }; 137 138 /** 139 * Minimal glue IPC-managed (new/delete) actor that is used by LSObject and its 140 * RequestHelper to perform synchronous requests on top of an asynchronous 141 * protocol. 142 * 143 * Takes an `LSReuestChildCallback` to be invoked when a response is received 144 * via __delete__. 145 * 146 * See `PBackgroundLSRequest.ipdl`, `LSObject`, and `RequestHelper` for more 147 * info. 148 */ 149 class LSRequestChild final : public PBackgroundLSRequestChild { 150 friend class LSObject; 151 friend class LocalStorageManager2; 152 153 RefPtr<LSRequestChildCallback> mCallback; 154 155 bool mFinishing; 156 157 NS_DECL_OWNINGTHREAD 158 159 public: 160 void AssertIsOnOwningThread() const { 161 NS_ASSERT_OWNINGTHREAD(LSReqeustChild); 162 } 163 164 bool Finishing() const; 165 166 private: 167 // Only created by LSObject. 168 LSRequestChild(); 169 170 // Only destroyed by mozilla::ipc::BackgroundChildImpl. 171 ~LSRequestChild(); 172 173 void SetCallback(LSRequestChildCallback* aCallback); 174 175 // IPDL methods are only called by IPDL. 176 void ActorDestroy(ActorDestroyReason aWhy) override; 177 178 mozilla::ipc::IPCResult Recv__delete__( 179 LSRequestResponse&& aResponse) override; 180 181 mozilla::ipc::IPCResult RecvReady() override; 182 }; 183 184 class NS_NO_VTABLE LSRequestChildCallback { 185 public: 186 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 187 188 virtual void OnResponse(LSRequestResponse&& aResponse) = 0; 189 190 protected: 191 virtual ~LSRequestChildCallback() = default; 192 }; 193 194 /** 195 * Minimal glue IPC-managed (new/delete) actor used by `LocalStorageManager2` to 196 * issue asynchronous requests in an asynchronous fashion. 197 * 198 * Takes an `LSSimpleRequestChildCallback` to be invoked when a response is 199 * received via __delete__. 200 * 201 * See `PBackgroundLSSimpleRequest.ipdl` for more info. 202 */ 203 class LSSimpleRequestChild final : public PBackgroundLSSimpleRequestChild { 204 friend class LocalStorageManager2; 205 206 RefPtr<LSSimpleRequestChildCallback> mCallback; 207 208 NS_DECL_OWNINGTHREAD 209 210 public: 211 void AssertIsOnOwningThread() const { 212 NS_ASSERT_OWNINGTHREAD(LSSimpleReqeustChild); 213 } 214 215 private: 216 // Only created by LocalStorageManager2. 217 LSSimpleRequestChild(); 218 219 void SetCallback(LSSimpleRequestChildCallback* aCallback); 220 221 // Only destroyed by mozilla::ipc::BackgroundChildImpl. 222 ~LSSimpleRequestChild(); 223 224 // IPDL methods are only called by IPDL. 225 void ActorDestroy(ActorDestroyReason aWhy) override; 226 227 mozilla::ipc::IPCResult Recv__delete__( 228 const LSSimpleRequestResponse& aResponse) override; 229 }; 230 231 class NS_NO_VTABLE LSSimpleRequestChildCallback { 232 public: 233 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 234 235 virtual void OnResponse(const LSSimpleRequestResponse& aResponse) = 0; 236 237 protected: 238 virtual ~LSSimpleRequestChildCallback() = default; 239 }; 240 241 /** 242 * Minimal IPC-managed (new/delete) actor that lasts as long as its owning 243 * LSSnapshot. 244 * 245 * Mutual raw pointers are maintained between LSSnapshot and this class that are 246 * cleared at either (expected) when the child starts the deletion process 247 * (SendDeleteMeInternal) or unexpected actor death (ActorDestroy). 248 * 249 * See `PBackgroundLSSnapshot.ipdl` and `LSSnapshot` for more info. 250 */ 251 class LSSnapshotChild final : public PBackgroundLSSnapshotChild { 252 friend class LSDatabase; 253 friend class LSSnapshot; 254 255 LSSnapshot* mSnapshot; 256 257 NS_DECL_OWNINGTHREAD 258 259 public: 260 void AssertIsOnOwningThread() const { 261 NS_ASSERT_OWNINGTHREAD(LSSnapshotChild); 262 } 263 264 private: 265 // Only created by LSDatabase. 266 explicit LSSnapshotChild(LSSnapshot* aSnapshot); 267 268 // Only destroyed by LSDatabaseChild. 269 ~LSSnapshotChild(); 270 271 void SendDeleteMeInternal(); 272 273 // IPDL methods are only called by IPDL. 274 void ActorDestroy(ActorDestroyReason aWhy) override; 275 276 mozilla::ipc::IPCResult RecvMarkDirty() override; 277 }; 278 279 } // namespace dom 280 } // namespace mozilla 281 282 #endif // mozilla_dom_localstorage_ActorsChild_h