OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/network_handler.h" | 5 #include "content/browser/devtools/protocol/network_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "content/browser/devtools/devtools_session.h" | 16 #include "content/browser/devtools/devtools_session.h" |
| 17 #include "content/browser/devtools/devtools_url_interceptor_request_job.h" |
17 #include "content/browser/devtools/protocol/page.h" | 18 #include "content/browser/devtools/protocol/page.h" |
18 #include "content/browser/devtools/protocol/security.h" | 19 #include "content/browser/devtools/protocol/security.h" |
19 #include "content/browser/frame_host/frame_tree_node.h" | 20 #include "content/browser/frame_host/frame_tree_node.h" |
20 #include "content/browser/frame_host/render_frame_host_impl.h" | 21 #include "content/browser/frame_host/render_frame_host_impl.h" |
21 #include "content/common/navigation_params.h" | 22 #include "content/common/navigation_params.h" |
22 #include "content/common/resource_request.h" | 23 #include "content/common/resource_request.h" |
23 #include "content/common/resource_request_completion_status.h" | 24 #include "content/common/resource_request_completion_status.h" |
24 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/content_browser_client.h" | 27 #include "content/public/browser/content_browser_client.h" |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 case blink::WebReferrerPolicyOriginWhenCrossOrigin: | 310 case blink::WebReferrerPolicyOriginWhenCrossOrigin: |
310 return Network::Request::ReferrerPolicyEnum::OriginWhenCrossOrigin; | 311 return Network::Request::ReferrerPolicyEnum::OriginWhenCrossOrigin; |
311 case blink::WebReferrerPolicyNoReferrerWhenDowngradeOriginWhenCrossOrigin: | 312 case blink::WebReferrerPolicyNoReferrerWhenDowngradeOriginWhenCrossOrigin: |
312 return Network::Request::ReferrerPolicyEnum:: | 313 return Network::Request::ReferrerPolicyEnum:: |
313 NoReferrerWhenDowngradeOriginWhenCrossOrigin; | 314 NoReferrerWhenDowngradeOriginWhenCrossOrigin; |
314 } | 315 } |
315 NOTREACHED(); | 316 NOTREACHED(); |
316 return Network::Request::ReferrerPolicyEnum::NoReferrerWhenDowngrade; | 317 return Network::Request::ReferrerPolicyEnum::NoReferrerWhenDowngrade; |
317 } | 318 } |
318 | 319 |
| 320 String referrerPolicy(net::URLRequest::ReferrerPolicy referrer_policy) { |
| 321 switch (referrer_policy) { |
| 322 case net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE: |
| 323 return Network::Request::ReferrerPolicyEnum::NoReferrerWhenDowngrade; |
| 324 case net::URLRequest:: |
| 325 REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN: |
| 326 return Network::Request::ReferrerPolicyEnum:: |
| 327 NoReferrerWhenDowngradeOriginWhenCrossOrigin; |
| 328 case net::URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN: |
| 329 return Network::Request::ReferrerPolicyEnum:: |
| 330 NoReferrerWhenDowngradeOriginWhenCrossOrigin; |
| 331 case net::URLRequest::NEVER_CLEAR_REFERRER: |
| 332 return Network::Request::ReferrerPolicyEnum::Origin; |
| 333 case net::URLRequest::ORIGIN: |
| 334 return Network::Request::ReferrerPolicyEnum::Origin; |
| 335 case net::URLRequest::NO_REFERRER: |
| 336 return Network::Request::ReferrerPolicyEnum::NoReferrer; |
| 337 default: |
| 338 break; |
| 339 } |
| 340 NOTREACHED(); |
| 341 return Network::Request::ReferrerPolicyEnum::NoReferrerWhenDowngrade; |
| 342 } |
| 343 |
319 String securityState(const GURL& url, const net::CertStatus& cert_status) { | 344 String securityState(const GURL& url, const net::CertStatus& cert_status) { |
320 if (!url.SchemeIsCryptographic()) | 345 if (!url.SchemeIsCryptographic()) |
321 return Security::SecurityStateEnum::Neutral; | 346 return Security::SecurityStateEnum::Neutral; |
322 if (net::IsCertStatusError(cert_status) && | 347 if (net::IsCertStatusError(cert_status) && |
323 !net::IsCertStatusMinorError(cert_status)) { | 348 !net::IsCertStatusMinorError(cert_status)) { |
324 return Security::SecurityStateEnum::Insecure; | 349 return Security::SecurityStateEnum::Insecure; |
325 } | 350 } |
326 return Security::SecurityStateEnum::Secure; | 351 return Security::SecurityStateEnum::Secure; |
327 } | 352 } |
328 | 353 |
| 354 net::Error NetErrorFromString(const std::string& error) { |
| 355 if (error == Network::ErrorReasonEnum::Failed) |
| 356 return net::ERR_FAILED; |
| 357 if (error == Network::ErrorReasonEnum::Aborted) |
| 358 return net::ERR_ABORTED; |
| 359 if (error == Network::ErrorReasonEnum::TimedOut) |
| 360 return net::ERR_TIMED_OUT; |
| 361 if (error == Network::ErrorReasonEnum::AccessDenied) |
| 362 return net::ERR_ACCESS_DENIED; |
| 363 if (error == Network::ErrorReasonEnum::ConnectionClosed) |
| 364 return net::ERR_CONNECTION_CLOSED; |
| 365 if (error == Network::ErrorReasonEnum::ConnectionReset) |
| 366 return net::ERR_CONNECTION_RESET; |
| 367 if (error == Network::ErrorReasonEnum::ConnectionRefused) |
| 368 return net::ERR_CONNECTION_REFUSED; |
| 369 if (error == Network::ErrorReasonEnum::ConnectionAborted) |
| 370 return net::ERR_CONNECTION_ABORTED; |
| 371 if (error == Network::ErrorReasonEnum::ConnectionFailed) |
| 372 return net::ERR_CONNECTION_FAILED; |
| 373 if (error == Network::ErrorReasonEnum::NameNotResolved) |
| 374 return net::ERR_NAME_NOT_RESOLVED; |
| 375 if (error == Network::ErrorReasonEnum::InternetDisconnected) |
| 376 return net::ERR_INTERNET_DISCONNECTED; |
| 377 if (error == Network::ErrorReasonEnum::AddressUnreachable) |
| 378 return net::ERR_ADDRESS_UNREACHABLE; |
| 379 return net::ERR_FAILED; |
| 380 } |
| 381 |
329 double timeDelta(base::TimeTicks time, | 382 double timeDelta(base::TimeTicks time, |
330 base::TimeTicks start, | 383 base::TimeTicks start, |
331 double invalid_value = -1) { | 384 double invalid_value = -1) { |
332 return time.is_null() ? invalid_value : (time - start).InMillisecondsF(); | 385 return time.is_null() ? invalid_value : (time - start).InMillisecondsF(); |
333 } | 386 } |
334 | 387 |
335 std::unique_ptr<Network::ResourceTiming> getTiming( | 388 std::unique_ptr<Network::ResourceTiming> getTiming( |
336 const net::LoadTimingInfo& load_timing) { | 389 const net::LoadTimingInfo& load_timing) { |
337 const base::TimeTicks kNullTicks; | 390 const base::TimeTicks kNullTicks; |
338 return Network::ResourceTiming::Create() | 391 return Network::ResourceTiming::Create() |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 445 } |
393 } | 446 } |
394 return protocol; | 447 return protocol; |
395 } | 448 } |
396 | 449 |
397 } // namespace | 450 } // namespace |
398 | 451 |
399 NetworkHandler::NetworkHandler() | 452 NetworkHandler::NetworkHandler() |
400 : DevToolsDomainHandler(Network::Metainfo::domainName), | 453 : DevToolsDomainHandler(Network::Metainfo::domainName), |
401 host_(nullptr), | 454 host_(nullptr), |
402 enabled_(false) { | 455 enabled_(false), |
403 } | 456 interception_enabled_(false) {} |
404 | 457 |
405 NetworkHandler::~NetworkHandler() { | 458 NetworkHandler::~NetworkHandler() { |
| 459 for (auto& pair : interceptor_map_) { |
| 460 pair.second->OnNetworkHandlerDeleted(); |
| 461 } |
406 } | 462 } |
407 | 463 |
408 // static | 464 // static |
409 NetworkHandler* NetworkHandler::FromSession(DevToolsSession* session) { | 465 NetworkHandler* NetworkHandler::FromSession(DevToolsSession* session) { |
410 return static_cast<NetworkHandler*>( | 466 return static_cast<NetworkHandler*>( |
411 session->GetHandlerByName(Network::Metainfo::domainName)); | 467 session->GetHandlerByName(Network::Metainfo::domainName)); |
412 } | 468 } |
413 | 469 |
414 void NetworkHandler::Wire(UberDispatcher* dispatcher) { | 470 void NetworkHandler::Wire(UberDispatcher* dispatcher) { |
415 frontend_.reset(new Network::Frontend(dispatcher->channel())); | 471 frontend_.reset(new Network::Frontend(dispatcher->channel())); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 request_id, | 774 request_id, |
719 base::TimeTicks::Now().ToInternalValue() / | 775 base::TimeTicks::Now().ToInternalValue() / |
720 static_cast<double>(base::Time::kMicrosecondsPerSecond), | 776 static_cast<double>(base::Time::kMicrosecondsPerSecond), |
721 Page::ResourceTypeEnum::Document, error_string, cancelled); | 777 Page::ResourceTypeEnum::Document, error_string, cancelled); |
722 } | 778 } |
723 | 779 |
724 std::string NetworkHandler::UserAgentOverride() const { | 780 std::string NetworkHandler::UserAgentOverride() const { |
725 return enabled_ ? user_agent_ : std::string(); | 781 return enabled_ ? user_agent_ : std::string(); |
726 } | 782 } |
727 | 783 |
| 784 DispatchResponse NetworkHandler::EnableFetchInterception(bool enabled) { |
| 785 base::AutoLock lock(interception_lock_); |
| 786 interception_enabled_ = enabled; |
| 787 return Response::OK(); |
| 788 } |
| 789 |
| 790 bool NetworkHandler::interception_enabled() const { |
| 791 base::AutoLock lock(interception_lock_); |
| 792 return interception_enabled_; |
| 793 } |
| 794 |
| 795 DispatchResponse NetworkHandler::AllowRequest(const String& intercept_id) { |
| 796 base::AutoLock lock(interception_lock_); |
| 797 const auto it = interceptor_map_.find(intercept_id); |
| 798 if (it == interceptor_map_.end()) { |
| 799 return Response::InvalidParams( |
| 800 base::StringPrintf("Invalid interceptId '%s'", intercept_id.c_str())); |
| 801 } |
| 802 if (!it->second->OnAllow()) { |
| 803 return Response::InvalidParams(base::StringPrintf( |
| 804 "Already processed interceptId '%s'", intercept_id.c_str())); |
| 805 } |
| 806 return Response::OK(); |
| 807 } |
| 808 |
| 809 DispatchResponse NetworkHandler::BlockRequest(const String& intercept_id, |
| 810 const std::string& error_reason) { |
| 811 const auto it = interceptor_map_.find(intercept_id); |
| 812 if (it == interceptor_map_.end()) { |
| 813 return Response::InvalidParams( |
| 814 base::StringPrintf("Invalid interceptId '%s'", intercept_id.c_str())); |
| 815 } |
| 816 if (!it->second->OnBlock(NetErrorFromString(error_reason))) { |
| 817 return Response::InvalidParams(base::StringPrintf( |
| 818 "Already processed interceptId '%s'", intercept_id.c_str())); |
| 819 } |
| 820 return Response::OK(); |
| 821 } |
| 822 |
| 823 DispatchResponse NetworkHandler::ModifyRequest( |
| 824 const String& intercept_id, |
| 825 Maybe<String> url, |
| 826 Maybe<String> method, |
| 827 Maybe<String> post_data, |
| 828 Maybe<protocol::Network::Headers> headers) { |
| 829 const auto it = interceptor_map_.find(intercept_id); |
| 830 if (it == interceptor_map_.end()) { |
| 831 return Response::InvalidParams( |
| 832 base::StringPrintf("Invalid interceptId '%s'", intercept_id.c_str())); |
| 833 } |
| 834 if (!it->second->OnModifyRequest(std::move(url), std::move(method), |
| 835 std::move(post_data), std::move(headers))) { |
| 836 return Response::InvalidParams(base::StringPrintf( |
| 837 "Already processed interceptId '%s'", intercept_id.c_str())); |
| 838 } |
| 839 return Response::OK(); |
| 840 } |
| 841 |
| 842 DispatchResponse NetworkHandler::MockResponse(const String& intercept_id, |
| 843 const String& raw_response) { |
| 844 const auto it = interceptor_map_.find(intercept_id); |
| 845 if (it == interceptor_map_.end()) { |
| 846 return Response::InvalidParams( |
| 847 base::StringPrintf("Invalid interceptId '%s'", intercept_id.c_str())); |
| 848 } |
| 849 if (!it->second->OnMockResponse(raw_response)) { |
| 850 return Response::InvalidParams(base::StringPrintf( |
| 851 "Already processed interceptId '%s'", intercept_id.c_str())); |
| 852 } |
| 853 return Response::OK(); |
| 854 } |
| 855 |
| 856 void NetworkHandler::RegisterURLInterceptor( |
| 857 const std::string& intercept_id, |
| 858 DevToolsURLInterceptorRequestJob* interceptor) { |
| 859 base::AutoLock lock(interception_lock_); |
| 860 DCHECK(interceptor_map_.find(intercept_id) == interceptor_map_.end()); |
| 861 interceptor_map_[intercept_id] = interceptor; |
| 862 } |
| 863 |
| 864 void NetworkHandler::UnregisterURLInterceptor( |
| 865 const std::string& intercept_id, |
| 866 DevToolsURLInterceptorRequestJob* interceptor) { |
| 867 base::AutoLock lock(interception_lock_); |
| 868 DCHECK(interceptor_map_.find(intercept_id) != interceptor_map_.end()); |
| 869 DCHECK_EQ(interceptor_map_[intercept_id], interceptor); |
| 870 interceptor_map_.erase(intercept_id); |
| 871 } |
| 872 |
| 873 // static |
| 874 std::unique_ptr<Network::Request> NetworkHandler::CreateRequestFromURLRequest( |
| 875 const net::URLRequest* request) { |
| 876 std::unique_ptr<DictionaryValue> headers_dict(DictionaryValue::create()); |
| 877 for (net::HttpRequestHeaders::Iterator it(request->extra_request_headers()); |
| 878 it.GetNext();) { |
| 879 headers_dict->setString(it.name(), it.value()); |
| 880 } |
| 881 // TODO(alexclarke): Support post data |
| 882 return Network::Request::Create() |
| 883 .SetUrl(request->url().spec()) |
| 884 .SetMethod(request->method()) |
| 885 .SetHeaders(Object::fromValue(headers_dict.get(), nullptr)) |
| 886 .SetInitialPriority(resourcePriority(request->priority())) |
| 887 .SetReferrerPolicy(referrerPolicy(request->referrer_policy())) |
| 888 .Build(); |
| 889 } |
| 890 |
728 } // namespace protocol | 891 } // namespace protocol |
729 } // namespace content | 892 } // namespace content |
OLD | NEW |