Home | History | Annotate | Download | only in indexed_db
      1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_IMPL_H_
      6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "content/browser/indexed_db/indexed_db_factory.h"
     13 
     14 namespace content {
     15 
     16 class IndexedDBContextImpl;
     17 
     18 class CONTENT_EXPORT IndexedDBFactoryImpl : public IndexedDBFactory {
     19  public:
     20   explicit IndexedDBFactoryImpl(IndexedDBContextImpl* context);
     21 
     22   // content::IndexedDBFactory overrides:
     23   virtual void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
     24                                bool forcedClose) OVERRIDE;
     25 
     26   virtual void GetDatabaseNames(
     27       scoped_refptr<IndexedDBCallbacks> callbacks,
     28       const GURL& origin_url,
     29       const base::FilePath& data_directory,
     30       net::URLRequestContext* request_context) OVERRIDE;
     31   virtual void Open(const base::string16& name,
     32                     const IndexedDBPendingConnection& connection,
     33                     net::URLRequestContext* request_context,
     34                     const GURL& origin_url,
     35                     const base::FilePath& data_directory) OVERRIDE;
     36 
     37   virtual void DeleteDatabase(const base::string16& name,
     38                               net::URLRequestContext* request_context,
     39                               scoped_refptr<IndexedDBCallbacks> callbacks,
     40                               const GURL& origin_url,
     41                               const base::FilePath& data_directory) OVERRIDE;
     42 
     43   virtual void HandleBackingStoreFailure(const GURL& origin_url) OVERRIDE;
     44   virtual void HandleBackingStoreCorruption(
     45       const GURL& origin_url,
     46       const IndexedDBDatabaseError& error) OVERRIDE;
     47 
     48   virtual OriginDBs GetOpenDatabasesForOrigin(
     49       const GURL& origin_url) const OVERRIDE;
     50 
     51   virtual void ForceClose(const GURL& origin_url) OVERRIDE;
     52 
     53   // Called by the IndexedDBContext destructor so the factory can do cleanup.
     54   virtual void ContextDestroyed() OVERRIDE;
     55 
     56   // Called by the IndexedDBActiveBlobRegistry.
     57   virtual void ReportOutstandingBlobs(const GURL& origin_url,
     58                                       bool blobs_outstanding) OVERRIDE;
     59 
     60   // Called by an IndexedDBDatabase when it is actually deleted.
     61   virtual void DatabaseDeleted(
     62       const IndexedDBDatabase::Identifier& identifier) OVERRIDE;
     63 
     64   virtual size_t GetConnectionCount(const GURL& origin_url) const OVERRIDE;
     65 
     66  protected:
     67   virtual ~IndexedDBFactoryImpl();
     68 
     69   virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
     70       const GURL& origin_url,
     71       const base::FilePath& data_directory,
     72       net::URLRequestContext* request_context,
     73       blink::WebIDBDataLoss* data_loss,
     74       std::string* data_loss_reason,
     75       bool* disk_full,
     76       leveldb::Status* s) OVERRIDE;
     77 
     78   virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper(
     79       const GURL& origin_url,
     80       const base::FilePath& data_directory,
     81       net::URLRequestContext* request_context,
     82       blink::WebIDBDataLoss* data_loss,
     83       std::string* data_loss_message,
     84       bool* disk_full,
     85       bool first_time,
     86       leveldb::Status* s) OVERRIDE;
     87 
     88   void ReleaseBackingStore(const GURL& origin_url, bool immediate);
     89   void CloseBackingStore(const GURL& origin_url);
     90   IndexedDBContextImpl* context() const { return context_; }
     91 
     92  private:
     93   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     94                            BackingStoreReleasedOnForcedClose);
     95   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     96                            BackingStoreReleaseDelayedOnClose);
     97   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
     98   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     99                            DeleteDatabaseClosesBackingStore);
    100   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
    101                            ForceCloseReleasesBackingStore);
    102   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
    103                            GetDatabaseNamesClosesBackingStore);
    104   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
    105                            ForceCloseOpenDatabasesOnCommitFailure);
    106 
    107   typedef std::map<IndexedDBDatabase::Identifier, IndexedDBDatabase*>
    108       IndexedDBDatabaseMap;
    109   typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
    110       IndexedDBBackingStoreMap;
    111 
    112   // Called internally after a database is closed, with some delay. If this
    113   // factory has the last reference, it will be released.
    114   void MaybeCloseBackingStore(const GURL& origin_url);
    115   bool HasLastBackingStoreReference(const GURL& origin_url) const;
    116 
    117   // Testing helpers, so unit tests don't need to grovel through internal state.
    118   bool IsDatabaseOpen(const GURL& origin_url, const base::string16& name) const;
    119   bool IsBackingStoreOpen(const GURL& origin_url) const;
    120   bool IsBackingStorePendingClose(const GURL& origin_url) const;
    121   void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
    122 
    123   IndexedDBContextImpl* context_;
    124 
    125   IndexedDBDatabaseMap database_map_;
    126   OriginDBMap origin_dbs_;
    127   IndexedDBBackingStoreMap backing_store_map_;
    128 
    129   std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
    130   IndexedDBBackingStoreMap backing_stores_with_active_blobs_;
    131   std::set<GURL> backends_opened_since_boot_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryImpl);
    134 };
    135 
    136 }  // namespace content
    137 
    138 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_IMPL_H_
    139