ServiceWorkerCloneData.h (2521B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_ServiceWorkerCloneData_h__ 8 #define mozilla_dom_ServiceWorkerCloneData_h__ 9 10 #include "mozilla/dom/DOMTypes.h" 11 #include "mozilla/dom/ipc/StructuredCloneData.h" 12 #include "nsCOMPtr.h" 13 #include "nsISupports.h" 14 15 class nsISerialEventTarget; 16 17 namespace mozilla { 18 namespace ipc { 19 class PBackgroundChild; 20 class PBackgroundParent; 21 } // namespace ipc 22 23 namespace dom { 24 25 class ClonedOrErrorMessageData; 26 27 // Helper class used to pack structured clone data so that it can be 28 // passed across thread and process boundaries. Currently the raw 29 // StructuredCloneData and StructureCloneHolder APIs both make it 30 // difficult to meet this needs directly. This helper class improves 31 // the situation by: 32 // 33 // 1. Provides a ref-counted version of StructuredCloneData. We need 34 // StructuredCloneData so we can serialize/deserialize across IPC. 35 // The move constructor problems in StructuredCloneData (bug 1462676), 36 // though, makes it hard to pass it around. Passing a ref-counted 37 // pointer addresses this problem. 38 // 2. Normally StructuredCloneData runs into problems if you try to move 39 // it across thread boundaries because it releases its SharedJSAllocatedData 40 // on the wrong thread. This helper will correctly proxy release the 41 // shared data on the correct thread. 42 // 43 // This helper class should really just be used to serialize on one thread 44 // and then move the reference across thread/process boundries to the 45 // target worker thread. This class is not intended to support simultaneous 46 // read/write operations from different threads at the same time. 47 class ServiceWorkerCloneData final : public ipc::StructuredCloneData { 48 nsCOMPtr<nsISerialEventTarget> mEventTarget; 49 bool mIsErrorMessageData; 50 51 ~ServiceWorkerCloneData(); 52 53 public: 54 ServiceWorkerCloneData(); 55 56 bool BuildClonedMessageData(ClonedOrErrorMessageData& aClonedData); 57 58 void CopyFromClonedMessageData(const ClonedOrErrorMessageData& aClonedData); 59 60 void SetAsErrorMessageData(); 61 62 bool IsErrorMessageData() const; 63 64 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ServiceWorkerCloneData) 65 }; 66 67 } // namespace dom 68 } // namespace mozilla 69 70 #endif // mozilla_dom_ServiceWorkerCloneData_h__