indexed_db_factory.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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
94  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper(
95      const GURL& origin_url,
96      const base::FilePath& data_directory,
97      net::URLRequestContext* request_context,
98      blink::WebIDBDataLoss* data_loss,
99      std::string* data_loss_message,
100      bool* disk_full,
101      bool first_time);
102
103  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
104  void CloseBackingStore(const GURL& origin_url);
105  IndexedDBContextImpl* context() const { return context_; }
106
107 private:
108  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
109                           BackingStoreReleasedOnForcedClose);
110  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
111                           BackingStoreReleaseDelayedOnClose);
112  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
113  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
114                           DeleteDatabaseClosesBackingStore);
115  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
116                           ForceCloseReleasesBackingStore);
117  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
118                           GetDatabaseNamesClosesBackingStore);
119  FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
120                           ForceCloseOpenDatabasesOnCommitFailure);
121
122  // Called internally after a database is closed, with some delay. If this
123  // factory has the last reference, it will be released.
124  void MaybeCloseBackingStore(const GURL& origin_url);
125  bool HasLastBackingStoreReference(const GURL& origin_url) const;
126
127  // Testing helpers, so unit tests don't need to grovel through internal state.
128  bool IsDatabaseOpen(const GURL& origin_url,
129                      const base::string16& name) const;
130  bool IsBackingStoreOpen(const GURL& origin_url) const;
131  bool IsBackingStorePendingClose(const GURL& origin_url) const;
132  void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
133
134  IndexedDBContextImpl* context_;
135
136  typedef std::map<IndexedDBDatabase::Identifier,
137                   IndexedDBDatabase*> IndexedDBDatabaseMap;
138  IndexedDBDatabaseMap database_map_;
139  OriginDBMap origin_dbs_;
140
141  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
142      IndexedDBBackingStoreMap;
143  IndexedDBBackingStoreMap backing_store_map_;
144
145  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
146  IndexedDBBackingStoreMap backing_stores_with_active_blobs_;
147  std::set<GURL> backends_opened_since_boot_;
148
149  DISALLOW_COPY_AND_ASSIGN(IndexedDBFactory);
150};
151
152}  // namespace content
153
154#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
155