indexed_db_factory.h revision 58537e28ecd584eab876aee8be7156509866d23a
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/browser/indexed_db/indexed_db_factory.h"
19#include "content/common/content_export.h"
20
21namespace content {
22
23class IndexedDBBackingStore;
24
25class CONTENT_EXPORT IndexedDBFactory
26    : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBFactory>) {
27 public:
28  IndexedDBFactory();
29
30  // Notifications from weak pointers.
31  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
32                       bool forcedClose);
33
34  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
35                        const std::string& origin_identifier,
36                        const base::FilePath& data_directory);
37  void Open(const string16& name,
38            int64 version,
39            int64 transaction_id,
40            scoped_refptr<IndexedDBCallbacks> callbacks,
41            scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
42            const std::string& origin_identifier,
43            const base::FilePath& data_directory);
44
45  void DeleteDatabase(const string16& name,
46                      scoped_refptr<IndexedDBCallbacks> callbacks,
47                      const std::string& origin_identifier,
48                      const base::FilePath& data_directory);
49
50  // Iterates over all databases; for diagnostics only.
51  std::vector<IndexedDBDatabase*> GetOpenDatabasesForOrigin(
52      const std::string& origin_identifier) const;
53
54 protected:
55  friend class base::RefCounted<IndexedDBFactory>;
56
57  virtual ~IndexedDBFactory();
58
59  scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
60      const std::string& origin_identifier,
61      const base::FilePath& data_directory,
62      WebKit::WebIDBCallbacks::DataLoss* data_loss);
63
64  void ReleaseBackingStore(const std::string& identifier, bool immediate);
65  void CloseBackingStore(const std::string& identifier);
66
67 private:
68  // Called internally after a database is closed, with some delay. If this
69  // factory has the last reference, it will be released.
70  void MaybeCloseBackingStore(const std::string& identifier);
71  bool HasLastBackingStoreReference(const std::string& identifier) const;
72
73  typedef std::map<IndexedDBDatabase::Identifier,
74                   scoped_refptr<IndexedDBDatabase> > IndexedDBDatabaseMap;
75  IndexedDBDatabaseMap database_map_;
76
77  typedef std::map<std::string, scoped_refptr<IndexedDBBackingStore> >
78      IndexedDBBackingStoreMap;
79  IndexedDBBackingStoreMap backing_store_map_;
80
81  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
82};
83
84}  // namespace content
85
86#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
87