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