dom/src/storage/DOMStorageIPC.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef nsDOMStorageIPC_h___
     7 #define nsDOMStorageIPC_h___
     9 #include "mozilla/dom/PStorageChild.h"
    10 #include "mozilla/dom/PStorageParent.h"
    11 #include "DOMStorageDBThread.h"
    12 #include "DOMStorageCache.h"
    13 #include "DOMStorageObserver.h"
    14 #include "mozilla/Mutex.h"
    16 namespace mozilla {
    17 namespace dom {
    19 class DOMLocalStorageManager;
    21 // Child side of the IPC protocol, exposes as DB interface but
    22 // is responsible to send all requests to the parent process
    23 // and expects asynchronous answers. Those are then transparently
    24 // forwarded back to consumers on the child process.
    25 class DOMStorageDBChild MOZ_FINAL : public DOMStorageDBBridge
    26                                   , public PStorageChild
    27 {
    28 public:
    29   DOMStorageDBChild(DOMLocalStorageManager* aManager);
    30   virtual ~DOMStorageDBChild();
    32   NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
    33   NS_IMETHOD_(MozExternalRefCountType) Release(void);
    35   void AddIPDLReference();
    36   void ReleaseIPDLReference();
    38   virtual nsresult Init();
    39   virtual nsresult Shutdown();
    41   virtual void AsyncPreload(DOMStorageCacheBridge* aCache, bool aPriority = false);
    42   virtual void AsyncGetUsage(DOMStorageUsageBridge* aUsage);
    44   virtual void SyncPreload(DOMStorageCacheBridge* aCache, bool aForceSync = false);
    46   virtual nsresult AsyncAddItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue);
    47   virtual nsresult AsyncUpdateItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue);
    48   virtual nsresult AsyncRemoveItem(DOMStorageCacheBridge* aCache, const nsAString& aKey);
    49   virtual nsresult AsyncClear(DOMStorageCacheBridge* aCache);
    51   virtual void AsyncClearAll()
    52   {
    53     if (mScopesHavingData) {
    54       mScopesHavingData->Clear(); /* NO-OP on the child process otherwise */
    55     }
    56   }
    58   virtual void AsyncClearMatchingScope(const nsACString& aScope)
    59     { /* NO-OP on the child process */ }
    61   virtual void AsyncFlush()
    62     { SendAsyncFlush(); }
    64   virtual bool ShouldPreloadScope(const nsACString& aScope);
    65   virtual void GetScopesHavingData(InfallibleTArray<nsCString>* aScopes)
    66     { NS_NOTREACHED("Not implemented for child process"); }
    68 private:
    69   bool RecvObserve(const nsCString& aTopic,
    70                    const nsCString& aScopePrefix);
    71   bool RecvLoadItem(const nsCString& aScope,
    72                     const nsString& aKey,
    73                     const nsString& aValue);
    74   bool RecvLoadDone(const nsCString& aScope,
    75                     const nsresult& aRv);
    76   bool RecvScopesHavingData(const InfallibleTArray<nsCString>& aScopes);
    77   bool RecvLoadUsage(const nsCString& aScope,
    78                      const int64_t& aUsage);
    79   bool RecvError(const nsresult& aRv);
    81   nsTHashtable<nsCStringHashKey>& ScopesHavingData();
    83   ThreadSafeAutoRefCnt mRefCnt;
    84   NS_DECL_OWNINGTHREAD
    86   // Held to get caches to forward answers to.
    87   nsRefPtr<DOMLocalStorageManager> mManager;
    89   // Scopes having data hash, for optimization purposes only
    90   nsAutoPtr<nsTHashtable<nsCStringHashKey> > mScopesHavingData;
    92   // List of caches waiting for preload.  This ensures the contract that
    93   // AsyncPreload call references the cache for time of the preload.
    94   nsTHashtable<nsRefPtrHashKey<DOMStorageCacheBridge> > mLoadingCaches;
    96   // Status of the remote database
    97   nsresult mStatus;
    99   bool mIPCOpen;
   100 };
   103 // Receives async requests from child processes and is responsible
   104 // to send back responses from the DB thread.  Exposes as a fake
   105 // DOMStorageCache consumer.
   106 // Also responsible for forwardning all chrome operation notifications
   107 // such as cookie cleaning etc to the child process.
   108 class DOMStorageDBParent MOZ_FINAL : public PStorageParent
   109                                    , public DOMStorageObserverSink
   110 {
   111 public:
   112   DOMStorageDBParent();
   113   virtual ~DOMStorageDBParent();
   115   virtual mozilla::ipc::IProtocol*
   116   CloneProtocol(Channel* aChannel,
   117                 mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE;
   119   NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
   120   NS_IMETHOD_(MozExternalRefCountType) Release(void);
   122   void AddIPDLReference();
   123   void ReleaseIPDLReference();
   125   bool IPCOpen() { return mIPCOpen; }
   127 public:
   128   // Fake cache class receiving async callbacks from DB thread, sending
   129   // them back to appropriate cache object on the child process.
   130   class CacheParentBridge : public DOMStorageCacheBridge {
   131   public:
   132     CacheParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope)
   133       : mParent(aParentDB), mScope(aScope), mLoaded(false), mLoadedCount(0) {}
   134     virtual ~CacheParentBridge() {}
   136     // DOMStorageCacheBridge
   137     virtual const nsCString& Scope() const
   138       { return mScope; }
   139     virtual bool Loaded()
   140       { return mLoaded; }
   141     virtual uint32_t LoadedCount()
   142       { return mLoadedCount; }
   144     virtual bool LoadItem(const nsAString& aKey, const nsString& aValue);
   145     virtual void LoadDone(nsresult aRv);
   146     virtual void LoadWait();
   148   private:
   149     nsRefPtr<DOMStorageDBParent> mParent;
   150     nsCString mScope;
   151     bool mLoaded;
   152     uint32_t mLoadedCount;
   153   };
   155   // Fake usage class receiving async callbacks from DB thread
   156   class UsageParentBridge : public DOMStorageUsageBridge
   157   {
   158   public:
   159     UsageParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope)
   160       : mParent(aParentDB), mScope(aScope) {}
   161     virtual ~UsageParentBridge() {}
   163     // DOMStorageUsageBridge
   164     virtual const nsCString& Scope() { return mScope; }
   165     virtual void LoadUsage(const int64_t usage);
   167   private:
   168     nsRefPtr<DOMStorageDBParent> mParent;
   169     nsCString mScope;
   170   };
   172 private:
   173   // IPC
   174   bool RecvAsyncPreload(const nsCString& aScope, const bool& aPriority);
   175   bool RecvPreload(const nsCString& aScope, const uint32_t& aAlreadyLoadedCount,
   176                    InfallibleTArray<nsString>* aKeys, InfallibleTArray<nsString>* aValues,
   177                    nsresult* aRv);
   178   bool RecvAsyncGetUsage(const nsCString& aScope);
   179   bool RecvAsyncAddItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue);
   180   bool RecvAsyncUpdateItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue);
   181   bool RecvAsyncRemoveItem(const nsCString& aScope, const nsString& aKey);
   182   bool RecvAsyncClear(const nsCString& aScope);
   183   bool RecvAsyncFlush();
   185   // DOMStorageObserverSink
   186   virtual nsresult Observe(const char* aTopic, const nsACString& aScopePrefix);
   188 private:
   189   CacheParentBridge* NewCache(const nsACString& aScope);
   191   ThreadSafeAutoRefCnt mRefCnt;
   192   NS_DECL_OWNINGTHREAD
   194 	// True when IPC channel is open and Send*() methods are OK to use.
   195   bool mIPCOpen;
   196 };
   198 } // ::dom
   199 } // ::mozilla
   201 #endif

mercurial