OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/proxy/proxy_config_service_linux.h" | 5 #include "net/proxy/proxy_config_service_linux.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 255 |
256 // This helper class runs ProxyConfigServiceLinux::GetLatestProxyConfig() on | 256 // This helper class runs ProxyConfigServiceLinux::GetLatestProxyConfig() on |
257 // the IO thread and synchronously waits for the result. | 257 // the IO thread and synchronously waits for the result. |
258 // Some code duplicated from proxy_script_fetcher_unittest.cc. | 258 // Some code duplicated from proxy_script_fetcher_unittest.cc. |
259 class SynchConfigGetter { | 259 class SynchConfigGetter { |
260 public: | 260 public: |
261 // Takes ownership of |config_service|. | 261 // Takes ownership of |config_service|. |
262 explicit SynchConfigGetter(net::ProxyConfigServiceLinux* config_service) | 262 explicit SynchConfigGetter(net::ProxyConfigServiceLinux* config_service) |
263 : event_(false, false), | 263 : event_(false, false), |
264 io_thread_("IO_Thread"), | 264 io_thread_("IO_Thread"), |
| 265 glib_task_runner_(base::MessageLoopProxy::current()), |
265 config_service_(config_service) { | 266 config_service_(config_service) { |
266 // Start an IO thread. | 267 // Start an IO thread. |
267 base::Thread::Options options; | 268 base::Thread::Options options; |
268 options.message_loop_type = MessageLoop::TYPE_IO; | 269 options.message_loop_type = MessageLoop::TYPE_IO; |
269 io_thread_.StartWithOptions(options); | 270 io_thread_.StartWithOptions(options); |
270 | 271 |
271 // Make sure the thread started. | 272 // Make sure the thread started. |
272 io_thread_.message_loop()->PostTask(FROM_HERE, | 273 io_thread_.message_loop()->PostTask(FROM_HERE, |
273 base::Bind(&SynchConfigGetter::Init, base::Unretained(this))); | 274 base::Bind(&SynchConfigGetter::Init, base::Unretained(this))); |
274 Wait(); | 275 Wait(); |
275 } | 276 } |
276 | 277 |
277 ~SynchConfigGetter() { | 278 ~SynchConfigGetter() { |
278 // Let the config service post a destroy message to the IO thread | 279 // Let the config service post a destroy message to the IO thread |
279 // before cleaning up that thread. | 280 // before cleaning up that thread. |
280 delete config_service_; | 281 delete config_service_; |
281 // Clean up the IO thread. | 282 // Clean up the IO thread. |
282 io_thread_.message_loop()->PostTask(FROM_HERE, | 283 io_thread_.message_loop()->PostTask(FROM_HERE, |
283 base::Bind(&SynchConfigGetter::CleanUp, base::Unretained(this))); | 284 base::Bind(&SynchConfigGetter::CleanUp, base::Unretained(this))); |
284 Wait(); | 285 Wait(); |
285 } | 286 } |
286 | 287 |
287 // Does gconf setup and initial fetch of the proxy config, | 288 // Does gconf setup and initial fetch of the proxy config. |
288 // all on the calling thread (meant to be the thread with the | |
289 // default glib main loop, which is the UI thread). | |
290 void SetupAndInitialFetch() { | 289 void SetupAndInitialFetch() { |
291 MessageLoop* file_loop = io_thread_.message_loop(); | 290 io_thread_.message_loop()->PostTask(FROM_HERE, |
292 DCHECK_EQ(MessageLoop::TYPE_IO, file_loop->type()); | 291 base::Bind(&SynchConfigGetter::SetupAndInitialFetchOnIOThread, |
293 // We pass the mock IO thread as both the IO and file threads. | 292 base::Unretained(this))); |
294 config_service_->SetupAndFetchInitialConfig( | 293 Wait(); |
295 base::MessageLoopProxy::current(), io_thread_.message_loop_proxy(), | 294 // ProxyConfigServiceLinux will post back to the UI thread, so let it run |
296 static_cast<MessageLoopForIO*>(file_loop)); | 295 MessageLoop::current()->RunAllPending(); |
| 296 // ProxyConfigServiceLinux will post back to the IO thread, so let it run |
| 297 io_thread_.message_loop()->PostTask(FROM_HERE, |
| 298 base::Bind(&SynchConfigGetter::CleanUp, base::Unretained(this))); |
| 299 Wait(); |
297 } | 300 } |
298 // Synchronously gets the proxy config. | 301 // Synchronously gets the proxy config. |
299 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( | 302 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( |
300 net::ProxyConfig* config) { | 303 net::ProxyConfig* config) { |
301 io_thread_.message_loop()->PostTask(FROM_HERE, | 304 io_thread_.message_loop()->PostTask(FROM_HERE, |
302 base::Bind(&SynchConfigGetter::GetLatestConfigOnIOThread, | 305 base::Bind(&SynchConfigGetter::GetLatestConfigOnIOThread, |
303 base::Unretained(this))); | 306 base::Unretained(this))); |
304 Wait(); | 307 Wait(); |
305 *config = proxy_config_; | 308 *config = proxy_config_; |
306 return get_latest_config_result_; | 309 return get_latest_config_result_; |
307 } | 310 } |
308 | 311 |
309 private: | 312 private: |
310 // [Runs on |io_thread_|] | 313 // [Runs on |io_thread_|] |
311 void Init() { | 314 void Init() { |
312 event_.Signal(); | 315 event_.Signal(); |
313 } | 316 } |
314 | 317 |
315 // Calls GetLatestProxyConfig, running on |io_thread_| Signals |event_| | 318 // Calls GetLatestProxyConfig, running on |io_thread_| Signals |event_| |
316 // on completion. | 319 // on completion. |
317 void GetLatestConfigOnIOThread() { | 320 void GetLatestConfigOnIOThread() { |
318 get_latest_config_result_ = | 321 get_latest_config_result_ = |
319 config_service_->GetLatestProxyConfig(&proxy_config_); | 322 config_service_->GetLatestProxyConfig(&proxy_config_); |
320 event_.Signal(); | 323 event_.Signal(); |
321 } | 324 } |
322 | 325 |
| 326 void SetupAndInitialFetchOnIOThread() { |
| 327 MessageLoop* file_loop = io_thread_.message_loop(); |
| 328 DCHECK_EQ(MessageLoop::TYPE_IO, file_loop->type()); |
| 329 DCHECK(io_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
| 330 // We pass the mock IO thread as both the IO and file threads. |
| 331 config_service_->SetupAndFetchInitialConfig( |
| 332 glib_task_runner_, io_thread_.message_loop_proxy(), |
| 333 static_cast<MessageLoopForIO*>(file_loop)); |
| 334 event_.Signal(); |
| 335 } |
| 336 |
323 // [Runs on |io_thread_|] Signals |event_| on cleanup completion. | 337 // [Runs on |io_thread_|] Signals |event_| on cleanup completion. |
324 void CleanUp() { | 338 void CleanUp() { |
325 MessageLoop::current()->RunAllPending(); | 339 MessageLoop::current()->RunAllPending(); |
326 event_.Signal(); | 340 event_.Signal(); |
327 } | 341 } |
328 | 342 |
329 void Wait() { | 343 void Wait() { |
330 event_.Wait(); | 344 event_.Wait(); |
331 event_.Reset(); | 345 event_.Reset(); |
332 } | 346 } |
333 | 347 |
334 base::WaitableEvent event_; | 348 base::WaitableEvent event_; |
335 base::Thread io_thread_; | 349 base::Thread io_thread_; |
| 350 base::SingleThreadTaskRunner *glib_task_runner_; |
336 | 351 |
337 net::ProxyConfigServiceLinux* config_service_; | 352 net::ProxyConfigServiceLinux* config_service_; |
338 | 353 |
339 // The config obtained by |io_thread_| and read back by the main | 354 // The config obtained by |io_thread_| and read back by the main |
340 // thread. | 355 // thread. |
341 net::ProxyConfig proxy_config_; | 356 net::ProxyConfig proxy_config_; |
342 | 357 |
343 // Return value from GetLatestProxyConfig(). | 358 // Return value from GetLatestProxyConfig(). |
344 net::ProxyConfigService::ConfigAvailability get_latest_config_result_; | 359 net::ProxyConfigService::ConfigAvailability get_latest_config_result_; |
345 }; | 360 }; |
(...skipping 1261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1607 ProxyConfig config; | 1622 ProxyConfig config; |
1608 sync_config_getter.SetupAndInitialFetch(); | 1623 sync_config_getter.SetupAndInitialFetch(); |
1609 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, | 1624 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, |
1610 sync_config_getter.SyncGetLatestProxyConfig(&config)); | 1625 sync_config_getter.SyncGetLatestProxyConfig(&config)); |
1611 EXPECT_TRUE(config.auto_detect()); | 1626 EXPECT_TRUE(config.auto_detect()); |
1612 EXPECT_EQ(GURL(), config.pac_url()); | 1627 EXPECT_EQ(GURL(), config.pac_url()); |
1613 } | 1628 } |
1614 } | 1629 } |
1615 | 1630 |
1616 } // namespace net | 1631 } // namespace net |
OLD | NEW |