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 "mojo/services/network/network_context.h" | 5 #include "mojo/services/network/network_context.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" |
11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "mojo/common/user_agent.h" |
12 #include "mojo/services/network/url_loader_impl.h" | 15 #include "mojo/services/network/url_loader_impl.h" |
| 16 #include "net/log/net_log_util.h" |
| 17 #include "net/log/write_to_file_net_log_observer.h" |
13 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
14 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_context_builder.h" | 20 #include "net/url_request/url_request_context_builder.h" |
16 | 21 |
17 namespace mojo { | 22 namespace mojo { |
18 | 23 |
| 24 namespace { |
| 25 // Logs network information to the specified file. |
| 26 const char kLogNetLog[] = "log-net-log"; |
| 27 } |
| 28 |
| 29 class NetworkContext::MojoNetLog : public net::NetLog { |
| 30 public: |
| 31 MojoNetLog() { |
| 32 const base::CommandLine* command_line = |
| 33 base::CommandLine::ForCurrentProcess(); |
| 34 if (!command_line->HasSwitch(kLogNetLog)) |
| 35 return; |
| 36 |
| 37 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); |
| 38 base::ScopedFILE file; |
| 39 #if defined(OS_WIN) |
| 40 file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 41 #elif defined(OS_POSIX) |
| 42 file.reset(fopen(log_path.value().c_str(), "w")); |
| 43 #endif |
| 44 if (!file) { |
| 45 LOG(ERROR) << "Could not open file " << log_path.value() |
| 46 << " for net logging"; |
| 47 } else { |
| 48 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 49 write_to_file_observer_->set_capture_mode( |
| 50 net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
| 51 write_to_file_observer_->StartObserving(this, file.Pass(), nullptr, |
| 52 nullptr); |
| 53 } |
| 54 } |
| 55 |
| 56 ~MojoNetLog() override { |
| 57 if (write_to_file_observer_) |
| 58 write_to_file_observer_->StopObserving(nullptr); |
| 59 } |
| 60 |
| 61 private: |
| 62 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); |
| 65 }; |
| 66 |
19 NetworkContext::NetworkContext( | 67 NetworkContext::NetworkContext( |
20 scoped_ptr<net::URLRequestContext> url_request_context) | 68 scoped_ptr<net::URLRequestContext> url_request_context) |
21 : url_request_context_(url_request_context.Pass()), | 69 : net_log_(new MojoNetLog), |
| 70 url_request_context_(url_request_context.Pass()), |
22 in_shutdown_(false) { | 71 in_shutdown_(false) { |
| 72 url_request_context_->set_net_log(net_log_.get()); |
23 } | 73 } |
24 | 74 |
25 NetworkContext::NetworkContext(const base::FilePath& base_path) | 75 NetworkContext::NetworkContext(const base::FilePath& base_path) |
26 : NetworkContext(MakeURLRequestContext(base_path)) { | 76 : NetworkContext(MakeURLRequestContext(base_path)) { |
27 } | 77 } |
28 | 78 |
29 NetworkContext::~NetworkContext() { | 79 NetworkContext::~NetworkContext() { |
30 in_shutdown_ = true; | 80 in_shutdown_ = true; |
31 // TODO(darin): Be careful about destruction order of member variables? | 81 // TODO(darin): Be careful about destruction order of member variables? |
32 | 82 |
(...skipping 20 matching lines...) Expand all Loading... |
53 | 103 |
54 size_t NetworkContext::GetURLLoaderCountForTesting() { | 104 size_t NetworkContext::GetURLLoaderCountForTesting() { |
55 return url_loaders_.size(); | 105 return url_loaders_.size(); |
56 } | 106 } |
57 | 107 |
58 // static | 108 // static |
59 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( | 109 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( |
60 const base::FilePath& base_path) { | 110 const base::FilePath& base_path) { |
61 net::URLRequestContextBuilder builder; | 111 net::URLRequestContextBuilder builder; |
62 builder.set_accept_language("en-us,en"); | 112 builder.set_accept_language("en-us,en"); |
63 // TODO(darin): This is surely the wrong UA string. | 113 builder.set_user_agent(mojo::common::GetUserAgent()); |
64 builder.set_user_agent("Mojo/0.1"); | |
65 builder.set_proxy_service(net::ProxyService::CreateDirect()); | 114 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
66 builder.set_transport_security_persister_path(base_path); | 115 builder.set_transport_security_persister_path(base_path); |
67 | 116 |
68 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 117 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
69 #if defined(OS_ANDROID) | 118 #if defined(OS_ANDROID) |
70 // On Android, we store the cache on disk becase we can run only a single | 119 // On Android, we store the cache on disk becase we can run only a single |
71 // instance of the shell at a time. | 120 // instance of the shell at a time. |
72 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 121 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
73 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); | 122 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); |
74 #else | 123 #else |
75 // On desktop, we store the cache in memory so we can run many shells | 124 // On desktop, we store the cache in memory so we can run many shells |
76 // in parallel when running tests, otherwise the network services in each | 125 // in parallel when running tests, otherwise the network services in each |
77 // shell will corrupt the disk cache. | 126 // shell will corrupt the disk cache. |
78 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
79 #endif | 128 #endif |
80 | 129 |
81 builder.EnableHttpCache(cache_params); | 130 builder.EnableHttpCache(cache_params); |
82 builder.set_file_enabled(true); | 131 builder.set_file_enabled(true); |
| 132 |
83 return make_scoped_ptr(builder.Build()); | 133 return make_scoped_ptr(builder.Build()); |
84 } | 134 } |
85 | 135 |
86 } // namespace mojo | 136 } // namespace mojo |
OLD | NEW |