indexed_db_factory.h revision a02191e04bc25c4935f804f2c080ae28663d096d
19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Copyright (c) 2013 The Chromium Authors. All rights reserved.
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Use of this source code is governed by a BSD-style license that can be
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// found in the LICENSE file.
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <map>
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <set>
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/basictypes.h"
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/files/file_path.h"
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/memory/ref_counted.h"
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/string16.h"
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "content/browser/indexed_db/indexed_db_callbacks.h"
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "content/browser/indexed_db/indexed_db_database.h"
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "content/browser/indexed_db/indexed_db_database_callbacks.h"
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "content/common/content_export.h"
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "url/gurl.h"
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallnamespace net {
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallclass URLRequestContext;
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallnamespace content {
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallclass IndexedDBBackingStore;
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallclass IndexedDBContextImpl;
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct IndexedDBPendingConnection;
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallclass CONTENT_EXPORT IndexedDBFactory
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    : NON_EXPORTED_BASE(public base::RefCountedThreadSafe<IndexedDBFactory>) {
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall public:
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  typedef std::multimap<GURL, IndexedDBDatabase*> OriginDBMap;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  typedef OriginDBMap::const_iterator OriginDBMapIterator;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  explicit IndexedDBFactory(IndexedDBContextImpl* context);
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void ReleaseDatabase(const IndexedDBDatabase::Identifier& identifier,
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       bool forcedClose);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void GetDatabaseNames(scoped_refptr<IndexedDBCallbacks> callbacks,
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        const GURL& origin_url,
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        const base::FilePath& data_directory);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void Open(const base::string16& name,
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            const IndexedDBPendingConnection& connection,
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            net::URLRequestContext* request_context,
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            const GURL& origin_url,
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            const base::FilePath& data_directory);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void DeleteDatabase(const base::string16& name,
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      net::URLRequestContext* request_context,
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      scoped_refptr<IndexedDBCallbacks> callbacks,
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      const GURL& origin_url,
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      const base::FilePath& data_directory);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void HandleBackingStoreFailure(const GURL& origin_url);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void HandleBackingStoreCorruption(const GURL& origin_url,
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    const IndexedDBDatabaseError& error);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  std::pair<OriginDBMapIterator, OriginDBMapIterator> GetOpenDatabasesForOrigin(
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      const GURL& origin_url) const;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void ForceClose(const GURL& origin_url);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Called by the IndexedDBContext destructor so the factory can do cleanup.
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void ContextDestroyed();
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Called by the IndexedDBActiveBlobRegistry.
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  virtual void ReportOutstandingBlobs(const GURL& origin_url,
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                      bool blobs_outstanding);
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Called by an IndexedDBDatabase when it is actually deleted.
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void DatabaseDeleted(const IndexedDBDatabase::Identifier& identifier);
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  size_t GetConnectionCount(const GURL& origin_url) const;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall protected:
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  friend class base::RefCountedThreadSafe<IndexedDBFactory>;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  virtual ~IndexedDBFactory();
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore(
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      const GURL& origin_url,
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      const base::FilePath& data_directory,
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      net::URLRequestContext* request_context,
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      blink::WebIDBDataLoss* data_loss,
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      std::string* data_loss_reason,
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      bool* disk_full);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void ReleaseBackingStore(const GURL& origin_url, bool immediate);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void CloseBackingStore(const GURL& origin_url);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IndexedDBContextImpl* context() const { return context_; }
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall private:
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           BackingStoreReleasedOnForcedClose);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           BackingStoreReleaseDelayedOnClose);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest, DatabaseFailedOpen);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           DeleteDatabaseClosesBackingStore);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           ForceCloseReleasesBackingStore);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBFactoryTest,
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           GetDatabaseNamesClosesBackingStore);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FRIEND_TEST_ALL_PREFIXES(IndexedDBTest,
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           ForceCloseOpenDatabasesOnCommitFailure);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Called internally after a database is closed, with some delay. If this
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // factory has the last reference, it will be released.
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void MaybeCloseBackingStore(const GURL& origin_url);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool HasLastBackingStoreReference(const GURL& origin_url) const;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Testing helpers, so unit tests don't need to grovel through internal state.
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool IsDatabaseOpen(const GURL& origin_url,
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      const base::string16& name) const;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool IsBackingStoreOpen(const GURL& origin_url) const;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool IsBackingStorePendingClose(const GURL& origin_url) const;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void RemoveDatabaseFromMaps(const IndexedDBDatabase::Identifier& identifier);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IndexedDBContextImpl* context_;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  typedef std::map<IndexedDBDatabase::Identifier,
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   IndexedDBDatabase*> IndexedDBDatabaseMap;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IndexedDBDatabaseMap database_map_;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  OriginDBMap origin_dbs_;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  typedef std::map<GURL, scoped_refptr<IndexedDBBackingStore> >
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      IndexedDBBackingStoreMap;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IndexedDBBackingStoreMap backing_store_map_;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  std::set<scoped_refptr<IndexedDBBackingStore> > session_only_backing_stores_;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IndexedDBBackingStoreMap backing_stores_with_active_blobs_;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}  // namespace content
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FACTORY_H_
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall