1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
* Copyright (C) 2015-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "InspectorIndexedDBAgent.h"
#include "AddEventListenerOptions.h"
#include "DOMStringList.h"
#include "Document.h"
#include "Event.h"
#include "EventListener.h"
#include "EventNames.h"
#include "EventTarget.h"
#include "IDBBindingUtilities.h"
#include "IDBCursor.h"
#include "IDBCursorWithValue.h"
#include "IDBDatabase.h"
#include "IDBFactory.h"
#include "IDBIndex.h"
#include "IDBKey.h"
#include "IDBKeyPath.h"
#include "IDBKeyRange.h"
#include "IDBObjectStore.h"
#include "IDBOpenDBRequest.h"
#include "IDBRequest.h"
#include "IDBTransaction.h"
#include "InspectorPageAgent.h"
#include "InstrumentingAgents.h"
#include "JSDOMWindowCustom.h"
#include "LocalDOMWindow.h"
#include "LocalFrame.h"
#include "SecurityOrigin.h"
#include "WindowOrWorkerGlobalScopeIndexedDatabase.h"
#include <JavaScriptCore/HeapInlines.h>
#include <JavaScriptCore/InjectedScript.h>
#include <JavaScriptCore/InjectedScriptManager.h>
#include <JavaScriptCore/InspectorFrontendDispatchers.h>
#include <JavaScriptCore/InspectorFrontendRouter.h>
#include <wtf/JSONValues.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/Vector.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
using namespace Inspector;
WTF_MAKE_TZONE_ALLOCATED_IMPL(InspectorIndexedDBAgent);
namespace {
class ExecutableWithDatabase : public RefCounted<ExecutableWithDatabase> {
public:
ExecutableWithDatabase(ScriptExecutionContext* context)
: m_context(context) { }
virtual ~ExecutableWithDatabase() = default;
void start(IDBFactory*, SecurityOrigin*, const String& databaseName);
virtual void execute(IDBDatabase&) = 0;
virtual BackendDispatcher::CallbackBase& requestCallback() = 0;
ScriptExecutionContext* context() const { return m_context.get(); }
private:
WeakPtr<ScriptExecutionContext> m_context;
};
class OpenDatabaseCallback final : public EventListener {
public:
static Ref<OpenDatabaseCallback> create(ExecutableWithDatabase& executableWithDatabase)
{
return adoptRef(*new OpenDatabaseCallback(executableWithDatabase));
}
void handleEvent(ScriptExecutionContext&, Event& event) final
{
if (event.type() != eventNames().successEvent) {
m_executableWithDatabase->requestCallback().sendFailure("Unexpected event type."_s);
return;
}
auto& request = static_cast<IDBOpenDBRequest&>(*event.target());
auto result = request.result();
if (result.hasException()) {
m_executableWithDatabase->requestCallback().sendFailure("Could not get result in callback."_s);
return;
}
auto resultValue = result.releaseReturnValue();
if (!std::holds_alternative<RefPtr<IDBDatabase>>(resultValue)) {
m_executableWithDatabase->requestCallback().sendFailure("Unexpected result type."_s);
return;
}
auto databaseResult = std::get<RefPtr<IDBDatabase>>(resultValue);
m_executableWithDatabase->execute(*databaseResult);
databaseResult->close();
}
private:
OpenDatabaseCallback(ExecutableWithDatabase& executableWithDatabase)
: EventListener(EventListener::CPPEventListenerType)
, m_executableWithDatabase(executableWithDatabase) { }
Ref<ExecutableWithDatabase> m_executableWithDatabase;
};
void ExecutableWithDatabase::start(IDBFactory* idbFactory, SecurityOrigin*, const String& databaseName)
{
if (!context()) {
requestCallback().sendFailure("Could not open database."_s);
return;
}
auto result = idbFactory->open(*context(), databaseName, std::nullopt);
if (result.hasException()) {
requestCallback().sendFailure("Could not open database."_s);
return;
}
result.releaseReturnValue()->addEventListener(eventNames().successEvent, OpenDatabaseCallback::create(*this), false);
}
static Ref<Inspector::Protocol::IndexedDB::KeyPath> keyPathFromIDBKeyPath(const std::optional<IDBKeyPath>& idbKeyPath)
{
if (!idbKeyPath)
return Inspector::Protocol::IndexedDB::KeyPath::create().setType(Inspector::Protocol::IndexedDB::KeyPath::Type::Null).release();
auto visitor = WTF::makeVisitor([](const String& string) {
auto keyPath = Inspector::Protocol::IndexedDB::KeyPath::create().setType(Inspector::Protocol::IndexedDB::KeyPath::Type::String).release();
keyPath->setString(string);
return keyPath;
}, [](const Vector<String>& vector) {
auto array = JSON::ArrayOf<String>::create();
for (auto& string : vector)
array->addItem(string);
auto keyPath = Inspector::Protocol::IndexedDB::KeyPath::create().setType(Inspector::Protocol::IndexedDB::KeyPath::Type::Array).release();
keyPath->setArray(WTFMove(array));
return keyPath;
});
return std::visit(visitor, idbKeyPath.value());
}
static RefPtr<IDBTransaction> transactionForDatabase(IDBDatabase* idbDatabase, const String& objectStoreName, IDBTransactionMode mode = IDBTransactionMode::Readonly)
{
auto result = idbDatabase->transaction(objectStoreName, mode);
if (result.hasException())
return nullptr;
return result.releaseReturnValue();
}
static RefPtr<IDBObjectStore> objectStoreForTransaction(IDBTransaction* idbTransaction, const String& objectStoreName)
{
auto result = idbTransaction->objectStore(objectStoreName);
if (result.hasException())
return nullptr;
return result.releaseReturnValue();
}
static RefPtr<IDBIndex> indexForObjectStore(IDBObjectStore* idbObjectStore, const String& indexName)
{
auto index = idbObjectStore->index(indexName);
if (index.hasException())
return nullptr;
return index.releaseReturnValue();
}
class DatabaseLoader final : public ExecutableWithDatabase {
public:
static Ref<DatabaseLoader> create(ScriptExecutionContext* context, Ref<IndexedDBBackendDispatcherHandler::RequestDatabaseCallback>&& requestCallback)
{
return adoptRef(*new DatabaseLoader(context, WTFMove(requestCallback)));
}
~DatabaseLoader() override = default;
void execute(IDBDatabase& database) override
{
if (!requestCallback().isActive())
return;
auto& databaseInfo = database.info();
auto objectStores = JSON::ArrayOf<Inspector::Protocol::IndexedDB::ObjectStore>::create();
auto objectStoreNames = databaseInfo.objectStoreNames();
for (auto& name : objectStoreNames) {
auto* objectStoreInfo = databaseInfo.infoForExistingObjectStore(name);
if (!objectStoreInfo)
continue;
auto indexes = JSON::ArrayOf<Inspector::Protocol::IndexedDB::ObjectStoreIndex>::create();
for (auto& indexInfo : objectStoreInfo->indexMap().values()) {
auto objectStoreIndex = Inspector::Protocol::IndexedDB::ObjectStoreIndex::create()
.setName(indexInfo.name())
.setKeyPath(keyPathFromIDBKeyPath(indexInfo.keyPath()))
.setUnique(indexInfo.unique())
.setMultiEntry(indexInfo.multiEntry())
.release();
indexes->addItem(WTFMove(objectStoreIndex));
}
auto objectStore = Inspector::Protocol::IndexedDB::ObjectStore::create()
.setName(objectStoreInfo->name())
.setKeyPath(keyPathFromIDBKeyPath(objectStoreInfo->keyPath()))
.setAutoIncrement(objectStoreInfo->autoIncrement())
.setIndexes(WTFMove(indexes))
.release();
objectStores->addItem(WTFMove(objectStore));
}
auto result = Inspector::Protocol::IndexedDB::DatabaseWithObjectStores::create()
.setName(databaseInfo.name())
.setVersion(databaseInfo.version())
.setObjectStores(WTFMove(objectStores))
.release();
m_requestCallback->sendSuccess(WTFMove(result));
}
BackendDispatcher::CallbackBase& requestCallback() override { return m_requestCallback.get(); }
private:
DatabaseLoader(ScriptExecutionContext* context, Ref<IndexedDBBackendDispatcherHandler::RequestDatabaseCallback>&& requestCallback)
: ExecutableWithDatabase(context)
, m_requestCallback(WTFMove(requestCallback)) { }
Ref<IndexedDBBackendDispatcherHandler::RequestDatabaseCallback> m_requestCallback;
};
static RefPtr<IDBKey> idbKeyFromInspectorObject(Ref<JSON::Object>&& key)
{
auto typeString = key->getString("type"_s);
if (!typeString)
return nullptr;
auto type = Inspector::Protocol::Helpers::parseEnumValueFromString<Inspector::Protocol::IndexedDB::Key::Type>(typeString);
if (!type)
return nullptr;
switch (*type) {
case Inspector::Protocol::IndexedDB::Key::Type::Number: {
auto number = key->getDouble("number"_s);
if (!number)
return nullptr;
return IDBKey::createNumber(*number);
}
case Inspector::Protocol::IndexedDB::Key::Type::String: {
auto string = key->getString("string"_s);
if (!string)
return nullptr;
return IDBKey::createString(string);
}
case Inspector::Protocol::IndexedDB::Key::Type::Date: {
auto date = key->getDouble("date"_s);
if (!date)
return nullptr;
return IDBKey::createDate(*date);
}
case Inspector::Protocol::IndexedDB::Key::Type::Array: {
auto array = key->getArray("array"_s);
if (!array)
return nullptr;
Vector<RefPtr<IDBKey>> keyArray;
for (size_t i = 0; i < array->length(); ++i) {
auto object = array->get(i)->asObject();
if (!object)
return nullptr;
keyArray.append(idbKeyFromInspectorObject(object.releaseNonNull()));
}
return IDBKey::createArray(keyArray);
}
}
ASSERT_NOT_REACHED();
return nullptr;
}
static RefPtr<IDBKeyRange> idbKeyRangeFromKeyRange(JSON::Object& keyRange)
{
RefPtr<IDBKey> idbLower;
if (auto lower = keyRange.getObject("lower"_s)) {
idbLower = idbKeyFromInspectorObject(lower.releaseNonNull());
if (!idbLower)
return nullptr;
}
RefPtr<IDBKey> idbUpper;
if (auto upper = keyRange.getObject("upper"_s)) {
idbUpper = idbKeyFromInspectorObject(upper.releaseNonNull());
if (!idbUpper)
return nullptr;
}
auto lowerOpen = keyRange.getBoolean("lowerOpen"_s);
if (!lowerOpen)
return nullptr;
auto upperOpen = keyRange.getBoolean("upperOpen"_s);
if (!upperOpen)
return nullptr;
return IDBKeyRange::create(WTFMove(idbLower), WTFMove(idbUpper), *lowerOpen, *upperOpen);
}
class OpenCursorCallback final : public EventListener {
public:
static Ref<OpenCursorCallback> create(InjectedScript injectedScript, Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback>&& requestCallback, int skipCount, unsigned pageSize)
{
return adoptRef(*new OpenCursorCallback(injectedScript, WTFMove(requestCallback), skipCount, pageSize));
}
~OpenCursorCallback() override = default;
void handleEvent(ScriptExecutionContext& context, Event& event) override
{
if (event.type() != eventNames().successEvent) {
m_requestCallback->sendFailure("Unexpected event type."_s);
return;
}
auto& request = static_cast<IDBRequest&>(*event.target());
auto result = request.result();
if (result.hasException()) {
m_requestCallback->sendFailure("Could not get result in callback."_s);
return;
}
auto resultValue = result.releaseReturnValue();
if (!std::holds_alternative<RefPtr<IDBCursor>>(resultValue)) {
end(false);
return;
}
auto cursor = std::get<RefPtr<IDBCursor>>(resultValue);
if (m_skipCount) {
if (cursor->advance(m_skipCount).hasException())
m_requestCallback->sendFailure("Could not advance cursor."_s);
m_skipCount = 0;
return;
}
if (m_result->length() == m_pageSize) {
end(true);
return;
}
// Continue cursor before making injected script calls, otherwise transaction might be finished.
if (cursor->continueFunction(nullptr).hasException()) {
m_requestCallback->sendFailure("Could not continue cursor."_s);
return;
}
auto* lexicalGlobalObject = context.globalObject();
auto key = m_injectedScript.wrapObject(toJS(*lexicalGlobalObject, *lexicalGlobalObject, cursor->key()), String(), true);
if (!key)
return;
auto primaryKey = m_injectedScript.wrapObject(toJS(*lexicalGlobalObject, *lexicalGlobalObject, cursor->primaryKey()), String(), true);
if (!primaryKey)
return;
auto value = m_injectedScript.wrapObject(deserializeIDBValueToJSValue(*lexicalGlobalObject, cursor->value()), String(), true);
if (!value)
return;
auto dataEntry = Inspector::Protocol::IndexedDB::DataEntry::create()
.setKey(key.releaseNonNull())
.setPrimaryKey(primaryKey.releaseNonNull())
.setValue(value.releaseNonNull())
.release();
m_result->addItem(WTFMove(dataEntry));
}
void end(bool hasMore)
{
if (!m_requestCallback->isActive())
return;
m_requestCallback->sendSuccess(WTFMove(m_result), hasMore);
}
private:
OpenCursorCallback(InjectedScript injectedScript, Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback>&& requestCallback, int skipCount, unsigned pageSize)
: EventListener(EventListener::CPPEventListenerType)
, m_injectedScript(injectedScript)
, m_requestCallback(WTFMove(requestCallback))
, m_result(JSON::ArrayOf<Inspector::Protocol::IndexedDB::DataEntry>::create())
, m_skipCount(skipCount)
, m_pageSize(pageSize)
{
}
InjectedScript m_injectedScript;
Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback> m_requestCallback;
Ref<JSON::ArrayOf<Inspector::Protocol::IndexedDB::DataEntry>> m_result;
int m_skipCount;
unsigned m_pageSize;
};
class DataLoader final : public ExecutableWithDatabase {
public:
static Ref<DataLoader> create(ScriptExecutionContext* context, Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback>&& requestCallback, const InjectedScript& injectedScript, const String& objectStoreName, const String& indexName, RefPtr<IDBKeyRange>&& idbKeyRange, int skipCount, unsigned pageSize)
{
return adoptRef(*new DataLoader(context, WTFMove(requestCallback), injectedScript, objectStoreName, indexName, WTFMove(idbKeyRange), skipCount, pageSize));
}
~DataLoader() override = default;
void execute(IDBDatabase& database) override
{
if (!requestCallback().isActive())
return;
auto idbTransaction = transactionForDatabase(&database, m_objectStoreName);
if (!idbTransaction) {
m_requestCallback->sendFailure("Could not get transaction"_s);
return;
}
auto idbObjectStore = objectStoreForTransaction(idbTransaction.get(), m_objectStoreName);
if (!idbObjectStore) {
m_requestCallback->sendFailure("Could not get object store"_s);
return;
}
TransactionActivator activator(idbTransaction.get());
RefPtr<IDBRequest> idbRequest;
if (!m_indexName.isEmpty()) {
auto idbIndex = indexForObjectStore(idbObjectStore.get(), m_indexName);
if (!idbIndex) {
m_requestCallback->sendFailure("Could not get index"_s);
return;
}
auto result = idbIndex->openCursor(m_idbKeyRange.get(), IDBCursorDirection::Next);
if (!result.hasException())
idbRequest = result.releaseReturnValue();
} else {
auto result = idbObjectStore->openCursor(m_idbKeyRange.get(), IDBCursorDirection::Next);
if (!result.hasException())
idbRequest = result.releaseReturnValue();
}
if (!idbRequest) {
m_requestCallback->sendFailure("Could not open cursor to populate database data"_s);
return;
}
auto openCursorCallback = OpenCursorCallback::create(m_injectedScript, m_requestCallback.copyRef(), m_skipCount, m_pageSize);
idbRequest->addEventListener(eventNames().successEvent, WTFMove(openCursorCallback), false);
}
BackendDispatcher::CallbackBase& requestCallback() override { return m_requestCallback.get(); }
DataLoader(ScriptExecutionContext* scriptExecutionContext, Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback>&& requestCallback, const InjectedScript& injectedScript, const String& objectStoreName, const String& indexName, RefPtr<IDBKeyRange> idbKeyRange, int skipCount, unsigned pageSize)
: ExecutableWithDatabase(scriptExecutionContext)
, m_requestCallback(WTFMove(requestCallback))
, m_injectedScript(injectedScript)
, m_objectStoreName(objectStoreName)
, m_indexName(indexName)
, m_idbKeyRange(WTFMove(idbKeyRange))
, m_skipCount(skipCount)
, m_pageSize(pageSize) { }
Ref<IndexedDBBackendDispatcherHandler::RequestDataCallback> m_requestCallback;
InjectedScript m_injectedScript;
String m_objectStoreName;
String m_indexName;
RefPtr<IDBKeyRange> m_idbKeyRange;
int m_skipCount;
unsigned m_pageSize;
};
} // namespace
InspectorIndexedDBAgent::InspectorIndexedDBAgent(PageAgentContext& context)
: InspectorAgentBase("IndexedDB"_s, context)
, m_injectedScriptManager(context.injectedScriptManager)
, m_backendDispatcher(Inspector::IndexedDBBackendDispatcher::create(context.backendDispatcher, this))
, m_inspectedPage(context.inspectedPage)
{
}
InspectorIndexedDBAgent::~InspectorIndexedDBAgent() = default;
void InspectorIndexedDBAgent::didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*)
{
}
void InspectorIndexedDBAgent::willDestroyFrontendAndBackend(Inspector::DisconnectReason)
{
disable();
}
Inspector::Protocol::ErrorStringOr<void> InspectorIndexedDBAgent::enable()
{
return { };
}
Inspector::Protocol::ErrorStringOr<void> InspectorIndexedDBAgent::disable()
{
return { };
}
static Inspector::Protocol::ErrorStringOr<Document*> documentFromFrame(LocalFrame* frame)
{
Document* document = frame ? frame->document() : nullptr;
if (!document)
return makeUnexpected("Missing document for given frame"_s);
return document;
}
static Inspector::Protocol::ErrorStringOr<IDBFactory*> IDBFactoryFromDocument(Document* document)
{
RefPtr domWindow = document->domWindow();
if (!domWindow)
return makeUnexpected("Missing window for given document"_s);
IDBFactory* idbFactory = WindowOrWorkerGlobalScopeIndexedDatabase::indexedDB(*domWindow);
if (!idbFactory)
makeUnexpected("Missing IndexedDB factory of window for given document"_s);
return idbFactory;
}
static bool getDocumentAndIDBFactoryFromFrameOrSendFailure(LocalFrame* frame, Document*& outDocument, IDBFactory*& outIDBFactory, BackendDispatcher::CallbackBase& callback)
{
Inspector::Protocol::ErrorStringOr<Document*> document = documentFromFrame(frame);
if (!document.has_value()) {
callback.sendFailure(document.error());
return false;
}
Inspector::Protocol::ErrorStringOr<IDBFactory*> idbFactory = IDBFactoryFromDocument(document.value());
if (!idbFactory.has_value()) {
callback.sendFailure(idbFactory.error());
return false;
}
outDocument = document.value();
outIDBFactory = idbFactory.value();
return true;
}
void InspectorIndexedDBAgent::requestDatabaseNames(const String& securityOrigin, Ref<RequestDatabaseNamesCallback>&& callback)
{
auto* frame = InspectorPageAgent::findFrameWithSecurityOrigin(m_inspectedPage, securityOrigin);
Document* document;
IDBFactory* idbFactory;
if (!getDocumentAndIDBFactoryFromFrameOrSendFailure(frame, document, idbFactory, callback))
return;
idbFactory->getAllDatabaseNames(*document, [callback = WTFMove(callback)](auto& databaseNames) {
if (!callback->isActive())
return;
Ref<JSON::ArrayOf<String>> databaseNameArray = JSON::ArrayOf<String>::create();
for (auto& databaseName : databaseNames)
databaseNameArray->addItem(databaseName);
callback->sendSuccess(WTFMove(databaseNameArray));
});
}
void InspectorIndexedDBAgent::requestDatabase(const String& securityOrigin, const String& databaseName, Ref<RequestDatabaseCallback>&& callback)
{
auto* frame = InspectorPageAgent::findFrameWithSecurityOrigin(m_inspectedPage, securityOrigin);
Document* document;
IDBFactory* idbFactory;
if (!getDocumentAndIDBFactoryFromFrameOrSendFailure(frame, document, idbFactory, callback))
return;
Ref<DatabaseLoader> databaseLoader = DatabaseLoader::create(document, WTFMove(callback));
databaseLoader->start(idbFactory, &document->securityOrigin(), databaseName);
}
void InspectorIndexedDBAgent::requestData(const String& securityOrigin, const String& databaseName, const String& objectStoreName, const String& indexName, int skipCount, int pageSize, RefPtr<JSON::Object>&& keyRange, Ref<RequestDataCallback>&& callback)
{
auto* frame = InspectorPageAgent::findFrameWithSecurityOrigin(m_inspectedPage, securityOrigin);
Document* document;
IDBFactory* idbFactory;
if (!getDocumentAndIDBFactoryFromFrameOrSendFailure(frame, document, idbFactory, callback))
return;
RefPtr<IDBKeyRange> idbKeyRange;
if (keyRange) {
idbKeyRange = idbKeyRangeFromKeyRange(*keyRange);
if (!idbKeyRange) {
callback->sendFailure("Could not parse key range."_s);
return;
}
}
auto injectedScript = m_injectedScriptManager.injectedScriptFor(&mainWorldGlobalObject(*frame));
auto dataLoader = DataLoader::create(document, WTFMove(callback), injectedScript, objectStoreName, indexName, WTFMove(idbKeyRange), skipCount, pageSize);
dataLoader->start(idbFactory, &document->securityOrigin(), databaseName);
}
namespace {
class ClearObjectStoreListener final : public EventListener {
WTF_MAKE_NONCOPYABLE(ClearObjectStoreListener);
public:
static Ref<ClearObjectStoreListener> create(Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback> requestCallback)
{
return adoptRef(*new ClearObjectStoreListener(WTFMove(requestCallback)));
}
~ClearObjectStoreListener() override = default;
void handleEvent(ScriptExecutionContext&, Event& event) override
{
if (!m_requestCallback->isActive())
return;
if (event.type() != eventNames().completeEvent) {
m_requestCallback->sendFailure("Unexpected event type."_s);
return;
}
m_requestCallback->sendSuccess();
}
private:
ClearObjectStoreListener(Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback>&& requestCallback)
: EventListener(EventListener::CPPEventListenerType)
, m_requestCallback(WTFMove(requestCallback))
{
}
Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback> m_requestCallback;
};
class ClearObjectStore final : public ExecutableWithDatabase {
public:
static Ref<ClearObjectStore> create(ScriptExecutionContext* context, const String& objectStoreName, Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback>&& requestCallback)
{
return adoptRef(*new ClearObjectStore(context, objectStoreName, WTFMove(requestCallback)));
}
ClearObjectStore(ScriptExecutionContext* context, const String& objectStoreName, Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback>&& requestCallback)
: ExecutableWithDatabase(context)
, m_objectStoreName(objectStoreName)
, m_requestCallback(WTFMove(requestCallback))
{
}
void execute(IDBDatabase& database) override
{
if (!requestCallback().isActive())
return;
auto idbTransaction = transactionForDatabase(&database, m_objectStoreName, IDBTransactionMode::Readwrite);
if (!idbTransaction) {
m_requestCallback->sendFailure("Could not get transaction"_s);
return;
}
auto idbObjectStore = objectStoreForTransaction(idbTransaction.get(), m_objectStoreName);
if (!idbObjectStore) {
m_requestCallback->sendFailure("Could not get object store"_s);
return;
}
TransactionActivator activator(idbTransaction.get());
auto result = idbObjectStore->clear();
ASSERT(!result.hasException());
if (result.hasException()) {
m_requestCallback->sendFailure(makeString("Could not clear object store '"_s, m_objectStoreName, "': "_s, static_cast<int>(result.releaseException().code())));
return;
}
idbTransaction->addEventListener(eventNames().completeEvent, ClearObjectStoreListener::create(m_requestCallback.copyRef()), false);
}
BackendDispatcher::CallbackBase& requestCallback() override { return m_requestCallback.get(); }
private:
const String m_objectStoreName;
Ref<IndexedDBBackendDispatcherHandler::ClearObjectStoreCallback> m_requestCallback;
};
} // anonymous namespace
void InspectorIndexedDBAgent::clearObjectStore(const String& securityOrigin, const String& databaseName, const String& objectStoreName, Ref<ClearObjectStoreCallback>&& callback)
{
auto* frame = InspectorPageAgent::findFrameWithSecurityOrigin(m_inspectedPage, securityOrigin);
Document* document;
IDBFactory* idbFactory;
if (!getDocumentAndIDBFactoryFromFrameOrSendFailure(frame, document, idbFactory, callback))
return;
Ref<ClearObjectStore> clearObjectStore = ClearObjectStore::create(document, objectStoreName, WTFMove(callback));
clearObjectStore->start(idbFactory, &document->securityOrigin(), databaseName);
}
} // namespace WebCore
|