indexed_db_factory.h revision 116680a4aac90f2aa7413d9095a592090648e557
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#include <string>
11#include <utility>
12
13#include "base/basictypes.h"
14#include "base/files/file_path.h"
15#include "base/memory/ref_counted.h"
16#include "base/strings/string16.h"
17#include "content/browser/indexed_db/indexed_db_callbacks.h"
18#include "content/browser/indexed_db/indexed_db_database.h"
19#include "content/browser/indexed_db/indexed_db_database_callbacks.h"
20#include "content/common/content_export.h"
21#include "url/gurl.h"
22
23namespace net {
24class URLRequestContext;
25}
26
27namespace content {
28
29class IndexedDBBackingStore;
30class IndexedDBContextImpl;
31struct IndexedDBPendingConnection;
32
33class CONTENT_EXPORT IndexedDBFactory
34    : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) {
35 public:
36  typedef std::multimap<GURL, IndexedDBDatabase*> OriginDBMap;
37  typedef OriginDBMap::const_iterator OriginDBMapIterator;
38
39  explicit IndexedDBFactory(IndexedDBContextImpl* context);
40
41  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
42                       bool forcedClose);
43
44  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
45                        const GURL& origin_url,
46                        const base::FilePath& data_directory,
47                        net::URLRequestContext* request_context);
48  void Open(const base::string16& name,
49            const IndexedDBPendingConnection& connection,
50            net::URLRequestContext* request_context,
51            const GURL& origin_url,
52            const base::FilePath& data_directory);
53
54  void DeleteDatabase(const base::string16& name,
55                      net::URLRequestContext* request_context,
56                      scoped_refptr<IndexedDBCallbacks> callbacks,
57                      const GURL& origin_url,
58                      const base::FilePath& data_directory);
59
60  void HandleBackingStoreFailure(const GURL& origin_url);
61  void HandleBackingStoreCorruption(const GURL& origin_url,
62                                    const IndexedDBDatabaseError& error);
63
64  std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin(
65      const GURL& origin_url) const;
66
67  void ForceClose(const GURL& origin_url);
68
69  // Called by the IndexedDBContext destructor so the factory can do cleanup.
70  void ContextDestroyed();
71
72  // Called by the IndexedDBActiveBlobRegistry.
73  virtual void ReportOutstandingBlobs(const GURL& origin_url,
74                                      bool blobs_outstanding);
75
76  // Called by an IndexedDBDatabase when it is actually deleted.
77  void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier);
78
79  size_t GetConnectionCount(const GURL& origin_url) const;
80
81 protected:
82  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
83
84  virtual ~IndexedDBFactory();
85
86  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
87      const GURL& origin_url,
88      const base::FilePath& data_directory,
89      net::URLRequestContext* request_context,
90      blink::WebIDBDataLoss* data_loss,
91      std::string* data_loss_reason,
92      bool* disk_full,
93      leveldb::Status* status);
94
95  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper(
96      const GURL& origin_url,
97      const base::FilePath& data_directory,
98      net::URLRequestContext* request_context,
99      blink::WebIDBDataLoss* data_loss,
100      std::string* data_loss_message,
101      bool* disk_full,
102      bool first_time,
103      leveldb::Status* status);
104
105  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
106  void CloseBackingStore(const GURL& origin_url);
107  IndexedDBContextImpl* context() const { return context_; }
108
109 private:
110  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
111                           BackingStoreReleasedOnForcedClose);
112  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
113                           BackingStoreReleaseDelayedOnClose);
114  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
115  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
116                           DeleteDatabaseClosesBackingStore);
117  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
118                           ForceCloseReleasesBackingStore);
119  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
120                           GetDatabaseNamesClosesBackingStore);
121  FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
122                           ForceCloseOpenDatabasesOnCommitFailure);
123
124  // Called internally after a database is closed, with some delay. If this
125  // factory has the last reference, it will be released.
126  void MaybeCloseBackingStore(const GURL& origin_url);
127  bool HasLastBackingStoreReference(const GURL& origin_url) const;
128
129  // Testing helpers, so unit tests don't need to grovel through internal state.
130  bool IsDatabaseOpen(const GURL& origin_url,
131                      const base::string16& name) const;
132  bool IsBackingStoreOpen(const GURL& origin_url) const;
133  bool IsBackingStorePendingClose(const GURL& origin_url) const;
134  void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
135
136  IndexedDBContextImpl* context_;
137
138  typedef std::map<IndexedDBDatabase::Identifier,
139                   IndexedDBDatabase*> IndexedDBDatabaseMap;
140  IndexedDBDatabaseMap database_map_;
141  OriginDBMap origin_dbs_;
142
143  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
144      IndexedDBBackingStoreMap;
145  IndexedDBBackingStoreMap backing_store_map_;
146
147  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
148  IndexedDBBackingStoreMap backing_stores_with_active_blobs_;
149  std::set<GURL> backends_opened_since_boot_;
150
151  DISALLOW_COPY_AND_ASSIGN(IndexedDBFactory);
152};
153
154}  // namespace content
155
156#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
157