indexed_db_factory.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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::RefCounted<IndexedDBFactory>) {
28 public:
29  explicit IndexedDBFactory(IndexedDBContextImpl* context);
30
31  // Notifications from weak pointers.
32  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
33                       const GURL& origin_url,
34                       bool forcedClose);
35
36  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
37                        const GURL& origin_url,
38                        const base::FilePath& data_directory);
39  void Open(const string16& name,
40            int64 version,
41            int64 transaction_id,
42            scoped_refptr<IndexedDBCallbacks> callbacks,
43            scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
44            const GURL& origin_url,
45            const base::FilePath& data_directory);
46
47  void DeleteDatabase(const 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
54  // Iterates over all databases; for diagnostics only.
55  std::vector<IndexedDBDatabase*> GetOpenDatabasesForOrigin(
56      const GURL& origin_url) const;
57
58  bool IsBackingStoreOpenForTesting(const GURL& origin_url) const;
59
60  // Called by the IndexedDBContext destructor so the factory can do cleanup.
61  void ContextDestroyed();
62
63 protected:
64  friend class base::RefCounted<IndexedDBFactory>;
65
66  virtual ~IndexedDBFactory();
67
68  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
69      const GURL& origin_url,
70      const base::FilePath& data_directory,
71      WebKit::WebIDBCallbacks::DataLoss* data_loss,
72      std::string* data_loss_reason,
73      bool* disk_full);
74
75  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
76  void CloseBackingStore(const GURL& origin_url);
77
78 private:
79  // Called internally after a database is closed, with some delay. If this
80  // factory has the last reference, it will be released.
81  void MaybeCloseBackingStore(const GURL& origin_url);
82  bool HasLastBackingStoreReference(const GURL& origin_url) const;
83
84  IndexedDBContextImpl* context_;
85
86  typedef std::map<IndexedDBDatabase::Identifier,
87                   scoped_refptr<IndexedDBDatabase> > IndexedDBDatabaseMap;
88  IndexedDBDatabaseMap database_map_;
89
90  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
91      IndexedDBBackingStoreMap;
92  IndexedDBBackingStoreMap backing_store_map_;
93
94  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
95};
96
97}  // namespace content
98
99#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
100