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_backing_store.h"
     17 #include "content/browser/indexed_db/indexed_db_callbacks.h"
     18 #include "content/browser/indexed_db/indexed_db_metadata.h"
     19 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
     20 #include "content/browser/indexed_db/list_set.h"
     21 #include "url/gurl.h"
     22 
     23 namespace content {
     24 
     25 class IndexedDBConnection;
     26 class IndexedDBDatabaseCallbacks;
     27 class IndexedDBFactory;
     28 class IndexedDBKey;
     29 class IndexedDBKeyPath;
     30 class IndexedDBKeyRange;
     31 class IndexedDBTransaction;
     32 
     33 class CONTENT_EXPORT IndexedDBDatabase
     34     : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) {
     35  public:
     36   enum TaskType {
     37     NORMAL_TASK = 0,
     38     PREEMPTIVE_TASK
     39   };
     40 
     41   enum PutMode {
     42     ADD_OR_UPDATE,
     43     ADD_ONLY,
     44     CURSOR_UPDATE
     45   };
     46 
     47   typedef std::vector<IndexedDBKey> IndexKeys;
     48   // Identifier is pair of (origin url, database name).
     49   typedef std::pair<GURL, base::string16> Identifier;
     50 
     51   static const int64 kInvalidId = 0;
     52   static const int64 kMinimumIndexId = 30;
     53 
     54   static scoped_refptr<IndexedDBDatabase> Create(
     55       const base::string16& name,
     56       IndexedDBBackingStore* backing_store,
     57       IndexedDBFactory* factory,
     58       const Identifier& unique_identifier);
     59 
     60   const Identifier& identifier() const { return identifier_; }
     61   IndexedDBBackingStore* backing_store() { return backing_store_.get(); }
     62 
     63   int64 id() const { return metadata_.id; }
     64   const base::string16& name() const { return metadata_.name; }
     65 
     66   void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata,
     67                       int64 new_max_object_store_id);
     68   void RemoveObjectStore(int64 object_store_id);
     69   void AddIndex(int64 object_store_id,
     70                 const IndexedDBIndexMetadata& metadata,
     71                 int64 new_max_index_id);
     72   void RemoveIndex(int64 object_store_id, int64 index_id);
     73 
     74   void OpenConnection(
     75       scoped_refptr<IndexedDBCallbacks> callbacks,
     76       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
     77       int64 transaction_id,
     78       int64 version);
     79   void OpenConnection(
     80       scoped_refptr<IndexedDBCallbacks> callbacks,
     81       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
     82       int64 transaction_id,
     83       int64 version,
     84       blink::WebIDBDataLoss data_loss,
     85       std::string data_loss_message);
     86   void DeleteDatabase(scoped_refptr<IndexedDBCallbacks> callbacks);
     87   const IndexedDBDatabaseMetadata& metadata() const { return metadata_; }
     88 
     89   void CreateObjectStore(int64 transaction_id,
     90                          int64 object_store_id,
     91                          const base::string16& name,
     92                          const IndexedDBKeyPath& key_path,
     93                          bool auto_increment);
     94   void DeleteObjectStore(int64 transaction_id, int64 object_store_id);
     95   void CreateTransaction(int64 transaction_id,
     96                          IndexedDBConnection* connection,
     97                          const std::vector<int64>& object_store_ids,
     98                          uint16 mode);
     99   void Close(IndexedDBConnection* connection, bool forced);
    100 
    101   void Commit(int64 transaction_id);
    102   void Abort(int64 transaction_id);
    103   void Abort(int64 transaction_id, const IndexedDBDatabaseError& error);
    104 
    105   void CreateIndex(int64 transaction_id,
    106                    int64 object_store_id,
    107                    int64 index_id,
    108                    const base::string16& name,
    109                    const IndexedDBKeyPath& key_path,
    110                    bool unique,
    111                    bool multi_entry);
    112   void DeleteIndex(int64 transaction_id, int64 object_store_id, int64 index_id);
    113 
    114   IndexedDBTransactionCoordinator& transaction_coordinator() {
    115     return transaction_coordinator_;
    116   }
    117   const IndexedDBTransactionCoordinator& transaction_coordinator() const {
    118     return transaction_coordinator_;
    119   }
    120 
    121   void TransactionCreated(scoped_refptr<IndexedDBTransaction> transaction);
    122   void TransactionStarted(IndexedDBTransaction* transaction);
    123   void TransactionFinished(IndexedDBTransaction* transaction);
    124   void TransactionFinishedAndCompleteFired(IndexedDBTransaction* transaction);
    125   void TransactionFinishedAndAbortFired(IndexedDBTransaction* transaction);
    126 
    127   // Called by transactions to report failure committing to the backing store.
    128   void TransactionCommitFailed();
    129 
    130   void Get(int64 transaction_id,
    131            int64 object_store_id,
    132            int64 index_id,
    133            scoped_ptr<IndexedDBKeyRange> key_range,
    134            bool key_only,
    135            scoped_refptr<IndexedDBCallbacks> callbacks);
    136   void Put(int64 transaction_id,
    137            int64 object_store_id,
    138            std::string* value,
    139            scoped_ptr<IndexedDBKey> key,
    140            PutMode mode,
    141            scoped_refptr<IndexedDBCallbacks> callbacks,
    142            const std::vector<int64>& index_ids,
    143            const std::vector<IndexKeys>& index_keys);
    144   void SetIndexKeys(int64 transaction_id,
    145                     int64 object_store_id,
    146                     scoped_ptr<IndexedDBKey> primary_key,
    147                     const std::vector<int64>& index_ids,
    148                     const std::vector<IndexKeys>& index_keys);
    149   void SetIndexesReady(int64 transaction_id,
    150                        int64 object_store_id,
    151                        const std::vector<int64>& index_ids);
    152   void OpenCursor(int64 transaction_id,
    153                   int64 object_store_id,
    154                   int64 index_id,
    155                   scoped_ptr<IndexedDBKeyRange> key_range,
    156                   indexed_db::CursorDirection,
    157                   bool key_only,
    158                   TaskType task_type,
    159                   scoped_refptr<IndexedDBCallbacks> callbacks);
    160   void Count(int64 transaction_id,
    161              int64 object_store_id,
    162              int64 index_id,
    163              scoped_ptr<IndexedDBKeyRange> key_range,
    164              scoped_refptr<IndexedDBCallbacks> callbacks);
    165   void DeleteRange(int64 transaction_id,
    166                    int64 object_store_id,
    167                    scoped_ptr<IndexedDBKeyRange> key_range,
    168                    scoped_refptr<IndexedDBCallbacks> callbacks);
    169   void Clear(int64 transaction_id,
    170              int64 object_store_id,
    171              scoped_refptr<IndexedDBCallbacks> callbacks);
    172 
    173   // Number of connections that have progressed passed initial open call.
    174   size_t ConnectionCount() const;
    175   // Number of open calls that are blocked on other connections.
    176   size_t PendingOpenCount() const;
    177   // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
    178   size_t PendingUpgradeCount() const;
    179   // Number of running upgrades (0 or 1). Also included in ConnectionCount().
    180   size_t RunningUpgradeCount() const;
    181   // Number of pending deletes, blocked on other connections.
    182   size_t PendingDeleteCount() const;
    183 
    184   // Asynchronous tasks scheduled within transactions:
    185   void CreateObjectStoreOperation(
    186       const IndexedDBObjectStoreMetadata& object_store_metadata,
    187       IndexedDBTransaction* transaction);
    188   void CreateObjectStoreAbortOperation(int64 object_store_id,
    189                                        IndexedDBTransaction* transaction);
    190   void DeleteObjectStoreOperation(
    191       const IndexedDBObjectStoreMetadata& object_store_metadata,
    192       IndexedDBTransaction* transaction);
    193   void DeleteObjectStoreAbortOperation(
    194       const IndexedDBObjectStoreMetadata& object_store_metadata,
    195       IndexedDBTransaction* transaction);
    196   void VersionChangeOperation(int64 version,
    197                               scoped_refptr<IndexedDBCallbacks> callbacks,
    198                               scoped_ptr<IndexedDBConnection> connection,
    199                               blink::WebIDBDataLoss data_loss,
    200                               std::string data_loss_message,
    201                               IndexedDBTransaction* transaction);
    202   void VersionChangeAbortOperation(const base::string16& previous_version,
    203                                    int64 previous_int_version,
    204                                    IndexedDBTransaction* transaction);
    205   void CreateIndexOperation(int64 object_store_id,
    206                             const IndexedDBIndexMetadata& index_metadata,
    207                             IndexedDBTransaction* transaction);
    208   void DeleteIndexOperation(int64 object_store_id,
    209                             const IndexedDBIndexMetadata& index_metadata,
    210                             IndexedDBTransaction* transaction);
    211   void CreateIndexAbortOperation(int64 object_store_id,
    212                                  int64 index_id,
    213                                  IndexedDBTransaction* transaction);
    214   void DeleteIndexAbortOperation(int64 object_store_id,
    215                                  const IndexedDBIndexMetadata& index_metadata,
    216                                  IndexedDBTransaction* transaction);
    217   void GetOperation(int64 object_store_id,
    218                     int64 index_id,
    219                     scoped_ptr<IndexedDBKeyRange> key_range,
    220                     indexed_db::CursorType cursor_type,
    221                     scoped_refptr<IndexedDBCallbacks> callbacks,
    222                     IndexedDBTransaction* transaction);
    223   struct PutOperationParams;
    224   void PutOperation(scoped_ptr<PutOperationParams> params,
    225                     IndexedDBTransaction* transaction);
    226   void SetIndexesReadyOperation(size_t index_count,
    227                                 IndexedDBTransaction* transaction);
    228   struct OpenCursorOperationParams;
    229   void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params,
    230                            IndexedDBTransaction* transaction);
    231   void CountOperation(int64 object_store_id,
    232                       int64 index_id,
    233                       scoped_ptr<IndexedDBKeyRange> key_range,
    234                       scoped_refptr<IndexedDBCallbacks> callbacks,
    235                       IndexedDBTransaction* transaction);
    236   void DeleteRangeOperation(int64 object_store_id,
    237                             scoped_ptr<IndexedDBKeyRange> key_range,
    238                             scoped_refptr<IndexedDBCallbacks> callbacks,
    239                             IndexedDBTransaction* transaction);
    240   void ClearOperation(int64 object_store_id,
    241                       scoped_refptr<IndexedDBCallbacks> callbacks,
    242                       IndexedDBTransaction* transaction);
    243 
    244  private:
    245   friend class base::RefCounted<IndexedDBDatabase>;
    246 
    247   IndexedDBDatabase(const base::string16& name,
    248                     IndexedDBBackingStore* backing_store,
    249                     IndexedDBFactory* factory,
    250                     const Identifier& unique_identifier);
    251   ~IndexedDBDatabase();
    252 
    253   bool IsOpenConnectionBlocked() const;
    254   bool OpenInternal();
    255   void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
    256                                    scoped_ptr<IndexedDBConnection> connection,
    257                                    int64 transaction_id,
    258                                    int64 requested_version,
    259                                    blink::WebIDBDataLoss data_loss,
    260                                    std::string data_loss_message);
    261   void RunVersionChangeTransactionFinal(
    262       scoped_refptr<IndexedDBCallbacks> callbacks,
    263       scoped_ptr<IndexedDBConnection> connection,
    264       int64 transaction_id,
    265       int64 requested_version);
    266   void RunVersionChangeTransactionFinal(
    267       scoped_refptr<IndexedDBCallbacks> callbacks,
    268       scoped_ptr<IndexedDBConnection> connection,
    269       int64 transaction_id,
    270       int64 requested_version,
    271       blink::WebIDBDataLoss data_loss,
    272       std::string data_loss_message);
    273   void ProcessPendingCalls();
    274 
    275   bool IsDeleteDatabaseBlocked() const;
    276   void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);
    277 
    278   IndexedDBTransaction* GetTransaction(int64 transaction_id) const;
    279 
    280   bool ValidateObjectStoreId(int64 object_store_id) const;
    281   bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
    282                                        int64 index_id) const;
    283   bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
    284                                                int64 index_id) const;
    285   bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
    286                                           int64 index_id) const;
    287 
    288   scoped_refptr<IndexedDBBackingStore> backing_store_;
    289   IndexedDBDatabaseMetadata metadata_;
    290 
    291   const Identifier identifier_;
    292   // This might not need to be a scoped_refptr since the factory's lifetime is
    293   // that of the page group, but it's better to be conservitive than sorry.
    294   scoped_refptr<IndexedDBFactory> factory_;
    295 
    296   IndexedDBTransactionCoordinator transaction_coordinator_;
    297   IndexedDBTransaction* running_version_change_transaction_;
    298 
    299   typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
    300   TransactionMap transactions_;
    301 
    302   class PendingOpenCall;
    303   typedef std::list<PendingOpenCall*> PendingOpenCallList;
    304   PendingOpenCallList pending_open_calls_;
    305 
    306   class PendingUpgradeCall;
    307   scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
    308   class PendingSuccessCall;
    309   scoped_ptr<PendingSuccessCall> pending_second_half_open_;
    310 
    311   class PendingDeleteCall;
    312   typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
    313   PendingDeleteCallList pending_delete_calls_;
    314 
    315   typedef list_set<IndexedDBConnection*> ConnectionSet;
    316   ConnectionSet connections_;
    317 };
    318 
    319 }  // namespace content
    320 
    321 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
    322