Skip to content

Commit

Permalink
IndexedDB: mojoify GetKeyGeneratorCurrentNumber
Browse files Browse the repository at this point in the history
Bug: 843764,627484
Change-Id: I0d4efc0e8d56560d59f7a971e44eaa0b90ac8018
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4632064
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Nathan Memmott <memmott@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1170293}
  • Loading branch information
Evan Stade authored and Chromium LUCI CQ committed Jul 14, 2023
1 parent 2477da2 commit dc38e97
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 152 deletions.
16 changes: 8 additions & 8 deletions content/browser/indexed_db/database_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_math.h"
#include "base/sequence_checker.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "content/browser/indexed_db/indexed_db_callback_helpers.h"
#include "content/browser/indexed_db/indexed_db_connection.h"
Expand Down Expand Up @@ -562,14 +563,13 @@ void DatabaseImpl::DeleteRange(int64_t transaction_id,
void DatabaseImpl::GetKeyGeneratorCurrentNumber(
int64_t transaction_id,
int64_t object_store_id,
mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
pending_callbacks) {
GetKeyGeneratorCurrentNumberCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto callbacks = std::make_unique<IndexedDBCallbacks>(
dispatcher_host_->AsWeakPtr(), bucket_info_, std::move(pending_callbacks),
idb_runner_);
if (!connection_->IsConnected())
return;
auto wrapped_callback = mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(callback), -1,
blink::mojom::IDBError::New(
blink::mojom::IDBException::kIgnorableAbortError,
u"Aborting due to unknown failure."));

IndexedDBTransaction* transaction =
connection_->GetTransaction(transaction_id);
Expand All @@ -588,7 +588,7 @@ void DatabaseImpl::GetKeyGeneratorCurrentNumber(
transaction->ScheduleTask(BindWeakOperation(
&IndexedDBDatabase::GetKeyGeneratorCurrentNumberOperation,
connection_->database()->AsWeakPtr(), object_store_id,
std::move(callbacks)));
std::move(wrapped_callback)));
}

void DatabaseImpl::Clear(int64_t transaction_id,
Expand Down
3 changes: 1 addition & 2 deletions content/browser/indexed_db/database_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ class DatabaseImpl : public blink::mojom::IDBDatabase {
void GetKeyGeneratorCurrentNumber(
int64_t transaction_id,
int64_t object_store_id,
mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
pending_callbacks) override;
GetKeyGeneratorCurrentNumberCallback callback) override;
void Clear(int64_t transaction_id,
int64_t object_store_id,
ClearCallback callback) override;
Expand Down
172 changes: 68 additions & 104 deletions content/browser/indexed_db/indexed_db_database.cc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion content/browser/indexed_db/indexed_db_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class CONTENT_EXPORT IndexedDBDatabase {

leveldb::Status GetKeyGeneratorCurrentNumberOperation(
int64_t object_store_id,
std::unique_ptr<IndexedDBCallbacks> callbacks,
blink::mojom::IDBDatabase::GetKeyGeneratorCurrentNumberCallback callback,
IndexedDBTransaction* transaction);

leveldb::Status ClearOperation(
Expand Down
4 changes: 2 additions & 2 deletions content/browser/indexed_db/indexed_db_database_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ IndexedDBDatabaseError::IndexedDBDatabaseError(blink::mojom::IDBException code)
: code_(code) {}

IndexedDBDatabaseError::IndexedDBDatabaseError(blink::mojom::IDBException code,
const char* message)
: code_(code), message_(base::ASCIIToUTF16(message)) {}
const std::string& message)
: code_(code), message_(base::UTF8ToUTF16(message)) {}

IndexedDBDatabaseError::IndexedDBDatabaseError(blink::mojom::IDBException code,
const std::u16string& message)
Expand Down
3 changes: 2 additions & 1 deletion content/browser/indexed_db/indexed_db_database_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class CONTENT_EXPORT IndexedDBDatabaseError {
public:
IndexedDBDatabaseError();
explicit IndexedDBDatabaseError(blink::mojom::IDBException code);
IndexedDBDatabaseError(blink::mojom::IDBException code, const char* message);
IndexedDBDatabaseError(blink::mojom::IDBException code,
const std::string& message);
IndexedDBDatabaseError(blink::mojom::IDBException code,
const std::u16string& message);
~IndexedDBDatabaseError();
Expand Down
13 changes: 7 additions & 6 deletions content/browser/indexed_db/indexed_db_index_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stddef.h>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "content/browser/indexed_db/indexed_db_backing_store.h"
#include "content/browser/indexed_db/indexed_db_transaction.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_metadata.h"
Expand Down Expand Up @@ -37,7 +38,7 @@ bool IndexWriter::VerifyIndexKeys(
int64_t index_id,
bool* can_add_keys,
const IndexedDBKey& primary_key,
std::u16string* error_message) const {
std::string* error_message) const {
*can_add_keys = false;
for (const auto& key : keys_) {
bool ok = AddingKeyAllowed(backing_store, transaction, database_id,
Expand All @@ -47,10 +48,10 @@ bool IndexWriter::VerifyIndexKeys(
return false;
if (!*can_add_keys) {
if (error_message) {
*error_message = u"Unable to add key to index '" +
index_metadata_.name +
u"': at least one key does not satisfy the uniqueness "
u"requirements.";
*error_message = "Unable to add key to index '" +
base::UTF16ToUTF8(index_metadata_.name) +
"': at least one key does not satisfy the uniqueness "
"requirements.";
}
return true;
}
Expand Down Expand Up @@ -116,7 +117,7 @@ bool MakeIndexWriters(IndexedDBTransaction* transaction,
bool key_was_generated,
const std::vector<IndexedDBIndexKeys>& index_keys,
std::vector<std::unique_ptr<IndexWriter>>* index_writers,
std::u16string* error_message,
std::string* error_message,
bool* completed) {
*completed = false;

Expand Down
4 changes: 2 additions & 2 deletions content/browser/indexed_db/indexed_db_index_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class IndexWriter {
int64_t index_id,
bool* can_add_keys,
const blink::IndexedDBKey& primary_key,
std::u16string* error_message) const;
std::string* error_message) const;

leveldb::Status WriteIndexKeys(
const IndexedDBBackingStore::RecordIdentifier& record,
Expand Down Expand Up @@ -77,7 +77,7 @@ class IndexWriter {
bool key_was_generated,
const std::vector<blink::IndexedDBIndexKeys>& index_keys,
std::vector<std::unique_ptr<IndexWriter>>* index_writers,
std::u16string* error_message,
std::string* error_message,
bool* completed);

} // namespace content
Expand Down
18 changes: 6 additions & 12 deletions third_party/blink/public/mojom/indexeddb/indexeddb.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,9 @@ struct IDBReturnValue {
IDBKeyPath key_path;
};

// The IDBCallbacks interface is used to return results for individual requests.
// Some requests may return multiple results before completion, such as
// UpgradeNeeded before SuccessDatabase.
//
// TODO(https://crbug.com/627484): Many of these could be replaced with
// replies associated with particular messages.
// The IDBCallbacks interface is used to return results for requests to open or
// delete databases.
// TODO(estade): this interface should be renamed.
interface IDBCallbacks {
Error(IDBException code, mojo_base.mojom.String16 message);

Expand Down Expand Up @@ -416,12 +413,9 @@ interface IDBDatabase {
DeleteRange(int64 transaction_id,
int64 object_store_id,
IDBKeyRange key_range) => (bool success);
// Gets the current number of an IndexedDB ObjectStore's key generator. It
// is implemented in the browser (more priviledged) and handles requests in
// the renderer process: WebIDBDatabaseImpl::GetKeyGeneratorCurrentNumber()
GetKeyGeneratorCurrentNumber(int64 transaction_id,
int64 object_store_id,
pending_associated_remote<IDBCallbacks> pending_callbacks);
// Gets the current number of an IndexedDB ObjectStore's key generator.
GetKeyGeneratorCurrentNumber(int64 transaction_id, int64 object_store_id)
=> (int64 result, IDBError? error);
// Correlates to IDBObjectStore::clear()
Clear(int64 transaction_id,
int64 object_store_id) => (bool success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ IDBRequest* IDBObjectStore::getKeyGeneratorCurrentNumber(
script_state, this, transaction_.Get(), std::move(metrics));

BackendDB()->GetKeyGeneratorCurrentNumber(
transaction_->Id(), Id(), request->CreateWebCallbacks().release());
transaction_->Id(), Id(),
WTF::BindOnce(&IDBRequest::OnGotKeyGeneratorCurrentNumber,
WrapWeakPersistent(request)));
return request;
}

Expand Down
11 changes: 11 additions & 0 deletions third_party/blink/renderer/modules/indexeddb/idb_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,17 @@ void IDBRequest::OnAdvanceCursor(mojom::blink::IDBCursorResultPtr result) {
std::move(result->get_values()->values[0]));
}

void IDBRequest::OnGotKeyGeneratorCurrentNumber(
int64_t number,
mojom::blink::IDBErrorPtr error) {
if (error) {
HandleError(std::move(error));
} else {
DCHECK_GE(number, 0);
HandleResponse(number);
}
}

void IDBRequest::EnqueueResponse(DOMException* error) {
TRACE_EVENT0("IndexedDB", "IDBRequest::EnqueueResponse(DOMException)");
if (!ShouldEnqueueEvent()) {
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/modules/indexeddb/idb_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class MODULES_EXPORT IDBRequest : public EventTarget,
void OnBatchGetAll(mojom::blink::IDBDatabaseBatchGetAllResultPtr result);
void OnOpenCursor(mojom::blink::IDBDatabaseOpenCursorResultPtr result);
void OnAdvanceCursor(mojom::blink::IDBCursorResultPtr result);
void OnGotKeyGeneratorCurrentNumber(int64_t number,
mojom::blink::IDBErrorPtr error);

// Only IDBOpenDBRequest instances should receive these:
virtual void EnqueueBlocked(int64_t old_version) { NOTREACHED(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ class BackendDatabaseWithMockedClose
void GetKeyGeneratorCurrentNumber(
int64_t transaction_id,
int64_t object_store_id,
mojo::PendingAssociatedRemote<mojom::blink::IDBCallbacks>
pending_callbacks) override {}
GetKeyGeneratorCurrentNumberCallback callback) override {}
void Clear(int64_t transaction_id,
int64_t object_store_id,
ClearCallback callback) override {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MockIDBDatabase : public testing::StrictMock<mojom::blink::IDBDatabase> {
GetKeyGeneratorCurrentNumber,
(int64_t transaction_id,
int64_t object_store_id,
mojo::PendingAssociatedRemote<mojom::blink::IDBCallbacks>),
GetKeyGeneratorCurrentNumberCallback),
(override));
MOCK_METHOD(void,
Clear,
Expand Down
13 changes: 6 additions & 7 deletions third_party/blink/renderer/modules/indexeddb/web_idb_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,12 @@ void WebIDBDatabase::DeleteRange(
std::move(key_range_ptr), std::move(success_callback));
}

void WebIDBDatabase::GetKeyGeneratorCurrentNumber(int64_t transaction_id,
int64_t object_store_id,
WebIDBCallbacks* callbacks) {
callbacks->SetState(transaction_id);
database_->GetKeyGeneratorCurrentNumber(
transaction_id, object_store_id,
GetCallbacksProxy(base::WrapUnique(callbacks)));
void WebIDBDatabase::GetKeyGeneratorCurrentNumber(
int64_t transaction_id,
int64_t object_store_id,
mojom::blink::IDBDatabase::GetKeyGeneratorCurrentNumberCallback callback) {
database_->GetKeyGeneratorCurrentNumber(transaction_id, object_store_id,
std::move(callback));
}

void WebIDBDatabase::Clear(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ class MODULES_EXPORT WebIDBDatabase final {
int64_t object_store_id,
const IDBKeyRange*,
mojom::blink::IDBDatabase::DeleteRangeCallback callback);
void GetKeyGeneratorCurrentNumber(int64_t transaction_id,
int64_t object_store_id,
WebIDBCallbacks*);
void GetKeyGeneratorCurrentNumber(
int64_t transaction_id,
int64_t object_store_id,
mojom::blink::IDBDatabase::GetKeyGeneratorCurrentNumberCallback callback);
void Clear(int64_t transaction_id,
int64_t object_store_id,
mojom::blink::IDBDatabase::ClearCallback callback);
Expand Down

0 comments on commit dc38e97

Please sign in to comment.