indexed_db_factory.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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 net {
22class URLRequestContext;
23}
24
25namespace content {
26
27class IndexedDBBackingStore;
28class IndexedDBContextImpl;
29struct IndexedDBPendingConnection;
30
31class CONTENT_EXPORT IndexedDBFactory
32    : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) {
33 public:
34  typedef std::multimap<GURL, IndexedDBDatabase*> OriginDBMap;
35  typedef OriginDBMap::const_iterator OriginDBMapIterator;
36
37  explicit IndexedDBFactory(IndexedDBContextImpl* context);
38
39  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
40                       bool forcedClose);
41
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);
51
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);
57
58  void HandleBackingStoreFailure(const GURL& origin_url);
59  void HandleBackingStoreCorruption(const GURL& origin_url,
60                                    const IndexedDBDatabaseError& error);
61
62  std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin(
63      const GURL& origin_url) const;
64
65  void ForceClose(const GURL& origin_url);
66
67  // Called by the IndexedDBContext destructor so the factory can do cleanup.
68  void ContextDestroyed();
69
70  // Called by the IndexedDBActiveBlobRegistry.
71  virtual void ReportOutstandingBlobs(const GURL& origin_url,
72                                      bool blobs_outstanding);
73
74  // Called by an IndexedDBDatabase when it is actually deleted.
75  void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier);
76
77  size_t GetConnectionCount(const GURL& origin_url) const;
78
79 protected:
80  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
81
82  virtual ~IndexedDBFactory();
83
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,
90      bool* disk_full);
91
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,
98      bool* disk_full,
99      bool first_time);
100
101  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
102  void CloseBackingStore(const GURL& origin_url);
103  IndexedDBContextImpl* context() const { return context_; }
104
105 private:
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);
119
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;
124
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);
131
132  IndexedDBContextImpl* context_;
133
134  typedef std::map<IndexedDBDatabase::Identifier,
135                   IndexedDBDatabase*> IndexedDBDatabaseMap;
136  IndexedDBDatabaseMap database_map_;
137  OriginDBMap origin_dbs_;
138
139  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
140      IndexedDBBackingStoreMap;
141  IndexedDBBackingStoreMap backing_store_map_;
142
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_;
146};
147
148}  // namespace content
149
150#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
151