[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / child / indexed_db / indexed_db_dispatcher.h
blob55f74ef635b2ebd4dcef753284233cffca4db1a0
1 // Copyright 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.
5 #ifndef CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_
6 #define CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/gtest_prod_util.h"
13 #include "base/id_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/strings/nullable_string16.h"
16 #include "content/child/worker_task_runner.h"
17 #include "content/common/content_export.h"
18 #include "ipc/ipc_sync_message_filter.h"
19 #include "third_party/WebKit/public/platform/WebBlobInfo.h"
20 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h"
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCallbacks.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
24 struct IndexedDBDatabaseMetadata;
25 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params;
26 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params;
27 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params;
28 struct IndexedDBMsg_CallbacksSuccessArray_Params;
29 struct IndexedDBMsg_CallbacksSuccessValue_Params;
30 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params;
32 namespace blink {
33 class WebData;
36 namespace content {
37 class IndexedDBKey;
38 class IndexedDBKeyPath;
39 class IndexedDBKeyRange;
40 class WebIDBCursorImpl;
41 class WebIDBDatabaseImpl;
42 class ThreadSafeSender;
44 CONTENT_EXPORT extern const size_t kMaxIDBValueSizeInBytes;
46 // Handle the indexed db related communication for this context thread - the
47 // main thread and each worker thread have their own copies.
48 class CONTENT_EXPORT IndexedDBDispatcher : public WorkerTaskRunner::Observer {
49 public:
50 // Constructor made public to allow RenderThreadImpl to own a copy without
51 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate
52 // two copies of RenderThreadImpl on the same thread. Everyone else probably
53 // wants to use ThreadSpecificInstance().
54 explicit IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender);
55 ~IndexedDBDispatcher() override;
57 // |thread_safe_sender| needs to be passed in because if the call leads to
58 // construction it will be needed.
59 static IndexedDBDispatcher* ThreadSpecificInstance(
60 ThreadSafeSender* thread_safe_sender);
62 // WorkerTaskRunner::Observer implementation.
63 void OnWorkerRunLoopStopped() override;
65 static blink::WebIDBMetadata ConvertMetadata(
66 const IndexedDBDatabaseMetadata& idb_metadata);
68 void OnMessageReceived(const IPC::Message& msg);
70 // This method is virtual so it can be overridden in unit tests.
71 virtual bool Send(IPC::Message* msg);
73 void RequestIDBFactoryGetDatabaseNames(
74 blink::WebIDBCallbacks* callbacks,
75 const std::string& database_identifier);
77 void RequestIDBFactoryOpen(
78 const base::string16& name,
79 int64 version,
80 int64 transaction_id,
81 blink::WebIDBCallbacks* callbacks,
82 blink::WebIDBDatabaseCallbacks* database_callbacks,
83 const std::string& database_identifier);
85 void RequestIDBFactoryDeleteDatabase(const base::string16& name,
86 blink::WebIDBCallbacks* callbacks,
87 const std::string& database_identifier);
89 // This method is virtual so it can be overridden in unit tests.
90 virtual void RequestIDBCursorAdvance(unsigned long count,
91 blink::WebIDBCallbacks* callbacks_ptr,
92 int32 ipc_cursor_id,
93 int64 transaction_id);
95 // This method is virtual so it can be overridden in unit tests.
96 virtual void RequestIDBCursorContinue(const IndexedDBKey& key,
97 const IndexedDBKey& primary_key,
98 blink::WebIDBCallbacks* callbacks_ptr,
99 int32 ipc_cursor_id,
100 int64 transaction_id);
102 // This method is virtual so it can be overridden in unit tests.
103 virtual void RequestIDBCursorPrefetch(int n,
104 blink::WebIDBCallbacks* callbacks_ptr,
105 int32 ipc_cursor_id);
107 // This method is virtual so it can be overridden in unit tests.
108 virtual void RequestIDBCursorPrefetchReset(int used_prefetches,
109 int unused_prefetches,
110 int32 ipc_cursor_id);
112 void RequestIDBDatabaseClose(int32 ipc_database_id,
113 int32 ipc_database_callbacks_id);
115 void NotifyIDBDatabaseVersionChangeIgnored(int32 ipc_database_id);
117 void RequestIDBDatabaseCreateTransaction(
118 int32 ipc_database_id,
119 int64 transaction_id,
120 blink::WebIDBDatabaseCallbacks* database_callbacks_ptr,
121 blink::WebVector<long long> object_store_ids,
122 blink::WebIDBTransactionMode mode);
124 void RequestIDBDatabaseGet(int32 ipc_database_id,
125 int64 transaction_id,
126 int64 object_store_id,
127 int64 index_id,
128 const IndexedDBKeyRange& key_range,
129 bool key_only,
130 blink::WebIDBCallbacks* callbacks);
132 void RequestIDBDatabaseGetAll(int32 ipc_database_id,
133 int64 transaction_id,
134 int64 object_store_id,
135 int64 index_id,
136 const IndexedDBKeyRange& key_range,
137 bool key_only,
138 int64 max_count,
139 blink::WebIDBCallbacks* callbacks);
141 void RequestIDBDatabasePut(
142 int32 ipc_database_id,
143 int64 transaction_id,
144 int64 object_store_id,
145 const blink::WebData& value,
146 const blink::WebVector<blink::WebBlobInfo>& web_blob_info,
147 const IndexedDBKey& key,
148 blink::WebIDBPutMode put_mode,
149 blink::WebIDBCallbacks* callbacks,
150 const blink::WebVector<long long>& index_ids,
151 const blink::WebVector<blink::WebVector<blink::WebIDBKey> >& index_keys);
153 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id,
154 int64 transaction_id,
155 int64 object_store_id,
156 int64 index_id,
157 const IndexedDBKeyRange& key_range,
158 blink::WebIDBCursorDirection direction,
159 bool key_only,
160 blink::WebIDBTaskType task_type,
161 blink::WebIDBCallbacks* callbacks);
163 void RequestIDBDatabaseCount(int32 ipc_database_id,
164 int64 transaction_id,
165 int64 object_store_id,
166 int64 index_id,
167 const IndexedDBKeyRange& key_range,
168 blink::WebIDBCallbacks* callbacks);
170 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id,
171 int64 transaction_id,
172 int64 object_store_id,
173 const IndexedDBKeyRange& key_range,
174 blink::WebIDBCallbacks* callbacks);
176 void RequestIDBDatabaseClear(int32 ipc_database_id,
177 int64 transaction_id,
178 int64 object_store_id,
179 blink::WebIDBCallbacks* callbacks);
181 virtual void CursorDestroyed(int32 ipc_cursor_id);
182 void DatabaseDestroyed(int32 ipc_database_id);
184 private:
185 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset);
186 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId);
187 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest);
188 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, KeyAndValueSizeTest);
190 enum { kAllCursors = -1 };
192 static int32 CurrentWorkerId() {
193 return WorkerTaskRunner::Instance()->CurrentWorkerId();
196 template <typename T>
197 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) {
198 scoped_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr);
199 params->ipc_thread_id = CurrentWorkerId();
200 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
203 // IDBCallback message handlers.
204 void OnSuccessIDBDatabase(int32 ipc_thread_id,
205 int32 ipc_callbacks_id,
206 int32 ipc_database_callbacks_id,
207 int32 ipc_object_id,
208 const IndexedDBDatabaseMetadata& idb_metadata);
209 void OnSuccessIndexedDBKey(int32 ipc_thread_id,
210 int32 ipc_callbacks_id,
211 const IndexedDBKey& key);
213 void OnSuccessOpenCursor(
214 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p);
215 void OnSuccessCursorContinue(
216 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p);
217 void OnSuccessCursorPrefetch(
218 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p);
219 void OnSuccessStringList(int32 ipc_thread_id,
220 int32 ipc_callbacks_id,
221 const std::vector<base::string16>& value);
222 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p);
223 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p);
224 void OnSuccessInteger(int32 ipc_thread_id,
225 int32 ipc_callbacks_id,
226 int64 value);
227 void OnSuccessUndefined(int32 ipc_thread_id, int32 ipc_callbacks_id);
228 void OnError(int32 ipc_thread_id,
229 int32 ipc_callbacks_id,
230 int code,
231 const base::string16& message);
232 void OnIntBlocked(int32 ipc_thread_id,
233 int32 ipc_callbacks_id,
234 int64 existing_version);
235 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p);
236 void OnAbort(int32 ipc_thread_id,
237 int32 ipc_database_id,
238 int64 transaction_id,
239 int code,
240 const base::string16& message);
241 void OnComplete(int32 ipc_thread_id,
242 int32 ipc_database_id,
243 int64 transaction_id);
244 void OnForcedClose(int32 ipc_thread_id, int32 ipc_database_id);
245 void OnIntVersionChange(int32 ipc_thread_id,
246 int32 ipc_database_id,
247 int64 old_version,
248 int64 new_version);
250 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
251 void ResetCursorPrefetchCaches(int64 transaction_id,
252 int32 ipc_exception_cursor_id);
254 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
256 // Maximum size (in bytes) of value/key pair allowed for put requests. Any
257 // requests larger than this size will be rejected.
258 // Used by unit tests to exercise behavior without allocating huge chunks
259 // of memory.
260 size_t max_put_value_size_;
262 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
263 // destroyed and used on the same thread it was created on.
264 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_;
265 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer>
266 pending_database_callbacks_;
268 // Maps the ipc_callback_id from an open cursor request to the request's
269 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
270 // when it is created.
271 std::map<int32, int64> cursor_transaction_ids_;
273 // Map from cursor id to WebIDBCursorImpl.
274 std::map<int32, WebIDBCursorImpl*> cursors_;
276 std::map<int32, WebIDBDatabaseImpl*> databases_;
278 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher);
281 } // namespace content
283 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_