|
| 1 | +#include"network_agent.h" |
| 2 | +#include"network_inspector.h" |
| 3 | + |
| 4 | +namespacenode { |
| 5 | +namespaceinspector { |
| 6 | +namespaceprotocol { |
| 7 | + |
| 8 | +std::unique_ptr<Network::Request> Request(const String& url, |
| 9 | +const String& method) { |
| 10 | +returnNetwork::Request::create().setUrl(url).setMethod(method).build(); |
| 11 | +} |
| 12 | + |
| 13 | +NetworkAgent::NetworkAgent(NetworkInspector* inspector) |
| 14 | + : inspector_(inspector) { |
| 15 | + event_notifier_map_["requestWillBeSent"] = &NetworkAgent::requestWillBeSent; |
| 16 | + event_notifier_map_["responseReceived"] = &NetworkAgent::responseReceived; |
| 17 | + event_notifier_map_["loadingFinished"] = &NetworkAgent::loadingFinished; |
| 18 | +} |
| 19 | + |
| 20 | +voidNetworkAgent::emitNotification( |
| 21 | +const String& event, std::unique_ptr<protocol::DictionaryValue> params) { |
| 22 | +if (!inspector_->IsEnabled()) return; |
| 23 | +auto it = event_notifier_map_.find(event); |
| 24 | +if (it != event_notifier_map_.end()) { |
| 25 | + (this->*(it->second))(std::move(params)); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +voidNetworkAgent::Wire(UberDispatcher* dispatcher) { |
| 30 | + frontend_ = std::make_unique<Network::Frontend>(dispatcher->channel()); |
| 31 | +Network::Dispatcher::wire(dispatcher, this); |
| 32 | +} |
| 33 | + |
| 34 | +DispatchResponse NetworkAgent::enable() { |
| 35 | + inspector_->Enable(); |
| 36 | +returnDispatchResponse::OK(); |
| 37 | +} |
| 38 | + |
| 39 | +DispatchResponse NetworkAgent::disable() { |
| 40 | + inspector_->Disable(); |
| 41 | +returnDispatchResponse::OK(); |
| 42 | +} |
| 43 | + |
| 44 | +voidNetworkAgent::requestWillBeSent( |
| 45 | + std::unique_ptr<protocol::DictionaryValue> params) { |
| 46 | + String request_id; |
| 47 | + params->getString("requestId", &request_id); |
| 48 | +double timestamp; |
| 49 | + params->getDouble("timestamp", ×tamp); |
| 50 | +double wall_time; |
| 51 | + params->getDouble("wallTime", &wall_time); |
| 52 | +auto request = params->getObject("request"); |
| 53 | + String url; |
| 54 | + request->getString("url", &url); |
| 55 | + String method; |
| 56 | + request->getString("method", &method); |
| 57 | + |
| 58 | + frontend_->requestWillBeSent( |
| 59 | + request_id, Request(url, method), timestamp, wall_time); |
| 60 | +} |
| 61 | + |
| 62 | +voidNetworkAgent::responseReceived( |
| 63 | + std::unique_ptr<protocol::DictionaryValue> params) { |
| 64 | + String request_id; |
| 65 | + params->getString("requestId", &request_id); |
| 66 | +double timestamp; |
| 67 | + params->getDouble("timestamp", ×tamp); |
| 68 | + |
| 69 | + frontend_->responseReceived(request_id, timestamp); |
| 70 | +} |
| 71 | + |
| 72 | +voidNetworkAgent::loadingFinished( |
| 73 | + std::unique_ptr<protocol::DictionaryValue> params) { |
| 74 | + String request_id; |
| 75 | + params->getString("requestId", &request_id); |
| 76 | +double timestamp; |
| 77 | + params->getDouble("timestamp", ×tamp); |
| 78 | + |
| 79 | + frontend_->loadingFinished(request_id, timestamp); |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace protocol |
| 83 | +} // namespace inspector |
| 84 | +} // namespace node |
0 commit comments