| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/network/network_service.h" | 5 #include "content/network/network_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 9 #include "content/network/cache_url_loader.h" | 10 #include "content/network/network_context.h" |
| 10 #include "content/network/network_service_url_loader_factory_impl.h" | 11 #include "content/public/common/content_switches.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "net/log/net_log_util.h" |
| 14 #include "net/log/write_to_file_net_log_observer.h" |
| 11 #include "services/service_manager/public/cpp/bind_source_info.h" | 15 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 12 | 16 |
| 13 namespace content { | 17 namespace content { |
| 14 | 18 |
| 19 class NetworkService::MojoNetLog : public net::NetLog { |
| 20 public: |
| 21 MojoNetLog() { |
| 22 const base::CommandLine* command_line = |
| 23 base::CommandLine::ForCurrentProcess(); |
| 24 if (!command_line->HasSwitch(switches::kLogNetLog)) |
| 25 return; |
| 26 base::FilePath log_path = |
| 27 command_line->GetSwitchValuePath(switches::kLogNetLog); |
| 28 base::ScopedFILE file; |
| 29 #if defined(OS_WIN) |
| 30 file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 31 #elif defined(OS_POSIX) |
| 32 file.reset(fopen(log_path.value().c_str(), "w")); |
| 33 #endif |
| 34 if (!file) { |
| 35 LOG(ERROR) << "Could not open file " << log_path.value() |
| 36 << " for net logging"; |
| 37 } else { |
| 38 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 39 write_to_file_observer_->set_capture_mode( |
| 40 net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
| 41 write_to_file_observer_->StartObserving(this, std::move(file), nullptr, |
| 42 nullptr); |
| 43 } |
| 44 } |
| 45 ~MojoNetLog() override { |
| 46 if (write_to_file_observer_) |
| 47 write_to_file_observer_->StopObserving(nullptr); |
| 48 } |
| 49 |
| 50 private: |
| 51 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 52 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); |
| 53 }; |
| 54 |
| 15 NetworkService::NetworkService( | 55 NetworkService::NetworkService( |
| 16 std::unique_ptr<service_manager::BinderRegistry> registry) | 56 std::unique_ptr<service_manager::BinderRegistry> registry) |
| 17 : registry_(std::move(registry)) { | 57 : net_log_(new MojoNetLog), registry_(std::move(registry)), binding_(this) { |
| 18 registry_->AddInterface<mojom::URLLoaderFactory>(base::Bind( | 58 registry_->AddInterface<mojom::NetworkService>( |
| 19 &NetworkService::CreateURLLoaderFactory, base::Unretained(this))); | 59 base::Bind(&NetworkService::Create, base::Unretained(this))); |
| 20 registry_->AddInterface<mojom::NetworkService>(base::Bind( | |
| 21 &NetworkService::CreateNetworkService, base::Unretained(this))); | |
| 22 } | 60 } |
| 23 | 61 |
| 24 NetworkService::~NetworkService() = default; | 62 NetworkService::~NetworkService() = default; |
| 25 | 63 |
| 26 void NetworkService::OnBindInterface( | 64 void NetworkService::OnBindInterface( |
| 27 const service_manager::BindSourceInfo& source_info, | 65 const service_manager::BindSourceInfo& source_info, |
| 28 const std::string& interface_name, | 66 const std::string& interface_name, |
| 29 mojo::ScopedMessagePipeHandle interface_pipe) { | 67 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 30 registry_->BindInterface(source_info, interface_name, | 68 registry_->BindInterface(source_info, interface_name, |
| 31 std::move(interface_pipe)); | 69 std::move(interface_pipe)); |
| 32 } | 70 } |
| 33 | 71 |
| 34 void NetworkService::CreateURLLoaderFactory( | 72 void NetworkService::Create(const service_manager::BindSourceInfo& source_info, |
| 35 const service_manager::BindSourceInfo& source_info, | 73 mojom::NetworkServiceRequest request) { |
| 36 mojom::URLLoaderFactoryRequest request) { | 74 DCHECK(!binding_.is_bound()); |
| 37 loader_factory_bindings_.AddBinding( | 75 binding_.Bind(std::move(request)); |
| 38 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(&context_), | 76 } |
| 77 |
| 78 void NetworkService::CreateNetworkContext( |
| 79 mojom::NetworkContextRequest request, |
| 80 mojom::NetworkContextParamsPtr params) { |
| 81 mojo::MakeStrongBinding( |
| 82 base::MakeUnique<NetworkContext>(std::move(request), std::move(params)), |
| 39 std::move(request)); | 83 std::move(request)); |
| 40 } | 84 } |
| 41 | 85 |
| 42 void NetworkService::CreateNetworkService( | |
| 43 const service_manager::BindSourceInfo& source_info, | |
| 44 mojom::NetworkServiceRequest request) { | |
| 45 network_service_bindings_.AddBinding(this, std::move(request)); | |
| 46 } | |
| 47 | |
| 48 void NetworkService::HandleViewCacheRequest(const ResourceRequest& request, | |
| 49 mojom::URLLoaderClientPtr client) { | |
| 50 StartCacheURLLoader(request, context_.url_request_context(), | |
| 51 std::move(client)); | |
| 52 } | |
| 53 | |
| 54 } // namespace content | 86 } // namespace content |
| OLD | NEW |