Home | History | Annotate | Download | only in indexed_db
      1 // Copyright (c) 2013 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_H_
      6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
      7 
      8 #include <map>
      9 #include <set>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/strings/string16.h"
     15 #include "content/browser/indexed_db/indexed_db_callbacks.h"
     16 #include "content/browser/indexed_db/indexed_db_database.h"
     17 #include "content/browser/indexed_db/indexed_db_database_callbacks.h"
     18 #include "content/common/content_export.h"
     19 #include "url/gurl.h"
     20 
     21 namespace content {
     22 
     23 class IndexedDBBackingStore;
     24 class IndexedDBContextImpl;
     25 
     26 class CONTENT_EXPORT IndexedDBFactory
     27     : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) {
     28  public:
     29   explicit IndexedDBFactory(IndexedDBContextImpl* context);
     30 
     31   // Notifications from weak pointers.
     32   void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
     33                        const GURL& origin_url,
     34                        bool forcedClose);
     35 
     36   void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
     37                         const GURL& origin_url,
     38                         const base::FilePath& data_directory);
     39   void Open(const base::string16& name,
     40             int64 version,
     41             int64 transaction_id,
     42             scoped_refptr<IndexedDBCallbacks> callbacks,
     43             scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
     44             const GURL& origin_url,
     45             const base::FilePath& data_directory);
     46 
     47   void DeleteDatabase(const base::string16& name,
     48                       scoped_refptr<IndexedDBCallbacks> callbacks,
     49                       const GURL& origin_url,
     50                       const base::FilePath& data_directory);
     51 
     52   void HandleBackingStoreFailure(const GURL& origin_url);
     53 
     54   // Iterates over all databases; for diagnostics only.
     55   std::vector<IndexedDBDatabase*> GetOpenDatabasesForOrigin(
     56       const GURL& origin_url) const;
     57 
     58   // Called by IndexedDBContext after all connections are closed, to
     59   // ensure the backing store closed immediately.
     60   void ForceClose(const GURL& origin_url);
     61 
     62   // Called by the IndexedDBContext destructor so the factory can do cleanup.
     63   void ContextDestroyed();
     64 
     65  protected:
     66   friend class base::RefCountedThreadSafe<IndexedDBFactory>;
     67 
     68   virtual ~IndexedDBFactory();
     69 
     70   virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
     71       const GURL& origin_url,
     72       const base::FilePath& data_directory,
     73       blink::WebIDBDataLoss* data_loss,
     74       std::string* data_loss_reason,
     75       bool* disk_full);
     76 
     77   void ReleaseBackingStore(const GURL& origin_url, bool immediate);
     78   void CloseBackingStore(const GURL& origin_url);
     79 
     80  private:
     81   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     82                            BackingStoreReleasedOnForcedClose);
     83   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     84                            BackingStoreReleaseDelayedOnClose);
     85   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
     86   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     87                            DeleteDatabaseClosesBackingStore);
     88   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     89                            ForceCloseReleasesBackingStore);
     90   FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
     91                            GetDatabaseNamesClosesBackingStore);
     92   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
     93                            ForceCloseOpenDatabasesOnCommitFailure);
     94 
     95   // Called internally after a database is closed, with some delay. If this
     96   // factory has the last reference, it will be released.
     97   void MaybeCloseBackingStore(const GURL& origin_url);
     98   bool HasLastBackingStoreReference(const GURL& origin_url) const;
     99 
    100   // Testing helpers, so unit tests don't need to grovel through internal state.
    101   bool IsDatabaseOpen(const GURL& origin_url,
    102                       const base::string16& name) const;
    103   bool IsBackingStoreOpen(const GURL& origin_url) const;
    104   bool IsBackingStorePendingClose(const GURL& origin_url) const;
    105 
    106   IndexedDBContextImpl* context_;
    107 
    108   typedef std::map<IndexedDBDatabase::Identifier,
    109                    scoped_refptr<IndexedDBDatabase> > IndexedDBDatabaseMap;
    110   IndexedDBDatabaseMap database_map_;
    111 
    112   typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
    113       IndexedDBBackingStoreMap;
    114   IndexedDBBackingStoreMap backing_store_map_;
    115 
    116   std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
    117 };
    118 
    119 }  // namespace content
    120 
    121 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
    122