Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / InspectorIndexedDBAgent.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "modules/indexeddb/InspectorIndexedDBAgent.h"
33
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
36 #include "bindings/core/v8/ScriptController.h"
37 #include "bindings/core/v8/ScriptState.h"
38 #include "core/dom/DOMStringList.h"
39 #include "core/dom/Document.h"
40 #include "core/events/EventListener.h"
41 #include "core/frame/LocalFrame.h"
42 #include "core/inspector/InspectorController.h"
43 #include "core/inspector/InspectorState.h"
44 #include "core/page/Page.h"
45 #include "modules/IndexedDBNames.h"
46 #include "modules/indexeddb/DOMWindowIndexedDatabase.h"
47 #include "modules/indexeddb/IDBCursor.h"
48 #include "modules/indexeddb/IDBCursorWithValue.h"
49 #include "modules/indexeddb/IDBDatabase.h"
50 #include "modules/indexeddb/IDBFactory.h"
51 #include "modules/indexeddb/IDBIndex.h"
52 #include "modules/indexeddb/IDBKey.h"
53 #include "modules/indexeddb/IDBKeyPath.h"
54 #include "modules/indexeddb/IDBKeyRange.h"
55 #include "modules/indexeddb/IDBMetadata.h"
56 #include "modules/indexeddb/IDBObjectStore.h"
57 #include "modules/indexeddb/IDBOpenDBRequest.h"
58 #include "modules/indexeddb/IDBPendingTransactionMonitor.h"
59 #include "modules/indexeddb/IDBRequest.h"
60 #include "modules/indexeddb/IDBTransaction.h"
61 #include "platform/JSONValues.h"
62 #include "platform/weborigin/SecurityOrigin.h"
63 #include "public/platform/WebIDBCursor.h"
64 #include "public/platform/WebIDBTypes.h"
65 #include "wtf/Vector.h"
66
67 using blink::TypeBuilder::Array;
68 using blink::TypeBuilder::IndexedDB::DatabaseWithObjectStores;
69 using blink::TypeBuilder::IndexedDB::DataEntry;
70 using blink::TypeBuilder::IndexedDB::Key;
71 using blink::TypeBuilder::IndexedDB::KeyPath;
72 using blink::TypeBuilder::IndexedDB::KeyRange;
73 using blink::TypeBuilder::IndexedDB::ObjectStore;
74 using blink::TypeBuilder::IndexedDB::ObjectStoreIndex;
75
76 typedef blink::InspectorBackendDispatcher::IndexedDBCommandHandler::RequestDatabaseNamesCallback RequestDatabaseNamesCallback;
77 typedef blink::InspectorBackendDispatcher::IndexedDBCommandHandler::RequestDatabaseCallback RequestDatabaseCallback;
78 typedef blink::InspectorBackendDispatcher::IndexedDBCommandHandler::RequestDataCallback RequestDataCallback;
79 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback;
80 typedef blink::InspectorBackendDispatcher::IndexedDBCommandHandler::ClearObjectStoreCallback ClearObjectStoreCallback;
81
82 namespace blink {
83
84 namespace IndexedDBAgentState {
85 static const char indexedDBAgentEnabled[] = "indexedDBAgentEnabled";
86 };
87
88 namespace {
89
90 class GetDatabaseNamesCallback FINAL : public EventListener {
91     WTF_MAKE_NONCOPYABLE(GetDatabaseNamesCallback);
92 public:
93     static PassRefPtr<GetDatabaseNamesCallback> create(PassRefPtr<RequestDatabaseNamesCallback> requestCallback, const String& securityOrigin)
94     {
95         return adoptRef(new GetDatabaseNamesCallback(requestCallback, securityOrigin));
96     }
97
98     virtual ~GetDatabaseNamesCallback() { }
99
100     virtual bool operator==(const EventListener& other) OVERRIDE
101     {
102         return this == &other;
103     }
104
105     virtual void handleEvent(ExecutionContext*, Event* event) OVERRIDE
106     {
107         if (!m_requestCallback->isActive())
108             return;
109         if (event->type() != EventTypeNames::success) {
110             m_requestCallback->sendFailure("Unexpected event type.");
111             return;
112         }
113
114         IDBRequest* idbRequest = static_cast<IDBRequest*>(event->target());
115         IDBAny* requestResult = idbRequest->resultAsAny();
116         if (requestResult->type() != IDBAny::DOMStringListType) {
117             m_requestCallback->sendFailure("Unexpected result type.");
118             return;
119         }
120
121         RefPtrWillBeRawPtr<DOMStringList> databaseNamesList = requestResult->domStringList();
122         RefPtr<TypeBuilder::Array<String> > databaseNames = TypeBuilder::Array<String>::create();
123         for (size_t i = 0; i < databaseNamesList->length(); ++i)
124             databaseNames->addItem(databaseNamesList->item(i));
125         m_requestCallback->sendSuccess(databaseNames.release());
126     }
127
128 private:
129     GetDatabaseNamesCallback(PassRefPtr<RequestDatabaseNamesCallback> requestCallback, const String& securityOrigin)
130         : EventListener(EventListener::CPPEventListenerType)
131         , m_requestCallback(requestCallback)
132         , m_securityOrigin(securityOrigin) { }
133     RefPtr<RequestDatabaseNamesCallback> m_requestCallback;
134     String m_securityOrigin;
135 };
136
137 class ExecutableWithDatabase : public RefCounted<ExecutableWithDatabase> {
138 public:
139     ExecutableWithDatabase(ScriptState* scriptState)
140         : m_scriptState(scriptState) { }
141     virtual ~ExecutableWithDatabase() { };
142     void start(IDBFactory*, SecurityOrigin*, const String& databaseName);
143     virtual void execute(IDBDatabase*) = 0;
144     virtual RequestCallback* requestCallback() = 0;
145     ExecutionContext* context() const { return m_scriptState->executionContext(); }
146     ScriptState* scriptState() const { return m_scriptState.get(); }
147 private:
148     RefPtr<ScriptState> m_scriptState;
149 };
150
151 class OpenDatabaseCallback FINAL : public EventListener {
152 public:
153     static PassRefPtr<OpenDatabaseCallback> create(ExecutableWithDatabase* executableWithDatabase)
154     {
155         return adoptRef(new OpenDatabaseCallback(executableWithDatabase));
156     }
157
158     virtual ~OpenDatabaseCallback() { }
159
160     virtual bool operator==(const EventListener& other) OVERRIDE
161     {
162         return this == &other;
163     }
164
165     virtual void handleEvent(ExecutionContext* context, Event* event) OVERRIDE
166     {
167         if (event->type() != EventTypeNames::success) {
168             m_executableWithDatabase->requestCallback()->sendFailure("Unexpected event type.");
169             return;
170         }
171
172         IDBOpenDBRequest* idbOpenDBRequest = static_cast<IDBOpenDBRequest*>(event->target());
173         IDBAny* requestResult = idbOpenDBRequest->resultAsAny();
174         if (requestResult->type() != IDBAny::IDBDatabaseType) {
175             m_executableWithDatabase->requestCallback()->sendFailure("Unexpected result type.");
176             return;
177         }
178
179         IDBDatabase* idbDatabase = requestResult->idbDatabase();
180         m_executableWithDatabase->execute(idbDatabase);
181         IDBPendingTransactionMonitor::from(*context).deactivateNewTransactions();
182         idbDatabase->close();
183     }
184
185 private:
186     OpenDatabaseCallback(ExecutableWithDatabase* executableWithDatabase)
187         : EventListener(EventListener::CPPEventListenerType)
188         , m_executableWithDatabase(executableWithDatabase) { }
189     RefPtr<ExecutableWithDatabase> m_executableWithDatabase;
190 };
191
192 void ExecutableWithDatabase::start(IDBFactory* idbFactory, SecurityOrigin*, const String& databaseName)
193 {
194     RefPtr<OpenDatabaseCallback> callback = OpenDatabaseCallback::create(this);
195     TrackExceptionState exceptionState;
196     IDBOpenDBRequest* idbOpenDBRequest = idbFactory->open(scriptState(), databaseName, exceptionState);
197     if (exceptionState.hadException()) {
198         requestCallback()->sendFailure("Could not open database.");
199         return;
200     }
201     idbOpenDBRequest->addEventListener(EventTypeNames::success, callback, false);
202 }
203
204 static IDBTransaction* transactionForDatabase(ExecutionContext* executionContext, IDBDatabase* idbDatabase, const String& objectStoreName, const String& mode = IndexedDBNames::readonly)
205 {
206     TrackExceptionState exceptionState;
207     IDBTransaction* idbTransaction = idbDatabase->transaction(executionContext, objectStoreName, mode, exceptionState);
208     if (exceptionState.hadException())
209         return 0;
210     return idbTransaction;
211 }
212
213 static IDBObjectStore* objectStoreForTransaction(IDBTransaction* idbTransaction, const String& objectStoreName)
214 {
215     TrackExceptionState exceptionState;
216     IDBObjectStore* idbObjectStore = idbTransaction->objectStore(objectStoreName, exceptionState);
217     if (exceptionState.hadException())
218         return 0;
219     return idbObjectStore;
220 }
221
222 static IDBIndex* indexForObjectStore(IDBObjectStore* idbObjectStore, const String& indexName)
223 {
224     TrackExceptionState exceptionState;
225     IDBIndex* idbIndex = idbObjectStore->index(indexName, exceptionState);
226     if (exceptionState.hadException())
227         return 0;
228     return idbIndex;
229 }
230
231 static PassRefPtr<KeyPath> keyPathFromIDBKeyPath(const IDBKeyPath& idbKeyPath)
232 {
233     RefPtr<KeyPath> keyPath;
234     switch (idbKeyPath.type()) {
235     case IDBKeyPath::NullType:
236         keyPath = KeyPath::create().setType(KeyPath::Type::Null);
237         break;
238     case IDBKeyPath::StringType:
239         keyPath = KeyPath::create().setType(KeyPath::Type::String);
240         keyPath->setString(idbKeyPath.string());
241         break;
242     case IDBKeyPath::ArrayType: {
243         keyPath = KeyPath::create().setType(KeyPath::Type::Array);
244         RefPtr<TypeBuilder::Array<String> > array = TypeBuilder::Array<String>::create();
245         const Vector<String>& stringArray = idbKeyPath.array();
246         for (size_t i = 0; i < stringArray.size(); ++i)
247             array->addItem(stringArray[i]);
248         keyPath->setArray(array);
249         break;
250     }
251     default:
252         ASSERT_NOT_REACHED();
253     }
254
255     return keyPath.release();
256 }
257
258 class DatabaseLoader FINAL : public ExecutableWithDatabase {
259 public:
260     static PassRefPtr<DatabaseLoader> create(ScriptState* scriptState, PassRefPtr<RequestDatabaseCallback> requestCallback)
261     {
262         return adoptRef(new DatabaseLoader(scriptState, requestCallback));
263     }
264
265     virtual ~DatabaseLoader() { }
266
267     virtual void execute(IDBDatabase* idbDatabase) OVERRIDE
268     {
269         if (!requestCallback()->isActive())
270             return;
271
272         const IDBDatabaseMetadata databaseMetadata = idbDatabase->metadata();
273
274         RefPtr<TypeBuilder::Array<TypeBuilder::IndexedDB::ObjectStore> > objectStores = TypeBuilder::Array<TypeBuilder::IndexedDB::ObjectStore>::create();
275
276         for (IDBDatabaseMetadata::ObjectStoreMap::const_iterator it = databaseMetadata.objectStores.begin(); it != databaseMetadata.objectStores.end(); ++it) {
277             const IDBObjectStoreMetadata& objectStoreMetadata = it->value;
278
279             RefPtr<TypeBuilder::Array<TypeBuilder::IndexedDB::ObjectStoreIndex> > indexes = TypeBuilder::Array<TypeBuilder::IndexedDB::ObjectStoreIndex>::create();
280
281             for (IDBObjectStoreMetadata::IndexMap::const_iterator it = objectStoreMetadata.indexes.begin(); it != objectStoreMetadata.indexes.end(); ++it) {
282                 const IDBIndexMetadata& indexMetadata = it->value;
283
284                 RefPtr<ObjectStoreIndex> objectStoreIndex = ObjectStoreIndex::create()
285                     .setName(indexMetadata.name)
286                     .setKeyPath(keyPathFromIDBKeyPath(indexMetadata.keyPath))
287                     .setUnique(indexMetadata.unique)
288                     .setMultiEntry(indexMetadata.multiEntry);
289                 indexes->addItem(objectStoreIndex);
290             }
291
292             RefPtr<ObjectStore> objectStore = ObjectStore::create()
293                 .setName(objectStoreMetadata.name)
294                 .setKeyPath(keyPathFromIDBKeyPath(objectStoreMetadata.keyPath))
295                 .setAutoIncrement(objectStoreMetadata.autoIncrement)
296                 .setIndexes(indexes);
297             objectStores->addItem(objectStore);
298         }
299         RefPtr<DatabaseWithObjectStores> result = DatabaseWithObjectStores::create()
300             .setName(databaseMetadata.name)
301             .setIntVersion(databaseMetadata.intVersion)
302             .setVersion(databaseMetadata.version)
303             .setObjectStores(objectStores);
304
305         m_requestCallback->sendSuccess(result);
306     }
307
308     virtual RequestCallback* requestCallback() OVERRIDE { return m_requestCallback.get(); }
309 private:
310     DatabaseLoader(ScriptState* scriptState, PassRefPtr<RequestDatabaseCallback> requestCallback)
311         : ExecutableWithDatabase(scriptState)
312         , m_requestCallback(requestCallback) { }
313     RefPtr<RequestDatabaseCallback> m_requestCallback;
314 };
315
316 static IDBKey* idbKeyFromInspectorObject(JSONObject* key)
317 {
318     IDBKey* idbKey;
319
320     String type;
321     if (!key->getString("type", &type))
322         return 0;
323
324     DEFINE_STATIC_LOCAL(String, number, ("number"));
325     DEFINE_STATIC_LOCAL(String, string, ("string"));
326     DEFINE_STATIC_LOCAL(String, date, ("date"));
327     DEFINE_STATIC_LOCAL(String, array, ("array"));
328
329     if (type == number) {
330         double number;
331         if (!key->getNumber("number", &number))
332             return 0;
333         idbKey = IDBKey::createNumber(number);
334     } else if (type == string) {
335         String string;
336         if (!key->getString("string", &string))
337             return 0;
338         idbKey = IDBKey::createString(string);
339     } else if (type == date) {
340         double date;
341         if (!key->getNumber("date", &date))
342             return 0;
343         idbKey = IDBKey::createDate(date);
344     } else if (type == array) {
345         IDBKey::KeyArray keyArray;
346         RefPtr<JSONArray> array = key->getArray("array");
347         for (size_t i = 0; i < array->length(); ++i) {
348             RefPtr<JSONValue> value = array->get(i);
349             RefPtr<JSONObject> object;
350             if (!value->asObject(&object))
351                 return 0;
352             keyArray.append(idbKeyFromInspectorObject(object.get()));
353         }
354         idbKey = IDBKey::createArray(keyArray);
355     } else {
356         return 0;
357     }
358
359     return idbKey;
360 }
361
362 static IDBKeyRange* idbKeyRangeFromKeyRange(JSONObject* keyRange)
363 {
364     RefPtr<JSONObject> lower = keyRange->getObject("lower");
365     IDBKey* idbLower = lower ? idbKeyFromInspectorObject(lower.get()) : 0;
366     if (lower && !idbLower)
367         return 0;
368
369     RefPtr<JSONObject> upper = keyRange->getObject("upper");
370     IDBKey* idbUpper = upper ? idbKeyFromInspectorObject(upper.get()) : 0;
371     if (upper && !idbUpper)
372         return 0;
373
374     bool lowerOpen;
375     if (!keyRange->getBoolean("lowerOpen", &lowerOpen))
376         return 0;
377     IDBKeyRange::LowerBoundType lowerBoundType = lowerOpen ? IDBKeyRange::LowerBoundOpen : IDBKeyRange::LowerBoundClosed;
378
379     bool upperOpen;
380     if (!keyRange->getBoolean("upperOpen", &upperOpen))
381         return 0;
382     IDBKeyRange::UpperBoundType upperBoundType = upperOpen ? IDBKeyRange::UpperBoundOpen : IDBKeyRange::UpperBoundClosed;
383
384     return IDBKeyRange::create(idbLower, idbUpper, lowerBoundType, upperBoundType);
385 }
386
387 class DataLoader;
388
389 class OpenCursorCallback FINAL : public EventListener {
390 public:
391     static PassRefPtr<OpenCursorCallback> create(ScriptState* scriptState, PassRefPtr<RequestDataCallback> requestCallback, int skipCount, unsigned pageSize)
392     {
393         return adoptRef(new OpenCursorCallback(scriptState, requestCallback, skipCount, pageSize));
394     }
395
396     virtual ~OpenCursorCallback() { }
397
398     virtual bool operator==(const EventListener& other) OVERRIDE
399     {
400         return this == &other;
401     }
402
403     virtual void handleEvent(ExecutionContext*, Event* event) OVERRIDE
404     {
405         if (event->type() != EventTypeNames::success) {
406             m_requestCallback->sendFailure("Unexpected event type.");
407             return;
408         }
409
410         IDBRequest* idbRequest = static_cast<IDBRequest*>(event->target());
411         IDBAny* requestResult = idbRequest->resultAsAny();
412         if (requestResult->type() == IDBAny::BufferType) {
413             end(false);
414             return;
415         }
416         if (requestResult->type() != IDBAny::IDBCursorWithValueType) {
417             m_requestCallback->sendFailure("Unexpected result type.");
418             return;
419         }
420
421         IDBCursorWithValue* idbCursor = requestResult->idbCursorWithValue();
422
423         if (m_skipCount) {
424             TrackExceptionState exceptionState;
425             idbCursor->advance(m_skipCount, exceptionState);
426             if (exceptionState.hadException())
427                 m_requestCallback->sendFailure("Could not advance cursor.");
428             m_skipCount = 0;
429             return;
430         }
431
432         if (m_result->length() == m_pageSize) {
433             end(true);
434             return;
435         }
436
437         // Continue cursor before making injected script calls, otherwise transaction might be finished.
438         TrackExceptionState exceptionState;
439         idbCursor->continueFunction(0, 0, exceptionState);
440         if (exceptionState.hadException()) {
441             m_requestCallback->sendFailure("Could not continue cursor.");
442             return;
443         }
444
445         Document* document = toDocument(m_scriptState->executionContext());
446         if (!document)
447             return;
448         RefPtr<DataEntry> dataEntry = DataEntry::create()
449             .setKey(idbCursor->key(m_scriptState.get()).toJSONValue(m_scriptState.get())->toJSONString())
450             .setPrimaryKey(idbCursor->primaryKey(m_scriptState.get()).toJSONValue(m_scriptState.get())->toJSONString())
451             .setValue(idbCursor->value(m_scriptState.get()).toJSONValue(m_scriptState.get())->toJSONString());
452         m_result->addItem(dataEntry);
453
454     }
455
456     void end(bool hasMore)
457     {
458         if (!m_requestCallback->isActive())
459             return;
460         m_requestCallback->sendSuccess(m_result.release(), hasMore);
461     }
462
463 private:
464     OpenCursorCallback(ScriptState* scriptState, PassRefPtr<RequestDataCallback> requestCallback, int skipCount, unsigned pageSize)
465         : EventListener(EventListener::CPPEventListenerType)
466         , m_scriptState(scriptState)
467         , m_requestCallback(requestCallback)
468         , m_skipCount(skipCount)
469         , m_pageSize(pageSize)
470     {
471         m_result = Array<DataEntry>::create();
472     }
473
474     RefPtr<ScriptState> m_scriptState;
475     RefPtr<RequestDataCallback> m_requestCallback;
476     int m_skipCount;
477     unsigned m_pageSize;
478     RefPtr<Array<DataEntry> > m_result;
479 };
480
481 class DataLoader FINAL : public ExecutableWithDatabase {
482 public:
483     static PassRefPtr<DataLoader> create(ScriptState* scriptState, PassRefPtr<RequestDataCallback> requestCallback, const String& objectStoreName, const String& indexName, IDBKeyRange* idbKeyRange, int skipCount, unsigned pageSize)
484     {
485         return adoptRef(new DataLoader(scriptState, requestCallback, objectStoreName, indexName, idbKeyRange, skipCount, pageSize));
486     }
487
488     virtual ~DataLoader() { }
489
490     virtual void execute(IDBDatabase* idbDatabase) OVERRIDE
491     {
492         if (!requestCallback()->isActive())
493             return;
494         IDBTransaction* idbTransaction = transactionForDatabase(context(), idbDatabase, m_objectStoreName);
495         if (!idbTransaction) {
496             m_requestCallback->sendFailure("Could not get transaction");
497             return;
498         }
499         IDBObjectStore* idbObjectStore = objectStoreForTransaction(idbTransaction, m_objectStoreName);
500         if (!idbObjectStore) {
501             m_requestCallback->sendFailure("Could not get object store");
502             return;
503         }
504
505         RefPtr<OpenCursorCallback> openCursorCallback = OpenCursorCallback::create(scriptState(), m_requestCallback, m_skipCount, m_pageSize);
506
507         IDBRequest* idbRequest;
508         if (!m_indexName.isEmpty()) {
509             IDBIndex* idbIndex = indexForObjectStore(idbObjectStore, m_indexName);
510             if (!idbIndex) {
511                 m_requestCallback->sendFailure("Could not get index");
512                 return;
513             }
514
515             idbRequest = idbIndex->openCursor(scriptState(), m_idbKeyRange.get(), WebIDBCursorDirectionNext);
516         } else {
517             idbRequest = idbObjectStore->openCursor(scriptState(), m_idbKeyRange.get(), WebIDBCursorDirectionNext);
518         }
519         idbRequest->addEventListener(EventTypeNames::success, openCursorCallback, false);
520     }
521
522     virtual RequestCallback* requestCallback() OVERRIDE { return m_requestCallback.get(); }
523     DataLoader(ScriptState* scriptState, PassRefPtr<RequestDataCallback> requestCallback, const String& objectStoreName, const String& indexName, IDBKeyRange* idbKeyRange, int skipCount, unsigned pageSize)
524         : ExecutableWithDatabase(scriptState)
525         , m_requestCallback(requestCallback)
526         , m_objectStoreName(objectStoreName)
527         , m_indexName(indexName)
528         , m_idbKeyRange(idbKeyRange)
529         , m_skipCount(skipCount)
530         , m_pageSize(pageSize)
531     {
532     }
533
534     RefPtr<RequestDataCallback> m_requestCallback;
535     String m_objectStoreName;
536     String m_indexName;
537     Persistent<IDBKeyRange> m_idbKeyRange;
538     int m_skipCount;
539     unsigned m_pageSize;
540 };
541
542 LocalFrame* findFrameWithSecurityOrigin(Page* page, const String& securityOrigin)
543 {
544     for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
545         if (!frame->isLocalFrame())
546             continue;
547         RefPtr<SecurityOrigin> documentOrigin = toLocalFrame(frame)->document()->securityOrigin();
548         if (documentOrigin->toRawString() == securityOrigin)
549             return toLocalFrame(frame);
550     }
551     return 0;
552 }
553
554 } // namespace
555
556 void InspectorIndexedDBAgent::provideTo(Page* page)
557 {
558     OwnPtrWillBeRawPtr<InspectorIndexedDBAgent> agent(adoptPtrWillBeNoop(new InspectorIndexedDBAgent(page)));
559     page->inspectorController().registerModuleAgent(agent.release());
560 }
561
562 InspectorIndexedDBAgent::InspectorIndexedDBAgent(Page* page)
563     : InspectorBaseAgent<InspectorIndexedDBAgent>("IndexedDB")
564     , m_page(page)
565 {
566 }
567
568 InspectorIndexedDBAgent::~InspectorIndexedDBAgent()
569 {
570 }
571
572 void InspectorIndexedDBAgent::clearFrontend()
573 {
574     disable(0);
575 }
576
577 void InspectorIndexedDBAgent::restore()
578 {
579     if (m_state->getBoolean(IndexedDBAgentState::indexedDBAgentEnabled)) {
580         ErrorString error;
581         enable(&error);
582     }
583 }
584
585 void InspectorIndexedDBAgent::enable(ErrorString*)
586 {
587     m_state->setBoolean(IndexedDBAgentState::indexedDBAgentEnabled, true);
588 }
589
590 void InspectorIndexedDBAgent::disable(ErrorString*)
591 {
592     m_state->setBoolean(IndexedDBAgentState::indexedDBAgentEnabled, false);
593 }
594
595 static Document* assertDocument(ErrorString* errorString, LocalFrame* frame)
596 {
597     Document* document = frame ? frame->document() : 0;
598
599     if (!document)
600         *errorString = "No document for given frame found";
601
602     return document;
603 }
604
605 static IDBFactory* assertIDBFactory(ErrorString* errorString, Document* document)
606 {
607     LocalDOMWindow* domWindow = document->domWindow();
608     if (!domWindow) {
609         *errorString = "No IndexedDB factory for given frame found";
610         return 0;
611     }
612     IDBFactory* idbFactory = DOMWindowIndexedDatabase::indexedDB(*domWindow);
613
614     if (!idbFactory)
615         *errorString = "No IndexedDB factory for given frame found";
616
617     return idbFactory;
618 }
619
620 void InspectorIndexedDBAgent::requestDatabaseNames(ErrorString* errorString, const String& securityOrigin, PassRefPtr<RequestDatabaseNamesCallback> requestCallback)
621 {
622     LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin);
623     Document* document = assertDocument(errorString, frame);
624     if (!document)
625         return;
626     IDBFactory* idbFactory = assertIDBFactory(errorString, document);
627     if (!idbFactory)
628         return;
629
630     ScriptState* scriptState = ScriptState::forMainWorld(frame);
631     ScriptState::Scope scope(scriptState);
632     TrackExceptionState exceptionState;
633     IDBRequest* idbRequest = idbFactory->getDatabaseNames(scriptState, exceptionState);
634     if (exceptionState.hadException()) {
635         requestCallback->sendFailure("Could not obtain database names.");
636         return;
637     }
638     idbRequest->addEventListener(EventTypeNames::success, GetDatabaseNamesCallback::create(requestCallback, document->securityOrigin()->toRawString()), false);
639 }
640
641 void InspectorIndexedDBAgent::requestDatabase(ErrorString* errorString, const String& securityOrigin, const String& databaseName, PassRefPtr<RequestDatabaseCallback> requestCallback)
642 {
643     LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin);
644     Document* document = assertDocument(errorString, frame);
645     if (!document)
646         return;
647     IDBFactory* idbFactory = assertIDBFactory(errorString, document);
648     if (!idbFactory)
649         return;
650
651     ScriptState* scriptState = ScriptState::forMainWorld(frame);
652     ScriptState::Scope scope(scriptState);
653     RefPtr<DatabaseLoader> databaseLoader = DatabaseLoader::create(scriptState, requestCallback);
654     databaseLoader->start(idbFactory, document->securityOrigin(), databaseName);
655 }
656
657 void InspectorIndexedDBAgent::requestData(ErrorString* errorString, const String& securityOrigin, const String& databaseName, const String& objectStoreName, const String& indexName, int skipCount, int pageSize, const RefPtr<JSONObject>* keyRange, PassRefPtr<RequestDataCallback> requestCallback)
658 {
659     LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin);
660     Document* document = assertDocument(errorString, frame);
661     if (!document)
662         return;
663     IDBFactory* idbFactory = assertIDBFactory(errorString, document);
664     if (!idbFactory)
665         return;
666
667     IDBKeyRange* idbKeyRange = keyRange ? idbKeyRangeFromKeyRange(keyRange->get()) : 0;
668     if (keyRange && !idbKeyRange) {
669         *errorString = "Can not parse key range.";
670         return;
671     }
672
673     ScriptState* scriptState = ScriptState::forMainWorld(frame);
674     ScriptState::Scope scope(scriptState);
675     RefPtr<DataLoader> dataLoader = DataLoader::create(scriptState, requestCallback, objectStoreName, indexName, idbKeyRange, skipCount, pageSize);
676     dataLoader->start(idbFactory, document->securityOrigin(), databaseName);
677 }
678
679 class ClearObjectStoreListener FINAL : public EventListener {
680     WTF_MAKE_NONCOPYABLE(ClearObjectStoreListener);
681 public:
682     static PassRefPtr<ClearObjectStoreListener> create(PassRefPtr<ClearObjectStoreCallback> requestCallback)
683     {
684         return adoptRef(new ClearObjectStoreListener(requestCallback));
685     }
686
687     virtual ~ClearObjectStoreListener() { }
688
689     virtual bool operator==(const EventListener& other) OVERRIDE
690     {
691         return this == &other;
692     }
693
694     virtual void handleEvent(ExecutionContext*, Event* event) OVERRIDE
695     {
696         if (!m_requestCallback->isActive())
697             return;
698         if (event->type() != EventTypeNames::complete) {
699             m_requestCallback->sendFailure("Unexpected event type.");
700             return;
701         }
702
703         m_requestCallback->sendSuccess();
704     }
705 private:
706     ClearObjectStoreListener(PassRefPtr<ClearObjectStoreCallback> requestCallback)
707         : EventListener(EventListener::CPPEventListenerType)
708         , m_requestCallback(requestCallback)
709     {
710     }
711
712     RefPtr<ClearObjectStoreCallback> m_requestCallback;
713 };
714
715
716 class ClearObjectStore FINAL : public ExecutableWithDatabase {
717 public:
718     static PassRefPtr<ClearObjectStore> create(ScriptState* scriptState, const String& objectStoreName, PassRefPtr<ClearObjectStoreCallback> requestCallback)
719     {
720         return adoptRef(new ClearObjectStore(scriptState, objectStoreName, requestCallback));
721     }
722
723     ClearObjectStore(ScriptState* scriptState, const String& objectStoreName, PassRefPtr<ClearObjectStoreCallback> requestCallback)
724         : ExecutableWithDatabase(scriptState)
725         , m_objectStoreName(objectStoreName)
726         , m_requestCallback(requestCallback)
727     {
728     }
729
730     virtual void execute(IDBDatabase* idbDatabase) OVERRIDE
731     {
732         if (!requestCallback()->isActive())
733             return;
734         IDBTransaction* idbTransaction = transactionForDatabase(context(), idbDatabase, m_objectStoreName, IndexedDBNames::readwrite);
735         if (!idbTransaction) {
736             m_requestCallback->sendFailure("Could not get transaction");
737             return;
738         }
739         IDBObjectStore* idbObjectStore = objectStoreForTransaction(idbTransaction, m_objectStoreName);
740         if (!idbObjectStore) {
741             m_requestCallback->sendFailure("Could not get object store");
742             return;
743         }
744
745         TrackExceptionState exceptionState;
746         idbObjectStore->clear(scriptState(), exceptionState);
747         ASSERT(!exceptionState.hadException());
748         if (exceptionState.hadException()) {
749             ExceptionCode ec = exceptionState.code();
750             m_requestCallback->sendFailure(String::format("Could not clear object store '%s': %d", m_objectStoreName.utf8().data(), ec));
751             return;
752         }
753         idbTransaction->addEventListener(EventTypeNames::complete, ClearObjectStoreListener::create(m_requestCallback), false);
754     }
755
756     virtual RequestCallback* requestCallback() OVERRIDE { return m_requestCallback.get(); }
757 private:
758     const String m_objectStoreName;
759     RefPtr<ClearObjectStoreCallback> m_requestCallback;
760 };
761
762 void InspectorIndexedDBAgent::clearObjectStore(ErrorString* errorString, const String& securityOrigin, const String& databaseName, const String& objectStoreName, PassRefPtr<ClearObjectStoreCallback> requestCallback)
763 {
764     LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin);
765     Document* document = assertDocument(errorString, frame);
766     if (!document)
767         return;
768     IDBFactory* idbFactory = assertIDBFactory(errorString, document);
769     if (!idbFactory)
770         return;
771
772     ScriptState* scriptState = ScriptState::forMainWorld(frame);
773     ScriptState::Scope scope(scriptState);
774     RefPtr<ClearObjectStore> clearObjectStore = ClearObjectStore::create(scriptState, objectStoreName, requestCallback);
775     clearObjectStore->start(idbFactory, document->securityOrigin(), databaseName);
776 }
777
778 void InspectorIndexedDBAgent::trace(Visitor* visitor)
779 {
780     visitor->trace(m_page);
781     InspectorBaseAgent::trace(visitor);
782 }
783
784 } // namespace blink