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.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
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"
23 class IndexedDBBackingStore
;
24 class IndexedDBContextImpl
;
26 class CONTENT_EXPORT IndexedDBFactory
27 : NON_EXPORTED_BASE(public base::RefCountedThreadSafe
<IndexedDBFactory
>) {
29 typedef std::multimap
<GURL
, IndexedDBDatabase
*> OriginDBMap
;
30 typedef OriginDBMap::const_iterator OriginDBMapIterator
;
32 explicit IndexedDBFactory(IndexedDBContextImpl
* context
);
34 // Notifications from weak pointers.
35 void ReleaseDatabase(const IndexedDBDatabase::Identifier
& identifier
,
38 void GetDatabaseNames(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
39 const GURL
& origin_url
,
40 const base::FilePath
& data_directory
);
41 void Open(const base::string16
& name
,
44 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
45 scoped_refptr
<IndexedDBDatabaseCallbacks
> database_callbacks
,
46 const GURL
& origin_url
,
47 const base::FilePath
& data_directory
);
49 void DeleteDatabase(const base::string16
& name
,
50 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
51 const GURL
& origin_url
,
52 const base::FilePath
& data_directory
);
54 void HandleBackingStoreFailure(const GURL
& origin_url
);
56 std::pair
<OriginDBMapIterator
, OriginDBMapIterator
> GetOpenDatabasesForOrigin(
57 const GURL
& origin_url
) const;
59 // Called by IndexedDBContext after all connections are closed, to
60 // ensure the backing store closed immediately.
61 void ForceClose(const GURL
& origin_url
);
63 // Called by the IndexedDBContext destructor so the factory can do cleanup.
64 void ContextDestroyed();
66 // Called by an IndexedDBDatabase when it is actually deleted.
67 void DatabaseDeleted(const IndexedDBDatabase::Identifier
& identifier
);
69 size_t GetConnectionCount(const GURL
& origin_url
) const;
72 friend class base::RefCountedThreadSafe
<IndexedDBFactory
>;
74 virtual ~IndexedDBFactory();
76 virtual scoped_refptr
<IndexedDBBackingStore
> OpenBackingStore(
77 const GURL
& origin_url
,
78 const base::FilePath
& data_directory
,
79 blink::WebIDBDataLoss
* data_loss
,
80 std::string
* data_loss_reason
,
83 void ReleaseBackingStore(const GURL
& origin_url
, bool immediate
);
84 void CloseBackingStore(const GURL
& origin_url
);
87 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
88 BackingStoreReleasedOnForcedClose
);
89 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
90 BackingStoreReleaseDelayedOnClose
);
91 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
, DatabaseFailedOpen
);
92 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
93 DeleteDatabaseClosesBackingStore
);
94 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
95 ForceCloseReleasesBackingStore
);
96 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
97 GetDatabaseNamesClosesBackingStore
);
98 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
,
99 ForceCloseOpenDatabasesOnCommitFailure
);
101 // Called internally after a database is closed, with some delay. If this
102 // factory has the last reference, it will be released.
103 void MaybeCloseBackingStore(const GURL
& origin_url
);
104 bool HasLastBackingStoreReference(const GURL
& origin_url
) const;
106 // Testing helpers, so unit tests don't need to grovel through internal state.
107 bool IsDatabaseOpen(const GURL
& origin_url
,
108 const base::string16
& name
) const;
109 bool IsBackingStoreOpen(const GURL
& origin_url
) const;
110 bool IsBackingStorePendingClose(const GURL
& origin_url
) const;
111 void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier
& identifier
);
113 IndexedDBContextImpl
* context_
;
115 typedef std::map
<IndexedDBDatabase::Identifier
,
116 IndexedDBDatabase
*> IndexedDBDatabaseMap
;
117 IndexedDBDatabaseMap database_map_
;
118 OriginDBMap origin_dbs_
;
120 typedef std::map
<GURL
, scoped_refptr
<IndexedDBBackingStore
> >
121 IndexedDBBackingStoreMap
;
122 IndexedDBBackingStoreMap backing_store_map_
;
124 std::set
<scoped_refptr
<IndexedDBBackingStore
> > session_only_backing_stores_
;
127 } // namespace content
129 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_