| 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" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "mojo/common/user_agent.h" | |
| 16 #include "mojo/services/network/url_loader_impl.h" | 15 #include "mojo/services/network/url_loader_impl.h" |
| 17 #include "net/log/net_log_util.h" | 16 #include "net/log/net_log_util.h" |
| 18 #include "net/log/write_to_file_net_log_observer.h" | 17 #include "net/log/write_to_file_net_log_observer.h" |
| 19 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
| 20 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 21 #include "net/url_request/url_request_context_builder.h" | 20 #include "net/url_request/url_request_context_builder.h" |
| 22 | 21 |
| 23 namespace mojo { | 22 namespace mojo { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 25 |
| 26 // Logs network information to the specified file. | 26 // Logs network information to the specified file. |
| 27 const char kLogNetLog[] = "log-net-log"; | 27 const char kLogNetLog[] = "log-net-log"; |
| 28 | 28 |
| 29 base::FilePath GetCacheDirectory(const base::FilePath& base_path) { | 29 base::FilePath GetCacheDirectory(const base::FilePath& base_path) { |
| 30 return base_path.Append(FILE_PATH_LITERAL("Cache")); | 30 return base_path.Append(FILE_PATH_LITERAL("Cache")); |
| 31 } | 31 } |
| 32 |
| 33 std::string GetUserAgent() { |
| 34 // TODO(vtl): Haha, very funny. |
| 35 return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like " |
| 36 "Gecko) Chrome/42.0.2311.68 Safari/537.36"; |
| 32 } | 37 } |
| 33 | 38 |
| 39 } // namespace |
| 40 |
| 34 class NetworkContext::MojoNetLog : public net::NetLog { | 41 class NetworkContext::MojoNetLog : public net::NetLog { |
| 35 public: | 42 public: |
| 36 MojoNetLog() { | 43 MojoNetLog() { |
| 37 const base::CommandLine* command_line = | 44 const base::CommandLine* command_line = |
| 38 base::CommandLine::ForCurrentProcess(); | 45 base::CommandLine::ForCurrentProcess(); |
| 39 if (!command_line->HasSwitch(kLogNetLog)) | 46 if (!command_line->HasSwitch(kLogNetLog)) |
| 40 return; | 47 return; |
| 41 | 48 |
| 42 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); | 49 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); |
| 43 base::ScopedFILE file; | 50 base::ScopedFILE file; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 141 |
| 135 size_t NetworkContext::GetURLLoaderCountForTesting() { | 142 size_t NetworkContext::GetURLLoaderCountForTesting() { |
| 136 return url_loaders_.size(); | 143 return url_loaders_.size(); |
| 137 } | 144 } |
| 138 | 145 |
| 139 // static | 146 // static |
| 140 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( | 147 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( |
| 141 const base::FilePath& base_path) { | 148 const base::FilePath& base_path) { |
| 142 net::URLRequestContextBuilder builder; | 149 net::URLRequestContextBuilder builder; |
| 143 builder.set_accept_language("en-us,en"); | 150 builder.set_accept_language("en-us,en"); |
| 144 builder.set_user_agent(mojo::common::GetUserAgent()); | 151 builder.set_user_agent(GetUserAgent()); |
| 145 builder.set_proxy_service(net::ProxyService::CreateDirect()); | 152 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
| 146 builder.set_transport_security_persister_path(base_path); | 153 builder.set_transport_security_persister_path(base_path); |
| 147 builder.set_data_enabled(true); | 154 builder.set_data_enabled(true); |
| 148 | 155 |
| 149 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 156 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
| 150 #if defined(OS_ANDROID) | 157 #if defined(OS_ANDROID) |
| 151 // On Android, we store the cache on disk becase we can run only a single | 158 // On Android, we store the cache on disk becase we can run only a single |
| 152 // instance of the shell at a time. | 159 // instance of the shell at a time. |
| 153 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 160 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
| 154 cache_params.path = GetCacheDirectory(base_path); | 161 cache_params.path = GetCacheDirectory(base_path); |
| 155 #else | 162 #else |
| 156 // On desktop, we store the cache in memory so we can run many shells | 163 // On desktop, we store the cache in memory so we can run many shells |
| 157 // in parallel when running tests, otherwise the network services in each | 164 // in parallel when running tests, otherwise the network services in each |
| 158 // shell will corrupt the disk cache. | 165 // shell will corrupt the disk cache. |
| 159 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 166 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
| 160 #endif | 167 #endif |
| 161 | 168 |
| 162 builder.EnableHttpCache(cache_params); | 169 builder.EnableHttpCache(cache_params); |
| 163 builder.set_file_enabled(true); | 170 builder.set_file_enabled(true); |
| 164 | 171 |
| 165 return make_scoped_ptr(builder.Build()); | 172 return make_scoped_ptr(builder.Build()); |
| 166 } | 173 } |
| 167 | 174 |
| 168 } // namespace mojo | 175 } // namespace mojo |
| OLD | NEW |