|
| 1 | +#include "network_agent.h" |
| 2 | +#include "network_inspector.h" |
| 3 | + |
| 4 | +namespace node { |
| 5 | +namespace inspector { |
| 6 | +namespace protocol { |
| 7 | + |
| 8 | +std::unique_ptr<Network::Request> Request(const String& url, |
| 9 | + const String& method) { |
| 10 | + return Network::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 | +void NetworkAgent::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 | +void NetworkAgent::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 | + return DispatchResponse::OK(); |
| 37 | +} |
| 38 | + |
| 39 | +DispatchResponse NetworkAgent::disable() { |
| 40 | + inspector_->Disable(); |
| 41 | + return DispatchResponse::OK(); |
| 42 | +} |
| 43 | + |
| 44 | +void NetworkAgent::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 | +void NetworkAgent::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 | +void NetworkAgent::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