| 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/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "mojo/common/user_agent.h" | 14 #include "mojo/common/user_agent.h" |
| 15 #include "mojo/services/network/url_loader_impl.h" | 15 #include "mojo/services/network/url_loader_impl.h" |
| 16 #include "net/cookies/cookie_monster.h" |
| 17 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
| 16 #include "net/log/net_log_util.h" | 18 #include "net/log/net_log_util.h" |
| 17 #include "net/log/write_to_file_net_log_observer.h" | 19 #include "net/log/write_to_file_net_log_observer.h" |
| 18 #include "net/proxy/proxy_service.h" | 20 #include "net/proxy/proxy_service.h" |
| 21 #include "net/ssl/channel_id_service.h" |
| 19 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_context_builder.h" | 23 #include "net/url_request/url_request_context_builder.h" |
| 21 | 24 |
| 22 namespace mojo { | 25 namespace mojo { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 // Logs network information to the specified file. | 28 // Logs network information to the specified file. |
| 26 const char kLogNetLog[] = "log-net-log"; | 29 const char kLogNetLog[] = "log-net-log"; |
| 27 } | 30 } // namespace |
| 28 | 31 |
| 29 class NetworkContext::MojoNetLog : public net::NetLog { | 32 class NetworkContext::MojoNetLog : public net::NetLog { |
| 30 public: | 33 public: |
| 31 MojoNetLog() { | 34 MojoNetLog() { |
| 32 const base::CommandLine* command_line = | 35 const base::CommandLine* command_line = |
| 33 base::CommandLine::ForCurrentProcess(); | 36 base::CommandLine::ForCurrentProcess(); |
| 34 if (!command_line->HasSwitch(kLogNetLog)) | 37 if (!command_line->HasSwitch(kLogNetLog)) |
| 35 return; | 38 return; |
| 36 | 39 |
| 37 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); | 40 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 65 }; | 68 }; |
| 66 | 69 |
| 67 NetworkContext::NetworkContext( | 70 NetworkContext::NetworkContext( |
| 68 scoped_ptr<net::URLRequestContext> url_request_context) | 71 scoped_ptr<net::URLRequestContext> url_request_context) |
| 69 : net_log_(new MojoNetLog), | 72 : net_log_(new MojoNetLog), |
| 70 url_request_context_(url_request_context.Pass()), | 73 url_request_context_(url_request_context.Pass()), |
| 71 in_shutdown_(false) { | 74 in_shutdown_(false) { |
| 72 url_request_context_->set_net_log(net_log_.get()); | 75 url_request_context_->set_net_log(net_log_.get()); |
| 73 } | 76 } |
| 74 | 77 |
| 75 NetworkContext::NetworkContext(const base::FilePath& base_path) | 78 NetworkContext::NetworkContext( |
| 76 : NetworkContext(MakeURLRequestContext(base_path)) { | 79 const base::FilePath& base_path, |
| 80 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) |
| 81 : NetworkContext(MakeURLRequestContext(base_path, background_task_runner)) { |
| 77 } | 82 } |
| 78 | 83 |
| 79 NetworkContext::~NetworkContext() { | 84 NetworkContext::~NetworkContext() { |
| 80 in_shutdown_ = true; | 85 in_shutdown_ = true; |
| 81 // TODO(darin): Be careful about destruction order of member variables? | 86 // TODO(darin): Be careful about destruction order of member variables? |
| 82 | 87 |
| 83 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the | 88 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the |
| 84 // corresponding net::URLRequestContext is going away with this | 89 // corresponding net::URLRequestContext is going away with this |
| 85 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), | 90 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), |
| 86 // so iterate over a copy. | 91 // so iterate over a copy. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 100 DCHECK(removed_count); | 105 DCHECK(removed_count); |
| 101 } | 106 } |
| 102 } | 107 } |
| 103 | 108 |
| 104 size_t NetworkContext::GetURLLoaderCountForTesting() { | 109 size_t NetworkContext::GetURLLoaderCountForTesting() { |
| 105 return url_loaders_.size(); | 110 return url_loaders_.size(); |
| 106 } | 111 } |
| 107 | 112 |
| 108 // static | 113 // static |
| 109 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( | 114 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( |
| 110 const base::FilePath& base_path) { | 115 const base::FilePath& base_path, |
| 116 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) { |
| 111 net::URLRequestContextBuilder builder; | 117 net::URLRequestContextBuilder builder; |
| 112 builder.set_accept_language("en-us,en"); | 118 builder.set_accept_language("en-us,en"); |
| 113 builder.set_user_agent(mojo::common::GetUserAgent()); | 119 builder.set_user_agent(mojo::common::GetUserAgent()); |
| 114 builder.set_proxy_service(net::ProxyService::CreateDirect()); | 120 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
| 115 builder.set_transport_security_persister_path(base_path); | 121 builder.set_transport_security_persister_path(base_path); |
| 116 | 122 |
| 117 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 123 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
| 118 #if defined(OS_ANDROID) | 124 #if defined(OS_ANDROID) |
| 119 // On Android, we store the cache on disk becase we can run only a single | 125 // On Android, we store the cache on disk becase we can run only a single |
| 120 // instance of the shell at a time. | 126 // instance of the shell at a time. |
| 121 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
| 122 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); | 128 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); |
| 123 #else | 129 #else |
| 124 // On desktop, we store the cache in memory so we can run many shells | 130 // On desktop, we store the cache in memory so we can run many shells |
| 125 // in parallel when running tests, otherwise the network services in each | 131 // in parallel when running tests, otherwise the network services in each |
| 126 // shell will corrupt the disk cache. | 132 // shell will corrupt the disk cache. |
| 127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 133 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
| 128 #endif | 134 #endif |
| 129 | 135 |
| 130 builder.EnableHttpCache(cache_params); | 136 builder.EnableHttpCache(cache_params); |
| 131 builder.set_file_enabled(true); | 137 builder.set_file_enabled(true); |
| 132 | 138 |
| 139 if (background_task_runner) { |
| 140 // TODO(erg): This only gets run on non-android system. Currently, any |
| 141 // attempts from the network_service trying to access the filesystem break |
| 142 // the apptests on android. (And only the apptests on android. Mandoline |
| 143 // shell works fine on android, as does apptests on desktop.) |
| 144 net::SQLitePersistentCookieStore* sqlite_store = |
| 145 new net::SQLitePersistentCookieStore( |
| 146 base::FilePath(FILE_PATH_LITERAL("Cookies")), |
| 147 base::MessageLoop::current()->task_runner(), |
| 148 background_task_runner, |
| 149 false, // TODO(erg): Make RESTORED_SESSION_COOKIES configurable. |
| 150 nullptr); |
| 151 builder.SetCookieAndChannelIdStores( |
| 152 new net::CookieMonster(sqlite_store, nullptr) , nullptr); |
| 153 } |
| 154 |
| 133 return make_scoped_ptr(builder.Build()); | 155 return make_scoped_ptr(builder.Build()); |
| 134 } | 156 } |
| 135 | 157 |
| 136 } // namespace mojo | 158 } // namespace mojo |
| OLD | NEW |