Commit 46d3cdb6 authored by Chase Phillips's avatar Chase Phillips Committed by Commit Bot

IndexedDB: Convert IDBDatabase.OpenCursor to use native Mojo callback

As part of this change, also remove IDBCallbacks.SuccessValue and
IDBCallbacks.SuccessCursor which are no longer used.

Bug: 717812
Change-Id: I5bb3b5d77ba7248b2286dc579d8d25805b72cd5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1626660
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#681877}
parent a7c549b9
......@@ -13,6 +13,7 @@
#include "base/sequence_checker.h"
#include "base/sequenced_task_runner.h"
#include "content/browser/bad_message.h"
#include "content/browser/indexed_db/indexed_db_callback_helpers.h"
#include "content/browser/indexed_db/indexed_db_connection.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/browser/indexed_db/indexed_db_dispatcher_host.h"
......@@ -169,7 +170,7 @@ void DatabaseImpl::Get(int64_t transaction_id,
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!connection_->IsConnected()) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error");
"Not connected.");
std::move(callback).Run(blink::mojom::IDBDatabaseGetResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
......@@ -179,7 +180,7 @@ void DatabaseImpl::Get(int64_t transaction_id,
connection_->GetTransaction(transaction_id);
if (!transaction) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error");
"Unknown transaction.");
std::move(callback).Run(blink::mojom::IDBDatabaseGetResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
......@@ -201,7 +202,7 @@ void DatabaseImpl::GetAll(int64_t transaction_id,
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!connection_->IsConnected()) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error");
"Not connected.");
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
......@@ -212,7 +213,7 @@ void DatabaseImpl::GetAll(int64_t transaction_id,
connection_->GetTransaction(transaction_id);
if (!transaction) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error");
"Unknown transaction.");
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
......@@ -280,18 +281,33 @@ void DatabaseImpl::OpenCursor(
blink::mojom::IDBCursorDirection direction,
bool key_only,
blink::mojom::IDBTaskType task_type,
blink::mojom::IDBCallbacksAssociatedPtrInfo callbacks_info) {
blink::mojom::IDBDatabase::OpenCursorCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scoped_refptr<IndexedDBCallbacks> callbacks(
new IndexedDBCallbacks(dispatcher_host_->AsWeakPtr(), origin_,
std::move(callbacks_info), idb_runner_));
if (!connection_->IsConnected())
if (!connection_->IsConnected()) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Not connected.");
std::move(callback).Run(
blink::mojom::IDBDatabaseOpenCursorResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
}
IndexedDBTransaction* transaction =
connection_->GetTransaction(transaction_id);
if (!transaction)
if (!transaction) {
IndexedDBDatabaseError error(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown transaction.");
std::move(callback).Run(
blink::mojom::IDBDatabaseOpenCursorResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
}
blink::mojom::IDBDatabase::OpenCursorCallback aborting_callback =
CreateCallbackAbortOnDestruct<
blink::mojom::IDBDatabase::OpenCursorCallback,
blink::mojom::IDBDatabaseOpenCursorResultPtr>(
std::move(callback), transaction->AsWeakPtr());
if (transaction->mode() != blink::mojom::IDBTransactionMode::VersionChange &&
task_type == blink::mojom::IDBTaskType::Preemptive) {
......@@ -304,7 +320,8 @@ void DatabaseImpl::OpenCursor(
connection_->database()->OpenCursor(
transaction, object_store_id, index_id,
std::make_unique<IndexedDBKeyRange>(key_range), direction, key_only,
task_type, std::move(callbacks));
task_type, std::move(aborting_callback), origin_,
dispatcher_host_->AsWeakPtr(), idb_runner_);
}
void DatabaseImpl::Count(
......
......@@ -83,7 +83,7 @@ class DatabaseImpl : public blink::mojom::IDBDatabase {
blink::mojom::IDBCursorDirection direction,
bool key_only,
blink::mojom::IDBTaskType task_type,
blink::mojom::IDBCallbacksAssociatedPtrInfo callbacks_info) override;
blink::mojom::IDBDatabase::OpenCursorCallback callback) override;
void Count(int64_t transaction_id,
int64_t object_store_id,
int64_t index_id,
......
......@@ -366,80 +366,6 @@ void IndexedDBCallbacks::OnSuccess(
complete_ = true;
}
void IndexedDBCallbacks::OnSuccess(std::unique_ptr<IndexedDBCursor> cursor,
const IndexedDBKey& key,
const IndexedDBKey& primary_key,
IndexedDBValue* value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!complete_);
DCHECK_EQ(blink::mojom::IDBDataLoss::None, data_loss_);
blink::mojom::IDBValuePtr mojo_value;
std::vector<IndexedDBBlobInfo> blob_info;
if (value) {
mojo_value = IndexedDBValue::ConvertAndEraseValue(value);
blob_info.swap(value->blob_info);
}
SafeCursorWrapper cursor_wrapper(std::move(cursor));
if (!callbacks_)
return;
if (!dispatcher_host_) {
OnConnectionError();
return;
}
auto cursor_impl =
std::make_unique<CursorImpl>(std::move(cursor_wrapper.cursor_), origin_,
dispatcher_host_.get(), idb_runner_);
if (mojo_value && !IndexedDBCallbacks::CreateAllBlobs(
dispatcher_host_->blob_storage_context(),
IndexedDBValueBlob::GetIndexedDBValueBlobs(
blob_info, &mojo_value->blob_or_file_info))) {
return;
}
blink::mojom::IDBCursorAssociatedPtrInfo ptr_info;
auto request = mojo::MakeRequest(&ptr_info);
dispatcher_host_->AddCursorBinding(std::move(cursor_impl),
std::move(request));
callbacks_->SuccessCursor(std::move(ptr_info), key, primary_key,
std::move(mojo_value));
complete_ = true;
}
void IndexedDBCallbacks::OnSuccess(IndexedDBReturnValue* value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!complete_);
DCHECK_EQ(blink::mojom::IDBDataLoss::None, data_loss_);
blink::mojom::IDBReturnValuePtr mojo_value;
std::vector<IndexedDBBlobInfo> blob_info;
if (value) {
mojo_value = IndexedDBReturnValue::ConvertReturnValue(value);
blob_info = value->blob_info;
}
if (!callbacks_)
return;
if (!dispatcher_host_) {
OnConnectionError();
return;
}
if (mojo_value &&
!IndexedDBCallbacks::CreateAllBlobs(
dispatcher_host_->blob_storage_context(),
IndexedDBValueBlob::GetIndexedDBValueBlobs(
blob_info, &mojo_value->value->blob_or_file_info))) {
return;
}
callbacks_->SuccessValue(std::move(mojo_value));
complete_ = true;
}
void IndexedDBCallbacks::OnSuccess(const IndexedDBKey& value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!complete_);
......
......@@ -38,7 +38,6 @@ class IndexedDBConnection;
class IndexedDBCursor;
class IndexedDBDatabase;
struct IndexedDBDataLossInfo;
struct IndexedDBReturnValue;
struct IndexedDBValue;
class CONTENT_EXPORT IndexedDBCallbacks
......@@ -109,16 +108,6 @@ class CONTENT_EXPORT IndexedDBCallbacks
virtual void OnSuccess(std::unique_ptr<IndexedDBConnection> connection,
const blink::IndexedDBDatabaseMetadata& metadata);
// IndexedDBDatabase::OpenCursor
virtual void OnSuccess(std::unique_ptr<IndexedDBCursor> cursor,
const blink::IndexedDBKey& key,
const blink::IndexedDBKey& primary_key,
IndexedDBValue* value);
// IndexedDBDatabase::Get
// IndexedDBCursor::Advance
virtual void OnSuccess(IndexedDBReturnValue* value);
// IndexedDBDatabase::Put / IndexedDBCursor::Update
virtual void OnSuccess(const blink::IndexedDBKey& key);
......
......@@ -20,8 +20,10 @@
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/indexed_db/cursor_impl.h"
#include "content/browser/indexed_db/indexed_db_blob_info.h"
#include "content/browser/indexed_db/indexed_db_callback_helpers.h"
#include "content/browser/indexed_db/indexed_db_callbacks.h"
#include "content/browser/indexed_db/indexed_db_class_factory.h"
#include "content/browser/indexed_db/indexed_db_connection.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
......@@ -1753,7 +1755,7 @@ struct IndexedDBDatabase::OpenCursorOperationParams {
blink::mojom::IDBCursorDirection direction;
indexed_db::CursorType cursor_type;
blink::mojom::IDBTaskType task_type;
scoped_refptr<IndexedDBCallbacks> callbacks;
blink::mojom::IDBDatabase::OpenCursorCallback callback;
private:
DISALLOW_COPY_AND_ASSIGN(OpenCursorOperationParams);
......@@ -1767,7 +1769,10 @@ void IndexedDBDatabase::OpenCursor(
blink::mojom::IDBCursorDirection direction,
bool key_only,
blink::mojom::IDBTaskType task_type,
scoped_refptr<IndexedDBCallbacks> callbacks) {
blink::mojom::IDBDatabase::OpenCursorCallback callback,
const url::Origin& origin,
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
scoped_refptr<base::SequencedTaskRunner> idb_runner) {
DCHECK(transaction);
IDB_TRACE1("IndexedDBDatabase::OpenCursor", "txn.id", transaction->id());
......@@ -1783,13 +1788,17 @@ void IndexedDBDatabase::OpenCursor(
params->cursor_type =
key_only ? indexed_db::CURSOR_KEY_ONLY : indexed_db::CURSOR_KEY_AND_VALUE;
params->task_type = task_type;
params->callbacks = callbacks;
params->callback = std::move(callback);
transaction->ScheduleTask(BindWeakOperation(
&IndexedDBDatabase::OpenCursorOperation, AsWeakPtr(), std::move(params)));
&IndexedDBDatabase::OpenCursorOperation, AsWeakPtr(), std::move(params),
origin, std::move(dispatcher_host), std::move(idb_runner)));
}
Status IndexedDBDatabase::OpenCursorOperation(
std::unique_ptr<OpenCursorOperationParams> params,
const url::Origin& origin,
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
scoped_refptr<base::SequencedTaskRunner> idb_runner,
IndexedDBTransaction* transaction) {
IDB_TRACE1("IndexedDBDatabase::OpenCursorOperation", "txn.id",
transaction->id());
......@@ -1834,7 +1843,8 @@ Status IndexedDBDatabase::OpenCursorOperation(
if (!backing_store_cursor) {
// Occurs when we've reached the end of cursor's data.
params->callbacks->OnSuccess(nullptr);
std::move(params->callback)
.Run(blink::mojom::IDBDatabaseOpenCursorResult::NewEmpty(true));
return s;
}
......@@ -1843,8 +1853,42 @@ Status IndexedDBDatabase::OpenCursorOperation(
transaction->AsWeakPtr());
IndexedDBCursor* cursor_ptr = cursor.get();
transaction->RegisterOpenCursor(cursor_ptr);
params->callbacks->OnSuccess(std::move(cursor), cursor_ptr->key(),
cursor_ptr->primary_key(), cursor_ptr->Value());
if (!dispatcher_host) {
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Dispatcher not connected.", transaction);
std::move(params->callback)
.Run(blink::mojom::IDBDatabaseOpenCursorResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return s;
}
blink::mojom::IDBValuePtr mojo_value;
std::vector<IndexedDBBlobInfo> blob_info;
if (cursor_ptr->Value()) {
mojo_value = IndexedDBValue::ConvertAndEraseValue(cursor_ptr->Value());
blob_info.swap(cursor_ptr->Value()->blob_info);
}
auto cursor_impl = std::make_unique<CursorImpl>(
std::move(cursor), origin, dispatcher_host.get(), idb_runner);
if (mojo_value &&
!IndexedDBCallbacks::CreateAllBlobs(
dispatcher_host->blob_storage_context(),
IndexedDBCallbacks::IndexedDBValueBlob::GetIndexedDBValueBlobs(
blob_info, &mojo_value->blob_or_file_info))) {
return s;
}
blink::mojom::IDBCursorAssociatedPtrInfo ptr_info;
auto request = mojo::MakeRequest(&ptr_info);
dispatcher_host->AddCursorBinding(std::move(cursor_impl), std::move(request));
std::move(params->callback)
.Run(blink::mojom::IDBDatabaseOpenCursorResult::NewValue(
blink::mojom::IDBDatabaseOpenCursorValue::New(
std::move(ptr_info), cursor_ptr->key(), cursor_ptr->primary_key(),
std::move(mojo_value))));
return s;
}
......
......@@ -194,7 +194,10 @@ class CONTENT_EXPORT IndexedDBDatabase {
blink::mojom::IDBCursorDirection,
bool key_only,
blink::mojom::IDBTaskType task_type,
scoped_refptr<IndexedDBCallbacks> callbacks);
blink::mojom::IDBDatabase::OpenCursorCallback callback,
const url::Origin& origin,
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
scoped_refptr<base::SequencedTaskRunner> idb_runner);
void Count(IndexedDBTransaction* transaction,
int64_t object_store_id,
int64_t index_id,
......@@ -270,6 +273,9 @@ class CONTENT_EXPORT IndexedDBDatabase {
struct OpenCursorOperationParams;
leveldb::Status OpenCursorOperation(
std::unique_ptr<OpenCursorOperationParams> params,
const url::Origin& origin,
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
scoped_refptr<base::SequencedTaskRunner> idb_runner,
IndexedDBTransaction* transaction);
leveldb::Status CountOperation(
int64_t object_store_id,
......
......@@ -64,24 +64,6 @@ class MockMojoIndexedDBCallbacks : public blink::mojom::IDBCallbacks {
MockedSuccessDatabase(&database_info, metadata);
}
MOCK_METHOD4(MockedSuccessCursor,
void(blink::mojom::IDBCursorAssociatedPtrInfo* cursor,
const blink::IndexedDBKey& key,
const blink::IndexedDBKey& primary_key,
blink::mojom::IDBValuePtr* value));
void SuccessCursor(blink::mojom::IDBCursorAssociatedPtrInfo cursor,
const blink::IndexedDBKey& key,
const blink::IndexedDBKey& primary_key,
blink::mojom::IDBValuePtr value) override {
MockedSuccessCursor(&cursor, key, primary_key, &value);
}
MOCK_METHOD1(MockedSuccessValue,
void(blink::mojom::IDBReturnValuePtr* value));
void SuccessValue(blink::mojom::IDBReturnValuePtr value) override {
MockedSuccessValue(&value);
}
MOCK_METHOD1(SuccessKey, void(const blink::IndexedDBKey& key));
MOCK_METHOD1(SuccessInteger, void(int64_t value));
MOCK_METHOD0(Success, void());
......
......@@ -227,15 +227,6 @@ interface IDBCallbacks {
SuccessDatabase(associated IDBDatabase? database,
IDBDatabaseMetadata metadata);
// Database::OpenCursor
SuccessCursor(associated IDBCursor cursor,
IDBKey key,
IDBKey primary_key,
IDBValue? value);
// Database::Get / Cursor::Advance
SuccessValue(IDBReturnValue? value);
// Database::Put / Cursor::Update
SuccessKey(IDBKey key);
......@@ -317,6 +308,19 @@ union IDBDatabaseGetAllResult {
array<IDBReturnValue> values;
};
struct IDBDatabaseOpenCursorValue {
associated IDBCursor cursor;
IDBKey key;
IDBKey primary_key;
IDBValue? value;
};
union IDBDatabaseOpenCursorResult {
IDBError error_result; // |error| is reserved, so call this |error_result|.
bool empty; // Only true values are allowed.
IDBDatabaseOpenCursorValue value;
};
interface IDBDatabase {
RenameObjectStore(int64 transaction_id,
int64 object_store_id,
......@@ -360,8 +364,8 @@ interface IDBDatabase {
IDBKeyRange key_range,
IDBCursorDirection direction,
bool key_only,
IDBTaskType task_type,
associated IDBCallbacks callbacks);
IDBTaskType task_type)
=> (IDBDatabaseOpenCursorResult result);
Count(int64 transaction_id,
int64 object_store_id,
int64 index_id,
......
......@@ -124,7 +124,7 @@ class BackendDatabaseWithMockedClose
mojom::blink::IDBCursorDirection direction,
bool key_only,
mojom::blink::IDBTaskType task_type,
mojom::blink::IDBCallbacksAssociatedPtrInfo callbacks) override {}
mojom::blink::IDBDatabase::OpenCursorCallback callback) override {}
void Count(int64_t transaction_id,
int64_t object_store_id,
int64_t index_id,
......
......@@ -45,11 +45,17 @@ class WebIDBCallbacks : public mojom::blink::IDBCallbacks {
virtual void DetachRequestFromCallback() = 0;
virtual void SetState(base::WeakPtr<WebIDBCursorImpl> cursor,
int64_t transaction_id) = 0;
virtual void SuccessCursor(
mojom::blink::IDBCursorAssociatedPtrInfo cursor_info,
std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
base::Optional<std::unique_ptr<IDBValue>> optional_value) = 0;
virtual void SuccessCursorContinue(
std::unique_ptr<IDBKey>,
std::unique_ptr<IDBKey> primary_key,
base::Optional<std::unique_ptr<IDBValue>>) = 0;
virtual void SuccessArray(Vector<mojom::blink::IDBReturnValuePtr> values) = 0;
virtual void SuccessValue(mojom::blink::IDBReturnValuePtr value) = 0;
};
} // namespace blink
......
......@@ -177,7 +177,8 @@ void WebIDBDatabaseImpl::OpenCursor(int64_t transaction_id,
mojom::IDBCursorDirection direction,
bool key_only,
mojom::IDBTaskType task_type,
WebIDBCallbacks* callbacks) {
WebIDBCallbacks* callbacks_ptr) {
std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
IndexedDBDispatcher::ResetCursorPrefetchCaches(transaction_id, nullptr);
mojom::blink::IDBKeyRangePtr key_range_ptr =
......@@ -186,7 +187,33 @@ void WebIDBDatabaseImpl::OpenCursor(int64_t transaction_id,
database_->OpenCursor(transaction_id, object_store_id, index_id,
std::move(key_range_ptr), direction, key_only,
task_type,
GetCallbacksProxy(base::WrapUnique(callbacks)));
WTF::Bind(&WebIDBDatabaseImpl::OpenCursorCallback,
WTF::Unretained(this), std::move(callbacks)));
}
void WebIDBDatabaseImpl::OpenCursorCallback(
std::unique_ptr<WebIDBCallbacks> callbacks,
mojom::blink::IDBDatabaseOpenCursorResultPtr result) {
if (result->is_error_result()) {
callbacks->Error(result->get_error_result()->error_code,
std::move(result->get_error_result()->error_message));
callbacks.reset();
return;
}
if (result->is_empty()) {
CHECK(result->get_empty()); // Only true values are allowed.
callbacks->SuccessValue(nullptr);
callbacks.reset();
return;
}
CHECK(result->is_value());
callbacks->SuccessCursor(std::move(result->get_value()->cursor),
std::move(result->get_value()->key),
std::move(result->get_value()->primary_key),
std::move(result->get_value()->value));
callbacks.reset();
}
void WebIDBDatabaseImpl::Count(int64_t transaction_id,
......
......@@ -77,6 +77,8 @@ class MODULES_EXPORT WebIDBDatabaseImpl : public WebIDBDatabase {
bool key_only,
mojom::IDBTaskType,
WebIDBCallbacks*) override;
void OpenCursorCallback(std::unique_ptr<WebIDBCallbacks> callbacks,
mojom::blink::IDBDatabaseOpenCursorResultPtr result);
void Count(int64_t transaction_id,
int64_t object_store_id,
int64_t index_id,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment