| 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 "chrome/browser/policy/cloud_policy_subsystem.h" | 5 #include "chrome/browser/policy/cloud_policy_subsystem.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/policy/cloud_policy_cache_base.h" | 12 #include "chrome/browser/policy/cloud_policy_cache_base.h" |
| 12 #include "chrome/browser/policy/cloud_policy_controller.h" | 13 #include "chrome/browser/policy/cloud_policy_controller.h" |
| 13 #include "chrome/browser/policy/cloud_policy_identity_strategy.h" | 14 #include "chrome/browser/policy/cloud_policy_identity_strategy.h" |
| 14 #include "chrome/browser/policy/configuration_policy_provider.h" | 15 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 15 #include "chrome/browser/policy/device_management_service.h" | 16 #include "chrome/browser/policy/device_management_service.h" |
| 16 #include "chrome/browser/policy/device_token_fetcher.h" | 17 #include "chrome/browser/policy/device_token_fetcher.h" |
| 17 #include "chrome/browser/policy/policy_notifier.h" | 18 #include "chrome/browser/policy/policy_notifier.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 43 policy_notifier_->AddObserver(observer); | 44 policy_notifier_->AddObserver(observer); |
| 44 } | 45 } |
| 45 | 46 |
| 46 CloudPolicySubsystem::ObserverRegistrar::~ObserverRegistrar() { | 47 CloudPolicySubsystem::ObserverRegistrar::~ObserverRegistrar() { |
| 47 if (policy_notifier_) | 48 if (policy_notifier_) |
| 48 policy_notifier_->RemoveObserver(observer_); | 49 policy_notifier_->RemoveObserver(observer_); |
| 49 } | 50 } |
| 50 | 51 |
| 51 CloudPolicySubsystem::CloudPolicySubsystem( | 52 CloudPolicySubsystem::CloudPolicySubsystem( |
| 52 CloudPolicyIdentityStrategy* identity_strategy, | 53 CloudPolicyIdentityStrategy* identity_strategy, |
| 53 CloudPolicyCacheBase* policy_cache) | 54 CloudPolicyCacheBase* policy_cache) { |
| 54 : prefs_(NULL) { | |
| 55 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 55 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 56 notifier_.reset(new PolicyNotifier()); | 56 notifier_.reset(new PolicyNotifier()); |
| 57 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 57 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 58 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | 58 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { |
| 59 device_management_service_.reset(new DeviceManagementService( | 59 device_management_service_.reset(new DeviceManagementService( |
| 60 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); | 60 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); |
| 61 cloud_policy_cache_.reset(policy_cache); | 61 cloud_policy_cache_.reset(policy_cache); |
| 62 cloud_policy_cache_->set_policy_notifier(notifier_.get()); | 62 cloud_policy_cache_->set_policy_notifier(notifier_.get()); |
| 63 cloud_policy_cache_->Load(); | 63 cloud_policy_cache_->Load(); |
| 64 | 64 |
| 65 device_token_fetcher_.reset( | 65 device_token_fetcher_.reset( |
| 66 new DeviceTokenFetcher(device_management_service_.get(), | 66 new DeviceTokenFetcher(device_management_service_.get(), |
| 67 cloud_policy_cache_.get(), | 67 cloud_policy_cache_.get(), |
| 68 notifier_.get())); | 68 notifier_.get())); |
| 69 | 69 |
| 70 cloud_policy_controller_.reset( | 70 cloud_policy_controller_.reset( |
| 71 new CloudPolicyController(device_management_service_.get(), | 71 new CloudPolicyController(device_management_service_.get(), |
| 72 cloud_policy_cache_.get(), | 72 cloud_policy_cache_.get(), |
| 73 device_token_fetcher_.get(), | 73 device_token_fetcher_.get(), |
| 74 identity_strategy, | 74 identity_strategy, |
| 75 notifier_.get())); | 75 notifier_.get())); |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 CloudPolicySubsystem::~CloudPolicySubsystem() { | 79 CloudPolicySubsystem::~CloudPolicySubsystem() { |
| 80 DCHECK(!prefs_); | |
| 81 cloud_policy_controller_.reset(); | 80 cloud_policy_controller_.reset(); |
| 82 device_token_fetcher_.reset(); | 81 device_token_fetcher_.reset(); |
| 83 cloud_policy_cache_.reset(); | 82 cloud_policy_cache_.reset(); |
| 84 device_management_service_.reset(); | 83 device_management_service_.reset(); |
| 85 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 84 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 86 } | 85 } |
| 87 | 86 |
| 88 void CloudPolicySubsystem::OnIPAddressChanged() { | 87 void CloudPolicySubsystem::OnIPAddressChanged() { |
| 89 if (state() == CloudPolicySubsystem::NETWORK_ERROR && | 88 if (state() == CloudPolicySubsystem::NETWORK_ERROR && |
| 90 cloud_policy_controller_.get()) { | 89 cloud_policy_controller_.get()) { |
| 91 cloud_policy_controller_->Retry(); | 90 cloud_policy_controller_->Retry(); |
| 92 } | 91 } |
| 93 } | 92 } |
| 94 | 93 |
| 95 void CloudPolicySubsystem::Initialize( | 94 void CloudPolicySubsystem::Initialize( |
| 96 PrefService* prefs, | 95 const char* refresh_pref_name, |
| 97 net::URLRequestContextGetter* request_context) { | 96 net::URLRequestContextGetter* request_context) { |
| 98 DCHECK(!prefs_); | |
| 99 prefs_ = prefs; | |
| 100 | |
| 101 if (device_management_service_.get()) | 97 if (device_management_service_.get()) |
| 102 device_management_service_->Initialize(request_context); | 98 device_management_service_->Initialize(request_context); |
| 103 | 99 |
| 104 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, prefs_, this); | 100 policy_refresh_rate_.Init(refresh_pref_name, |
| 101 g_browser_process->local_state(), |
| 102 this); |
| 103 |
| 105 UpdatePolicyRefreshRate(); | 104 UpdatePolicyRefreshRate(); |
| 106 } | 105 } |
| 107 | 106 |
| 108 void CloudPolicySubsystem::Shutdown() { | 107 void CloudPolicySubsystem::Shutdown() { |
| 109 if (device_management_service_.get()) | 108 if (device_management_service_.get()) |
| 110 device_management_service_->Shutdown(); | 109 device_management_service_->Shutdown(); |
| 111 cloud_policy_controller_.reset(); | 110 cloud_policy_controller_.reset(); |
| 112 cloud_policy_cache_.reset(); | 111 cloud_policy_cache_.reset(); |
| 113 policy_refresh_rate_.Destroy(); | 112 policy_refresh_rate_.Destroy(); |
| 114 prefs_ = NULL; | |
| 115 } | 113 } |
| 116 | 114 |
| 117 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { | 115 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { |
| 118 return notifier_->state(); | 116 return notifier_->state(); |
| 119 } | 117 } |
| 120 | 118 |
| 121 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { | 119 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { |
| 122 return notifier_->error_details(); | 120 return notifier_->error_details(); |
| 123 } | 121 } |
| 124 | 122 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 137 ConfigurationPolicyProvider* | 135 ConfigurationPolicyProvider* |
| 138 CloudPolicySubsystem::GetRecommendedPolicyProvider() { | 136 CloudPolicySubsystem::GetRecommendedPolicyProvider() { |
| 139 if (cloud_policy_cache_.get()) | 137 if (cloud_policy_cache_.get()) |
| 140 return cloud_policy_cache_->GetRecommendedPolicyProvider(); | 138 return cloud_policy_cache_->GetRecommendedPolicyProvider(); |
| 141 | 139 |
| 142 return NULL; | 140 return NULL; |
| 143 } | 141 } |
| 144 | 142 |
| 145 // static | 143 // static |
| 146 void CloudPolicySubsystem::RegisterPrefs(PrefService* pref_service) { | 144 void CloudPolicySubsystem::RegisterPrefs(PrefService* pref_service) { |
| 147 pref_service->RegisterIntegerPref(prefs::kPolicyRefreshRate, | 145 pref_service->RegisterIntegerPref(prefs::kDevicePolicyRefreshRate, |
| 146 kDefaultPolicyRefreshRateMs, |
| 147 PrefService::UNSYNCABLE_PREF); |
| 148 pref_service->RegisterIntegerPref(prefs::kUserPolicyRefreshRate, |
| 148 kDefaultPolicyRefreshRateMs, | 149 kDefaultPolicyRefreshRateMs, |
| 149 PrefService::UNSYNCABLE_PREF); | 150 PrefService::UNSYNCABLE_PREF); |
| 150 } | 151 } |
| 151 | 152 |
| 152 void CloudPolicySubsystem::UpdatePolicyRefreshRate() { | 153 void CloudPolicySubsystem::UpdatePolicyRefreshRate() { |
| 153 if (cloud_policy_controller_.get()) { | 154 if (cloud_policy_controller_.get()) { |
| 154 // Clamp to sane values. | 155 // Clamp to sane values. |
| 155 int64 refresh_rate = policy_refresh_rate_.GetValue(); | 156 int64 refresh_rate = policy_refresh_rate_.GetValue(); |
| 156 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); | 157 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); |
| 157 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); | 158 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); |
| 158 cloud_policy_controller_->SetRefreshRate(refresh_rate); | 159 cloud_policy_controller_->SetRefreshRate(refresh_rate); |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 void CloudPolicySubsystem::Observe(NotificationType type, | 163 void CloudPolicySubsystem::Observe(NotificationType type, |
| 163 const NotificationSource& source, | 164 const NotificationSource& source, |
| 164 const NotificationDetails& details) { | 165 const NotificationDetails& details) { |
| 165 if (type == NotificationType::PREF_CHANGED && | 166 if (type == NotificationType::PREF_CHANGED && |
| 166 policy_refresh_rate_.GetPrefName() == | 167 policy_refresh_rate_.GetPrefName() == |
| 167 *(Details<std::string>(details).ptr()) && | 168 *(Details<std::string>(details).ptr())) { |
| 168 prefs_ == Source<PrefService>(source).ptr()) { | 169 DCHECK_EQ(Source<PrefService>(source).ptr(), |
| 170 g_browser_process->local_state()); |
| 169 UpdatePolicyRefreshRate(); | 171 UpdatePolicyRefreshRate(); |
| 170 } else { | 172 } else { |
| 171 NOTREACHED(); | 173 NOTREACHED(); |
| 172 } | 174 } |
| 173 } | 175 } |
| 174 | 176 |
| 175 } // namespace policy | 177 } // namespace policy |
| OLD | NEW |