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_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ |
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ |
7 | 7 |
| 8 #include <list> |
| 9 #include <map> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/string16.h" | |
14 #include "content/browser/indexed_db/indexed_db.h" | 14 #include "content/browser/indexed_db/indexed_db.h" |
15 #include "content/browser/indexed_db/indexed_db_database_error.h" | 15 #include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h" |
16 #include "content/common/indexed_db/indexed_db_key.h" | 16 #include "content/browser/indexed_db/indexed_db_metadata.h" |
17 #include "content/common/indexed_db/indexed_db_key_path.h" | 17 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" |
18 #include "content/common/indexed_db/indexed_db_key_range.h" | 18 #include "content/browser/indexed_db/list_set.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 class IndexedDBCallbacksWrapper; | 22 class IndexedDBBackingStore; |
23 class IndexedDBDatabaseCallbacksWrapper; | 23 class IndexedDBFactory; |
24 struct IndexedDBDatabaseMetadata; | 24 class IndexedDBKey; |
| 25 class IndexedDBKeyPath; |
| 26 class IndexedDBKeyRange; |
| 27 class IndexedDBTransaction; |
25 | 28 |
26 // This is implemented by IndexedDBDatabaseImpl and optionally others (in order | 29 class CONTENT_EXPORT IndexedDBDatabase |
27 // to proxy calls across process barriers). All calls to these classes should be | 30 : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) { |
28 // non-blocking and trigger work on a background thread if necessary. | |
29 class IndexedDBDatabase : public base::RefCounted<IndexedDBDatabase> { | |
30 public: | 31 public: |
31 virtual void CreateObjectStore(int64 transaction_id, | |
32 int64 object_store_id, | |
33 const string16& name, | |
34 const IndexedDBKeyPath& key_path, | |
35 bool auto_increment) = 0; | |
36 virtual void DeleteObjectStore(int64 transaction_id, | |
37 int64 object_store_id) = 0; | |
38 virtual void CreateTransaction( | |
39 int64 transaction_id, | |
40 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks, | |
41 const std::vector<int64>& object_store_ids, | |
42 uint16 mode) = 0; | |
43 virtual void Close( | |
44 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks) = 0; | |
45 | |
46 // Transaction-specific operations. | |
47 virtual void Commit(int64 transaction_id) = 0; | |
48 virtual void Abort(int64 transaction_id) = 0; | |
49 virtual void Abort(int64 transaction_id, | |
50 const IndexedDBDatabaseError& error) = 0; | |
51 | |
52 virtual void CreateIndex(int64 transaction_id, | |
53 int64 object_store_id, | |
54 int64 index_id, | |
55 const string16& name, | |
56 const IndexedDBKeyPath& key_path, | |
57 bool unique, | |
58 bool multi_entry) = 0; | |
59 virtual void DeleteIndex(int64 transaction_id, | |
60 int64 object_store_id, | |
61 int64 index_id) = 0; | |
62 | |
63 enum TaskType { | 32 enum TaskType { |
64 NORMAL_TASK = 0, | 33 NORMAL_TASK = 0, |
65 PREEMPTIVE_TASK | 34 PREEMPTIVE_TASK |
66 }; | 35 }; |
67 | 36 |
68 enum PutMode { | 37 enum PutMode { |
69 ADD_OR_UPDATE, | 38 ADD_OR_UPDATE, |
70 ADD_ONLY, | 39 ADD_ONLY, |
71 CURSOR_UPDATE | 40 CURSOR_UPDATE |
72 }; | 41 }; |
73 | 42 |
| 43 typedef std::vector<IndexedDBKey> IndexKeys; |
| 44 |
| 45 static const int64 kInvalidId = 0; |
74 static const int64 kMinimumIndexId = 30; | 46 static const int64 kMinimumIndexId = 30; |
75 | 47 |
76 typedef std::vector<IndexedDBKey> IndexKeys; | 48 static scoped_refptr<IndexedDBDatabase> Create( |
| 49 const string16& name, |
| 50 IndexedDBBackingStore* database, |
| 51 IndexedDBFactory* factory, |
| 52 const string16& unique_identifier); |
| 53 scoped_refptr<IndexedDBBackingStore> BackingStore() const; |
77 | 54 |
78 virtual void Get(int64 transaction_id, | 55 int64 id() const { return metadata_.id; } |
| 56 void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata, |
| 57 int64 new_max_object_store_id); |
| 58 void RemoveObjectStore(int64 object_store_id); |
| 59 void AddIndex(int64 object_store_id, |
| 60 const IndexedDBIndexMetadata& metadata, |
| 61 int64 new_max_index_id); |
| 62 void RemoveIndex(int64 object_store_id, int64 index_id); |
| 63 |
| 64 void OpenConnection( |
| 65 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
| 66 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks, |
| 67 int64 transaction_id, |
| 68 int64 version); |
| 69 void DeleteDatabase(scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
| 70 const IndexedDBDatabaseMetadata& metadata() const { return metadata_; } |
| 71 |
| 72 void CreateObjectStore(int64 transaction_id, |
| 73 int64 object_store_id, |
| 74 const string16& name, |
| 75 const IndexedDBKeyPath& key_path, |
| 76 bool auto_increment); |
| 77 void DeleteObjectStore(int64 transaction_id, int64 object_store_id); |
| 78 void CreateTransaction( |
| 79 int64 transaction_id, |
| 80 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks, |
| 81 const std::vector<int64>& object_store_ids, |
| 82 uint16 mode); |
| 83 void Close(scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks); |
| 84 |
| 85 void Commit(int64 transaction_id); |
| 86 void Abort(int64 transaction_id); |
| 87 void Abort(int64 transaction_id, const IndexedDBDatabaseError& error); |
| 88 |
| 89 void CreateIndex(int64 transaction_id, |
79 int64 object_store_id, | 90 int64 object_store_id, |
80 int64 index_id, | 91 int64 index_id, |
| 92 const string16& name, |
| 93 const IndexedDBKeyPath& key_path, |
| 94 bool unique, |
| 95 bool multi_entry); |
| 96 void DeleteIndex(int64 transaction_id, int64 object_store_id, int64 index_id); |
| 97 |
| 98 IndexedDBTransactionCoordinator& transaction_coordinator() { |
| 99 return transaction_coordinator_; |
| 100 } |
| 101 |
| 102 void TransactionStarted(IndexedDBTransaction* transaction); |
| 103 void TransactionFinished(IndexedDBTransaction* transaction); |
| 104 void TransactionFinishedAndCompleteFired(IndexedDBTransaction* transaction); |
| 105 void TransactionFinishedAndAbortFired(IndexedDBTransaction* transaction); |
| 106 |
| 107 void Get(int64 transaction_id, |
| 108 int64 object_store_id, |
| 109 int64 index_id, |
| 110 scoped_ptr<IndexedDBKeyRange> key_range, |
| 111 bool key_only, |
| 112 scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
| 113 void Put(int64 transaction_id, |
| 114 int64 object_store_id, |
| 115 std::vector<char>* value, |
| 116 scoped_ptr<IndexedDBKey> key, |
| 117 PutMode mode, |
| 118 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
| 119 const std::vector<int64>& index_ids, |
| 120 const std::vector<IndexKeys>& index_keys); |
| 121 void SetIndexKeys(int64 transaction_id, |
| 122 int64 object_store_id, |
| 123 scoped_ptr<IndexedDBKey> primary_key, |
| 124 const std::vector<int64>& index_ids, |
| 125 const std::vector<IndexKeys>& index_keys); |
| 126 void SetIndexesReady(int64 transaction_id, |
| 127 int64 object_store_id, |
| 128 const std::vector<int64>& index_ids); |
| 129 void OpenCursor(int64 transaction_id, |
| 130 int64 object_store_id, |
| 131 int64 index_id, |
| 132 scoped_ptr<IndexedDBKeyRange> key_range, |
| 133 indexed_db::CursorDirection, |
| 134 bool key_only, |
| 135 TaskType task_type, |
| 136 scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
| 137 void Count(int64 transaction_id, |
| 138 int64 object_store_id, |
| 139 int64 index_id, |
| 140 scoped_ptr<IndexedDBKeyRange> key_range, |
| 141 scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
| 142 void DeleteRange(int64 transaction_id, |
| 143 int64 object_store_id, |
81 scoped_ptr<IndexedDBKeyRange> key_range, | 144 scoped_ptr<IndexedDBKeyRange> key_range, |
82 bool key_only, | 145 scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
83 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) = 0; | 146 void Clear(int64 transaction_id, |
84 // This will swap() with value. | 147 int64 object_store_id, |
85 virtual void Put(int64 transaction_id, | 148 scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
86 int64 object_store_id, | 149 |
87 std::vector<char>* value, | 150 private: |
88 scoped_ptr<IndexedDBKey> key, | 151 friend class base::RefCounted<IndexedDBDatabase>; |
89 PutMode mode, | 152 |
90 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, | 153 IndexedDBDatabase(const string16& name, |
91 const std::vector<int64>& index_ids, | 154 IndexedDBBackingStore* database, |
92 const std::vector<IndexKeys>& index_keys) = 0; | 155 IndexedDBFactory* factory, |
93 virtual void SetIndexKeys(int64 transaction_id, | 156 const string16& unique_identifier); |
94 int64 object_store_id, | 157 ~IndexedDBDatabase(); |
95 scoped_ptr<IndexedDBKey> primary_key, | 158 |
96 const std::vector<int64>& index_ids, | 159 bool IsOpenConnectionBlocked() const; |
97 const std::vector<IndexKeys>& index_keys) = 0; | 160 bool OpenInternal(); |
98 virtual void SetIndexesReady(int64 transaction_id, | 161 void RunVersionChangeTransaction( |
99 int64 object_store_id, | 162 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
100 const std::vector<int64>& index_ids) = 0; | 163 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks, |
101 virtual void OpenCursor( | |
102 int64 transaction_id, | 164 int64 transaction_id, |
103 int64 object_store_id, | 165 int64 requested_version); |
104 int64 index_id, | 166 void RunVersionChangeTransactionFinal( |
105 scoped_ptr<IndexedDBKeyRange> key_range, | 167 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
106 indexed_db::CursorDirection direction, | 168 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks, |
107 bool key_only, | |
108 TaskType task_type, | |
109 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) = 0; | |
110 virtual void Count(int64 transaction_id, | |
111 int64 object_store_id, | |
112 int64 index_id, | |
113 scoped_ptr<IndexedDBKeyRange> key_range, | |
114 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) = 0; | |
115 virtual void DeleteRange( | |
116 int64 transaction_id, | 169 int64 transaction_id, |
117 int64 object_store_id, | 170 int64 requested_version); |
118 scoped_ptr<IndexedDBKeyRange> key_range, | 171 size_t ConnectionCount() const; |
119 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) = 0; | 172 void ProcessPendingCalls(); |
120 virtual void Clear(int64 transaction_id, | |
121 int64 object_store_id, | |
122 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) = 0; | |
123 | 173 |
124 protected: | 174 bool IsDeleteDatabaseBlocked() const; |
125 virtual ~IndexedDBDatabase() {} | 175 void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacksWrapper> callbacks); |
126 friend class base::RefCounted<IndexedDBDatabase>; | 176 |
| 177 class VersionChangeOperation; |
| 178 |
| 179 // When a "versionchange" transaction aborts, these restore the back-end |
| 180 // object hierarchy. |
| 181 class VersionChangeAbortOperation; |
| 182 |
| 183 scoped_refptr<IndexedDBBackingStore> backing_store_; |
| 184 IndexedDBDatabaseMetadata metadata_; |
| 185 |
| 186 string16 identifier_; |
| 187 // This might not need to be a scoped_refptr since the factory's lifetime is |
| 188 // that of the page group, but it's better to be conservitive than sorry. |
| 189 scoped_refptr<IndexedDBFactory> factory_; |
| 190 |
| 191 IndexedDBTransactionCoordinator transaction_coordinator_; |
| 192 IndexedDBTransaction* running_version_change_transaction_; |
| 193 |
| 194 typedef std::map<int64, IndexedDBTransaction*> TransactionMap; |
| 195 TransactionMap transactions_; |
| 196 |
| 197 class PendingOpenCall; |
| 198 typedef std::list<PendingOpenCall*> PendingOpenCallList; |
| 199 PendingOpenCallList pending_open_calls_; |
| 200 scoped_ptr<PendingOpenCall> pending_run_version_change_transaction_call_; |
| 201 scoped_ptr<PendingOpenCall> pending_second_half_open_; |
| 202 |
| 203 class PendingDeleteCall; |
| 204 typedef std::list<PendingDeleteCall*> PendingDeleteCallList; |
| 205 PendingDeleteCallList pending_delete_calls_; |
| 206 |
| 207 typedef list_set<scoped_refptr<IndexedDBDatabaseCallbacksWrapper> > |
| 208 DatabaseCallbacksSet; |
| 209 DatabaseCallbacksSet database_callbacks_set_; |
| 210 |
| 211 bool closing_connection_; |
127 }; | 212 }; |
128 | 213 |
129 } // namespace content | 214 } // namespace content |
130 | 215 |
131 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ | 216 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ |
OLD | NEW |