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" | |
18 #include "net/log/net_log_util.h" | 16 #include "net/log/net_log_util.h" |
19 #include "net/log/write_to_file_net_log_observer.h" | 17 #include "net/log/write_to_file_net_log_observer.h" |
20 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
21 #include "net/ssl/channel_id_service.h" | |
22 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
23 #include "net/url_request/url_request_context_builder.h" | 20 #include "net/url_request/url_request_context_builder.h" |
24 | 21 |
25 namespace mojo { | 22 namespace mojo { |
26 | 23 |
27 namespace { | 24 namespace { |
28 // Logs network information to the specified file. | 25 // Logs network information to the specified file. |
29 const char kLogNetLog[] = "log-net-log"; | 26 const char kLogNetLog[] = "log-net-log"; |
30 } // namespace | 27 } |
31 | 28 |
32 class NetworkContext::MojoNetLog : public net::NetLog { | 29 class NetworkContext::MojoNetLog : public net::NetLog { |
33 public: | 30 public: |
34 MojoNetLog() { | 31 MojoNetLog() { |
35 const base::CommandLine* command_line = | 32 const base::CommandLine* command_line = |
36 base::CommandLine::ForCurrentProcess(); | 33 base::CommandLine::ForCurrentProcess(); |
37 if (!command_line->HasSwitch(kLogNetLog)) | 34 if (!command_line->HasSwitch(kLogNetLog)) |
38 return; | 35 return; |
39 | 36 |
40 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); | 37 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog); |
(...skipping 27 matching lines...) Expand all Loading... |
68 }; | 65 }; |
69 | 66 |
70 NetworkContext::NetworkContext( | 67 NetworkContext::NetworkContext( |
71 scoped_ptr<net::URLRequestContext> url_request_context) | 68 scoped_ptr<net::URLRequestContext> url_request_context) |
72 : net_log_(new MojoNetLog), | 69 : net_log_(new MojoNetLog), |
73 url_request_context_(url_request_context.Pass()), | 70 url_request_context_(url_request_context.Pass()), |
74 in_shutdown_(false) { | 71 in_shutdown_(false) { |
75 url_request_context_->set_net_log(net_log_.get()); | 72 url_request_context_->set_net_log(net_log_.get()); |
76 } | 73 } |
77 | 74 |
78 NetworkContext::NetworkContext( | 75 NetworkContext::NetworkContext(const base::FilePath& base_path) |
79 const base::FilePath& base_path, | 76 : NetworkContext(MakeURLRequestContext(base_path)) { |
80 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) | |
81 : NetworkContext(MakeURLRequestContext(base_path, background_task_runner)) { | |
82 } | 77 } |
83 | 78 |
84 NetworkContext::~NetworkContext() { | 79 NetworkContext::~NetworkContext() { |
85 in_shutdown_ = true; | 80 in_shutdown_ = true; |
86 // TODO(darin): Be careful about destruction order of member variables? | 81 // TODO(darin): Be careful about destruction order of member variables? |
87 | 82 |
88 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the | 83 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the |
89 // corresponding net::URLRequestContext is going away with this | 84 // corresponding net::URLRequestContext is going away with this |
90 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), | 85 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), |
91 // so iterate over a copy. | 86 // so iterate over a copy. |
(...skipping 13 matching lines...) Expand all Loading... |
105 DCHECK(removed_count); | 100 DCHECK(removed_count); |
106 } | 101 } |
107 } | 102 } |
108 | 103 |
109 size_t NetworkContext::GetURLLoaderCountForTesting() { | 104 size_t NetworkContext::GetURLLoaderCountForTesting() { |
110 return url_loaders_.size(); | 105 return url_loaders_.size(); |
111 } | 106 } |
112 | 107 |
113 // static | 108 // static |
114 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( | 109 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext( |
115 const base::FilePath& base_path, | 110 const base::FilePath& base_path) { |
116 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) { | |
117 net::URLRequestContextBuilder builder; | 111 net::URLRequestContextBuilder builder; |
118 builder.set_accept_language("en-us,en"); | 112 builder.set_accept_language("en-us,en"); |
119 builder.set_user_agent(mojo::common::GetUserAgent()); | 113 builder.set_user_agent(mojo::common::GetUserAgent()); |
120 builder.set_proxy_service(net::ProxyService::CreateDirect()); | 114 builder.set_proxy_service(net::ProxyService::CreateDirect()); |
121 builder.set_transport_security_persister_path(base_path); | 115 builder.set_transport_security_persister_path(base_path); |
122 | 116 |
123 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 117 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
124 #if defined(OS_ANDROID) | 118 #if defined(OS_ANDROID) |
125 // 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 |
126 // instance of the shell at a time. | 120 // instance of the shell at a time. |
127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 121 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
128 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); | 122 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); |
129 #else | 123 #else |
130 // 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 |
131 // in parallel when running tests, otherwise the network services in each | 125 // in parallel when running tests, otherwise the network services in each |
132 // shell will corrupt the disk cache. | 126 // shell will corrupt the disk cache. |
133 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
134 #endif | 128 #endif |
135 | 129 |
136 builder.EnableHttpCache(cache_params); | 130 builder.EnableHttpCache(cache_params); |
137 builder.set_file_enabled(true); | 131 builder.set_file_enabled(true); |
138 | 132 |
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 | |
155 return make_scoped_ptr(builder.Build()); | 133 return make_scoped_ptr(builder.Build()); |
156 } | 134 } |
157 | 135 |
158 } // namespace mojo | 136 } // namespace mojo |
OLD | NEW |