| 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 23 matching lines...) Expand all Loading... |
| 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 : identity_strategy_(identity_strategy) { |
| 55 identity_strategy_(identity_strategy) { | |
| 56 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 56 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 57 notifier_.reset(new PolicyNotifier()); | 57 notifier_.reset(new PolicyNotifier()); |
| 58 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 58 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 59 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | 59 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { |
| 60 device_management_service_.reset(new DeviceManagementService( | 60 device_management_service_.reset(new DeviceManagementService( |
| 61 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); | 61 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); |
| 62 cloud_policy_cache_.reset(policy_cache); | 62 cloud_policy_cache_.reset(policy_cache); |
| 63 cloud_policy_cache_->set_policy_notifier(notifier_.get()); | 63 cloud_policy_cache_->set_policy_notifier(notifier_.get()); |
| 64 cloud_policy_cache_->Load(); | 64 cloud_policy_cache_->Load(); |
| 65 | 65 |
| 66 device_token_fetcher_.reset( | 66 device_token_fetcher_.reset( |
| 67 new DeviceTokenFetcher(device_management_service_.get(), | 67 new DeviceTokenFetcher(device_management_service_.get(), |
| 68 cloud_policy_cache_.get(), | 68 cloud_policy_cache_.get(), |
| 69 notifier_.get())); | 69 notifier_.get())); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 CloudPolicySubsystem::~CloudPolicySubsystem() { | 73 CloudPolicySubsystem::~CloudPolicySubsystem() { |
| 74 DCHECK(!prefs_); | |
| 75 cloud_policy_controller_.reset(); | 74 cloud_policy_controller_.reset(); |
| 76 device_token_fetcher_.reset(); | 75 device_token_fetcher_.reset(); |
| 77 cloud_policy_cache_.reset(); | 76 cloud_policy_cache_.reset(); |
| 78 device_management_service_.reset(); | 77 device_management_service_.reset(); |
| 79 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 78 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 80 } | 79 } |
| 81 | 80 |
| 82 void CloudPolicySubsystem::OnIPAddressChanged() { | 81 void CloudPolicySubsystem::OnIPAddressChanged() { |
| 83 if (state() == CloudPolicySubsystem::NETWORK_ERROR && | 82 if (state() == CloudPolicySubsystem::NETWORK_ERROR && |
| 84 cloud_policy_controller_.get()) { | 83 cloud_policy_controller_.get()) { |
| 85 cloud_policy_controller_->Retry(); | 84 cloud_policy_controller_->Retry(); |
| 86 } | 85 } |
| 87 } | 86 } |
| 88 | 87 |
| 89 void CloudPolicySubsystem::Initialize( | 88 void CloudPolicySubsystem::Initialize(const char* refresh_pref_name, |
| 90 PrefService* prefs, | 89 int64 delay_milliseconds) { |
| 91 int64 delay_milliseconds) { | |
| 92 DCHECK(!prefs_); | |
| 93 prefs_ = prefs; | |
| 94 | |
| 95 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 90 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 96 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | 91 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { |
| 92 DCHECK(device_management_service_.get()); |
| 93 DCHECK(cloud_policy_cache_.get()); |
| 94 DCHECK(device_token_fetcher_.get()); |
| 95 DCHECK(identity_strategy_); |
| 96 |
| 97 refresh_pref_name_ = refresh_pref_name; |
| 97 DCHECK(!cloud_policy_controller_.get()); | 98 DCHECK(!cloud_policy_controller_.get()); |
| 98 cloud_policy_controller_.reset( | 99 cloud_policy_controller_.reset( |
| 99 new CloudPolicyController(device_management_service_.get(), | 100 new CloudPolicyController(device_management_service_.get(), |
| 100 cloud_policy_cache_.get(), | 101 cloud_policy_cache_.get(), |
| 101 device_token_fetcher_.get(), | 102 device_token_fetcher_.get(), |
| 102 identity_strategy_, | 103 identity_strategy_, |
| 103 notifier_.get())); | 104 notifier_.get())); |
| 104 } | |
| 105 | 105 |
| 106 if (device_management_service_.get()) | |
| 107 device_management_service_->ScheduleInitialization(delay_milliseconds); | 106 device_management_service_->ScheduleInitialization(delay_milliseconds); |
| 108 | 107 |
| 109 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, prefs_, this); | 108 PrefService* local_state = g_browser_process->local_state(); |
| 110 UpdatePolicyRefreshRate(); | 109 DCHECK(pref_change_registrar_.IsEmpty()); |
| 110 pref_change_registrar_.Init(local_state); |
| 111 pref_change_registrar_.Add(refresh_pref_name_, this); |
| 112 UpdatePolicyRefreshRate(local_state->GetInteger(refresh_pref_name_)); |
| 113 } |
| 111 } | 114 } |
| 112 | 115 |
| 113 void CloudPolicySubsystem::Shutdown() { | 116 void CloudPolicySubsystem::Shutdown() { |
| 114 if (device_management_service_.get()) | 117 if (device_management_service_.get()) |
| 115 device_management_service_->Shutdown(); | 118 device_management_service_->Shutdown(); |
| 116 cloud_policy_controller_.reset(); | 119 cloud_policy_controller_.reset(); |
| 117 cloud_policy_cache_.reset(); | 120 cloud_policy_cache_.reset(); |
| 118 policy_refresh_rate_.Destroy(); | 121 pref_change_registrar_.RemoveAll(); |
| 119 prefs_ = NULL; | |
| 120 } | 122 } |
| 121 | 123 |
| 122 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { | 124 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { |
| 123 return notifier_->state(); | 125 return notifier_->state(); |
| 124 } | 126 } |
| 125 | 127 |
| 126 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { | 128 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { |
| 127 return notifier_->error_details(); | 129 return notifier_->error_details(); |
| 128 } | 130 } |
| 129 | 131 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 142 ConfigurationPolicyProvider* | 144 ConfigurationPolicyProvider* |
| 143 CloudPolicySubsystem::GetRecommendedPolicyProvider() { | 145 CloudPolicySubsystem::GetRecommendedPolicyProvider() { |
| 144 if (cloud_policy_cache_.get()) | 146 if (cloud_policy_cache_.get()) |
| 145 return cloud_policy_cache_->GetRecommendedPolicyProvider(); | 147 return cloud_policy_cache_->GetRecommendedPolicyProvider(); |
| 146 | 148 |
| 147 return NULL; | 149 return NULL; |
| 148 } | 150 } |
| 149 | 151 |
| 150 // static | 152 // static |
| 151 void CloudPolicySubsystem::RegisterPrefs(PrefService* pref_service) { | 153 void CloudPolicySubsystem::RegisterPrefs(PrefService* pref_service) { |
| 152 pref_service->RegisterIntegerPref(prefs::kPolicyRefreshRate, | 154 pref_service->RegisterIntegerPref(prefs::kDevicePolicyRefreshRate, |
| 155 kDefaultPolicyRefreshRateMs, |
| 156 PrefService::UNSYNCABLE_PREF); |
| 157 pref_service->RegisterIntegerPref(prefs::kUserPolicyRefreshRate, |
| 153 kDefaultPolicyRefreshRateMs, | 158 kDefaultPolicyRefreshRateMs, |
| 154 PrefService::UNSYNCABLE_PREF); | 159 PrefService::UNSYNCABLE_PREF); |
| 155 } | 160 } |
| 156 | 161 |
| 157 void CloudPolicySubsystem::UpdatePolicyRefreshRate() { | 162 void CloudPolicySubsystem::UpdatePolicyRefreshRate(int64 refresh_rate) { |
| 158 if (cloud_policy_controller_.get()) { | 163 if (cloud_policy_controller_.get()) { |
| 159 // Clamp to sane values. | 164 // Clamp to sane values. |
| 160 int64 refresh_rate = policy_refresh_rate_.GetValue(); | |
| 161 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); | 165 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); |
| 162 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); | 166 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); |
| 163 cloud_policy_controller_->SetRefreshRate(refresh_rate); | 167 cloud_policy_controller_->SetRefreshRate(refresh_rate); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 void CloudPolicySubsystem::Observe(NotificationType type, | 171 void CloudPolicySubsystem::Observe(NotificationType type, |
| 168 const NotificationSource& source, | 172 const NotificationSource& source, |
| 169 const NotificationDetails& details) { | 173 const NotificationDetails& details) { |
| 170 if (type == NotificationType::PREF_CHANGED && | 174 if (type == NotificationType::PREF_CHANGED) { |
| 171 policy_refresh_rate_.GetPrefName() == | 175 DCHECK_EQ(*(Details<std::string>(details).ptr()), |
| 172 *(Details<std::string>(details).ptr()) && | 176 std::string(refresh_pref_name_)); |
| 173 prefs_ == Source<PrefService>(source).ptr()) { | 177 PrefService* pref_service = Source<PrefService>(source).ptr(); |
| 174 UpdatePolicyRefreshRate(); | 178 // Temporarily also consider the profile preference service as a valid |
| 179 // source, since we cannot yet push user cloud policy to |local_state|. |
| 180 UpdatePolicyRefreshRate(pref_service->GetInteger(refresh_pref_name_)); |
| 175 } else { | 181 } else { |
| 176 NOTREACHED(); | 182 NOTREACHED(); |
| 177 } | 183 } |
| 178 } | 184 } |
| 179 | 185 |
| 180 void CloudPolicySubsystem::ScheduleServiceInitialization( | 186 void CloudPolicySubsystem::ScheduleServiceInitialization( |
| 181 int64 delay_milliseconds) { | 187 int64 delay_milliseconds) { |
| 182 if (device_management_service_.get()) | 188 if (device_management_service_.get()) |
| 183 device_management_service_->ScheduleInitialization(delay_milliseconds); | 189 device_management_service_->ScheduleInitialization(delay_milliseconds); |
| 184 } | 190 } |
| 185 | 191 |
| 186 } // namespace policy | 192 } // namespace policy |
| OLD | NEW |