tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ServiceWorkerCloneData.cpp (2376B)


      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 #include "ServiceWorkerCloneData.h"
      8 
      9 #include <utility>
     10 
     11 #include "mozilla/RefPtr.h"
     12 #include "mozilla/dom/DOMTypes.h"
     13 #include "mozilla/dom/StructuredCloneHolder.h"
     14 #include "nsISerialEventTarget.h"
     15 #include "nsProxyRelease.h"
     16 #include "nsThreadUtils.h"
     17 
     18 namespace mozilla::dom {
     19 
     20 ServiceWorkerCloneData::~ServiceWorkerCloneData() {
     21  RefPtr<ipc::SharedJSAllocatedData> sharedData = TakeSharedData();
     22  if (sharedData) {
     23    NS_ProxyRelease(__func__, mEventTarget, sharedData.forget());
     24  }
     25 }
     26 
     27 ServiceWorkerCloneData::ServiceWorkerCloneData()
     28    : ipc::StructuredCloneData(
     29          StructuredCloneHolder::StructuredCloneScope::UnknownDestination,
     30          StructuredCloneHolder::TransferringSupported),
     31      mEventTarget(GetCurrentSerialEventTarget()),
     32      mIsErrorMessageData(false) {
     33  MOZ_DIAGNOSTIC_ASSERT(mEventTarget);
     34 }
     35 
     36 bool ServiceWorkerCloneData::BuildClonedMessageData(
     37    ClonedOrErrorMessageData& aClonedData) {
     38  if (IsErrorMessageData()) {
     39    aClonedData = ErrorMessageData();
     40    return true;
     41  }
     42 
     43  MOZ_DIAGNOSTIC_ASSERT(
     44      CloneScope() ==
     45      StructuredCloneHolder::StructuredCloneScope::DifferentProcess);
     46 
     47  ClonedMessageData messageData;
     48  if (!StructuredCloneData::BuildClonedMessageData(messageData)) {
     49    return false;
     50  }
     51 
     52  aClonedData = std::move(messageData);
     53 
     54  return true;
     55 }
     56 
     57 void ServiceWorkerCloneData::CopyFromClonedMessageData(
     58    const ClonedOrErrorMessageData& aClonedData) {
     59  if (aClonedData.type() == ClonedOrErrorMessageData::TErrorMessageData) {
     60    mIsErrorMessageData = true;
     61    return;
     62  }
     63 
     64  MOZ_DIAGNOSTIC_ASSERT(aClonedData.type() ==
     65                        ClonedOrErrorMessageData::TClonedMessageData);
     66 
     67  StructuredCloneData::CopyFromClonedMessageData(aClonedData);
     68 }
     69 
     70 void ServiceWorkerCloneData::SetAsErrorMessageData() {
     71  MOZ_ASSERT(CloneScope() ==
     72             StructuredCloneHolder::StructuredCloneScope::SameProcess);
     73 
     74  mIsErrorMessageData = true;
     75 }
     76 
     77 bool ServiceWorkerCloneData::IsErrorMessageData() const {
     78  return mIsErrorMessageData;
     79 }
     80 
     81 }  // namespace mozilla::dom