indexed_db_factory.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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  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 base::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 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
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  bool IsBackingStorePendingCloseForTesting(const GURL& origin_url) const;
60
61  // Called by IndexedDBContext after all connections are closed, to
62  // ensure the backing store closed immediately.
63  void ForceClose(const GURL& origin_url);
64
65  // Called by the IndexedDBContext destructor so the factory can do cleanup.
66  void ContextDestroyed();
67
68 protected:
69  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
70
71  virtual ~IndexedDBFactory();
72
73  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
74      const GURL& origin_url,
75      const base::FilePath& data_directory,
76      blink::WebIDBDataLoss* data_loss,
77      std::string* data_loss_reason,
78      bool* disk_full);
79
80  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
81  void CloseBackingStore(const GURL& origin_url);
82
83 private:
84  // Called internally after a database is closed, with some delay. If this
85  // factory has the last reference, it will be released.
86  void MaybeCloseBackingStore(const GURL& origin_url);
87  bool HasLastBackingStoreReference(const GURL& origin_url) const;
88
89  IndexedDBContextImpl* context_;
90
91  typedef std::map<IndexedDBDatabase::Identifier,
92                   scoped_refptr<IndexedDBDatabase> > IndexedDBDatabaseMap;
93  IndexedDBDatabaseMap database_map_;
94
95  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
96      IndexedDBBackingStoreMap;
97  IndexedDBBackingStoreMap backing_store_map_;
98
99  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
100};
101
102}  // namespace content
103
104#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
105