indexed_db_factory.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 content {
22
23class IndexedDBBackingStore;
24class IndexedDBContextImpl;
25
26class CONTENT_EXPORT IndexedDBFactory
27    : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) {
28 public:
29  typedef std::multimap<GURL, IndexedDBDatabase*> OriginDBMap;
30  typedef OriginDBMap::const_iterator OriginDBMapIterator;
31
32  explicit IndexedDBFactory(IndexedDBContextImpl* context);
33
34  // Notifications from weak pointers.
35  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
36                       bool forcedClose);
37
38  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
39                        const GURL& origin_url,
40                        const base::FilePath& data_directory);
41  void Open(const base::string16& name,
42            int64 version,
43            int64 transaction_id,
44            scoped_refptr<IndexedDBCallbacks> callbacks,
45            scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
46            const GURL& origin_url,
47            const base::FilePath& data_directory);
48
49  void DeleteDatabase(const base::string16& name,
50                      scoped_refptr<IndexedDBCallbacks> callbacks,
51                      const GURL& origin_url,
52                      const base::FilePath& data_directory);
53
54  void HandleBackingStoreFailure(const GURL& origin_url);
55
56  std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin(
57      const GURL& origin_url) const;
58
59  // Called by IndexedDBContext after all connections are closed, to
60  // ensure the backing store closed immediately.
61  void ForceClose(const GURL& origin_url);
62
63  // Called by the IndexedDBContext destructor so the factory can do cleanup.
64  void ContextDestroyed();
65
66  // Called by an IndexedDBDatabase when it is actually deleted.
67  void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier);
68
69  size_t GetConnectionCount(const GURL& origin_url) const;
70
71 protected:
72  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
73
74  virtual ~IndexedDBFactory();
75
76  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
77      const GURL& origin_url,
78      const base::FilePath& data_directory,
79      blink::WebIDBDataLoss* data_loss,
80      std::string* data_loss_reason,
81      bool* disk_full);
82
83  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
84  void CloseBackingStore(const GURL& origin_url);
85
86 private:
87  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
88                           BackingStoreReleasedOnForcedClose);
89  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
90                           BackingStoreReleaseDelayedOnClose);
91  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
92  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
93                           DeleteDatabaseClosesBackingStore);
94  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
95                           ForceCloseReleasesBackingStore);
96  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
97                           GetDatabaseNamesClosesBackingStore);
98  FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
99                           ForceCloseOpenDatabasesOnCommitFailure);
100
101  // Called internally after a database is closed, with some delay. If this
102  // factory has the last reference, it will be released.
103  void MaybeCloseBackingStore(const GURL& origin_url);
104  bool HasLastBackingStoreReference(const GURL& origin_url) const;
105
106  // Testing helpers, so unit tests don't need to grovel through internal state.
107  bool IsDatabaseOpen(const GURL& origin_url,
108                      const base::string16& name) const;
109  bool IsBackingStoreOpen(const GURL& origin_url) const;
110  bool IsBackingStorePendingClose(const GURL& origin_url) const;
111  void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
112
113  IndexedDBContextImpl* context_;
114
115  typedef std::map<IndexedDBDatabase::Identifier,
116                   IndexedDBDatabase*> IndexedDBDatabaseMap;
117  IndexedDBDatabaseMap database_map_;
118  OriginDBMap origin_dbs_;
119
120  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
121      IndexedDBBackingStoreMap;
122  IndexedDBBackingStoreMap backing_store_map_;
123
124  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
125};
126
127}  // namespace content
128
129#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
130