|
| 1 | +#include "dom_storage_agent.h" |
| 2 | + |
| 3 | +#include "crdtp/dispatch.h" |
| 4 | +#include "env-inl.h" |
| 5 | +#include "inspector/inspector_object_utils.h" |
| 6 | +#include "v8-isolate.h" |
| 7 | + |
| 8 | +namespace node { |
| 9 | +namespace inspector { |
| 10 | + |
| 11 | +using v8::Array; |
| 12 | +using v8::HandleScope; |
| 13 | +using v8::Local; |
| 14 | +using v8::Object; |
| 15 | +using v8::Value; |
| 16 | + |
| 17 | +std::unique_ptr<protocol::DOMStorage::StorageId> createStorageIdFromObject( |
| 18 | + v8::Local<v8::Context> context, v8::Local<v8::Object> storage_id_obj) { |
| 19 | + protocol::String security_origin; |
| 20 | + if (!ObjectGetProtocolString(context, storage_id_obj, "securityOrigin") |
| 21 | + .To(&security_origin)) { |
| 22 | + return {}; |
| 23 | + } |
| 24 | + bool is_local_storage = |
| 25 | + ObjectGetBool(context, storage_id_obj, "isLocalStorage").FromMaybe(false); |
| 26 | + protocol::String storageKey; |
| 27 | + if (!ObjectGetProtocolString(context, storage_id_obj, "storageKey") |
| 28 | + .To(&storageKey)) { |
| 29 | + return {}; |
| 30 | + } |
| 31 | + |
| 32 | + return protocol::DOMStorage::StorageId::create() |
| 33 | + .setSecurityOrigin(security_origin) |
| 34 | + .setIsLocalStorage(is_local_storage) |
| 35 | + .setStorageKey(storageKey) |
| 36 | + .build(); |
| 37 | +} |
| 38 | + |
| 39 | +DOMStorageAgent::DOMStorageAgent(Environment* env) : env_(env) {} |
| 40 | + |
| 41 | +DOMStorageAgent::~DOMStorageAgent() {} |
| 42 | + |
| 43 | +void DOMStorageAgent::Wire(protocol::UberDispatcher* dispatcher) { |
| 44 | + frontend_ = |
| 45 | + std::make_unique<protocol::DOMStorage::Frontend>(dispatcher->channel()); |
| 46 | + protocol::DOMStorage::Dispatcher::wire(dispatcher, this); |
| 47 | + event_notifier_map_["domStorageItemAdded"] = |
| 48 | + (EventNotifier)(&DOMStorageAgent::domStorageItemAdded); |
| 49 | + event_notifier_map_["domStorageItemRemoved"] = |
| 50 | + (EventNotifier)(&DOMStorageAgent::domStorageItemRemoved); |
| 51 | + event_notifier_map_["domStorageItemUpdated"] = |
| 52 | + (EventNotifier)(&DOMStorageAgent::domStorageItemUpdated); |
| 53 | + event_notifier_map_["domStorageItemsCleared"] = |
| 54 | + (EventNotifier)(&DOMStorageAgent::domStorageItemsCleared); |
| 55 | + event_notifier_map_["registerStorage"] = |
| 56 | + (EventNotifier)(&DOMStorageAgent::registerStorage); |
| 57 | +} |
| 58 | + |
| 59 | +protocol::DispatchResponse DOMStorageAgent::enable() { |
| 60 | + this->enabled_ = true; |
| 61 | + return protocol::DispatchResponse::Success(); |
| 62 | +} |
| 63 | + |
| 64 | +protocol::DispatchResponse DOMStorageAgent::disable() { |
| 65 | + this->enabled_ = false; |
| 66 | + return protocol::DispatchResponse::Success(); |
| 67 | +} |
| 68 | + |
| 69 | +protocol::DispatchResponse DOMStorageAgent::getDOMStorageItems( |
| 70 | + std::unique_ptr<protocol::DOMStorage::StorageId> storageId, |
| 71 | + std::unique_ptr<protocol::Array<protocol::Array<protocol::String>>>* |
| 72 | + items) { |
| 73 | + if (!enabled_) { |
| 74 | + return protocol::DispatchResponse::ServerError( |
| 75 | + "DOMStorage domain is not enabled"); |
| 76 | + } |
| 77 | + bool is_local_storage = storageId->getIsLocalStorage(); |
| 78 | + const std::unordered_map<std::string, std::string>& storage_map = |
| 79 | + is_local_storage ? local_storage_map_ : session_storage_map_; |
| 80 | + auto result = |
| 81 | + std::make_unique<protocol::Array<protocol::Array<protocol::String>>>(); |
| 82 | + for (const auto& pair : storage_map) { |
| 83 | + auto item = std::make_unique<protocol::Array<protocol::String>>(); |
| 84 | + item->push_back(pair.first); |
| 85 | + item->push_back(pair.second); |
| 86 | + result->push_back(std::move(item)); |
| 87 | + } |
| 88 | + *items = std::move(result); |
| 89 | + return protocol::DispatchResponse::Success(); |
| 90 | +} |
| 91 | + |
| 92 | +protocol::DispatchResponse DOMStorageAgent::setDOMStorageItem( |
| 93 | + std::unique_ptr<protocol::DOMStorage::StorageId> storageId, |
| 94 | + const std::string& key, |
| 95 | + const std::string& value) { |
| 96 | + return protocol::DispatchResponse::ServerError("Not implemented"); |
| 97 | +} |
| 98 | + |
| 99 | +protocol::DispatchResponse DOMStorageAgent::removeDOMStorageItem( |
| 100 | + std::unique_ptr<protocol::DOMStorage::StorageId> storageId, |
| 101 | + const std::string& key) { |
| 102 | + return protocol::DispatchResponse::ServerError("Not implemented"); |
| 103 | +} |
| 104 | + |
| 105 | +protocol::DispatchResponse DOMStorageAgent::clear( |
| 106 | + std::unique_ptr<protocol::DOMStorage::StorageId> storageId) { |
| 107 | + return protocol::DispatchResponse::ServerError("Not implemented"); |
| 108 | +} |
| 109 | + |
| 110 | +void DOMStorageAgent::domStorageItemAdded(v8::Local<v8::Context> context, |
| 111 | + v8::Local<v8::Object> params) { |
| 112 | + Local<Object> storage_id_obj; |
| 113 | + if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + std::unique_ptr<protocol::DOMStorage::StorageId> storage_id = |
| 118 | + createStorageIdFromObject(context, storage_id_obj); |
| 119 | + if (!storage_id) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + protocol::String key; |
| 124 | + if (!ObjectGetProtocolString(context, params, "key").To(&key)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + protocol::String new_value; |
| 128 | + if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) { |
| 129 | + return; |
| 130 | + } |
| 131 | + frontend_->domStorageItemAdded(std::move(storage_id), key, new_value); |
| 132 | +} |
| 133 | + |
| 134 | +void DOMStorageAgent::domStorageItemRemoved(v8::Local<v8::Context> context, |
| 135 | + v8::Local<v8::Object> params) { |
| 136 | + Local<Object> storage_id_obj; |
| 137 | + if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) { |
| 138 | + return; |
| 139 | + } |
| 140 | + std::unique_ptr<protocol::DOMStorage::StorageId> storage_id = |
| 141 | + createStorageIdFromObject(context, storage_id_obj); |
| 142 | + |
| 143 | + if (!storage_id) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + protocol::String key; |
| 148 | + if (!ObjectGetProtocolString(context, params, "key").To(&key)) { |
| 149 | + return; |
| 150 | + } |
| 151 | + frontend_->domStorageItemRemoved(std::move(storage_id), key); |
| 152 | +} |
| 153 | + |
| 154 | +void DOMStorageAgent::domStorageItemUpdated(v8::Local<v8::Context> context, |
| 155 | + v8::Local<v8::Object> params) { |
| 156 | + Local<Object> storage_id_obj; |
| 157 | + if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + std::unique_ptr<protocol::DOMStorage::StorageId> storage_id = |
| 162 | + createStorageIdFromObject(context, storage_id_obj); |
| 163 | + |
| 164 | + if (!storage_id) { |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + protocol::String key; |
| 169 | + if (!ObjectGetProtocolString(context, params, "key").To(&key)) { |
| 170 | + return; |
| 171 | + } |
| 172 | + protocol::String old_value; |
| 173 | + if (!ObjectGetProtocolString(context, params, "oldValue").To(&old_value)) { |
| 174 | + return; |
| 175 | + } |
| 176 | + protocol::String new_value; |
| 177 | + if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) { |
| 178 | + return; |
| 179 | + } |
| 180 | + frontend_->domStorageItemUpdated( |
| 181 | + std::move(storage_id), key, old_value, new_value); |
| 182 | +} |
| 183 | + |
| 184 | +void DOMStorageAgent::domStorageItemsCleared(v8::Local<v8::Context> context, |
| 185 | + v8::Local<v8::Object> params) { |
| 186 | + Local<Object> storage_id_obj; |
| 187 | + if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) { |
| 188 | + return; |
| 189 | + } |
| 190 | + std::unique_ptr<protocol::DOMStorage::StorageId> storage_id = |
| 191 | + createStorageIdFromObject(context, storage_id_obj); |
| 192 | + |
| 193 | + if (!storage_id) { |
| 194 | + return; |
| 195 | + } |
| 196 | + frontend_->domStorageItemsCleared(std::move(storage_id)); |
| 197 | +} |
| 198 | + |
| 199 | +void DOMStorageAgent::registerStorage(v8::Local<v8::Context> context, |
| 200 | + v8::Local<v8::Object> params) { |
| 201 | + v8::Isolate* isolate = env_->isolate(); |
| 202 | + HandleScope handle_scope(isolate); |
| 203 | + bool is_local_storage; |
| 204 | + if (!ObjectGetBool(context, params, "isLocalStorage").To(&is_local_storage)) { |
| 205 | + return; |
| 206 | + } |
| 207 | + Local<Object> storage_map_obj; |
| 208 | + if (!ObjectGetObject(context, params, "storageMap") |
| 209 | + .ToLocal(&storage_map_obj)) { |
| 210 | + return; |
| 211 | + } |
| 212 | + std::unordered_map<std::string, std::string>& storage_map = |
| 213 | + is_local_storage ? local_storage_map_ : session_storage_map_; |
| 214 | + Local<Array> property_names; |
| 215 | + if (!storage_map_obj->GetOwnPropertyNames(context).ToLocal(&property_names)) { |
| 216 | + return; |
| 217 | + } |
| 218 | + uint32_t length = property_names->Length(); |
| 219 | + for (uint32_t i = 0; i < length; ++i) { |
| 220 | + Local<Value> key_value; |
| 221 | + if (!property_names->Get(context, i).ToLocal(&key_value)) { |
| 222 | + return; |
| 223 | + } |
| 224 | + Local<Value> value_value; |
| 225 | + if (!storage_map_obj->Get(context, key_value).ToLocal(&value_value)) { |
| 226 | + return; |
| 227 | + } |
| 228 | + node::Utf8Value key_utf8(isolate, key_value); |
| 229 | + node::Utf8Value value_utf8(isolate, value_value); |
| 230 | + storage_map[*key_utf8] = *value_utf8; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +bool DOMStorageAgent::canEmit(const std::string& domain) { |
| 235 | + return domain == "DOMStorage"; |
| 236 | +} |
| 237 | +} // namespace inspector |
| 238 | +} // namespace node |
0 commit comments