indexed_db_factory.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <map>
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <set>
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_path.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/ref_counted.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/browser/indexed_db/indexed_db_callbacks.h"
16ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/browser/indexed_db/indexed_db_database.h"
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/browser/indexed_db/indexed_db_database_callbacks.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/common/content_export.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "url/gurl.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class IndexedDBBackingStore;
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class IndexedDBContextImpl;
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class CONTENT_EXPORT IndexedDBFactory
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBFactory>) {
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  explicit IndexedDBFactory(IndexedDBContextImpl* context);
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Notifications from weak pointers.
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                       const GURL& origin_url,
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       bool forcedClose);
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                        const GURL& origin_url,
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const base::FilePath& data_directory);
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Open(const string16& name,
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            int64 version,
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            int64 transaction_id,
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            scoped_refptr<IndexedDBCallbacks> callbacks,
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            const GURL& origin_url,
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            const base::FilePath& data_directory);
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void DeleteDatabase(const string16& name,
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                      scoped_refptr<IndexedDBCallbacks> callbacks,
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                      const GURL& origin_url,
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      const base::FilePath& data_directory);
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void HandleBackingStoreFailure(const GURL& origin_url);
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
54ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Iterates over all databases; for diagnostics only.
55ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  std::vector<IndexedDBDatabase*> GetOpenDatabasesForOrigin(
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const GURL& origin_url) const;
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool IsBackingStoreOpenForTesting(const GURL& origin_url) const;
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Called by the IndexedDBContext destructor so the factory can do cleanup.
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void ContextDestroyed();
62ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) protected:
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  friend class base::RefCounted<IndexedDBFactory>;
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~IndexedDBFactory();
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const GURL& origin_url,
70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const base::FilePath& data_directory,
7168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      WebKit::WebIDBCallbacks::DataLoss* data_loss,
7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      bool* disk_full);
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void CloseBackingStore(const GURL& origin_url);
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Called internally after a database is closed, with some delay. If this
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // factory has the last reference, it will be released.
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void MaybeCloseBackingStore(const GURL& origin_url);
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool HasLastBackingStoreReference(const GURL& origin_url) const;
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  IndexedDBContextImpl* context_;
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
85ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  typedef std::map<IndexedDBDatabase::Identifier,
86ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                   scoped_refptr<IndexedDBDatabase> > IndexedDBDatabaseMap;
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  IndexedDBDatabaseMap database_map_;
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      IndexedDBBackingStoreMap;
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IndexedDBBackingStoreMap backing_store_map_;
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
99