Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/inspector/dom_storage_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ using v8::Local;
using v8::Object;
using v8::Value;

static void ThrowEventError(v8::Isolate* isolate, const std::string& message) {
isolate->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8(isolate, message.c_str()).ToLocalChecked()));
}

std::unique_ptr<protocol::DOMStorage::StorageId> createStorageIdFromObject(
Local<Context> context, Local<Object> storage_id_obj) {
protocol::String security_origin;
Isolate* isolate = Isolate::GetCurrent();
if (!ObjectGetProtocolString(context, storage_id_obj, "securityOrigin")
.To(&security_origin)) {
ThrowEventError(isolate, "Missing securityOrigin in storageId");
Copy link
Member

@legendecas legendecas Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this would throw an identical error for each session connected. Can we validate the object shape once so that the error is only thrown once? This would also prevent returning an array of errors in the JS API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If errors that depend on session-specific state are introduced in the future, this may need to be changed. However, since we currently only perform type checks, would it make sense to throw the error directly?
I’ve added a commit to implement this change.

return {};
}
bool is_local_storage =
ObjectGetBool(context, storage_id_obj, "isLocalStorage").FromMaybe(false);
protocol::String storageKey;
if (!ObjectGetProtocolString(context, storage_id_obj, "storageKey")
.To(&storageKey)) {
ThrowEventError(isolate, "Missing storageKey in storageId");
return {};
}

Expand Down Expand Up @@ -119,8 +127,10 @@ protocol::DispatchResponse DOMStorageAgent::clear(

void DOMStorageAgent::domStorageItemAdded(Local<Context> context,
Local<Object> params) {
Isolate* isolate = env_->isolate();
Local<Object> storage_id_obj;
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
ThrowEventError(isolate, "Missing storageId in event");
return;
}

Expand All @@ -132,19 +142,23 @@ void DOMStorageAgent::domStorageItemAdded(Local<Context> context,

protocol::String key;
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
ThrowEventError(isolate, "Missing key in event");
return;
}
protocol::String new_value;
if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
ThrowEventError(isolate, "Missing newValue in event");
return;
}
frontend_->domStorageItemAdded(std::move(storage_id), key, new_value);
}

void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,
Local<Object> params) {
Isolate* isolate = env_->isolate();
Local<Object> storage_id_obj;
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
ThrowEventError(isolate, "Missing storageId in event");
return;
}
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
Expand All @@ -156,15 +170,18 @@ void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,

protocol::String key;
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
ThrowEventError(isolate, "Missing key in event");
return;
}
frontend_->domStorageItemRemoved(std::move(storage_id), key);
}

void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
Local<Object> params) {
Isolate* isolate = env_->isolate();
Local<Object> storage_id_obj;
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
ThrowEventError(isolate, "Missing storageId in event");
return;
}

Expand All @@ -177,14 +194,17 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,

protocol::String key;
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
ThrowEventError(isolate, "Missing key in event");
return;
}
protocol::String old_value;
if (!ObjectGetProtocolString(context, params, "oldValue").To(&old_value)) {
ThrowEventError(isolate, "Missing oldValue in event");
return;
}
protocol::String new_value;
if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
ThrowEventError(isolate, "Missing newValue in event");
return;
}
frontend_->domStorageItemUpdated(
Expand All @@ -193,8 +213,10 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,

void DOMStorageAgent::domStorageItemsCleared(Local<Context> context,
Local<Object> params) {
Isolate* isolate = env_->isolate();
Local<Object> storage_id_obj;
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
ThrowEventError(isolate, "Missing storageId in event");
return;
}
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
Expand All @@ -212,27 +234,32 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
HandleScope handle_scope(isolate);
bool is_local_storage;
if (!ObjectGetBool(context, params, "isLocalStorage").To(&is_local_storage)) {
ThrowEventError(isolate, "Missing isLocalStorage in event");
return;
}
Local<Object> storage_map_obj;
if (!ObjectGetObject(context, params, "storageMap")
.ToLocal(&storage_map_obj)) {
ThrowEventError(isolate, "Missing storageMap in event");
return;
}
std::unordered_map<std::string, std::string>& storage_map =
is_local_storage ? local_storage_map_ : session_storage_map_;
Local<Array> property_names;
if (!storage_map_obj->GetOwnPropertyNames(context).ToLocal(&property_names)) {
ThrowEventError(isolate, "Failed to get property names from storageMap");
return;
}
uint32_t length = property_names->Length();
for (uint32_t i = 0; i < length; ++i) {
Local<Value> key_value;
if (!property_names->Get(context, i).ToLocal(&key_value)) {
ThrowEventError(isolate, "Failed to get key from storageMap");
return;
}
Local<Value> value_value;
if (!storage_map_obj->Get(context, key_value).ToLocal(&value_value)) {
ThrowEventError(isolate, "Failed to get value from storageMap");
return;
}
node::Utf8Value key_utf8(isolate, key_value);
Expand Down
Loading
Loading