| 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/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 20 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 20 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 21 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 21 #include "content/browser/indexed_db/indexed_db_database.h" | 22 #include "content/browser/indexed_db/indexed_db_database.h" |
| 22 #include "content/browser/indexed_db/indexed_db_database_error.h" | 23 #include "content/browser/indexed_db/indexed_db_database_error.h" |
| 24 #include "content/browser/indexed_db/indexed_db_observer.h" |
| 23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
| 24 | 26 |
| 25 namespace content { | 27 namespace content { |
| 26 | 28 |
| 27 class BlobWriteCallbackImpl; | 29 class BlobWriteCallbackImpl; |
| 28 class IndexedDBCursor; | 30 class IndexedDBCursor; |
| 29 class IndexedDBDatabaseCallbacks; | 31 class IndexedDBDatabaseCallbacks; |
| 32 class IndexedDBObservation; |
| 33 class IndexedDBObserverChanges; |
| 30 | 34 |
| 31 class CONTENT_EXPORT IndexedDBTransaction | 35 class CONTENT_EXPORT IndexedDBTransaction |
| 32 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) { | 36 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) { |
| 33 public: | 37 public: |
| 34 typedef base::Callback<void(IndexedDBTransaction*)> Operation; | 38 typedef base::Callback<void(IndexedDBTransaction*)> Operation; |
| 35 | 39 |
| 36 enum State { | 40 enum State { |
| 37 CREATED, // Created, but not yet started by coordinator. | 41 CREATED, // Created, but not yet started by coordinator. |
| 38 STARTED, // Started by the coordinator. | 42 STARTED, // Started by the coordinator. |
| 39 COMMITTING, // In the process of committing, possibly waiting for blobs | 43 COMMITTING, // In the process of committing, possibly waiting for blobs |
| (...skipping 16 matching lines...) Expand all Loading... |
| 56 } | 60 } |
| 57 void ScheduleTask(blink::WebIDBTaskType, Operation task); | 61 void ScheduleTask(blink::WebIDBTaskType, Operation task); |
| 58 void ScheduleAbortTask(Operation abort_task); | 62 void ScheduleAbortTask(Operation abort_task); |
| 59 void RegisterOpenCursor(IndexedDBCursor* cursor); | 63 void RegisterOpenCursor(IndexedDBCursor* cursor); |
| 60 void UnregisterOpenCursor(IndexedDBCursor* cursor); | 64 void UnregisterOpenCursor(IndexedDBCursor* cursor); |
| 61 void AddPreemptiveEvent() { pending_preemptive_events_++; } | 65 void AddPreemptiveEvent() { pending_preemptive_events_++; } |
| 62 void DidCompletePreemptiveEvent() { | 66 void DidCompletePreemptiveEvent() { |
| 63 pending_preemptive_events_--; | 67 pending_preemptive_events_--; |
| 64 DCHECK_GE(pending_preemptive_events_, 0); | 68 DCHECK_GE(pending_preemptive_events_, 0); |
| 65 } | 69 } |
| 70 void AddPendingObserver(int32_t observer_id, IndexedDBObserver::Options); |
| 71 // Delete pending observers with ID's listed in |pending_observer_ids|. |
| 72 void RemovePendingObservers(const std::vector<int32_t>& pending_observer_ids); |
| 73 |
| 74 // Adds observation for the connection. |
| 75 void AddObservation(int32_t connection_id, |
| 76 std::unique_ptr<IndexedDBObservation>); |
| 77 // Adds the last observation index to observer_id's list of recorded |
| 78 // observation indices. |
| 79 void RecordObserverForLastObservation(int32_t connection_id, |
| 80 int32_t observer_id); |
| 81 |
| 66 IndexedDBBackingStore::Transaction* BackingStoreTransaction() { | 82 IndexedDBBackingStore::Transaction* BackingStoreTransaction() { |
| 67 return transaction_.get(); | 83 return transaction_.get(); |
| 68 } | 84 } |
| 69 int64_t id() const { return id_; } | 85 int64_t id() const { return id_; } |
| 70 | 86 |
| 71 IndexedDBDatabase* database() const { return database_.get(); } | 87 IndexedDBDatabase* database() const { return database_.get(); } |
| 72 IndexedDBDatabaseCallbacks* connection() const { return callbacks_.get(); } | 88 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } |
| 89 IndexedDBConnection* connection() const { return connection_.get(); } |
| 73 | 90 |
| 74 State state() const { return state_; } | 91 State state() const { return state_; } |
| 75 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } | 92 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } |
| 76 | 93 |
| 77 struct Diagnostics { | 94 struct Diagnostics { |
| 78 base::Time creation_time; | 95 base::Time creation_time; |
| 79 base::Time start_time; | 96 base::Time start_time; |
| 80 int tasks_scheduled; | 97 int tasks_scheduled; |
| 81 int tasks_completed; | 98 int tasks_completed; |
| 82 }; | 99 }; |
| 83 | 100 |
| 84 const Diagnostics& diagnostics() const { return diagnostics_; } | 101 const Diagnostics& diagnostics() const { return diagnostics_; } |
| 85 | 102 |
| 86 protected: | 103 protected: |
| 87 // Test classes may derive, but most creation should be done via | 104 // Test classes may derive, but most creation should be done via |
| 88 // IndexedDBClassFactory. | 105 // IndexedDBClassFactory. |
| 89 IndexedDBTransaction( | 106 IndexedDBTransaction( |
| 90 int64_t id, | 107 int64_t id, |
| 91 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks, | 108 base::WeakPtr<IndexedDBConnection> connection, |
| 92 const std::set<int64_t>& object_store_ids, | 109 const std::set<int64_t>& object_store_ids, |
| 93 blink::WebIDBTransactionMode mode, | 110 blink::WebIDBTransactionMode mode, |
| 94 IndexedDBDatabase* db, | |
| 95 IndexedDBBackingStore::Transaction* backing_store_transaction); | 111 IndexedDBBackingStore::Transaction* backing_store_transaction); |
| 96 virtual ~IndexedDBTransaction(); | 112 virtual ~IndexedDBTransaction(); |
| 97 | 113 |
| 98 // May be overridden in tests. | 114 // May be overridden in tests. |
| 99 virtual base::TimeDelta GetInactivityTimeout() const; | 115 virtual base::TimeDelta GetInactivityTimeout() const; |
| 100 | 116 |
| 101 private: | 117 private: |
| 102 friend class BlobWriteCallbackImpl; | 118 friend class BlobWriteCallbackImpl; |
| 103 friend class IndexedDBClassFactory; | 119 friend class IndexedDBClassFactory; |
| 104 friend class base::RefCounted<IndexedDBTransaction>; | 120 friend class base::RefCounted<IndexedDBTransaction>; |
| 105 | 121 |
| 106 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); | 122 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); |
| 107 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); | 123 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); |
| 108 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); | 124 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); |
| 109 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, | 125 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, |
| 110 SchedulePreemptiveTask); | 126 SchedulePreemptiveTask); |
| 111 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, | 127 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, |
| 112 ScheduleNormalTask); | 128 ScheduleNormalTask); |
| 113 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); | 129 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); |
| 130 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver); |
| 114 | 131 |
| 115 void RunTasksIfStarted(); | 132 void RunTasksIfStarted(); |
| 116 | 133 |
| 117 bool IsTaskQueueEmpty() const; | 134 bool IsTaskQueueEmpty() const; |
| 118 bool HasPendingTasks() const; | 135 bool HasPendingTasks() const; |
| 119 | 136 |
| 120 void BlobWriteComplete(bool success); | 137 void BlobWriteComplete(bool success); |
| 121 void ProcessTaskQueue(); | 138 void ProcessTaskQueue(); |
| 122 void CloseOpenCursors(); | 139 void CloseOpenCursors(); |
| 123 leveldb::Status CommitPhaseTwo(); | 140 leveldb::Status CommitPhaseTwo(); |
| 124 void Timeout(); | 141 void Timeout(); |
| 125 | 142 |
| 126 const int64_t id_; | 143 const int64_t id_; |
| 127 const std::set<int64_t> object_store_ids_; | 144 const std::set<int64_t> object_store_ids_; |
| 128 const blink::WebIDBTransactionMode mode_; | 145 const blink::WebIDBTransactionMode mode_; |
| 129 | 146 |
| 130 bool used_; | 147 bool used_; |
| 131 State state_; | 148 State state_; |
| 132 bool commit_pending_; | 149 bool commit_pending_; |
| 150 base::WeakPtr<IndexedDBConnection> connection_; |
| 133 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; | 151 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; |
| 134 scoped_refptr<IndexedDBDatabase> database_; | 152 scoped_refptr<IndexedDBDatabase> database_; |
| 135 | 153 |
| 154 // Observers in pending queue do not listen to changes until activated. |
| 155 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_; |
| 156 std::map<int32_t, std::unique_ptr<IndexedDBObserverChanges>> |
| 157 connection_changes_map_; |
| 158 |
| 136 class TaskQueue { | 159 class TaskQueue { |
| 137 public: | 160 public: |
| 138 TaskQueue(); | 161 TaskQueue(); |
| 139 ~TaskQueue(); | 162 ~TaskQueue(); |
| 140 bool empty() const { return queue_.empty(); } | 163 bool empty() const { return queue_.empty(); } |
| 141 void push(Operation task) { queue_.push(task); } | 164 void push(Operation task) { queue_.push(task); } |
| 142 Operation pop(); | 165 Operation pop(); |
| 143 void clear(); | 166 void clear(); |
| 144 | 167 |
| 145 private: | 168 private: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // requests are processed before the timer fires, assume the script is | 202 // requests are processed before the timer fires, assume the script is |
| 180 // unresponsive and abort to unblock the transaction queue. | 203 // unresponsive and abort to unblock the transaction queue. |
| 181 base::OneShotTimer timeout_timer_; | 204 base::OneShotTimer timeout_timer_; |
| 182 | 205 |
| 183 Diagnostics diagnostics_; | 206 Diagnostics diagnostics_; |
| 184 }; | 207 }; |
| 185 | 208 |
| 186 } // namespace content | 209 } // namespace content |
| 187 | 210 |
| 188 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ | 211 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ |
| OLD | NEW |