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

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

Issue 14846004: Migrate ProxyConfigServiceImpl to NetworkStateHandler and NetworkProfileHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Mattias' comments. Created 7 years, 7 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) 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 "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/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_string_value_serializer.h" 10 #include "base/json/json_writer.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/prefs/pref_registry_simple.h" 12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/cros/cros_library.h" 17 #include "chrome/browser/chromeos/cros/cros_library.h"
18 #include "chrome/browser/chromeos/cros/network_library.h"
17 #include "chrome/browser/chromeos/cros/network_property_ui_data.h" 19 #include "chrome/browser/chromeos/cros/network_property_ui_data.h"
18 #include "chrome/browser/chromeos/login/user_manager.h" 20 #include "chrome/browser/chromeos/login/user_manager.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h" 21 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 22 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
21 #include "chrome/browser/policy/browser_policy_connector.h" 23 #include "chrome/browser/policy/browser_policy_connector.h"
22 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" 24 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
23 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h" 25 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h"
24 #include "chrome/browser/prefs/proxy_config_dictionary.h" 26 #include "chrome/browser/prefs/proxy_config_dictionary.h"
25 #include "chrome/browser/prefs/proxy_prefs.h" 27 #include "chrome/browser/prefs/proxy_prefs.h"
26 #include "chrome/browser/profiles/profile_manager.h" 28 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/common/chrome_notification_types.h" 29 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
31 #include "chromeos/network/network_profile.h"
32 #include "chromeos/network/network_profile_handler.h"
33 #include "chromeos/network/network_state.h"
34 #include "chromeos/network/network_state_handler.h"
29 #include "chromeos/network/network_ui_data.h" 35 #include "chromeos/network/network_ui_data.h"
30 #include "chromeos/network/onc/onc_constants.h" 36 #include "chromeos/network/onc/onc_constants.h"
31 #include "components/user_prefs/pref_registry_syncable.h" 37 #include "components/user_prefs/pref_registry_syncable.h"
32 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
33 #include "grit/generated_resources.h" 39 #include "grit/generated_resources.h"
34 #include "ui/base/l10n/l10n_util.h" 40 #include "ui/base/l10n/l10n_util.h"
35 41
36 namespace em = enterprise_management; 42 namespace em = enterprise_management;
37 43
38 namespace chromeos { 44 namespace chromeos {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 return "config_network"; // For ChromeOS, system is network. 96 return "config_network"; // For ChromeOS, system is network.
91 case ProxyPrefs::CONFIG_FALLBACK: 97 case ProxyPrefs::CONFIG_FALLBACK:
92 return "config_recommended"; // Fallback is recommended. 98 return "config_recommended"; // Fallback is recommended.
93 case ProxyPrefs::CONFIG_UNSET: 99 case ProxyPrefs::CONFIG_UNSET:
94 return "config_unset"; 100 return "config_unset";
95 } 101 }
96 NOTREACHED() << "Unrecognized config state type"; 102 NOTREACHED() << "Unrecognized config state type";
97 return ""; 103 return "";
98 } 104 }
99 105
100 // Returns true if proxy settings from |network| is editable. 106 // Returns true if proxy settings of |network_state| are editable.
101 bool IsNetworkProxySettingsEditable(const Network* network) { 107 bool IsNetworkProxySettingsEditable(const NetworkState& network_state) {
102 if (!network) 108 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
103 return true; // editable if no network given. 109 Network* network = network_library->FindNetworkByPath(network_state.path());
104 110
105 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); 111 if (!network) {
112 LOG(ERROR) << "Could not find network " << network_state.path()
113 << " in NetworkLibrary. Falling back to user-editable proxy"
114 << " settings";
115 return true;
116 }
stevenjb 2013/05/08 20:32:09 Mixing NetworkLibrary and NetworkStateHandler is p
pneubeck (no reviews) 2013/05/10 10:58:52 I removed the dependency of NetworkPropertyUIData
117
106 const base::DictionaryValue* onc = 118 const base::DictionaryValue* onc =
107 network_library->FindOncForNetwork(network->unique_id()); 119 network_library->FindOncForNetwork(network->unique_id());
108 120
109 NetworkPropertyUIData proxy_settings_ui_data; 121 NetworkPropertyUIData proxy_settings_ui_data;
110 proxy_settings_ui_data.ParseOncProperty( 122 proxy_settings_ui_data.ParseOncProperty(
111 network->ui_data(), 123 network->ui_data(),
112 onc, 124 onc,
113 onc::network_config::kProxySettings); 125 onc::network_config::kProxySettings);
114 return proxy_settings_ui_data.editable(); 126 return proxy_settings_ui_data.editable();
115 } 127 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 socks_proxy.server = rules.fallback_proxies.Get(); 244 socks_proxy.server = rules.fallback_proxies.Get();
233 bypass_rules = rules.bypass_rules; 245 bypass_rules = rules.bypass_rules;
234 return true; 246 return true;
235 default: 247 default:
236 NOTREACHED() << "Unrecognized proxy config mode"; 248 NOTREACHED() << "Unrecognized proxy config mode";
237 break; 249 break;
238 } 250 }
239 return false; 251 return false;
240 } 252 }
241 253
242 DictionaryValue* ProxyConfigServiceImpl::ProxyConfig::ToPrefProxyConfig() { 254 base::DictionaryValue*
255 ProxyConfigServiceImpl::ProxyConfig::ToPrefProxyConfig() {
243 switch (mode) { 256 switch (mode) {
244 case MODE_DIRECT: { 257 case MODE_DIRECT: {
245 return ProxyConfigDictionary::CreateDirect(); 258 return ProxyConfigDictionary::CreateDirect();
246 } 259 }
247 case MODE_AUTO_DETECT: { 260 case MODE_AUTO_DETECT: {
248 return ProxyConfigDictionary::CreateAutoDetect(); 261 return ProxyConfigDictionary::CreateAutoDetect();
249 } 262 }
250 case MODE_PAC_SCRIPT: { 263 case MODE_PAC_SCRIPT: {
251 return ProxyConfigDictionary::CreatePacScript( 264 return ProxyConfigDictionary::CreatePacScript(
252 automatic_proxy.pac_url.spec(), false); 265 automatic_proxy.pac_url.spec(), false);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 return true; 359 return true;
347 } 360 }
348 361
349 bool ProxyConfigServiceImpl::ProxyConfig::SerializeForNetwork( 362 bool ProxyConfigServiceImpl::ProxyConfig::SerializeForNetwork(
350 std::string* output) { 363 std::string* output) {
351 scoped_ptr<DictionaryValue> proxy_dict_ptr(ToPrefProxyConfig()); 364 scoped_ptr<DictionaryValue> proxy_dict_ptr(ToPrefProxyConfig());
352 if (!proxy_dict_ptr.get()) 365 if (!proxy_dict_ptr.get())
353 return false; 366 return false;
354 367
355 // Return empty string for direct mode for portal check to work correctly. 368 // Return empty string for direct mode for portal check to work correctly.
356 DictionaryValue *dict = proxy_dict_ptr.get(); 369 base::DictionaryValue *dict = proxy_dict_ptr.get();
357 ProxyConfigDictionary proxy_dict(dict); 370 ProxyConfigDictionary proxy_dict(dict);
358 ProxyPrefs::ProxyMode mode; 371 ProxyPrefs::ProxyMode mode;
359 if (proxy_dict.GetMode(&mode)) { 372 if (proxy_dict.GetMode(&mode)) {
360 if (mode == ProxyPrefs::MODE_DIRECT) { 373 if (mode == ProxyPrefs::MODE_DIRECT) {
361 output->clear(); 374 output->clear();
362 return true; 375 return true;
363 } 376 }
364 } 377 }
365 JSONStringValueSerializer serializer(output); 378 base::JSONWriter::Write(dict, output);
366 return serializer.Serialize(*dict); 379 return true;
367 } 380 }
368 381
369 //----------- ProxyConfigServiceImpl::ProxyConfig: private methods ------------- 382 //----------- ProxyConfigServiceImpl::ProxyConfig: private methods -------------
370 383
371 // static 384 // static
372 void ProxyConfigServiceImpl::ProxyConfig::EncodeAndAppendProxyServer( 385 void ProxyConfigServiceImpl::ProxyConfig::EncodeAndAppendProxyServer(
373 const std::string& url_scheme, 386 const std::string& url_scheme,
374 const net::ProxyServer& server, 387 const net::ProxyServer& server,
375 std::string* spec) { 388 std::string* spec) {
376 if (!server.is_valid()) 389 if (!server.is_valid())
(...skipping 19 matching lines...) Expand all
396 // Register for notifications of UseSharedProxies user preference. 409 // Register for notifications of UseSharedProxies user preference.
397 if (pref_service->FindPreference(prefs::kUseSharedProxies)) { 410 if (pref_service->FindPreference(prefs::kUseSharedProxies)) {
398 use_shared_proxies_.Init( 411 use_shared_proxies_.Init(
399 prefs::kUseSharedProxies, pref_service, 412 prefs::kUseSharedProxies, pref_service,
400 base::Bind(&ProxyConfigServiceImpl::OnUseSharedProxiesChanged, 413 base::Bind(&ProxyConfigServiceImpl::OnUseSharedProxiesChanged,
401 base::Unretained(this))); 414 base::Unretained(this)));
402 } 415 }
403 416
404 FetchProxyPolicy(); 417 FetchProxyPolicy();
405 418
406 // Register for shill network notifications. 419 // Register for changes to the default network.
407 NetworkLibrary* network_lib = CrosLibrary::Get()->GetNetworkLibrary(); 420 NetworkStateHandler* state_handler = NetworkStateHandler::Get();
408 OnActiveNetworkChanged(network_lib, network_lib->active_network()); 421 state_handler->AddObserver(this);
409 network_lib->AddNetworkManagerObserver(this); 422 DefaultNetworkChanged(state_handler->DefaultNetwork());
410 } 423 }
411 424
412 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { 425 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
413 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 426 NetworkStateHandler::Get()->RemoveObserver(this);
414 if (netlib) {
415 netlib->RemoveNetworkManagerObserver(this);
416 netlib->RemoveObserverForAllNetworks(this);
417 }
418 } 427 }
419 428
420 void ProxyConfigServiceImpl::UISetCurrentNetwork( 429 void ProxyConfigServiceImpl::UISetCurrentNetwork(
421 const std::string& current_network) { 430 const std::string& current_network) {
422 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 431 const NetworkState* network =
423 current_network); 432 NetworkStateHandler::Get()->GetNetworkState(current_network);
424 if (!network) { 433 if (!network) {
425 ResetUICache(); 434 ResetUICache();
426 LOG(ERROR) << "Can't find requested network " << current_network; 435 LOG(ERROR) << "Can't find requested network " << current_network;
427 return; 436 return;
428 } 437 }
429 current_ui_network_ = current_network; 438 current_ui_network_ = current_network;
430 OnUISetCurrentNetwork(network); 439
440 DetermineEffectiveConfig(network, false);
441 VLOG(1) << "Current ui network: "
442 << network->name()
443 << ", " << ModeToString(current_ui_config_.mode)
444 << ", " << ConfigStateToString(current_ui_config_.state)
445 << ", modifiable:" << current_ui_config_.user_modifiable;
446 // Notify observers.
447 std::vector<base::Closure>::iterator iter = callbacks_.begin();
448 while (iter != callbacks_.end()) {
449 if (iter->is_null()) {
450 iter = callbacks_.erase(iter);
451 } else {
452 iter->Run();
453 ++iter;
454 }
455 }
431 } 456 }
432 457
433 void ProxyConfigServiceImpl::UIMakeActiveNetworkCurrent() { 458 void ProxyConfigServiceImpl::UIMakeDefaultNetworkCurrent() {
434 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 459 const NetworkState* default_network =
435 active_network_); 460 NetworkStateHandler::Get()->DefaultNetwork();
436 if (!network) { 461 UISetCurrentNetwork(
437 ResetUICache(); 462 default_network ? default_network->path() : std::string());
438 LOG(ERROR) << "Can't find requested network " << active_network_;
439 return;
440 }
441 current_ui_network_ = active_network_;
442 OnUISetCurrentNetwork(network);
443 } 463 }
444 464
445 void ProxyConfigServiceImpl::UIGetCurrentNetworkName( 465 void ProxyConfigServiceImpl::UIGetCurrentNetworkName(
446 std::string* network_name) { 466 std::string* network_name) {
447 if (!network_name) 467 if (!network_name)
448 return; 468 return;
449 network_name->clear(); 469 network_name->clear();
450 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 470 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
451 current_ui_network_); 471 current_ui_network_);
stevenjb 2013/05/08 20:32:09 I don't see anything here that can't use NetworkSt
pneubeck (no reviews) 2013/05/10 10:58:52 I tried to not change the UI part too much.
452 if (!network) { 472 if (!network) {
453 LOG(ERROR) << "Can't find requested network " << current_ui_network_; 473 LOG(ERROR) << "Can't find requested network " << current_ui_network_;
454 return; 474 return;
455 } 475 }
456 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) { 476 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) {
457 *network_name = 477 *network_name =
458 l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); 478 l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
459 } else { 479 } else {
460 *network_name = network->name(); 480 *network_name = network->name();
461 } 481 }
462 } 482 }
463 483
464 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) { 484 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) {
465 // Simply returns the copy last set from UI via UISetCurrentNetwork or 485 // Simply returns the copy last set from UI via UISetCurrentNetwork or
466 // UIMakeActiveNetworkCurrent. 486 // UIMakeDefaultNetworkCurrent.
467 *config = current_ui_config_; 487 *config = current_ui_config_;
468 } 488 }
469 489
470 bool ProxyConfigServiceImpl::UISetProxyConfigToDirect() { 490 bool ProxyConfigServiceImpl::UISetProxyConfigToDirect() {
471 current_ui_config_.mode = ProxyConfig::MODE_DIRECT; 491 current_ui_config_.mode = ProxyConfig::MODE_DIRECT;
472 OnUISetProxyConfig(); 492 OnUISetProxyConfig();
473 return true; 493 return true;
474 } 494 }
475 495
476 bool ProxyConfigServiceImpl::UISetProxyConfigToAutoDetect() { 496 bool ProxyConfigServiceImpl::UISetProxyConfigToAutoDetect() {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 findIfEqual(callbacks_.begin(), callbacks_.end(), callback); 555 findIfEqual(callbacks_.begin(), callbacks_.end(), callback);
536 if (iter != callbacks_.end()) 556 if (iter != callbacks_.end())
537 callbacks_.erase(iter); 557 callbacks_.erase(iter);
538 } 558 }
539 559
540 void ProxyConfigServiceImpl::OnProxyConfigChanged( 560 void ProxyConfigServiceImpl::OnProxyConfigChanged(
541 ProxyPrefs::ConfigState config_state, 561 ProxyPrefs::ConfigState config_state,
542 const net::ProxyConfig& config) { 562 const net::ProxyConfig& config) {
543 VLOG(1) << "Got prefs change: " << ConfigStateToString(config_state) 563 VLOG(1) << "Got prefs change: " << ConfigStateToString(config_state)
544 << ", mode=" << config.proxy_rules().type; 564 << ", mode=" << config.proxy_rules().type;
545 Network* network = NULL; 565 UpdateProxyConfigOfDefaultNetwork();
546 if (!active_network_.empty()) {
547 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
548 active_network_);
549 if (!network)
550 LOG(ERROR) << "can't find requested network " << active_network_;
551 }
552 DetermineEffectiveConfig(network, true);
553 }
554
555 void ProxyConfigServiceImpl::OnNetworkManagerChanged(
556 NetworkLibrary* network_lib) {
557 VLOG(1) << "OnNetworkManagerChanged: use-shared-proxies="
558 << GetUseSharedProxies();
559 OnActiveNetworkChanged(network_lib, network_lib->active_network());
560 }
561
562 void ProxyConfigServiceImpl::OnNetworkChanged(NetworkLibrary* network_lib,
563 const Network* network) {
564 if (!network)
565 return;
566 VLOG(1) << "OnNetworkChanged: "
567 << (network->name().empty() ? network->service_path() :
568 network->name())
569 << ", use-shared-proxies=" << GetUseSharedProxies();
570 // We only care about active network.
571 if (network == network_lib->active_network())
572 OnActiveNetworkChanged(network_lib, network);
573 } 566 }
574 567
575 // static 568 // static
576 bool ProxyConfigServiceImpl::ParseProxyConfig(const Network* network, 569 bool ProxyConfigServiceImpl::ParseProxyConfig(const NetworkState& network,
577 net::ProxyConfig* proxy_config) { 570 net::ProxyConfig* proxy_config) {
578 if (!network || !proxy_config) 571 if (!proxy_config || network.proxy_config().empty())
579 return false; 572 return false;
580 JSONStringValueSerializer serializer(network->proxy_config()); 573
581 scoped_ptr<Value> value(serializer.Deserialize(NULL, NULL)); 574 ProxyConfigDictionary proxy_dict(&network.proxy_config());
582 if (!value.get() || value->GetType() != Value::TYPE_DICTIONARY)
583 return false;
584 ProxyConfigDictionary proxy_dict(static_cast<DictionaryValue*>(value.get()));
585 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(proxy_dict, 575 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(proxy_dict,
586 proxy_config); 576 proxy_config);
587 } 577 }
588 578
589 // static 579 // static
590 void ProxyConfigServiceImpl::RegisterPrefs(PrefRegistrySimple* registry) { 580 void ProxyConfigServiceImpl::RegisterPrefs(PrefRegistrySimple* registry) {
591 // Use shared proxies default to off. GetUseSharedProxies will return the 581 // Use shared proxies default to off. GetUseSharedProxies will return the
592 // correct value based on pre-login and login. 582 // correct value based on pre-login and login.
593 registry->RegisterBooleanPref(prefs::kUseSharedProxies, true); 583 registry->RegisterBooleanPref(prefs::kUseSharedProxies, true);
594 } 584 }
595 585
596 // static 586 // static
597 void ProxyConfigServiceImpl::RegisterUserPrefs( 587 void ProxyConfigServiceImpl::RegisterUserPrefs(
598 user_prefs::PrefRegistrySyncable* registry) { 588 user_prefs::PrefRegistrySyncable* registry) {
599 registry->RegisterBooleanPref( 589 registry->RegisterBooleanPref(
600 prefs::kUseSharedProxies, 590 prefs::kUseSharedProxies,
601 true, 591 true,
602 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 592 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
603 } 593 }
604 594
605 //------------------ ProxyConfigServiceImpl: private methods ------------------- 595 //------------------ ProxyConfigServiceImpl: private methods -------------------
606 596
607 void ProxyConfigServiceImpl::OnUseSharedProxiesChanged() { 597 void ProxyConfigServiceImpl::OnUseSharedProxiesChanged() {
608 VLOG(1) << "New use-shared-proxies = " << GetUseSharedProxies(); 598 VLOG(1) << "New use-shared-proxies = " << GetUseSharedProxies();
609 599 UpdateProxyConfigOfDefaultNetwork();
610 // Determine new proxy config which may have changed because of new
611 // use-shared-proxies. If necessary, activate it.
612 Network* network = NULL;
613 if (!active_network_.empty()) {
614 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
615 active_network_);
616 if (!network)
617 LOG(WARNING) << "Can't find requested network " << active_network_;
618 }
619 DetermineEffectiveConfig(network, true);
620 } 600 }
621 601
622 void ProxyConfigServiceImpl::OnUISetProxyConfig() { 602 void ProxyConfigServiceImpl::OnUISetProxyConfig() {
623 if (current_ui_network_.empty()) 603 if (current_ui_network_.empty())
624 return; 604 return;
625 // Update config to shill. 605 // Update config to shill.
626 std::string value; 606 std::string value;
627 if (current_ui_config_.SerializeForNetwork(&value)) { 607 if (current_ui_config_.SerializeForNetwork(&value)) {
628 VLOG(1) << "Set proxy (mode=" << current_ui_config_.mode 608 VLOG(1) << "Set proxy (mode=" << current_ui_config_.mode
629 << ") for " << current_ui_network_; 609 << ") for " << current_ui_network_;
630 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; 610 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
631 SetProxyConfigForNetwork(current_ui_network_, value, false); 611 SetProxyConfigForNetwork(current_ui_network_, value, false);
632 } 612 }
633 } 613 }
634 614
635 void ProxyConfigServiceImpl::OnActiveNetworkChanged(NetworkLibrary* network_lib, 615 void ProxyConfigServiceImpl::DefaultNetworkChanged(
636 const Network* active_network) { 616 const NetworkState* new_network) {
637 std::string new_network; 617 std::string new_network_path;
638 if (active_network) 618 if (new_network)
639 new_network = active_network->service_path(); 619 new_network_path = new_network->path();
640 620
641 if (active_network_ == new_network) { // Same active network. 621 VLOG(1) << "DefaultNetworkChanged: network "
642 VLOG(1) << "Same active network: " 622 << default_network_ << " -> " << new_network_path << ".";
643 << (new_network.empty() ? "empty" : 623 VLOG_IF(1, new_network) << "New network: name=" << new_network->name()
644 (active_network->name().empty() ? 624 << ", proxy=" << new_network->proxy_config()
645 new_network : active_network->name())); 625 << ", profile=" << new_network->profile_path();
646 // Even though network is the same, its proxy config (e.g. if private
647 // version of network replaces the shared version after login), or
648 // use-shared-proxies setting (e.g. after login) may have changed,
649 // so re-determine effective proxy config, and activate if different.
650 if (active_network) {
651 VLOG(1) << "Profile=" << active_network->profile_type()
652 << "," << active_network->profile_path()
653 << ", proxy=" << active_network->proxy_config();
654 DetermineEffectiveConfig(active_network, true);
655 }
656 return;
657 }
658 626
659 // If there was a previous active network, remove it as observer. 627 default_network_ = new_network_path;
660 if (!active_network_.empty())
661 network_lib->RemoveNetworkObserver(active_network_, this);
662 628
663 active_network_ = new_network; 629 // Even if the default network is the same, its proxy config (e.g. if private
630 // version of network replaces the shared version after login), or
631 // use-shared-proxies setting (e.g. after login) may have changed, so
632 // re-determine effective proxy config, and activate if different.
664 633
665 if (active_network_.empty()) { 634 // If necessary, migrate device proxy settings to per network settings.
666 VLOG(1) << "New active network: empty"; 635 if (new_network && new_network->proxy_config().empty() &&
667 DetermineEffectiveConfig(active_network, true); 636 !device_config_.empty()) {
668 return; 637 VLOG(1) << "Try migrating device config to " << new_network_path;
669 } 638 SetProxyConfigForNetwork(new_network_path, device_config_, true);
670 639 // This will trigger a notification afterwards, so application
671 VLOG(1) << "New active network: path=" << active_network->service_path() 640 // of the migrated proxy config is not necessary here.
672 << ", name=" << active_network->name()
673 << ", profile=" << active_network->profile_type()
674 << "," << active_network->profile_path()
675 << ", proxy=" << active_network->proxy_config();
676
677 // Register observer for new network.
678 network_lib->AddNetworkObserver(active_network_, this);
679
680 // If necessary, migrate config to shill.
681 if (active_network->proxy_config().empty() && !device_config_.empty()) {
682 VLOG(1) << "Try migrating device config to " << active_network_;
683 SetProxyConfigForNetwork(active_network_, device_config_, true);
684 } else { 641 } else {
685 // Otherwise, determine and activate possibly new effective proxy config. 642 UpdateProxyConfigOfDefaultNetwork();
686 DetermineEffectiveConfig(active_network, true);
687 } 643 }
688 } 644 }
689 645
690 void ProxyConfigServiceImpl::SetProxyConfigForNetwork( 646 void ProxyConfigServiceImpl::SetProxyConfigForNetwork(
691 const std::string& network_path, const std::string& value, 647 const std::string& network_path, const std::string& value,
692 bool only_set_if_empty) { 648 bool only_set_if_empty) {
693 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 649 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
694 network_path); 650 network_path);
695 if (!network) { 651 if (!network) {
696 NOTREACHED() << "Can't find requested network " << network_path; 652 NOTREACHED() << "Can't find requested network " << network_path;
697 return; 653 return;
698 } 654 }
699 if (!only_set_if_empty || network->proxy_config().empty()) { 655 if (!only_set_if_empty || network->proxy_config().empty()) {
700 network->SetProxyConfig(value); 656 network->SetProxyConfig(value);
stevenjb 2013/05/08 20:32:09 We should switch to using NetworkConfigurationHand
pneubeck (no reviews) 2013/05/10 10:58:52 What means "at the same time"? The problem with us
701 VLOG(1) << "Set proxy for " 657 VLOG(1) << "Set proxy for "
702 << (network->name().empty() ? network_path : network->name()) 658 << (network->name().empty() ? network_path : network->name())
703 << ", value=" << value; 659 << ", value=" << value;
704 if (network_path == active_network_) 660 // Even in case the default network was changed, there's no need to apply
705 DetermineEffectiveConfig(network, true); 661 // the new proxy settings, because we will be notified afterwards.
706 } 662 }
707 } 663 }
708 664
709 bool ProxyConfigServiceImpl::GetUseSharedProxies() { 665 bool ProxyConfigServiceImpl::GetUseSharedProxies() {
710 const PrefService::Preference* use_shared_proxies_pref = 666 const PrefService::Preference* use_shared_proxies_pref =
711 prefs()->FindPreference(prefs::kUseSharedProxies); 667 prefs()->FindPreference(prefs::kUseSharedProxies);
712 if (!use_shared_proxies_pref) { 668 if (!use_shared_proxies_pref) {
713 // Make sure that proxies are always enabled at sign in screen. 669 // Make sure that proxies are always enabled at sign in screen.
714 return !UserManager::Get()->IsUserLoggedIn(); 670 return !UserManager::Get()->IsUserLoggedIn();
715 } 671 }
716 return use_shared_proxies_.GetValue(); 672 return use_shared_proxies_.GetValue();
717 } 673 }
718 674
719 bool ProxyConfigServiceImpl::IgnoreProxy(const Network* network) { 675 bool ProxyConfigServiceImpl::IgnoreProxy(const NetworkState& network) {
720 if (network->profile_type() == PROFILE_USER) 676 const NetworkProfile* profile =
677 NetworkProfileHandler::Get()->GetProfileForPath(network.profile_path());
678 if (!profile)
679 return true;
680 if (profile->type() == NetworkProfile::TYPE_USER)
721 return false; 681 return false;
722 682
723 if (network->ui_data().onc_source() == onc::ONC_SOURCE_DEVICE_POLICY && 683 if (network.onc_source() == onc::ONC_SOURCE_DEVICE_POLICY &&
724 UserManager::Get()->IsUserLoggedIn()) { 684 UserManager::Get()->IsUserLoggedIn()) {
725 policy::BrowserPolicyConnector* connector = 685 policy::BrowserPolicyConnector* connector =
726 g_browser_process->browser_policy_connector(); 686 g_browser_process->browser_policy_connector();
727 const User* logged_in_user = UserManager::Get()->GetLoggedInUser(); 687 const User* logged_in_user = UserManager::Get()->GetLoggedInUser();
728 if (connector->GetUserAffiliation(logged_in_user->email()) == 688 if (connector->GetUserAffiliation(logged_in_user->email()) ==
729 policy::USER_AFFILIATION_MANAGED) { 689 policy::USER_AFFILIATION_MANAGED) {
730 VLOG(1) << "Respecting proxy for network " << network->name() 690 VLOG(1) << "Respecting proxy for network " << network.name()
731 << ", as logged-in user belongs to the domain the device " 691 << ", as logged-in user belongs to the domain the device "
732 << "is enrolled to."; 692 << "is enrolled to.";
733 return false; 693 return false;
734 } 694 }
735 } 695 }
736 696
737 return !GetUseSharedProxies(); 697 return !GetUseSharedProxies();
738 } 698 }
739 699
740 void ProxyConfigServiceImpl::DetermineEffectiveConfig(const Network* network, 700 void ProxyConfigServiceImpl::UpdateProxyConfigOfDefaultNetwork() {
741 bool activate) { 701 const NetworkState* network = NetworkStateHandler::Get()->DefaultNetwork();
702 LOG_IF(ERROR, network && network->path() != default_network_)
703 << "Missed network change notification: " << default_network_
704 << " -> " << network->path();
705 DetermineEffectiveConfig(network, true);
706 }
707
708 void ProxyConfigServiceImpl::DetermineEffectiveConfig(
709 const NetworkState* network,
710 bool activate) {
742 // Get prefs proxy config if available. 711 // Get prefs proxy config if available.
743 net::ProxyConfig pref_config; 712 net::ProxyConfig pref_config;
744 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config); 713 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);
745 714
746 // Get network proxy config if available. 715 // Get network proxy config if available.
747 net::ProxyConfig network_config; 716 net::ProxyConfig network_config;
748 net::ProxyConfigService::ConfigAvailability network_availability = 717 net::ProxyConfigService::ConfigAvailability network_availability =
749 net::ProxyConfigService::CONFIG_UNSET; 718 net::ProxyConfigService::CONFIG_UNSET;
750 bool ignore_proxy = activate; 719 bool ignore_proxy = activate;
751 if (network) { 720 if (network) {
752 // If we're activating proxy, ignore proxy if necessary; 721 // If we're activating proxy, ignore proxy if necessary;
753 // otherwise, for ui, get actual proxy to show user. 722 // otherwise, for ui, get actual proxy to show user.
754 ignore_proxy = activate ? IgnoreProxy(network) : false; 723 ignore_proxy = activate ? IgnoreProxy(*network) : false;
755 // If network is shared but use-shared-proxies is off, use direct mode. 724 // If network is shared but use-shared-proxies is off, use direct mode.
756 if (ignore_proxy) { 725 if (ignore_proxy) {
757 VLOG(1) << "Shared network && !use-shared-proxies, use direct"; 726 VLOG(1) << "Shared network && !use-shared-proxies, use direct";
758 network_availability = net::ProxyConfigService::CONFIG_VALID; 727 network_availability = net::ProxyConfigService::CONFIG_VALID;
759 } else if (!network->proxy_config().empty()) { 728 } else if (ParseProxyConfig(*network, &network_config)) {
760 // Network is private or shared with user using shared proxies. 729 // Network is private or shared with user using shared proxies.
761 if (ParseProxyConfig(network, &network_config)) { 730 VLOG(1) << this << ": using network proxy: "
762 VLOG(1) << this << ": using network proxy: " 731 << network->proxy_config();
763 << network->proxy_config(); 732 network_availability = net::ProxyConfigService::CONFIG_VALID;
764 network_availability = net::ProxyConfigService::CONFIG_VALID;
765 }
766 } 733 }
767 } 734 }
768 735
769 // Determine effective proxy config, either from prefs or network. 736 // Determine effective proxy config, either from prefs or network.
770 ProxyPrefs::ConfigState effective_config_state; 737 ProxyPrefs::ConfigState effective_config_state;
771 net::ProxyConfig effective_config; 738 net::ProxyConfig effective_config;
772 GetEffectiveProxyConfig(pref_state, pref_config, 739 GetEffectiveProxyConfig(pref_state, pref_config,
773 network_availability, network_config, ignore_proxy, 740 network_availability, network_config, ignore_proxy,
774 &effective_config_state, &effective_config); 741 &effective_config_state, &effective_config);
775 742
(...skipping 17 matching lines...) Expand all
793 // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService. 760 // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService.
794 if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM) 761 if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM)
795 effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE; 762 effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
796 // If config is manual, add rule to bypass local host. 763 // If config is manual, add rule to bypass local host.
797 if (effective_config.proxy_rules().type != 764 if (effective_config.proxy_rules().type !=
798 net::ProxyConfig::ProxyRules::TYPE_NO_RULES) 765 net::ProxyConfig::ProxyRules::TYPE_NO_RULES)
799 effective_config.proxy_rules().bypass_rules.AddRuleToBypassLocal(); 766 effective_config.proxy_rules().bypass_rules.AddRuleToBypassLocal();
800 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state, 767 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state,
801 effective_config); 768 effective_config);
802 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful. 769 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful.
803 scoped_ptr<DictionaryValue> config_dict(static_cast<DictionaryValue*>( 770 scoped_ptr<base::DictionaryValue> config_dict(
804 effective_config.ToValue())); 771 effective_config.ToValue());
805 std::string config_value;
806 JSONStringValueSerializer serializer(&config_value);
807 serializer.Serialize(*config_dict.get());
808 VLOG(1) << this << ": Proxy changed: " 772 VLOG(1) << this << ": Proxy changed: "
809 << ConfigStateToString(active_config_state_) 773 << ConfigStateToString(active_config_state_)
810 << ", " << config_value; 774 << ", " << *config_dict;
811 } 775 }
812 } 776 }
813 } else { // For UI, store effective proxy into |current_ui_config_|. 777 } else { // For UI, store effective proxy into |current_ui_config_|.
778 DCHECK(network);
814 current_ui_config_.FromNetProxyConfig(effective_config); 779 current_ui_config_.FromNetProxyConfig(effective_config);
815 current_ui_config_.state = effective_config_state; 780 current_ui_config_.state = effective_config_state;
816 if (PrefPrecedes(effective_config_state)) { 781 if (PrefPrecedes(effective_config_state)) {
817 current_ui_config_.user_modifiable = false; 782 current_ui_config_.user_modifiable = false;
818 } else if (!IsNetworkProxySettingsEditable(network)) { 783 } else if (!IsNetworkProxySettingsEditable(*network)) {
819 // TODO(xiyuan): Figure out the right way to set config state for managed 784 // TODO(xiyuan): Figure out the right way to set config state for managed
820 // network. 785 // network.
821 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; 786 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
822 current_ui_config_.user_modifiable = false; 787 current_ui_config_.user_modifiable = false;
823 } else { 788 } else {
824 current_ui_config_.user_modifiable = !network || !IgnoreProxy(network); 789 current_ui_config_.user_modifiable = !IgnoreProxy(*network);
825 } 790 }
826 } 791 }
827 } 792 }
828
829 void ProxyConfigServiceImpl::OnUISetCurrentNetwork(const Network* network) {
830 DetermineEffectiveConfig(network, false);
831 VLOG(1) << "Current ui network: "
832 << (network->name().empty() ? current_ui_network_ : network->name())
833 << ", " << ModeToString(current_ui_config_.mode)
834 << ", " << ConfigStateToString(current_ui_config_.state)
835 << ", modifiable:" << current_ui_config_.user_modifiable;
836 // Notify whoever is interested in this change.
837 std::vector<base::Closure>::iterator iter = callbacks_.begin();
838 while (iter != callbacks_.end()) {
839 if (iter->is_null()) {
840 iter = callbacks_.erase(iter);
841 } else {
842 iter->Run();
843 ++iter;
844 }
845 }
846 }
847 793
848 void ProxyConfigServiceImpl::ResetUICache() { 794 void ProxyConfigServiceImpl::ResetUICache() {
849 current_ui_network_.clear(); 795 current_ui_network_.clear();
850 current_ui_config_ = ProxyConfig(); 796 current_ui_config_ = ProxyConfig();
851 } 797 }
852 798
853 void ProxyConfigServiceImpl::FetchProxyPolicy() { 799 void ProxyConfigServiceImpl::FetchProxyPolicy() {
854 if (CrosSettingsProvider::TRUSTED != 800 if (CrosSettingsProvider::TRUSTED !=
855 CrosSettings::Get()->PrepareTrustedValues( 801 CrosSettings::Get()->PrepareTrustedValues(
856 base::Bind(&ProxyConfigServiceImpl::FetchProxyPolicy, 802 base::Bind(&ProxyConfigServiceImpl::FetchProxyPolicy,
(...skipping 11 matching lines...) Expand all
868 814
869 VLOG(1) << "Retrieved proxy setting from device, value=[" 815 VLOG(1) << "Retrieved proxy setting from device, value=["
870 << policy_value << "]"; 816 << policy_value << "]";
871 ProxyConfig device_config; 817 ProxyConfig device_config;
872 if (!device_config.DeserializeForDevice(policy_value) || 818 if (!device_config.DeserializeForDevice(policy_value) ||
873 !device_config.SerializeForNetwork(&device_config_)) { 819 !device_config.SerializeForNetwork(&device_config_)) {
874 LOG(WARNING) << "Can't deserialize device setting or serialize for network"; 820 LOG(WARNING) << "Can't deserialize device setting or serialize for network";
875 device_config_.clear(); 821 device_config_.clear();
876 return; 822 return;
877 } 823 }
878 if (!active_network_.empty()) { 824 const NetworkState* default_network =
879 VLOG(1) << "Try migrating device config to " << active_network_; 825 NetworkStateHandler::Get()->DefaultNetwork();
880 SetProxyConfigForNetwork(active_network_, device_config_, true); 826 if (default_network) {
827 VLOG(1) << "Try migrating device config to " << default_network->path();
828 SetProxyConfigForNetwork(default_network->path(), device_config_, true);
881 } 829 }
882 } 830 }
883 831
884 } // namespace chromeos 832 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698