tor-browser

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

BoundStorageKeyCache.h (6214B)


      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_cache_BoundStorageKeyCache_h
      8 #define mozilla_dom_cache_BoundStorageKeyCache_h
      9 
     10 #include "mozilla/dom/cache/CacheTypes.h"
     11 #include "mozilla/dom/cache/TypeUtils.h"
     12 #include "mozilla/dom/cache/Types.h"
     13 #include "nsCOMPtr.h"
     14 #include "nsISupportsImpl.h"
     15 
     16 class nsIGlobalObject;
     17 
     18 namespace mozilla {
     19 class ErrorResult;
     20 
     21 namespace dom {
     22 
     23 class OwningRequestOrUTF8String;
     24 struct CacheQueryOptions;
     25 class RequestOrUTF8String;
     26 class Response;
     27 class Request;
     28 template <typename T>
     29 class Optional;
     30 template <typename T>
     31 class Sequence;
     32 enum class CallerType : uint32_t;
     33 
     34 namespace cache {
     35 class AutoChildOpArgs;
     36 class CacheChild;
     37 
     38 using CachePromise = MozPromiseBase;
     39 
     40 /* This is similar to Cache class as BoundStorageKeyCacheStorage is to
     41 * CacheStorage i.e.
     42 * 1. Exposes and implements Cache APIs but uses MozPromise as it's return value
     43 * rather than JS Promise
     44 * 2. IPC communication can be retargetted to any event target as it's protocol
     45 * gets created on a top-level actor
     46 */
     47 class BoundStorageKeyCache final : public nsISupports,
     48                                   public TypeUtils,
     49                                   public CacheChildListener {
     50 public:
     51  NS_DECL_ISUPPORTS
     52 
     53  using MatchResultPromise =
     54      mozilla::MozPromise<RefPtr<dom::Response>, ErrorResult,
     55                          true /* IsExclusive= */>;
     56  using MatchAllResultPromise =
     57      mozilla::MozPromise<nsTArray<RefPtr<dom::Response>>, ErrorResult,
     58                          true /* IsExclusive= */>;
     59  using AddResultPromise =
     60      mozilla::MozPromise<bool, ErrorResult, true /* IsExclusive= */>;
     61  using AddAllResultPromise =
     62      mozilla::MozPromise<bool, ErrorResult, true /* IsExclusive= */>;
     63  using PutResultPromise =
     64      mozilla::MozPromise<bool, ErrorResult, true /* IsExclusive= */>;
     65  using PutAllResultPromise =
     66      mozilla::MozPromise<bool, ErrorResult, true /* IsExclusive= */>;
     67  using DeleteResultPromise =
     68      mozilla::MozPromise<bool, ErrorResult, true /* IsExclusive= */>;
     69  using KeysResultPromise =
     70      mozilla::MozPromise<nsTArray<SafeRefPtr<dom::Request>>, ErrorResult,
     71                          true /* IsExclusive= */>;
     72 
     73  BoundStorageKeyCache(nsIGlobalObject* aGlobal, CacheChild* aActor,
     74                       Namespace aNamespace);
     75 
     76  static bool CachesEnabled(JSContext* aCx, JSObject*);
     77 
     78  already_AddRefed<CachePromise> Match(JSContext* aCx,
     79                                       const RequestOrUTF8String& aRequest,
     80                                       const CacheQueryOptions& aOptions,
     81                                       ErrorResult& aRv);
     82 
     83  already_AddRefed<CachePromise> MatchAll(
     84      JSContext* aCx, const Optional<RequestOrUTF8String>& aRequest,
     85      const CacheQueryOptions& aOptions, ErrorResult& aRv);
     86 
     87  already_AddRefed<CachePromise> Add(JSContext* aContext,
     88                                     const RequestOrUTF8String& aRequest,
     89                                     CallerType aCallerType, ErrorResult& aRv);
     90 
     91  already_AddRefed<CachePromise> AddAll(
     92      JSContext* aContext,
     93      const Sequence<OwningRequestOrUTF8String>& aRequestList,
     94      CallerType aCallerType, ErrorResult& aRv);
     95 
     96  already_AddRefed<CachePromise> Put(JSContext* aCx,
     97                                     const RequestOrUTF8String& aRequest,
     98                                     Response& aResponse, ErrorResult& aRv);
     99 
    100  already_AddRefed<CachePromise> Delete(JSContext* aCx,
    101                                        const RequestOrUTF8String& aRequest,
    102                                        const CacheQueryOptions& aOptions,
    103                                        ErrorResult& aRv);
    104 
    105  already_AddRefed<CachePromise> Keys(
    106      JSContext* aCx, const Optional<RequestOrUTF8String>& aRequest,
    107      const CacheQueryOptions& aOptions, ErrorResult& aRv);
    108 
    109  // Called when CacheChild actor is being destroyed
    110  void OnActorDestroy(CacheChild* aActor) override;
    111 
    112  // TypeUtils methods
    113  virtual nsIGlobalObject* GetGlobalObject() const override;
    114 
    115 #ifdef DEBUG
    116  virtual void AssertOwningThread() const override;
    117 #endif
    118 
    119 private:
    120  ~BoundStorageKeyCache();
    121 
    122  // Called when we're destroyed or CCed.
    123  void DisconnectFromActor();
    124 
    125  void ExecuteOp(AutoChildOpArgs& aOpArgs, RefPtr<CachePromise>& aPromise,
    126                 ErrorResult& aRv);
    127 
    128  already_AddRefed<CachePromise> AddAll(
    129      const GlobalObject& aGlobal, nsTArray<SafeRefPtr<Request>>&& aRequestList,
    130      CallerType aCallerType, ErrorResult& aRv);
    131 
    132  already_AddRefed<CachePromise> PutAll(
    133      JSContext* aCx, const nsTArray<SafeRefPtr<Request>>& aRequestList,
    134      const nsTArray<RefPtr<Response>>& aResponseList, ErrorResult& aRv);
    135 
    136  OpenMode GetOpenMode() const;
    137 
    138  nsCOMPtr<nsIGlobalObject> mGlobal;
    139  CacheChild* mActor;
    140  const Namespace mNamespace;
    141 };
    142 
    143 }  // namespace cache
    144 
    145 template <dom::cache::CacheOpResult::Type OP_TYPE>
    146 struct cachestorage_traits;
    147 
    148 template <>
    149 struct cachestorage_traits<dom::cache::CacheOpResult::Type::TCacheMatchResult> {
    150  using PromiseType = cache::BoundStorageKeyCache::MatchResultPromise::Private;
    151 };
    152 
    153 template <>
    154 struct cachestorage_traits<
    155    dom::cache::CacheOpResult::Type::TCacheMatchAllResult> {
    156  using PromiseType =
    157      cache::BoundStorageKeyCache::MatchAllResultPromise::Private;
    158 };
    159 
    160 template <>
    161 struct cachestorage_traits<
    162    dom::cache::CacheOpResult::Type::TCachePutAllResult> {
    163  using PromiseType = cache::BoundStorageKeyCache::PutAllResultPromise::Private;
    164 };
    165 
    166 template <>
    167 struct cachestorage_traits<
    168    dom::cache::CacheOpResult::Type::TCacheDeleteResult> {
    169  using PromiseType = cache::BoundStorageKeyCache::DeleteResultPromise::Private;
    170 };
    171 
    172 template <>
    173 struct cachestorage_traits<dom::cache::CacheOpResult::Type::TCacheKeysResult> {
    174  using PromiseType = cache::BoundStorageKeyCache::KeysResultPromise::Private;
    175 };
    176 
    177 }  // namespace dom
    178 }  // namespace mozilla
    179 
    180 #endif  // mozilla_dom_cache_BoundStorageKeyCache_h