| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 
| 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 
| 7 | 7 | 
| 8 #include <stdint.h> | 8 #include <stdint.h> | 
| 9 | 9 | 
| 10 #include <memory> | 10 #include <memory> | 
| 11 #include <queue> | 11 #include <queue> | 
| 12 #include <set> | 12 #include <set> | 
| 13 #include <stack> | 13 #include <stack> | 
| 14 | 14 | 
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" | 
| 16 #include "base/macros.h" | 16 #include "base/macros.h" | 
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" | 
|  | 18 #include "base/memory/weak_ptr.h" | 
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" | 
| 19 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" | 
| 20 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 21 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 
| 21 #include "content/browser/indexed_db/indexed_db_connection.h" | 22 #include "content/browser/indexed_db/indexed_db_connection.h" | 
| 22 #include "content/browser/indexed_db/indexed_db_database.h" | 23 #include "content/browser/indexed_db/indexed_db_database.h" | 
| 23 #include "content/browser/indexed_db/indexed_db_database_error.h" | 24 #include "content/browser/indexed_db/indexed_db_database_error.h" | 
| 24 #include "content/browser/indexed_db/indexed_db_observer.h" | 25 #include "content/browser/indexed_db/indexed_db_observer.h" | 
| 25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 
|  | 27 #include "url/origin.h" | 
| 26 | 28 | 
| 27 namespace content { | 29 namespace content { | 
| 28 | 30 | 
| 29 class BlobWriteCallbackImpl; | 31 class BlobWriteCallbackImpl; | 
| 30 class IndexedDBCursor; | 32 class IndexedDBCursor; | 
| 31 class IndexedDBDatabaseCallbacks; | 33 class IndexedDBDatabaseCallbacks; | 
| 32 | 34 | 
| 33 class CONTENT_EXPORT IndexedDBTransaction | 35 class CONTENT_EXPORT IndexedDBTransaction { | 
| 34     : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) { |  | 
| 35  public: | 36  public: | 
| 36   using Operation = base::Callback<leveldb::Status(IndexedDBTransaction*)>; | 37   using Operation = base::Callback<leveldb::Status(IndexedDBTransaction*)>; | 
| 37   using AbortOperation = base::Closure; | 38   using AbortOperation = base::Closure; | 
| 38 | 39 | 
| 39   enum State { | 40   enum State { | 
| 40     CREATED,     // Created, but not yet started by coordinator. | 41     CREATED,     // Created, but not yet started by coordinator. | 
| 41     STARTED,     // Started by the coordinator. | 42     STARTED,     // Started by the coordinator. | 
| 42     COMMITTING,  // In the process of committing, possibly waiting for blobs | 43     COMMITTING,  // In the process of committing, possibly waiting for blobs | 
| 43                  // to be written. | 44                  // to be written. | 
| 44     FINISHED,    // Either aborted or committed. | 45     FINISHED,    // Either aborted or committed. | 
| 45   }; | 46   }; | 
| 46 | 47 | 
|  | 48   virtual ~IndexedDBTransaction(); | 
|  | 49 | 
|  | 50   leveldb::Status Commit(); | 
|  | 51 | 
|  | 52   // This object is destroyed by these method calls. | 
| 47   virtual void Abort(); | 53   virtual void Abort(); | 
| 48   leveldb::Status Commit(); |  | 
| 49   void Abort(const IndexedDBDatabaseError& error); | 54   void Abort(const IndexedDBDatabaseError& error); | 
| 50 | 55 | 
| 51   // Called by the transaction coordinator when this transaction is unblocked. | 56   // Called by the transaction coordinator when this transaction is unblocked. | 
| 52   void Start(); | 57   void Start(); | 
| 53 | 58 | 
| 54   blink::WebIDBTransactionMode mode() const { return mode_; } | 59   blink::WebIDBTransactionMode mode() const { return mode_; } | 
| 55   const std::set<int64_t>& scope() const { return object_store_ids_; } | 60   const std::set<int64_t>& scope() const { return object_store_ids_; } | 
| 56 | 61 | 
|  | 62   // Tasks cannot call Commit. | 
| 57   void ScheduleTask(Operation task) { | 63   void ScheduleTask(Operation task) { | 
| 58     ScheduleTask(blink::WebIDBTaskTypeNormal, task); | 64     ScheduleTask(blink::WebIDBTaskTypeNormal, task); | 
| 59   } | 65   } | 
| 60   void ScheduleTask(blink::WebIDBTaskType, Operation task); | 66   void ScheduleTask(blink::WebIDBTaskType, Operation task); | 
| 61   void ScheduleAbortTask(AbortOperation abort_task); | 67   void ScheduleAbortTask(AbortOperation abort_task); | 
| 62   void RegisterOpenCursor(IndexedDBCursor* cursor); | 68   void RegisterOpenCursor(IndexedDBCursor* cursor); | 
| 63   void UnregisterOpenCursor(IndexedDBCursor* cursor); | 69   void UnregisterOpenCursor(IndexedDBCursor* cursor); | 
| 64   void AddPreemptiveEvent() { pending_preemptive_events_++; } | 70   void AddPreemptiveEvent() { pending_preemptive_events_++; } | 
| 65   void DidCompletePreemptiveEvent() { | 71   void DidCompletePreemptiveEvent() { | 
| 66     pending_preemptive_events_--; | 72     pending_preemptive_events_--; | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 79   void RecordObserverForLastObservation(int32_t connection_id, | 85   void RecordObserverForLastObservation(int32_t connection_id, | 
| 80                                         int32_t observer_id); | 86                                         int32_t observer_id); | 
| 81 | 87 | 
| 82   IndexedDBBackingStore::Transaction* BackingStoreTransaction() { | 88   IndexedDBBackingStore::Transaction* BackingStoreTransaction() { | 
| 83     return transaction_.get(); | 89     return transaction_.get(); | 
| 84   } | 90   } | 
| 85   int64_t id() const { return id_; } | 91   int64_t id() const { return id_; } | 
| 86 | 92 | 
| 87   IndexedDBDatabase* database() const { return database_.get(); } | 93   IndexedDBDatabase* database() const { return database_.get(); } | 
| 88   IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } | 94   IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } | 
| 89   IndexedDBConnection* connection() const { return connection_.get(); } | 95   IndexedDBConnection* connection() const { return connection_; } | 
| 90 | 96 | 
| 91   State state() const { return state_; } | 97   State state() const { return state_; } | 
| 92   bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } | 98   bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } | 
| 93 | 99 | 
| 94   struct Diagnostics { | 100   struct Diagnostics { | 
| 95     base::Time creation_time; | 101     base::Time creation_time; | 
| 96     base::Time start_time; | 102     base::Time start_time; | 
| 97     int tasks_scheduled; | 103     int tasks_scheduled; | 
| 98     int tasks_completed; | 104     int tasks_completed; | 
| 99   }; | 105   }; | 
| 100 | 106 | 
| 101   const Diagnostics& diagnostics() const { return diagnostics_; } | 107   const Diagnostics& diagnostics() const { return diagnostics_; } | 
| 102 | 108 | 
|  | 109   void set_size(int64_t size) { size_ = size; } | 
|  | 110   int64_t size() const { return size_; } | 
|  | 111 | 
| 103  protected: | 112  protected: | 
| 104   // Test classes may derive, but most creation should be done via | 113   // Test classes may derive, but most creation should be done via | 
| 105   // IndexedDBClassFactory. | 114   // IndexedDBClassFactory. | 
| 106   IndexedDBTransaction( | 115   IndexedDBTransaction( | 
| 107       int64_t id, | 116       int64_t id, | 
| 108       base::WeakPtr<IndexedDBConnection> connection, | 117       IndexedDBConnection* connection, | 
| 109       const std::set<int64_t>& object_store_ids, | 118       const std::set<int64_t>& object_store_ids, | 
| 110       blink::WebIDBTransactionMode mode, | 119       blink::WebIDBTransactionMode mode, | 
| 111       IndexedDBBackingStore::Transaction* backing_store_transaction); | 120       IndexedDBBackingStore::Transaction* backing_store_transaction); | 
| 112   virtual ~IndexedDBTransaction(); |  | 
| 113 | 121 | 
| 114   // May be overridden in tests. | 122   // May be overridden in tests. | 
| 115   virtual base::TimeDelta GetInactivityTimeout() const; | 123   virtual base::TimeDelta GetInactivityTimeout() const; | 
| 116 | 124 | 
| 117  private: | 125  private: | 
| 118   friend class BlobWriteCallbackImpl; | 126   friend class BlobWriteCallbackImpl; | 
| 119   friend class IndexedDBClassFactory; | 127   friend class IndexedDBClassFactory; | 
|  | 128   friend class IndexedDBConnection; | 
| 120   friend class base::RefCounted<IndexedDBTransaction>; | 129   friend class base::RefCounted<IndexedDBTransaction>; | 
| 121 | 130 | 
| 122   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); | 131   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); | 
| 123   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); | 132   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); | 
| 124   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); | 133   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); | 
| 125   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, | 134   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, | 
| 126                            SchedulePreemptiveTask); | 135                            SchedulePreemptiveTask); | 
| 127   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, | 136   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, | 
| 128                            ScheduleNormalTask); | 137                            ScheduleNormalTask); | 
| 129   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, TaskFails); | 138   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, TaskFails); | 
| 130   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); | 139   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); | 
| 131   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver); | 140   FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver); | 
| 132 | 141 | 
| 133   void RunTasksIfStarted(); | 142   void RunTasksIfStarted(); | 
| 134 | 143 | 
| 135   bool IsTaskQueueEmpty() const; | 144   bool IsTaskQueueEmpty() const; | 
| 136   bool HasPendingTasks() const; | 145   bool HasPendingTasks() const; | 
| 137 | 146 | 
| 138   void BlobWriteComplete(bool success); | 147   leveldb::Status BlobWriteComplete(bool success, bool sync_call); | 
| 139   void ProcessTaskQueue(); | 148   void ProcessTaskQueue(); | 
| 140   void CloseOpenCursors(); | 149   void CloseOpenCursors(); | 
| 141   leveldb::Status CommitPhaseTwo(); | 150   leveldb::Status CommitPhaseTwo(); | 
| 142   void Timeout(); | 151   void Timeout(); | 
| 143 | 152 | 
| 144   const int64_t id_; | 153   const int64_t id_; | 
| 145   const std::set<int64_t> object_store_ids_; | 154   const std::set<int64_t> object_store_ids_; | 
| 146   const blink::WebIDBTransactionMode mode_; | 155   const blink::WebIDBTransactionMode mode_; | 
| 147 | 156 | 
| 148   bool used_ = false; | 157   bool used_ = false; | 
| 149   State state_ = CREATED; | 158   State state_ = CREATED; | 
| 150   bool commit_pending_ = false; | 159   bool commit_pending_ = false; | 
| 151   base::WeakPtr<IndexedDBConnection> connection_; | 160   // We are owned by the connection object. | 
|  | 161   IndexedDBConnection* connection_; | 
| 152   scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; | 162   scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; | 
| 153   scoped_refptr<IndexedDBDatabase> database_; | 163   scoped_refptr<IndexedDBDatabase> database_; | 
| 154 | 164 | 
| 155   // Observers in pending queue do not listen to changes until activated. | 165   // Observers in pending queue do not listen to changes until activated. | 
| 156   std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_; | 166   std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_; | 
| 157   std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> | 167   std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> | 
| 158       connection_changes_map_; | 168       connection_changes_map_; | 
| 159 | 169 | 
|  | 170   // Metrics for quota. | 
|  | 171   int64_t size_ = 0; | 
|  | 172 | 
| 160   class TaskQueue { | 173   class TaskQueue { | 
| 161    public: | 174    public: | 
| 162     TaskQueue(); | 175     TaskQueue(); | 
| 163     ~TaskQueue(); | 176     ~TaskQueue(); | 
| 164     bool empty() const { return queue_.empty(); } | 177     bool empty() const { return queue_.empty(); } | 
| 165     void push(Operation task) { queue_.push(std::move(task)); } | 178     void push(Operation task) { queue_.push(std::move(task)); } | 
| 166     Operation pop(); | 179     Operation pop(); | 
| 167     void clear(); | 180     void clear(); | 
| 168 | 181 | 
| 169    private: | 182    private: | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
| 199   bool processing_event_queue_ = false; | 212   bool processing_event_queue_ = false; | 
| 200 | 213 | 
| 201   std::set<IndexedDBCursor*> open_cursors_; | 214   std::set<IndexedDBCursor*> open_cursors_; | 
| 202 | 215 | 
| 203   // This timer is started after requests have been processed. If no subsequent | 216   // This timer is started after requests have been processed. If no subsequent | 
| 204   // requests are processed before the timer fires, assume the script is | 217   // requests are processed before the timer fires, assume the script is | 
| 205   // unresponsive and abort to unblock the transaction queue. | 218   // unresponsive and abort to unblock the transaction queue. | 
| 206   base::OneShotTimer timeout_timer_; | 219   base::OneShotTimer timeout_timer_; | 
| 207 | 220 | 
| 208   Diagnostics diagnostics_; | 221   Diagnostics diagnostics_; | 
|  | 222 | 
|  | 223   base::WeakPtrFactory<IndexedDBTransaction> ptr_factory_; | 
| 209 }; | 224 }; | 
| 210 | 225 | 
| 211 }  // namespace content | 226 }  // namespace content | 
| 212 | 227 | 
| 213 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 228 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 
| OLD | NEW | 
|---|