indexed_db_factory.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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  void Open(const base::string16& name,
46            const IndexedDBPendingConnection& connection,
47            net::URLRequestContext* request_context,
48            const GURL& origin_url,
49            const base::FilePath& data_directory);
50
51  void DeleteDatabase(const base::string16& name,
52                      net::URLRequestContext* request_context,
53                      scoped_refptr<IndexedDBCallbacks> callbacks,
54                      const GURL& origin_url,
55                      const base::FilePath& data_directory);
56
57  void HandleBackingStoreFailure(const GURL& origin_url);
58  void HandleBackingStoreCorruption(const GURL& origin_url,
59                                    const IndexedDBDatabaseError& error);
60
61  std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin(
62      const GURL& origin_url) const;
63
64  void ForceClose(const GURL& origin_url);
65
66  // Called by the IndexedDBContext destructor so the factory can do cleanup.
67  void ContextDestroyed();
68
69  // Called by the IndexedDBActiveBlobRegistry.
70  virtual void ReportOutstandingBlobs(const GURL& origin_url,
71                                      bool blobs_outstanding);
72
73  // Called by an IndexedDBDatabase when it is actually deleted.
74  void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier);
75
76  size_t GetConnectionCount(const GURL& origin_url) const;
77
78 protected:
79  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
80
81  virtual ~IndexedDBFactory();
82
83  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
84      const GURL& origin_url,
85      const base::FilePath& data_directory,
86      net::URLRequestContext* request_context,
87      blink::WebIDBDataLoss* data_loss,
88      std::string* data_loss_reason,
89      bool* disk_full);
90
91  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
92  void CloseBackingStore(const GURL& origin_url);
93  IndexedDBContextImpl* context() const { return context_; }
94
95 private:
96  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
97                           BackingStoreReleasedOnForcedClose);
98  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
99                           BackingStoreReleaseDelayedOnClose);
100  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
101  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
102                           DeleteDatabaseClosesBackingStore);
103  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
104                           ForceCloseReleasesBackingStore);
105  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
106                           GetDatabaseNamesClosesBackingStore);
107  FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
108                           ForceCloseOpenDatabasesOnCommitFailure);
109
110  // Called internally after a database is closed, with some delay. If this
111  // factory has the last reference, it will be released.
112  void MaybeCloseBackingStore(const GURL& origin_url);
113  bool HasLastBackingStoreReference(const GURL& origin_url) const;
114
115  // Testing helpers, so unit tests don't need to grovel through internal state.
116  bool IsDatabaseOpen(const GURL& origin_url,
117                      const base::string16& name) const;
118  bool IsBackingStoreOpen(const GURL& origin_url) const;
119  bool IsBackingStorePendingClose(const GURL& origin_url) const;
120  void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
121
122  IndexedDBContextImpl* context_;
123
124  typedef std::map<IndexedDBDatabase::Identifier,
125                   IndexedDBDatabase*> IndexedDBDatabaseMap;
126  IndexedDBDatabaseMap database_map_;
127  OriginDBMap origin_dbs_;
128
129  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
130      IndexedDBBackingStoreMap;
131  IndexedDBBackingStoreMap backing_store_map_;
132
133  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
134  IndexedDBBackingStoreMap backing_stores_with_active_blobs_;
135};
136
137}  // namespace content
138
139#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
140