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"
22 class URLRequestContext
;
27 class IndexedDBBackingStore
;
28 class IndexedDBContextImpl
;
29 struct IndexedDBPendingConnection
;
31 class CONTENT_EXPORT IndexedDBFactory
32 : NON_EXPORTED_BASE(public base::RefCountedThreadSafe
<IndexedDBFactory
>) {
34 typedef std::multimap
<GURL
, IndexedDBDatabase
*> OriginDBMap
;
35 typedef OriginDBMap::const_iterator OriginDBMapIterator
;
37 explicit IndexedDBFactory(IndexedDBContextImpl
* context
);
39 void ReleaseDatabase(const IndexedDBDatabase::Identifier
& identifier
,
42 void GetDatabaseNames(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
43 const GURL
& origin_url
,
44 const base::FilePath
& data_directory
,
45 net::URLRequestContext
* request_context
);
46 void Open(const base::string16
& name
,
47 const IndexedDBPendingConnection
& connection
,
48 net::URLRequestContext
* request_context
,
49 const GURL
& origin_url
,
50 const base::FilePath
& data_directory
);
52 void DeleteDatabase(const base::string16
& name
,
53 net::URLRequestContext
* request_context
,
54 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
55 const GURL
& origin_url
,
56 const base::FilePath
& data_directory
);
58 void HandleBackingStoreFailure(const GURL
& origin_url
);
59 void HandleBackingStoreCorruption(const GURL
& origin_url
,
60 const IndexedDBDatabaseError
& error
);
62 std::pair
<OriginDBMapIterator
, OriginDBMapIterator
> GetOpenDatabasesForOrigin(
63 const GURL
& origin_url
) const;
65 void ForceClose(const GURL
& origin_url
);
67 // Called by the IndexedDBContext destructor so the factory can do cleanup.
68 void ContextDestroyed();
70 // Called by the IndexedDBActiveBlobRegistry.
71 virtual void ReportOutstandingBlobs(const GURL
& origin_url
,
72 bool blobs_outstanding
);
74 // Called by an IndexedDBDatabase when it is actually deleted.
75 void DatabaseDeleted(const IndexedDBDatabase::Identifier
& identifier
);
77 size_t GetConnectionCount(const GURL
& origin_url
) const;
80 friend class base::RefCountedThreadSafe
<IndexedDBFactory
>;
82 virtual ~IndexedDBFactory();
84 virtual scoped_refptr
<IndexedDBBackingStore
> OpenBackingStore(
85 const GURL
& origin_url
,
86 const base::FilePath
& data_directory
,
87 net::URLRequestContext
* request_context
,
88 blink::WebIDBDataLoss
* data_loss
,
89 std::string
* data_loss_reason
,
92 virtual scoped_refptr
<IndexedDBBackingStore
> OpenBackingStoreHelper(
93 const GURL
& origin_url
,
94 const base::FilePath
& data_directory
,
95 net::URLRequestContext
* request_context
,
96 blink::WebIDBDataLoss
* data_loss
,
97 std::string
* data_loss_message
,
101 void ReleaseBackingStore(const GURL
& origin_url
, bool immediate
);
102 void CloseBackingStore(const GURL
& origin_url
);
103 IndexedDBContextImpl
* context() const { return context_
; }
106 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
107 BackingStoreReleasedOnForcedClose
);
108 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
109 BackingStoreReleaseDelayedOnClose
);
110 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
, DatabaseFailedOpen
);
111 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
112 DeleteDatabaseClosesBackingStore
);
113 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
114 ForceCloseReleasesBackingStore
);
115 FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest
,
116 GetDatabaseNamesClosesBackingStore
);
117 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
,
118 ForceCloseOpenDatabasesOnCommitFailure
);
120 // Called internally after a database is closed, with some delay. If this
121 // factory has the last reference, it will be released.
122 void MaybeCloseBackingStore(const GURL
& origin_url
);
123 bool HasLastBackingStoreReference(const GURL
& origin_url
) const;
125 // Testing helpers, so unit tests don't need to grovel through internal state.
126 bool IsDatabaseOpen(const GURL
& origin_url
,
127 const base::string16
& name
) const;
128 bool IsBackingStoreOpen(const GURL
& origin_url
) const;
129 bool IsBackingStorePendingClose(const GURL
& origin_url
) const;
130 void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier
& identifier
);
132 IndexedDBContextImpl
* context_
;
134 typedef std::map
<IndexedDBDatabase::Identifier
,
135 IndexedDBDatabase
*> IndexedDBDatabaseMap
;
136 IndexedDBDatabaseMap database_map_
;
137 OriginDBMap origin_dbs_
;
139 typedef std::map
<GURL
, scoped_refptr
<IndexedDBBackingStore
> >
140 IndexedDBBackingStoreMap
;
141 IndexedDBBackingStoreMap backing_store_map_
;
143 std::set
<scoped_refptr
<IndexedDBBackingStore
> > session_only_backing_stores_
;
144 IndexedDBBackingStoreMap backing_stores_with_active_blobs_
;
145 std::set
<GURL
> backends_opened_since_boot_
;
148 } // namespace content
150 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_