| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
| 11 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 14 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 17 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 18 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/task.h" | 20 #include "base/task.h" |
| 20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 explicit SynchConfigGetter(net::ProxyConfigServiceLinux* config_service) | 263 explicit SynchConfigGetter(net::ProxyConfigServiceLinux* config_service) |
| 263 : event_(false, false), | 264 : event_(false, false), |
| 264 io_thread_("IO_Thread"), | 265 io_thread_("IO_Thread"), |
| 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, NewRunnableMethod( | 273 io_thread_.message_loop()->PostTask(FROM_HERE, |
| 273 this, &SynchConfigGetter::Init)); | 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, NewRunnableMethod( | 283 io_thread_.message_loop()->PostTask(FROM_HERE, |
| 283 this, &SynchConfigGetter::Cleanup)); | 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 // all on the calling thread (meant to be the thread with the |
| 289 // default glib main loop, which is the UI thread). | 290 // default glib main loop, which is the UI thread). |
| 290 void SetupAndInitialFetch() { | 291 void SetupAndInitialFetch() { |
| 291 MessageLoop* file_loop = io_thread_.message_loop(); | 292 MessageLoop* file_loop = io_thread_.message_loop(); |
| 292 DCHECK_EQ(MessageLoop::TYPE_IO, file_loop->type()); | 293 DCHECK_EQ(MessageLoop::TYPE_IO, file_loop->type()); |
| 293 // We pass the mock IO thread as both the IO and file threads. | 294 // We pass the mock IO thread as both the IO and file threads. |
| 294 config_service_->SetupAndFetchInitialConfig( | 295 config_service_->SetupAndFetchInitialConfig( |
| 295 MessageLoop::current(), io_thread_.message_loop(), | 296 MessageLoop::current(), io_thread_.message_loop(), |
| 296 static_cast<MessageLoopForIO*>(file_loop)); | 297 static_cast<MessageLoopForIO*>(file_loop)); |
| 297 } | 298 } |
| 298 // Synchronously gets the proxy config. | 299 // Synchronously gets the proxy config. |
| 299 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( | 300 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( |
| 300 net::ProxyConfig* config) { | 301 net::ProxyConfig* config) { |
| 301 io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 302 io_thread_.message_loop()->PostTask(FROM_HERE, |
| 302 this, &SynchConfigGetter::GetLatestConfigOnIOThread)); | 303 base::Bind(&SynchConfigGetter::GetLatestConfigOnIOThread, |
| 304 base::Unretained(this))); |
| 303 Wait(); | 305 Wait(); |
| 304 *config = proxy_config_; | 306 *config = proxy_config_; |
| 305 return get_latest_config_result_; | 307 return get_latest_config_result_; |
| 306 } | 308 } |
| 307 | 309 |
| 308 private: | 310 private: |
| 309 // [Runs on |io_thread_|] | 311 // [Runs on |io_thread_|] |
| 310 void Init() { | 312 void Init() { |
| 311 event_.Signal(); | 313 event_.Signal(); |
| 312 } | 314 } |
| 313 | 315 |
| 314 // Calls GetLatestProxyConfig, running on |io_thread_| Signals |event_| | 316 // Calls GetLatestProxyConfig, running on |io_thread_| Signals |event_| |
| 315 // on completion. | 317 // on completion. |
| 316 void GetLatestConfigOnIOThread() { | 318 void GetLatestConfigOnIOThread() { |
| 317 get_latest_config_result_ = | 319 get_latest_config_result_ = |
| 318 config_service_->GetLatestProxyConfig(&proxy_config_); | 320 config_service_->GetLatestProxyConfig(&proxy_config_); |
| 319 event_.Signal(); | 321 event_.Signal(); |
| 320 } | 322 } |
| 321 | 323 |
| 322 // [Runs on |io_thread_|] Signals |event_| on cleanup completion. | 324 // [Runs on |io_thread_|] Signals |event_| on cleanup completion. |
| 323 void Cleanup() { | 325 void CleanUp() { |
| 324 MessageLoop::current()->RunAllPending(); | 326 MessageLoop::current()->RunAllPending(); |
| 325 event_.Signal(); | 327 event_.Signal(); |
| 326 } | 328 } |
| 327 | 329 |
| 328 void Wait() { | 330 void Wait() { |
| 329 event_.Wait(); | 331 event_.Wait(); |
| 330 event_.Reset(); | 332 event_.Reset(); |
| 331 } | 333 } |
| 332 | 334 |
| 333 base::WaitableEvent event_; | 335 base::WaitableEvent event_; |
| (...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1542 ProxyConfig config; | 1544 ProxyConfig config; |
| 1543 sync_config_getter.SetupAndInitialFetch(); | 1545 sync_config_getter.SetupAndInitialFetch(); |
| 1544 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, | 1546 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, |
| 1545 sync_config_getter.SyncGetLatestProxyConfig(&config)); | 1547 sync_config_getter.SyncGetLatestProxyConfig(&config)); |
| 1546 EXPECT_TRUE(config.auto_detect()); | 1548 EXPECT_TRUE(config.auto_detect()); |
| 1547 EXPECT_EQ(GURL(), config.pac_url()); | 1549 EXPECT_EQ(GURL(), config.pac_url()); |
| 1548 } | 1550 } |
| 1549 } | 1551 } |
| 1550 | 1552 |
| 1551 } // namespace net | 1553 } // namespace net |
| OLD | NEW |