Home | History | Annotate | Download | only in indexed_db
      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_DATABASE_H_
      6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "content/browser/indexed_db/indexed_db.h"
     16 #include "content/browser/indexed_db/indexed_db_callbacks.h"
     17 #include "content/browser/indexed_db/indexed_db_metadata.h"
     18 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
     19 #include "content/browser/indexed_db/list_set.h"
     20 
     21 namespace content {
     22 
     23 class IndexedDBConnection;
     24 class IndexedDBDatabaseCallbacks;
     25 class IndexedDBBackingStore;
     26 class IndexedDBFactory;
     27 class IndexedDBKey;
     28 class IndexedDBKeyPath;
     29 class IndexedDBKeyRange;
     30 class IndexedDBTransaction;
     31 
     32 class CONTENT_EXPORT IndexedDBDatabase
     33     : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) {
     34  public:
     35   enum TaskType {
     36     NORMAL_TASK = 0,
     37     PREEMPTIVE_TASK
     38   };
     39 
     40   enum PutMode {
     41     ADD_OR_UPDATE,
     42     ADD_ONLY,
     43     CURSOR_UPDATE
     44   };
     45 
     46   typedef std::vector<IndexedDBKey> IndexKeys;
     47   // Identifier is pair of (origin identifier, database name).
     48   typedef std::pair<std::string, base::string16> Identifier;
     49 
     50   static const int64 kInvalidId = 0;
     51   static const int64 kMinimumIndexId = 30;
     52 
     53   static scoped_refptr<IndexedDBDatabase> Create(
     54       const string16& name,
     55       IndexedDBBackingStore* database,
     56       IndexedDBFactory* factory,
     57       const Identifier& unique_identifier);
     58   scoped_refptr<IndexedDBBackingStore> BackingStore() const;
     59 
     60   int64 id() const { return metadata_.id; }
     61   const base::string16& name() const { return metadata_.name; }
     62 
     63   void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata,
     64                       int64 new_max_object_store_id);
     65   void RemoveObjectStore(int64 object_store_id);
     66   void AddIndex(int64 object_store_id,
     67                 const IndexedDBIndexMetadata& metadata,
     68                 int64 new_max_index_id);
     69   void RemoveIndex(int64 object_store_id, int64 index_id);
     70 
     71   void OpenConnection(
     72       scoped_refptr<IndexedDBCallbacks> callbacks,
     73       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
     74       int64 transaction_id,
     75       int64 version);
     76   void OpenConnection(
     77       scoped_refptr<IndexedDBCallbacks> callbacks,
     78       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
     79       int64 transaction_id,
     80       int64 version,
     81       WebKit::WebIDBCallbacks::DataLoss data_loss);
     82   void DeleteDatabase(scoped_refptr<IndexedDBCallbacks> callbacks);
     83   const IndexedDBDatabaseMetadata& metadata() const { return metadata_; }
     84 
     85   void CreateObjectStore(int64 transaction_id,
     86                          int64 object_store_id,
     87                          const string16& name,
     88                          const IndexedDBKeyPath& key_path,
     89                          bool auto_increment);
     90   void DeleteObjectStore(int64 transaction_id, int64 object_store_id);
     91   void CreateTransaction(int64 transaction_id,
     92                          IndexedDBConnection* connection,
     93                          const std::vector<int64>& object_store_ids,
     94                          uint16 mode);
     95   void Close(IndexedDBConnection* connection);
     96 
     97   void Commit(int64 transaction_id);
     98   void Abort(int64 transaction_id);
     99   void Abort(int64 transaction_id, const IndexedDBDatabaseError& error);
    100 
    101   void CreateIndex(int64 transaction_id,
    102                    int64 object_store_id,
    103                    int64 index_id,
    104                    const string16& name,
    105                    const IndexedDBKeyPath& key_path,
    106                    bool unique,
    107                    bool multi_entry);
    108   void DeleteIndex(int64 transaction_id, int64 object_store_id, int64 index_id);
    109 
    110   IndexedDBTransactionCoordinator& transaction_coordinator() {
    111     return transaction_coordinator_;
    112   }
    113   const IndexedDBTransactionCoordinator& transaction_coordinator() const {
    114     return transaction_coordinator_;
    115   }
    116 
    117   void TransactionStarted(IndexedDBTransaction* transaction);
    118   void TransactionFinished(IndexedDBTransaction* transaction);
    119   void TransactionFinishedAndCompleteFired(IndexedDBTransaction* transaction);
    120   void TransactionFinishedAndAbortFired(IndexedDBTransaction* transaction);
    121 
    122   void Get(int64 transaction_id,
    123            int64 object_store_id,
    124            int64 index_id,
    125            scoped_ptr<IndexedDBKeyRange> key_range,
    126            bool key_only,
    127            scoped_refptr<IndexedDBCallbacks> callbacks);
    128   void Put(int64 transaction_id,
    129            int64 object_store_id,
    130            std::string* value,
    131            scoped_ptr<IndexedDBKey> key,
    132            PutMode mode,
    133            scoped_refptr<IndexedDBCallbacks> callbacks,
    134            const std::vector<int64>& index_ids,
    135            const std::vector<IndexKeys>& index_keys);
    136   void SetIndexKeys(int64 transaction_id,
    137                     int64 object_store_id,
    138                     scoped_ptr<IndexedDBKey> primary_key,
    139                     const std::vector<int64>& index_ids,
    140                     const std::vector<IndexKeys>& index_keys);
    141   void SetIndexesReady(int64 transaction_id,
    142                        int64 object_store_id,
    143                        const std::vector<int64>& index_ids);
    144   void OpenCursor(int64 transaction_id,
    145                   int64 object_store_id,
    146                   int64 index_id,
    147                   scoped_ptr<IndexedDBKeyRange> key_range,
    148                   indexed_db::CursorDirection,
    149                   bool key_only,
    150                   TaskType task_type,
    151                   scoped_refptr<IndexedDBCallbacks> callbacks);
    152   void Count(int64 transaction_id,
    153              int64 object_store_id,
    154              int64 index_id,
    155              scoped_ptr<IndexedDBKeyRange> key_range,
    156              scoped_refptr<IndexedDBCallbacks> callbacks);
    157   void DeleteRange(int64 transaction_id,
    158                    int64 object_store_id,
    159                    scoped_ptr<IndexedDBKeyRange> key_range,
    160                    scoped_refptr<IndexedDBCallbacks> callbacks);
    161   void Clear(int64 transaction_id,
    162              int64 object_store_id,
    163              scoped_refptr<IndexedDBCallbacks> callbacks);
    164 
    165   // Number of connections that have progressed passed initial open call.
    166   size_t ConnectionCount() const;
    167   // Number of open calls that are blocked on other connections.
    168   size_t PendingOpenCount() const;
    169   // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
    170   size_t PendingUpgradeCount() const;
    171   // Number of running upgrades (0 or 1). Also included in ConnectionCount().
    172   size_t RunningUpgradeCount() const;
    173   // Number of pending deletes, blocked on other connections.
    174   size_t PendingDeleteCount() const;
    175 
    176  private:
    177   friend class base::RefCounted<IndexedDBDatabase>;
    178 
    179   IndexedDBDatabase(
    180       const string16& name,
    181       IndexedDBBackingStore* database,
    182       IndexedDBFactory* factory,
    183       const Identifier& unique_identifier);
    184   ~IndexedDBDatabase();
    185 
    186   bool IsOpenConnectionBlocked() const;
    187   bool OpenInternal();
    188   void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
    189                                    scoped_ptr<IndexedDBConnection> connection,
    190                                    int64 transaction_id,
    191                                    int64 requested_version,
    192                                    WebKit::WebIDBCallbacks::DataLoss data_loss);
    193   void RunVersionChangeTransactionFinal(
    194       scoped_refptr<IndexedDBCallbacks> callbacks,
    195       scoped_ptr<IndexedDBConnection> connection,
    196       int64 transaction_id,
    197       int64 requested_version);
    198   void RunVersionChangeTransactionFinal(
    199       scoped_refptr<IndexedDBCallbacks> callbacks,
    200       scoped_ptr<IndexedDBConnection> connection,
    201       int64 transaction_id,
    202       int64 requested_version,
    203       WebKit::WebIDBCallbacks::DataLoss data_loss);
    204   void ProcessPendingCalls();
    205 
    206   bool IsDeleteDatabaseBlocked() const;
    207   void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);
    208 
    209   IndexedDBTransaction* GetTransaction(int64 transaction_id) const;
    210 
    211   bool ValidateObjectStoreId(int64 object_store_id) const;
    212   bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
    213                                        int64 index_id) const;
    214   bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
    215                                                int64 index_id) const;
    216   bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
    217                                           int64 index_id) const;
    218 
    219   class VersionChangeOperation;
    220 
    221   // When a "versionchange" transaction aborts, these restore the back-end
    222   // object hierarchy.
    223   class VersionChangeAbortOperation;
    224 
    225   scoped_refptr<IndexedDBBackingStore> backing_store_;
    226   IndexedDBDatabaseMetadata metadata_;
    227 
    228   const Identifier identifier_;
    229   // This might not need to be a scoped_refptr since the factory's lifetime is
    230   // that of the page group, but it's better to be conservitive than sorry.
    231   scoped_refptr<IndexedDBFactory> factory_;
    232 
    233   IndexedDBTransactionCoordinator transaction_coordinator_;
    234   IndexedDBTransaction* running_version_change_transaction_;
    235 
    236   typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
    237   TransactionMap transactions_;
    238 
    239   class PendingOpenCall;
    240   typedef std::list<PendingOpenCall*> PendingOpenCallList;
    241   PendingOpenCallList pending_open_calls_;
    242 
    243   class PendingUpgradeCall;
    244   scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
    245   class PendingSuccessCall;
    246   scoped_ptr<PendingSuccessCall> pending_second_half_open_;
    247 
    248   class PendingDeleteCall;
    249   typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
    250   PendingDeleteCallList pending_delete_calls_;
    251 
    252   typedef list_set<IndexedDBConnection*> ConnectionSet;
    253   ConnectionSet connections_;
    254 
    255   bool closing_connection_;
    256 };
    257 
    258 }  // namespace content
    259 
    260 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
    261