- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / indexed_db / indexed_db_database.h
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 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       WebKit::WebIDBCallbacks::DataLoss 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 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 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 TransactionStarted(IndexedDBTransaction* transaction);
122   void TransactionFinished(IndexedDBTransaction* transaction);
123   void TransactionFinishedAndCompleteFired(IndexedDBTransaction* transaction);
124   void TransactionFinishedAndAbortFired(IndexedDBTransaction* transaction);
125
126   // Called by transactions to report failure committing to the backing store.
127   void TransactionCommitFailed();
128
129   void Get(int64 transaction_id,
130            int64 object_store_id,
131            int64 index_id,
132            scoped_ptr<IndexedDBKeyRange> key_range,
133            bool key_only,
134            scoped_refptr<IndexedDBCallbacks> callbacks);
135   void Put(int64 transaction_id,
136            int64 object_store_id,
137            std::string* value,
138            scoped_ptr<IndexedDBKey> key,
139            PutMode mode,
140            scoped_refptr<IndexedDBCallbacks> callbacks,
141            const std::vector<int64>& index_ids,
142            const std::vector<IndexKeys>& index_keys);
143   void SetIndexKeys(int64 transaction_id,
144                     int64 object_store_id,
145                     scoped_ptr<IndexedDBKey> primary_key,
146                     const std::vector<int64>& index_ids,
147                     const std::vector<IndexKeys>& index_keys);
148   void SetIndexesReady(int64 transaction_id,
149                        int64 object_store_id,
150                        const std::vector<int64>& index_ids);
151   void OpenCursor(int64 transaction_id,
152                   int64 object_store_id,
153                   int64 index_id,
154                   scoped_ptr<IndexedDBKeyRange> key_range,
155                   indexed_db::CursorDirection,
156                   bool key_only,
157                   TaskType task_type,
158                   scoped_refptr<IndexedDBCallbacks> callbacks);
159   void Count(int64 transaction_id,
160              int64 object_store_id,
161              int64 index_id,
162              scoped_ptr<IndexedDBKeyRange> key_range,
163              scoped_refptr<IndexedDBCallbacks> callbacks);
164   void DeleteRange(int64 transaction_id,
165                    int64 object_store_id,
166                    scoped_ptr<IndexedDBKeyRange> key_range,
167                    scoped_refptr<IndexedDBCallbacks> callbacks);
168   void Clear(int64 transaction_id,
169              int64 object_store_id,
170              scoped_refptr<IndexedDBCallbacks> callbacks);
171
172   // Number of connections that have progressed passed initial open call.
173   size_t ConnectionCount() const;
174   // Number of open calls that are blocked on other connections.
175   size_t PendingOpenCount() const;
176   // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
177   size_t PendingUpgradeCount() const;
178   // Number of running upgrades (0 or 1). Also included in ConnectionCount().
179   size_t RunningUpgradeCount() const;
180   // Number of pending deletes, blocked on other connections.
181   size_t PendingDeleteCount() const;
182
183   // Asynchronous tasks scheduled within transactions:
184   void CreateObjectStoreOperation(
185       const IndexedDBObjectStoreMetadata& object_store_metadata,
186       IndexedDBTransaction* transaction);
187   void CreateObjectStoreAbortOperation(int64 object_store_id,
188                                        IndexedDBTransaction* transaction);
189   void DeleteObjectStoreOperation(
190       const IndexedDBObjectStoreMetadata& object_store_metadata,
191       IndexedDBTransaction* transaction);
192   void DeleteObjectStoreAbortOperation(
193       const IndexedDBObjectStoreMetadata& object_store_metadata,
194       IndexedDBTransaction* transaction);
195   void VersionChangeOperation(int64 version,
196                               scoped_refptr<IndexedDBCallbacks> callbacks,
197                               scoped_ptr<IndexedDBConnection> connection,
198                               WebKit::WebIDBCallbacks::DataLoss data_loss,
199                               std::string data_loss_message,
200                               IndexedDBTransaction* transaction);
201   void VersionChangeAbortOperation(const string16& previous_version,
202                                    int64 previous_int_version,
203                                    IndexedDBTransaction* transaction);
204   void CreateIndexOperation(int64 object_store_id,
205                             const IndexedDBIndexMetadata& index_metadata,
206                             IndexedDBTransaction* transaction);
207   void DeleteIndexOperation(int64 object_store_id,
208                             const IndexedDBIndexMetadata& index_metadata,
209                             IndexedDBTransaction* transaction);
210   void CreateIndexAbortOperation(int64 object_store_id,
211                                  int64 index_id,
212                                  IndexedDBTransaction* transaction);
213   void DeleteIndexAbortOperation(int64 object_store_id,
214                                  const IndexedDBIndexMetadata& index_metadata,
215                                  IndexedDBTransaction* transaction);
216   void GetOperation(int64 object_store_id,
217                     int64 index_id,
218                     scoped_ptr<IndexedDBKeyRange> key_range,
219                     indexed_db::CursorType cursor_type,
220                     scoped_refptr<IndexedDBCallbacks> callbacks,
221                     IndexedDBTransaction* transaction);
222   struct PutOperationParams;
223   void PutOperation(scoped_ptr<PutOperationParams> params,
224                     IndexedDBTransaction* transaction);
225   void SetIndexesReadyOperation(size_t index_count,
226                                 IndexedDBTransaction* transaction);
227   struct OpenCursorOperationParams;
228   void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params,
229                            IndexedDBTransaction* transaction);
230   void CountOperation(int64 object_store_id,
231                       int64 index_id,
232                       scoped_ptr<IndexedDBKeyRange> key_range,
233                       scoped_refptr<IndexedDBCallbacks> callbacks,
234                       IndexedDBTransaction* transaction);
235   void DeleteRangeOperation(int64 object_store_id,
236                             scoped_ptr<IndexedDBKeyRange> key_range,
237                             scoped_refptr<IndexedDBCallbacks> callbacks,
238                             IndexedDBTransaction* transaction);
239   void ClearOperation(int64 object_store_id,
240                       scoped_refptr<IndexedDBCallbacks> callbacks,
241                       IndexedDBTransaction* transaction);
242
243  private:
244   friend class base::RefCounted<IndexedDBDatabase>;
245
246   IndexedDBDatabase(const string16& name,
247                     IndexedDBBackingStore* backing_store,
248                     IndexedDBFactory* factory,
249                     const Identifier& unique_identifier);
250   ~IndexedDBDatabase();
251
252   bool IsOpenConnectionBlocked() const;
253   bool OpenInternal();
254   void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
255                                    scoped_ptr<IndexedDBConnection> connection,
256                                    int64 transaction_id,
257                                    int64 requested_version,
258                                    WebKit::WebIDBCallbacks::DataLoss data_loss,
259                                    std::string data_loss_message);
260   void RunVersionChangeTransactionFinal(
261       scoped_refptr<IndexedDBCallbacks> callbacks,
262       scoped_ptr<IndexedDBConnection> connection,
263       int64 transaction_id,
264       int64 requested_version);
265   void RunVersionChangeTransactionFinal(
266       scoped_refptr<IndexedDBCallbacks> callbacks,
267       scoped_ptr<IndexedDBConnection> connection,
268       int64 transaction_id,
269       int64 requested_version,
270       WebKit::WebIDBCallbacks::DataLoss data_loss,
271       std::string data_loss_message);
272   void ProcessPendingCalls();
273
274   bool IsDeleteDatabaseBlocked() const;
275   void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);
276
277   IndexedDBTransaction* GetTransaction(int64 transaction_id) const;
278
279   bool ValidateObjectStoreId(int64 object_store_id) const;
280   bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
281                                        int64 index_id) const;
282   bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
283                                                int64 index_id) const;
284   bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
285                                           int64 index_id) const;
286
287   scoped_refptr<IndexedDBBackingStore> backing_store_;
288   IndexedDBDatabaseMetadata metadata_;
289
290   const Identifier identifier_;
291   // This might not need to be a scoped_refptr since the factory's lifetime is
292   // that of the page group, but it's better to be conservitive than sorry.
293   scoped_refptr<IndexedDBFactory> factory_;
294
295   IndexedDBTransactionCoordinator transaction_coordinator_;
296   IndexedDBTransaction* running_version_change_transaction_;
297
298   typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
299   TransactionMap transactions_;
300
301   class PendingOpenCall;
302   typedef std::list<PendingOpenCall*> PendingOpenCallList;
303   PendingOpenCallList pending_open_calls_;
304
305   class PendingUpgradeCall;
306   scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
307   class PendingSuccessCall;
308   scoped_ptr<PendingSuccessCall> pending_second_half_open_;
309
310   class PendingDeleteCall;
311   typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
312   PendingDeleteCallList pending_delete_calls_;
313
314   typedef list_set<IndexedDBConnection*> ConnectionSet;
315   ConnectionSet connections_;
316 };
317
318 }  // namespace content
319
320 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_