Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: chrome/browser/chromeos/proxy_config_service_impl.cc

Issue 6597070: Allow ProxyConfigService to report "no configuration set" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cosmetics. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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 "chrome/browser/chromeos/proxy_config_service_impl.h" 5 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
6 6
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/task.h" 11 #include "base/task.h"
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 DictionaryValue* proxy_dict; 384 DictionaryValue* proxy_dict;
385 if (!dict->GetDictionary(key_name, &proxy_dict)) 385 if (!dict->GetDictionary(key_name, &proxy_dict))
386 return ok_if_absent; 386 return ok_if_absent;
387 return manual_proxy->Decode(proxy_dict, scheme); 387 return manual_proxy->Decode(proxy_dict, scheme);
388 } 388 }
389 389
390 //------------------- ProxyConfigServiceImpl: public methods ------------------- 390 //------------------- ProxyConfigServiceImpl: public methods -------------------
391 391
392 ProxyConfigServiceImpl::ProxyConfigServiceImpl() 392 ProxyConfigServiceImpl::ProxyConfigServiceImpl()
393 : can_post_task_(false), 393 : can_post_task_(false),
394 has_config_(false), 394 config_availability_(net::ProxyConfigService::CONFIG_PENDING),
395 persist_to_device_(true), 395 persist_to_device_(true),
396 persist_to_device_pending_(false) { 396 persist_to_device_pending_(false) {
397 // Start async fetch of proxy config from settings persisted on device. 397 // Start async fetch of proxy config from settings persisted on device.
398 // TODO(kuan): retrieve config from policy and owner and merge them 398 // TODO(kuan): retrieve config from policy and owner and merge them
399 bool use_default = true; 399 bool use_default = true;
400 if (CrosLibrary::Get()->EnsureLoaded()) { 400 if (CrosLibrary::Get()->EnsureLoaded()) {
401 retrieve_property_op_ = SignedSettings::CreateRetrievePropertyOp( 401 retrieve_property_op_ = SignedSettings::CreateRetrievePropertyOp(
402 kSettingProxyEverywhere, this); 402 kSettingProxyEverywhere, this);
403 if (retrieve_property_op_) { 403 if (retrieve_property_op_) {
404 retrieve_property_op_->Execute(); 404 retrieve_property_op_->Execute();
405 VLOG(1) << "Start retrieving proxy setting from device"; 405 VLOG(1) << "Start retrieving proxy setting from device";
406 use_default = false; 406 use_default = false;
407 } else { 407 } else {
408 VLOG(1) << "Fail to retrieve proxy setting from device"; 408 VLOG(1) << "Fail to retrieve proxy setting from device";
409 } 409 }
410 } 410 }
411 if (use_default) 411 if (use_default)
412 InitConfigToDefault(false); 412 config_availability_ = net::ProxyConfigService::CONFIG_UNSET;
413 can_post_task_ = true; 413 can_post_task_ = true;
414 } 414 }
415 415
416 ProxyConfigServiceImpl::ProxyConfigServiceImpl(const ProxyConfig& init_config) 416 ProxyConfigServiceImpl::ProxyConfigServiceImpl(const ProxyConfig& init_config)
417 : can_post_task_(true), 417 : can_post_task_(true),
418 has_config_(true),
419 persist_to_device_(false), 418 persist_to_device_(false),
420 persist_to_device_pending_(false) { 419 persist_to_device_pending_(false) {
421 reference_config_ = init_config; 420 reference_config_ = init_config;
422 // Update the IO-accessible copy in |cached_config_| as well. 421 // Update the IO-accessible copy in |cached_config_| as well.
423 cached_config_ = reference_config_; 422 cached_config_ = reference_config_;
423 config_availability_ = net::ProxyConfigService::CONFIG_VALID;
kuan 2011/03/10 22:59:05 why not initialize this in initializer list?
Mattias Nissler (ping if slow) 2011/03/15 12:38:33 Done.
424 } 424 }
425 425
426 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { 426 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
427 } 427 }
428 428
429 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) { 429 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) {
430 // Should be called from UI thread. 430 // Should be called from UI thread.
431 CheckCurrentlyOnUIThread(); 431 CheckCurrentlyOnUIThread();
432 // Simply returns the copy on the UI thread. 432 // Simply returns the copy on the UI thread.
433 *config = reference_config_; 433 *config = reference_config_;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 observers_.AddObserver(observer); 507 observers_.AddObserver(observer);
508 } 508 }
509 509
510 void ProxyConfigServiceImpl::RemoveObserver( 510 void ProxyConfigServiceImpl::RemoveObserver(
511 net::ProxyConfigService::Observer* observer) { 511 net::ProxyConfigService::Observer* observer) {
512 // Should be called from IO thread. 512 // Should be called from IO thread.
513 CheckCurrentlyOnIOThread(); 513 CheckCurrentlyOnIOThread();
514 observers_.RemoveObserver(observer); 514 observers_.RemoveObserver(observer);
515 } 515 }
516 516
517 bool ProxyConfigServiceImpl::IOGetProxyConfig(net::ProxyConfig* net_config) { 517 net::ProxyConfigService::ConfigAvailability
518 ProxyConfigServiceImpl::IOGetProxyConfig(net::ProxyConfig* net_config) {
518 // Should be called from IO thread. 519 // Should be called from IO thread.
519 CheckCurrentlyOnIOThread(); 520 CheckCurrentlyOnIOThread();
520 if (has_config_) { 521 if (config_availability_ == net::ProxyConfigService::CONFIG_VALID)
521 // Simply return the last cached proxy configuration.
522 cached_config_.ToNetProxyConfig(net_config); 522 cached_config_.ToNetProxyConfig(net_config);
523 return true; 523
524 } 524 return config_availability_;
525 return false;
526 } 525 }
527 526
528 void ProxyConfigServiceImpl::OnSettingsOpCompleted( 527 void ProxyConfigServiceImpl::OnSettingsOpCompleted(
529 SignedSettings::ReturnCode code, 528 SignedSettings::ReturnCode code,
530 bool value) { 529 bool value) {
531 if (SignedSettings::SUCCESS == code) 530 if (SignedSettings::SUCCESS == code)
532 VLOG(1) << "Stored proxy setting to device"; 531 VLOG(1) << "Stored proxy setting to device";
533 else 532 else
534 LOG(WARNING) << "Error storing proxy setting to device"; 533 LOG(WARNING) << "Error storing proxy setting to device";
535 store_property_op_ = NULL; 534 store_property_op_ = NULL;
536 if (persist_to_device_pending_) 535 if (persist_to_device_pending_)
537 PersistConfigToDevice(); 536 PersistConfigToDevice();
538 } 537 }
539 538
540 void ProxyConfigServiceImpl::OnSettingsOpCompleted( 539 void ProxyConfigServiceImpl::OnSettingsOpCompleted(
541 SignedSettings::ReturnCode code, 540 SignedSettings::ReturnCode code,
542 std::string value) { 541 std::string value) {
542 retrieve_property_op_ = NULL;
543 if (SignedSettings::SUCCESS == code) { 543 if (SignedSettings::SUCCESS == code) {
544 VLOG(1) << "Retrieved proxy setting from device, value=[" << value << "]"; 544 VLOG(1) << "Retrieved proxy setting from device, value=[" << value << "]";
545 if (reference_config_.Deserialize(value)) { 545 if (reference_config_.Deserialize(value)) {
546 OnUISetProxyConfig(false); 546 IOSetProxyConfig(reference_config_,
547 net::ProxyConfigService::CONFIG_VALID);
548 return;
547 } else { 549 } else {
548 LOG(WARNING) << "Error deserializing device's proxy setting"; 550 LOG(WARNING) << "Error deserializing device's proxy setting";
549 InitConfigToDefault(true);
550 } 551 }
551 } else { 552 } else {
552 LOG(WARNING) << "Error retrieving proxy setting from device"; 553 LOG(WARNING) << "Error retrieving proxy setting from device";
553 InitConfigToDefault(true);
554 } 554 }
555 retrieve_property_op_ = NULL; 555
556 // Update the configuration state on the IO thread.
557 IOSetProxyConfig(reference_config_, net::ProxyConfigService::CONFIG_UNSET);
556 } 558 }
557 559
558 //------------------ ProxyConfigServiceImpl: private methods ------------------- 560 //------------------ ProxyConfigServiceImpl: private methods -------------------
559 561
560 void ProxyConfigServiceImpl::InitConfigToDefault(bool post_to_io_thread) {
561 VLOG(1) << "Using default proxy config: auto-detect";
562 reference_config_.mode = ProxyConfig::MODE_AUTO_DETECT;
563 reference_config_.automatic_proxy.source = ProxyConfig::SOURCE_OWNER;
564 if (post_to_io_thread && can_post_task_) {
565 OnUISetProxyConfig(false);
566 } else {
567 // Update the IO-accessible copy in |cached_config_| as well.
568 cached_config_ = reference_config_;
569 has_config_ = true;
570 }
571 }
572
573 void ProxyConfigServiceImpl::PersistConfigToDevice() { 562 void ProxyConfigServiceImpl::PersistConfigToDevice() {
574 DCHECK(!store_property_op_); 563 DCHECK(!store_property_op_);
575 persist_to_device_pending_ = false; 564 persist_to_device_pending_ = false;
576 std::string value; 565 std::string value;
577 if (!reference_config_.Serialize(&value)) { 566 if (!reference_config_.Serialize(&value)) {
578 LOG(WARNING) << "Error serializing proxy config"; 567 LOG(WARNING) << "Error serializing proxy config";
579 return; 568 return;
580 } 569 }
581 store_property_op_ = SignedSettings::CreateStorePropertyOp( 570 store_property_op_ = SignedSettings::CreateStorePropertyOp(
582 kSettingProxyEverywhere, value, this); 571 kSettingProxyEverywhere, value, this);
583 store_property_op_->Execute(); 572 store_property_op_->Execute();
584 VLOG(1) << "Start storing proxy setting to device, value=" << value; 573 VLOG(1) << "Start storing proxy setting to device, value=" << value;
585 } 574 }
586 575
587 void ProxyConfigServiceImpl::OnUISetProxyConfig(bool persist_to_device) { 576 void ProxyConfigServiceImpl::OnUISetProxyConfig(bool persist_to_device) {
588 // Posts a task to IO thread with the new config, so it can update 577 IOSetProxyConfig(reference_config_, net::ProxyConfigService::CONFIG_VALID);
589 // |cached_config_|.
590 Task* task = NewRunnableMethod(this,
591 &ProxyConfigServiceImpl::IOSetProxyConfig, reference_config_);
592 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
593 VLOG(1) << "Couldn't post task to IO thread to set new proxy config";
594 delete task;
595 }
596
597 if (persist_to_device && CrosLibrary::Get()->EnsureLoaded()) { 578 if (persist_to_device && CrosLibrary::Get()->EnsureLoaded()) {
598 if (store_property_op_) { 579 if (store_property_op_) {
599 persist_to_device_pending_ = true; 580 persist_to_device_pending_ = true;
600 VLOG(1) << "Pending persisting proxy setting to device"; 581 VLOG(1) << "Pending persisting proxy setting to device";
601 } else { 582 } else {
602 PersistConfigToDevice(); 583 PersistConfigToDevice();
603 } 584 }
604 } 585 }
605 } 586 }
606 587
588 void ProxyConfigServiceImpl::IOSetProxyConfig(
589 const ProxyConfig& new_config,
590 net::ProxyConfigService::ConfigAvailability new_availability) {
591 if (!BrowserThread::CurrentlyOn(BrowserThread::IO) && can_post_task_) {
592 // Posts a task to IO thread with the new config, so it can update
593 // |cached_config_|.
594 Task* task = NewRunnableMethod(this,
595 &ProxyConfigServiceImpl::IOSetProxyConfig,
596 new_config,
597 new_availability);
598 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
599 VLOG(1) << "Couldn't post task to IO thread to set new proxy config";
600 delete task;
601 }
602 return;
603 }
604
605 // Now guaranteed to be on the correct thread.
606 VLOG(1) << "Proxy configuration changed";
607 cached_config_ = new_config;
608 config_availability_ = new_availability;
609 // Notify observers of new proxy config.
610 net::ProxyConfig net_config;
611 cached_config_.ToNetProxyConfig(&net_config);
612 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
613 OnProxyConfigChanged(net_config, config_availability_));
614 }
615
607 void ProxyConfigServiceImpl::CheckCurrentlyOnIOThread() { 616 void ProxyConfigServiceImpl::CheckCurrentlyOnIOThread() {
608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 617 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
609 } 618 }
610 619
611 void ProxyConfigServiceImpl::CheckCurrentlyOnUIThread() { 620 void ProxyConfigServiceImpl::CheckCurrentlyOnUIThread() {
612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 621 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
613 } 622 }
614 623
615 void ProxyConfigServiceImpl::IOSetProxyConfig(const ProxyConfig& new_config) {
616 // This is called on the IO thread (posted from UI thread).
617 CheckCurrentlyOnIOThread();
618 VLOG(1) << "Proxy configuration changed";
619 has_config_ = true;
620 cached_config_ = new_config;
621 // Notify observers of new proxy config.
622 net::ProxyConfig net_config;
623 cached_config_.ToNetProxyConfig(&net_config);
624 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
625 OnProxyConfigChanged(net_config));
626 }
627
628 } // namespace chromeos 624 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/proxy_config_service_impl.h ('k') | chrome/browser/chromeos/proxy_config_service_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698