-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathdom_storage_agent.cc
More file actions
275 lines (247 loc) Β· 9.53 KB
/
dom_storage_agent.cc
File metadata and controls
275 lines (247 loc) Β· 9.53 KB
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
#include "dom_storage_agent.h"
#include "env-inl.h"
#include "inspector/inspector_object_utils.h"
#include "v8-isolate.h"
namespace node {
namespace inspector {
using v8::Array;
using v8::Context;
using v8::HandleScope;
using v8::Isolate;
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");
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 {};
}
return protocol::DOMStorage::StorageId::create()
.setSecurityOrigin(security_origin)
.setIsLocalStorage(is_local_storage)
.setStorageKey(storageKey)
.build();
}
DOMStorageAgent::DOMStorageAgent(Environment* env) : env_(env) {}
DOMStorageAgent::~DOMStorageAgent() {}
void DOMStorageAgent::Wire(protocol::UberDispatcher* dispatcher) {
frontend_ =
std::make_unique<protocol::DOMStorage::Frontend>(dispatcher->channel());
protocol::DOMStorage::Dispatcher::wire(dispatcher, this);
addEventNotifier("domStorageItemAdded",
[this](v8::Local<v8::Context> ctx, v8::Local<v8::Object> p) {
this->domStorageItemAdded(ctx, p);
});
addEventNotifier("domStorageItemRemoved",
[this](v8::Local<v8::Context> ctx, v8::Local<v8::Object> p) {
this->domStorageItemRemoved(ctx, p);
});
addEventNotifier("domStorageItemUpdated",
[this](v8::Local<v8::Context> ctx, v8::Local<v8::Object> p) {
this->domStorageItemUpdated(ctx, p);
});
addEventNotifier("domStorageItemsCleared",
[this](v8::Local<v8::Context> ctx, v8::Local<v8::Object> p) {
this->domStorageItemsCleared(ctx, p);
});
addEventNotifier("registerStorage",
[this](v8::Local<v8::Context> ctx, v8::Local<v8::Object> p) {
this->registerStorage(ctx, p);
});
}
protocol::DispatchResponse DOMStorageAgent::enable() {
this->enabled_ = true;
return protocol::DispatchResponse::Success();
}
protocol::DispatchResponse DOMStorageAgent::disable() {
this->enabled_ = false;
return protocol::DispatchResponse::Success();
}
protocol::DispatchResponse DOMStorageAgent::getDOMStorageItems(
std::unique_ptr<protocol::DOMStorage::StorageId> storageId,
std::unique_ptr<protocol::Array<protocol::Array<protocol::String>>>*
items) {
if (!enabled_) {
return protocol::DispatchResponse::ServerError(
"DOMStorage domain is not enabled");
}
bool is_local_storage = storageId->getIsLocalStorage();
const std::unordered_map<std::string, std::string>& storage_map =
is_local_storage ? local_storage_map_ : session_storage_map_;
auto result =
std::make_unique<protocol::Array<protocol::Array<protocol::String>>>();
for (const auto& pair : storage_map) {
auto item = std::make_unique<protocol::Array<protocol::String>>();
item->push_back(pair.first);
item->push_back(pair.second);
result->push_back(std::move(item));
}
*items = std::move(result);
return protocol::DispatchResponse::Success();
}
protocol::DispatchResponse DOMStorageAgent::setDOMStorageItem(
std::unique_ptr<protocol::DOMStorage::StorageId> storageId,
const std::string& key,
const std::string& value) {
return protocol::DispatchResponse::ServerError("Not implemented");
}
protocol::DispatchResponse DOMStorageAgent::removeDOMStorageItem(
std::unique_ptr<protocol::DOMStorage::StorageId> storageId,
const std::string& key) {
return protocol::DispatchResponse::ServerError("Not implemented");
}
protocol::DispatchResponse DOMStorageAgent::clear(
std::unique_ptr<protocol::DOMStorage::StorageId> storageId) {
return protocol::DispatchResponse::ServerError("Not implemented");
}
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;
}
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
createStorageIdFromObject(context, storage_id_obj);
if (!storage_id) {
return;
}
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 =
createStorageIdFromObject(context, storage_id_obj);
if (!storage_id) {
return;
}
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;
}
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
createStorageIdFromObject(context, storage_id_obj);
if (!storage_id) {
return;
}
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(
std::move(storage_id), key, old_value, new_value);
}
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 =
createStorageIdFromObject(context, storage_id_obj);
if (!storage_id) {
return;
}
frontend_->domStorageItemsCleared(std::move(storage_id));
}
void DOMStorageAgent::registerStorage(Local<Context> context,
Local<Object> params) {
Isolate* isolate = env_->isolate();
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);
node::Utf8Value value_utf8(isolate, value_value);
storage_map[*key_utf8] = *value_utf8;
}
}
bool DOMStorageAgent::canEmit(const std::string& domain) {
return domain == "DOMStorage";
}
} // namespace inspector
} // namespace node