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/mojo_persistent_cookie_store.h" |
15 #include "mojo/services/network/url_loader_impl.h" | 16 #include "mojo/services/network/url_loader_impl.h" |
| 17 #include "net/cookies/cookie_monster.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 NetworkServiceDelegate* delegate) |
| 82 : NetworkContext(MakeURLRequestContext(base_path, background_task_runner, |
| 83 delegate)) { |
77 } | 84 } |
78 | 85 |
79 NetworkContext::~NetworkContext() { | 86 NetworkContext::~NetworkContext() { |
80 in_shutdown_ = true; | 87 in_shutdown_ = true; |
81 // TODO(darin): Be careful about destruction order of member variables? | 88 // TODO(darin): Be careful about destruction order of member variables? |
82 | 89 |
83 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the | 90 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the |
84 // corresponding net::URLRequestContext is going away with this | 91 // corresponding net::URLRequestContext is going away with this |
85 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), | 92 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), |
86 // so iterate over a copy. | 93 // so iterate over a copy. |
(...skipping 13 matching lines...) Expand all Loading... |
100 DCHECK(removed_count); | 107 DCHECK(removed_count); |
101 } | 108 } |
102 } | 109 } |
103 | 110 |
104 size_t NetworkContext::GetURLLoaderCountForTesting() { | 111 size_t NetworkContext::GetURLLoaderCountForTesting() { |
105 return url_loaders_.size(); | 112 return url_loaders_.size(); |
106 } | 113 } |
107 | 114 |
108 // static | 115 // static |
109 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( | 116 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( |
110 const base::FilePath& base_path) { | 117 const base::FilePath& base_path, |
| 118 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 119 NetworkServiceDelegate* delegate) { |
111 net::URLRequestContextBuilder builder; | 120 net::URLRequestContextBuilder builder; |
112 builder.set_accept_language("en-us,en"); | 121 builder.set_accept_language("en-us,en"); |
113 builder.set_user_agent(mojo::common::GetUserAgent()); | 122 builder.set_user_agent(mojo::common::GetUserAgent()); |
114 builder.set_proxy_service(net::ProxyService::CreateDirect()); | 123 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
115 builder.set_transport_security_persister_path(base_path); | 124 builder.set_transport_security_persister_path(base_path); |
116 | 125 |
117 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 126 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
118 #if defined(OS_ANDROID) | 127 #if defined(OS_ANDROID) |
119 // On Android, we store the cache on disk becase we can run only a single | 128 // On Android, we store the cache on disk becase we can run only a single |
120 // instance of the shell at a time. | 129 // instance of the shell at a time. |
121 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 130 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
122 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); | 131 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); |
123 #else | 132 #else |
124 // On desktop, we store the cache in memory so we can run many shells | 133 // 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 | 134 // in parallel when running tests, otherwise the network services in each |
126 // shell will corrupt the disk cache. | 135 // shell will corrupt the disk cache. |
127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 136 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
128 #endif | 137 #endif |
129 | 138 |
130 builder.EnableHttpCache(cache_params); | 139 builder.EnableHttpCache(cache_params); |
131 builder.set_file_enabled(true); | 140 builder.set_file_enabled(true); |
132 | 141 |
| 142 if (background_task_runner) { |
| 143 // TODO(erg): This only gets run on non-android system. Currently, any |
| 144 // attempts from the network_service trying to access the filesystem break |
| 145 // the apptests on android. (And only the apptests on android. Mandoline |
| 146 // shell works fine on android, as does apptests on desktop.) |
| 147 MojoPersistentCookieStore* cookie_store = |
| 148 new MojoPersistentCookieStore( |
| 149 delegate, |
| 150 base::FilePath(FILE_PATH_LITERAL("Cookies")), |
| 151 base::MessageLoop::current()->task_runner(), |
| 152 background_task_runner, |
| 153 false, // TODO(erg): Make RESTORED_SESSION_COOKIES configurable. |
| 154 nullptr); |
| 155 builder.SetCookieAndChannelIdStores( |
| 156 new net::CookieMonster(cookie_store, nullptr), nullptr); |
| 157 } |
| 158 |
133 return make_scoped_ptr(builder.Build()); | 159 return make_scoped_ptr(builder.Build()); |
134 } | 160 } |
135 | 161 |
136 } // namespace mojo | 162 } // namespace mojo |
OLD | NEW |