indexed_db_factory.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 21namespace content { 22 23class IndexedDBBackingStore; 24class IndexedDBContextImpl; 25struct IndexedDBPendingConnection; 26 27class CONTENT_EXPORT IndexedDBFactory 28 : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) { 29 public: 30 typedef std::multimap<GURL, IndexedDBDatabase*> OriginDBMap; 31 typedef OriginDBMap::const_iterator OriginDBMapIterator; 32 33 explicit IndexedDBFactory(IndexedDBContextImpl* context); 34 35 // Notifications from weak pointers. 36 void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier, 37 bool forcedClose); 38 39 void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks, 40 const GURL& origin_url, 41 const base::FilePath& data_directory); 42 void Open(const base::string16& name, 43 const IndexedDBPendingConnection& connection, 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 std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin( 55 const GURL& origin_url) const; 56 57 // Called by IndexedDBContext after all connections are closed, to 58 // ensure the backing store closed immediately. 59 void ForceClose(const GURL& origin_url); 60 61 // Called by the IndexedDBContext destructor so the factory can do cleanup. 62 void ContextDestroyed(); 63 64 // Called by an IndexedDBDatabase when it is actually deleted. 65 void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier); 66 67 size_t GetConnectionCount(const GURL& origin_url) const; 68 69 protected: 70 friend class base::RefCountedThreadSafe<IndexedDBFactory>; 71 72 virtual ~IndexedDBFactory(); 73 74 virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore( 75 const GURL& origin_url, 76 const base::FilePath& data_directory, 77 blink::WebIDBDataLoss* data_loss, 78 std::string* data_loss_reason, 79 bool* disk_full); 80 81 void ReleaseBackingStore(const GURL& origin_url, bool immediate); 82 void CloseBackingStore(const GURL& origin_url); 83 84 private: 85 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, 86 BackingStoreReleasedOnForcedClose); 87 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, 88 BackingStoreReleaseDelayedOnClose); 89 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen); 90 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, 91 DeleteDatabaseClosesBackingStore); 92 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, 93 ForceCloseReleasesBackingStore); 94 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, 95 GetDatabaseNamesClosesBackingStore); 96 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, 97 ForceCloseOpenDatabasesOnCommitFailure); 98 99 // Called internally after a database is closed, with some delay. If this 100 // factory has the last reference, it will be released. 101 void MaybeCloseBackingStore(const GURL& origin_url); 102 bool HasLastBackingStoreReference(const GURL& origin_url) const; 103 104 // Testing helpers, so unit tests don't need to grovel through internal state. 105 bool IsDatabaseOpen(const GURL& origin_url, 106 const base::string16& name) const; 107 bool IsBackingStoreOpen(const GURL& origin_url) const; 108 bool IsBackingStorePendingClose(const GURL& origin_url) const; 109 void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier); 110 111 IndexedDBContextImpl* context_; 112 113 typedef std::map<IndexedDBDatabase::Identifier, 114 IndexedDBDatabase*> IndexedDBDatabaseMap; 115 IndexedDBDatabaseMap database_map_; 116 OriginDBMap origin_dbs_; 117 118 typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> > 119 IndexedDBBackingStoreMap; 120 IndexedDBBackingStoreMap backing_store_map_; 121 122 std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_; 123}; 124 125} // namespace content 126 127#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_ 128