DBAction.h (2532B)
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_DBAction_h 8 #define mozilla_dom_cache_DBAction_h 9 10 #include "CacheCipherKeyManager.h" 11 #include "mozilla/dom/cache/Action.h" 12 #include "nsString.h" 13 14 class mozIStorageConnection; 15 class nsIFile; 16 17 namespace mozilla::dom::cache { 18 19 Result<nsCOMPtr<mozIStorageConnection>, nsresult> OpenDBConnection( 20 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBFile, 21 const Maybe<CipherKey>& aMaybeCipherKey); 22 23 class DBAction : public Action { 24 protected: 25 // The mode specifies whether the database should already exist or if its 26 // ok to create a new database. 27 enum Mode { Existing, Create }; 28 29 explicit DBAction(Mode aMode); 30 31 // Action objects are deleted through their base pointer 32 virtual ~DBAction(); 33 34 // Just as the resolver must be ref'd until resolve, you may also 35 // ref the DB connection. The connection can only be referenced from the 36 // target thread and must be released upon resolve. 37 virtual void RunWithDBOnTarget( 38 SafeRefPtr<Resolver> aResolver, 39 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, 40 mozIStorageConnection* aConn) = 0; 41 42 private: 43 void RunOnTarget(SafeRefPtr<Resolver> aResolver, 44 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, 45 Data* aOptionalData, 46 const Maybe<CipherKey>& aMaybeCipherKey) override; 47 48 Result<nsCOMPtr<mozIStorageConnection>, nsresult> OpenConnection( 49 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBDir, 50 const Maybe<CipherKey>& aMaybeCipherKey); 51 52 const Mode mMode; 53 }; 54 55 class SyncDBAction : public DBAction { 56 protected: 57 explicit SyncDBAction(Mode aMode); 58 59 // Action objects are deleted through their base pointer 60 virtual ~SyncDBAction(); 61 62 virtual nsresult RunSyncWithDBOnTarget( 63 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, 64 mozIStorageConnection* aConn) = 0; 65 66 private: 67 virtual void RunWithDBOnTarget( 68 SafeRefPtr<Resolver> aResolver, 69 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, 70 mozIStorageConnection* aConn) override; 71 }; 72 73 } // namespace mozilla::dom::cache 74 75 #endif // mozilla_dom_cache_DBAction_h