tor-browser

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

ActorsChild.cpp (5649B)


      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 #include "ActorsChild.h"
      8 
      9 // Local includes
     10 #include "QuotaManagerService.h"
     11 #include "QuotaRequests.h"
     12 #include "QuotaResults.h"
     13 
     14 // Global includes
     15 #include <new>
     16 
     17 #include "mozilla/Assertions.h"
     18 #include "mozilla/dom/quota/PQuotaRequest.h"
     19 #include "nsError.h"
     20 #include "nsID.h"
     21 #include "nsIEventTarget.h"
     22 #include "nsIQuotaResults.h"
     23 #include "nsISupports.h"
     24 #include "nsIVariant.h"
     25 #include "nsString.h"
     26 #include "nsThreadUtils.h"
     27 #include "nsVariant.h"
     28 
     29 namespace mozilla::dom::quota {
     30 
     31 /*******************************************************************************
     32 * QuotaChild
     33 ******************************************************************************/
     34 
     35 QuotaChild::QuotaChild(QuotaManagerService* aService)
     36    : mService(aService)
     37 #ifdef DEBUG
     38      ,
     39      mOwningThread(GetCurrentSerialEventTarget())
     40 #endif
     41 {
     42  AssertIsOnOwningThread();
     43  MOZ_ASSERT(aService);
     44 
     45  MOZ_COUNT_CTOR(quota::QuotaChild);
     46 }
     47 
     48 QuotaChild::~QuotaChild() {
     49  AssertIsOnOwningThread();
     50 
     51  MOZ_COUNT_DTOR(quota::QuotaChild);
     52 }
     53 
     54 #ifdef DEBUG
     55 
     56 void QuotaChild::AssertIsOnOwningThread() const {
     57  MOZ_ASSERT(mOwningThread);
     58 
     59  bool current;
     60  MOZ_ASSERT(NS_SUCCEEDED(mOwningThread->IsOnCurrentThread(&current)));
     61  MOZ_ASSERT(current);
     62 }
     63 
     64 #endif  // DEBUG
     65 
     66 void QuotaChild::ActorDestroy(ActorDestroyReason aWhy) {
     67  AssertIsOnOwningThread();
     68 
     69  if (mService) {
     70    mService->ClearBackgroundActor();
     71 #ifdef DEBUG
     72    mService = nullptr;
     73 #endif
     74  }
     75 }
     76 
     77 PQuotaRequestChild* QuotaChild::AllocPQuotaRequestChild(
     78    const RequestParams& aParams) {
     79  AssertIsOnOwningThread();
     80 
     81  MOZ_CRASH("PQuotaRequestChild actors should be manually constructed!");
     82 }
     83 
     84 bool QuotaChild::DeallocPQuotaRequestChild(PQuotaRequestChild* aActor) {
     85  AssertIsOnOwningThread();
     86  MOZ_ASSERT(aActor);
     87 
     88  delete static_cast<QuotaRequestChild*>(aActor);
     89  return true;
     90 }
     91 
     92 /*******************************************************************************
     93 * QuotaRequestChild
     94 ******************************************************************************/
     95 
     96 QuotaRequestChild::QuotaRequestChild(Request* aRequest) : mRequest(aRequest) {
     97  AssertIsOnOwningThread();
     98 
     99  MOZ_COUNT_CTOR(quota::QuotaRequestChild);
    100 }
    101 
    102 QuotaRequestChild::~QuotaRequestChild() {
    103  AssertIsOnOwningThread();
    104 
    105  MOZ_COUNT_DTOR(quota::QuotaRequestChild);
    106 }
    107 
    108 #ifdef DEBUG
    109 
    110 void QuotaRequestChild::AssertIsOnOwningThread() const {
    111  MOZ_ASSERT(mRequest);
    112  mRequest->AssertIsOnOwningThread();
    113 }
    114 
    115 #endif  // DEBUG
    116 
    117 void QuotaRequestChild::HandleResponse(nsresult aResponse) {
    118  AssertIsOnOwningThread();
    119  MOZ_ASSERT(NS_FAILED(aResponse));
    120  MOZ_ASSERT(mRequest);
    121 
    122  mRequest->SetError(aResponse);
    123 }
    124 
    125 void QuotaRequestChild::HandleResponse() {
    126  AssertIsOnOwningThread();
    127  MOZ_ASSERT(mRequest);
    128 
    129  RefPtr<nsVariant> variant = new nsVariant();
    130  variant->SetAsVoid();
    131 
    132  mRequest->SetResult(variant);
    133 }
    134 
    135 void QuotaRequestChild::HandleResponse(bool aResponse) {
    136  AssertIsOnOwningThread();
    137  MOZ_ASSERT(mRequest);
    138 
    139  RefPtr<nsVariant> variant = new nsVariant();
    140  variant->SetAsBool(aResponse);
    141 
    142  mRequest->SetResult(variant);
    143 }
    144 
    145 void QuotaRequestChild::HandleResponse(const nsAString& aResponse) {
    146  AssertIsOnOwningThread();
    147  MOZ_ASSERT(mRequest);
    148 
    149  RefPtr<nsVariant> variant = new nsVariant();
    150  variant->SetAsAString(aResponse);
    151 
    152  mRequest->SetResult(variant);
    153 }
    154 
    155 void QuotaRequestChild::HandleResponse(const EstimateResponse& aResponse) {
    156  AssertIsOnOwningThread();
    157  MOZ_ASSERT(mRequest);
    158 
    159  RefPtr<EstimateResult> result =
    160      new EstimateResult(aResponse.usage(), aResponse.limit());
    161 
    162  RefPtr<nsVariant> variant = new nsVariant();
    163  variant->SetAsInterface(NS_GET_IID(nsIQuotaEstimateResult), result);
    164 
    165  mRequest->SetResult(variant);
    166 }
    167 
    168 void QuotaRequestChild::HandleResponse(
    169    const GetFullOriginMetadataResponse& aResponse) {
    170  AssertIsOnOwningThread();
    171  MOZ_ASSERT(mRequest);
    172 
    173  RefPtr<nsVariant> variant = new nsVariant();
    174 
    175  if (aResponse.maybeFullOriginMetadata()) {
    176    RefPtr<FullOriginMetadataResult> result =
    177        new FullOriginMetadataResult(*aResponse.maybeFullOriginMetadata());
    178 
    179    variant->SetAsInterface(NS_GET_IID(nsIQuotaFullOriginMetadataResult),
    180                            result);
    181 
    182  } else {
    183    variant->SetAsVoid();
    184  }
    185 
    186  mRequest->SetResult(variant);
    187 }
    188 
    189 void QuotaRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
    190  AssertIsOnOwningThread();
    191 }
    192 
    193 mozilla::ipc::IPCResult QuotaRequestChild::Recv__delete__(
    194    const RequestResponse& aResponse) {
    195  AssertIsOnOwningThread();
    196  MOZ_ASSERT(mRequest);
    197 
    198  switch (aResponse.type()) {
    199    case RequestResponse::Tnsresult:
    200      HandleResponse(aResponse.get_nsresult());
    201      break;
    202 
    203    case RequestResponse::TStorageNameResponse:
    204      HandleResponse(aResponse.get_StorageNameResponse().name());
    205      break;
    206 
    207    case RequestResponse::TPersistResponse:
    208      HandleResponse();
    209      break;
    210 
    211    case RequestResponse::TGetFullOriginMetadataResponse:
    212      HandleResponse(aResponse.get_GetFullOriginMetadataResponse());
    213      break;
    214 
    215    case RequestResponse::TPersistedResponse:
    216      HandleResponse(aResponse.get_PersistedResponse().persisted());
    217      break;
    218 
    219    case RequestResponse::TEstimateResponse:
    220      HandleResponse(aResponse.get_EstimateResponse());
    221      break;
    222 
    223    default:
    224      MOZ_CRASH("Unknown response type!");
    225  }
    226 
    227  return IPC_OK();
    228 }
    229 
    230 }  // namespace mozilla::dom::quota