Commit 379c0975 authored by Chase Phillips's avatar Chase Phillips Committed by Commit Bot

IndexedDB: Convert IDBDatabase.GetAll to native Mojo callback

Bug: 717812
Change-Id: Ide1572f90b9b576af0f0bc53a6c735b3394d9c75
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1534709
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658224}
parent 2bfa7c2b
......@@ -176,30 +176,38 @@ void DatabaseImpl::Get(
key_only, callbacks);
}
void DatabaseImpl::GetAll(
int64_t transaction_id,
void DatabaseImpl::GetAll(int64_t transaction_id,
int64_t object_store_id,
int64_t index_id,
const IndexedDBKeyRange& key_range,
bool key_only,
int64_t max_count,
blink::mojom::IDBCallbacksAssociatedPtrInfo callbacks_info) {
blink::mojom::IDBDatabase::GetAllCallback 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,
"Unknown error");
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::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 error");
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
}
connection_->database()->GetAll(
transaction, object_store_id, index_id,
dispatcher_host_->AsWeakPtr(), transaction, object_store_id, index_id,
std::make_unique<IndexedDBKeyRange>(key_range), key_only, max_count,
std::move(callbacks));
std::move(callback));
}
void DatabaseImpl::SetIndexKeys(
......
......@@ -66,7 +66,7 @@ class DatabaseImpl : public blink::mojom::IDBDatabase {
const blink::IndexedDBKeyRange& key_range,
bool key_only,
int64_t max_count,
blink::mojom::IDBCallbacksAssociatedPtrInfo callbacks) override;
blink::mojom::IDBDatabase::GetAllCallback callback) override;
void SetIndexKeys(
int64_t transaction_id,
int64_t object_store_id,
......
......@@ -922,24 +922,33 @@ void IndexedDBDatabase::SendObservations(
}
}
void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction,
void IndexedDBDatabase::GetAll(
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
IndexedDBTransaction* transaction,
int64_t object_store_id,
int64_t index_id,
std::unique_ptr<IndexedDBKeyRange> key_range,
bool key_only,
int64_t max_count,
scoped_refptr<IndexedDBCallbacks> callbacks) {
blink::mojom::IDBDatabase::GetAllCallback callback) {
DCHECK(transaction);
IDB_TRACE1("IndexedDBDatabase::GetAll", "txn.id", transaction->id());
if (!ValidateObjectStoreId(object_store_id))
if (!ValidateObjectStoreId(object_store_id)) {
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error", transaction);
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return;
}
transaction->ScheduleTask(base::BindOnce(
&IndexedDBDatabase::GetAllOperation, this, object_store_id, index_id,
std::move(key_range),
&IndexedDBDatabase::GetAllOperation, this, std::move(dispatcher_host),
object_store_id, index_id, std::move(key_range),
key_only ? indexed_db::CURSOR_KEY_ONLY : indexed_db::CURSOR_KEY_AND_VALUE,
max_count, callbacks));
max_count, std::move(callback)));
}
void IndexedDBDatabase::Get(IndexedDBTransaction* transaction,
......@@ -1089,12 +1098,13 @@ static_assert(blink::mojom::kIDBMaxMessageOverhead <= INT32_MAX,
"kIDBMaxMessageOverhead is more than INT32_MAX");
Status IndexedDBDatabase::GetAllOperation(
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
int64_t object_store_id,
int64_t index_id,
std::unique_ptr<IndexedDBKeyRange> key_range,
indexed_db::CursorType cursor_type,
int64_t max_count,
scoped_refptr<IndexedDBCallbacks> callbacks,
blink::mojom::IDBDatabase::GetAllCallback callback,
IndexedDBTransaction* transaction) {
IDB_TRACE1("IndexedDBDatabase::GetAllOperation", "txn.id", transaction->id());
......@@ -1106,6 +1116,15 @@ Status IndexedDBDatabase::GetAllOperation(
metadata_.object_stores[object_store_id];
Status s = Status::OK();
if (!dispatcher_host) {
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Unknown error", transaction);
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return s;
}
std::unique_ptr<IndexedDBBackingStore::Cursor> cursor;
......@@ -1139,6 +1158,12 @@ Status IndexedDBDatabase::GetAllOperation(
if (!s.ok()) {
DLOG(ERROR) << "Unable to open cursor operation: " << s.ToString();
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Corruption detected, unable to continue", transaction);
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return s;
}
......@@ -1147,7 +1172,9 @@ Status IndexedDBDatabase::GetAllOperation(
if (!cursor) {
// Doesn't matter if key or value array here - will be empty array when it
// hits JavaScript.
callbacks->OnSuccessArray(&found_values);
std::vector<blink::mojom::IDBReturnValuePtr> mojo_found_values;
std::move(callback).Run(blink::mojom::IDBDatabaseGetAllResult::NewValues(
std::move(mojo_found_values)));
return s;
}
......@@ -1165,8 +1192,15 @@ Status IndexedDBDatabase::GetAllOperation(
cursor_valid = cursor->FirstSeek(&s);
did_first_seek = true;
}
if (!s.ok())
if (!s.ok()) {
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Seek failure, unable to continue", transaction);
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return s;
}
if (!cursor_valid)
break;
......@@ -1190,9 +1224,12 @@ Status IndexedDBDatabase::GetAllOperation(
else
response_size += return_value.SizeEstimate();
if (response_size > GetUsableMessageSizeInBytes()) {
callbacks->OnError(
IndexedDBDatabaseError error =
CreateError(blink::kWebIDBDatabaseExceptionUnknownError,
"Maximum IPC message size exceeded.", transaction));
"Maximum IPC message size exceeded.", transaction);
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewErrorResult(
blink::mojom::IDBError::New(error.code(), error.message())));
return s;
}
......@@ -1205,10 +1242,33 @@ Status IndexedDBDatabase::GetAllOperation(
if (cursor_type == indexed_db::CURSOR_KEY_ONLY) {
// IndexedDBKey already supports an array of values so we can leverage this
// to return an array of keys - no need to create our own array of keys.
callbacks->OnSuccess(IndexedDBKey(std::move(found_keys)));
} else {
callbacks->OnSuccessArray(&found_values);
std::move(callback).Run(blink::mojom::IDBDatabaseGetAllResult::NewKey(
IndexedDBKey(std::move(found_keys))));
return s;
}
std::vector<blink::mojom::IDBReturnValuePtr> mojo_values;
mojo_values.reserve(found_values.size());
for (size_t i = 0; i < found_values.size(); ++i) {
mojo_values.push_back(
IndexedDBReturnValue::ConvertReturnValue(&found_values[i]));
}
std::vector<IndexedDBCallbacks::IndexedDBValueBlob> value_blobs;
for (size_t i = 0; i < mojo_values.size(); ++i) {
IndexedDBCallbacks::IndexedDBValueBlob::GetIndexedDBValueBlobs(
&value_blobs, found_values[i].blob_info,
&mojo_values[i]->value->blob_or_file_info);
}
if (!IndexedDBCallbacks::CreateAllBlobs(
dispatcher_host->blob_storage_context(),
dispatcher_host->context()->TaskRunner(), std::move(value_blobs))) {
return s;
}
std::move(callback).Run(
blink::mojom::IDBDatabaseGetAllResult::NewValues(std::move(mojo_values)));
return s;
}
......
......@@ -161,13 +161,14 @@ class CONTENT_EXPORT IndexedDBDatabase
std::unique_ptr<blink::IndexedDBKeyRange> key_range,
bool key_only,
scoped_refptr<IndexedDBCallbacks> callbacks);
void GetAll(IndexedDBTransaction* transaction,
void GetAll(base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
IndexedDBTransaction* transaction,
int64_t object_store_id,
int64_t index_id,
std::unique_ptr<blink::IndexedDBKeyRange> key_range,
bool key_only,
int64_t max_count,
scoped_refptr<IndexedDBCallbacks> callbacks);
blink::mojom::IDBDatabase::GetAllCallback callback);
void Put(IndexedDBTransaction* transaction,
int64_t object_store_id,
IndexedDBValue* value,
......@@ -248,12 +249,13 @@ class CONTENT_EXPORT IndexedDBDatabase
scoped_refptr<IndexedDBCallbacks> callbacks,
IndexedDBTransaction* transaction);
leveldb::Status GetAllOperation(
base::WeakPtr<IndexedDBDispatcherHost> dispatcher_host,
int64_t object_store_id,
int64_t index_id,
std::unique_ptr<blink::IndexedDBKeyRange> key_range,
indexed_db::CursorType cursor_type,
int64_t max_count,
scoped_refptr<IndexedDBCallbacks> callbacks,
blink::mojom::IDBDatabase::GetAllCallback callback,
IndexedDBTransaction* transaction);
struct PutOperationParams;
leveldb::Status PutOperation(std::unique_ptr<PutOperationParams> params,
......
......@@ -302,6 +302,12 @@ interface IDBTransaction {
Commit(int64 num_errors_handled);
};
union IDBDatabaseGetAllResult {
IDBError error_result; // |error| is reserved, so call this |error_result|.
IDBKey key;
array<IDBReturnValue> values;
};
interface IDBDatabase {
RenameObjectStore(int64 transaction_id,
int64 object_store_id,
......@@ -330,8 +336,8 @@ interface IDBDatabase {
int64 index_id,
IDBKeyRange key_range,
bool key_only,
int64 max_count,
associated IDBCallbacks callbacks);
int64 max_count)
=> (IDBDatabaseGetAllResult result);
SetIndexKeys(int64 transaction_id,
int64 object_store_id,
IDBKey primary_key,
......
......@@ -106,7 +106,7 @@ class BackendDatabaseWithMockedClose
mojom::blink::IDBKeyRangePtr key_range,
bool key_only,
int64_t max_count,
mojom::blink::IDBCallbacksAssociatedPtrInfo callbacks) override {}
mojom::blink::IDBDatabase::GetAllCallback callback) override {}
void SetIndexKeys(int64_t transaction_id,
int64_t object_store_id,
std::unique_ptr<::blink::IDBKey> primary_key,
......
......@@ -11,6 +11,7 @@
#include "third_party/blink/renderer/modules/indexeddb/idb_key_range.h"
#include "third_party/blink/renderer/modules/indexeddb/indexed_db_blink_mojom_traits.h"
#include "third_party/blink/renderer/modules/indexeddb/indexed_db_dispatcher.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
......@@ -87,7 +88,8 @@ void WebIDBDatabaseImpl::GetAll(int64_t transaction_id,
const IDBKeyRange* key_range,
int64_t max_count,
bool key_only,
WebIDBCallbacks* callbacks) {
WebIDBCallbacks* callbacks_ptr) {
std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
IndexedDBDispatcher::ResetCursorPrefetchCaches(transaction_id, nullptr);
mojom::blink::IDBKeyRangePtr key_range_ptr =
......@@ -95,7 +97,31 @@ void WebIDBDatabaseImpl::GetAll(int64_t transaction_id,
callbacks->SetState(nullptr, transaction_id);
database_->GetAll(transaction_id, object_store_id, index_id,
std::move(key_range_ptr), key_only, max_count,
GetCallbacksProxy(base::WrapUnique(callbacks)));
WTF::Bind(&WebIDBDatabaseImpl::GetAllCallback,
WTF::Unretained(this), std::move(callbacks)));
}
void WebIDBDatabaseImpl::GetAllCallback(
std::unique_ptr<WebIDBCallbacks> callbacks,
mojom::blink::IDBDatabaseGetAllResultPtr 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_key()) {
callbacks->SuccessKey(std::move(result->get_key()));
callbacks.reset();
return;
}
if (result->is_values()) {
callbacks->SuccessArray(std::move(result->get_values()));
callbacks.reset();
return;
}
}
void WebIDBDatabaseImpl::SetIndexKeys(int64_t transaction_id,
......
......@@ -60,6 +60,8 @@ class MODULES_EXPORT WebIDBDatabaseImpl : public WebIDBDatabase {
int64_t max_count,
bool key_only,
WebIDBCallbacks*) override;
void GetAllCallback(std::unique_ptr<WebIDBCallbacks> callbacks,
mojom::blink::IDBDatabaseGetAllResultPtr result);
void SetIndexKeys(int64_t transaction_id,
int64_t object_store_id,
std::unique_ptr<IDBKey> primary_key,
......
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