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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/policy/cloud_policy_subsystem.h" 6 7 #include <algorithm> 8 #include <string> 9 10 #include "base/command_line.h" 11 #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_identity_strategy.h" 14 #include "chrome/browser/policy/configuration_policy_provider.h" 15 #include "chrome/browser/policy/device_management_service.h" 16 #include "chrome/browser/policy/device_token_fetcher.h" 17 #include "chrome/browser/policy/policy_notifier.h" 18 #include "chrome/browser/prefs/pref_service.h" 19 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/pref_names.h" 21 #include "content/common/notification_details.h" 22 #include "content/common/notification_source.h" 23 #include "content/common/notification_type.h" 24 25 namespace { 26 27 // Default refresh rate. 28 const int kDefaultPolicyRefreshRateMs = 3 * 60 * 60 * 1000; // 3 hours. 29 30 // Refresh rate sanity interval bounds. 31 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes 32 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day 33 34 } // namespace 35 36 namespace policy { 37 38 CloudPolicySubsystem::ObserverRegistrar::ObserverRegistrar( 39 CloudPolicySubsystem* cloud_policy_subsystem, 40 CloudPolicySubsystem::Observer* observer) 41 : observer_(observer) { 42 policy_notifier_ = cloud_policy_subsystem->notifier(); 43 policy_notifier_->AddObserver(observer); 44 } 45 46 CloudPolicySubsystem::ObserverRegistrar::~ObserverRegistrar() { 47 if (policy_notifier_) 48 policy_notifier_->RemoveObserver(observer_); 49 } 50 51 CloudPolicySubsystem::CloudPolicySubsystem( 52 CloudPolicyIdentityStrategy* identity_strategy, 53 CloudPolicyCacheBase* policy_cache) 54 : prefs_(NULL) { 55 net::NetworkChangeNotifier::AddIPAddressObserver(this); 56 notifier_.reset(new PolicyNotifier()); 57 CommandLine* command_line = CommandLine::ForCurrentProcess(); 58 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { 59 device_management_service_.reset(new DeviceManagementService( 60 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); 61 cloud_policy_cache_.reset(policy_cache); 62 cloud_policy_cache_->set_policy_notifier(notifier_.get()); 63 cloud_policy_cache_->Load(); 64 65 device_token_fetcher_.reset( 66 new DeviceTokenFetcher(device_management_service_.get(), 67 cloud_policy_cache_.get(), 68 notifier_.get())); 69 70 cloud_policy_controller_.reset( 71 new CloudPolicyController(device_management_service_.get(), 72 cloud_policy_cache_.get(), 73 device_token_fetcher_.get(), 74 identity_strategy, 75 notifier_.get())); 76 } 77 } 78 79 CloudPolicySubsystem::~CloudPolicySubsystem() { 80 DCHECK(!prefs_); 81 cloud_policy_controller_.reset(); 82 device_token_fetcher_.reset(); 83 cloud_policy_cache_.reset(); 84 device_management_service_.reset(); 85 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); 86 } 87 88 void CloudPolicySubsystem::OnIPAddressChanged() { 89 if (state() == CloudPolicySubsystem::NETWORK_ERROR && 90 cloud_policy_controller_.get()) { 91 cloud_policy_controller_->Retry(); 92 } 93 } 94 95 void CloudPolicySubsystem::Initialize( 96 PrefService* prefs, 97 net::URLRequestContextGetter* request_context) { 98 DCHECK(!prefs_); 99 prefs_ = prefs; 100 101 if (device_management_service_.get()) 102 device_management_service_->Initialize(request_context); 103 104 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, prefs_, this); 105 UpdatePolicyRefreshRate(); 106 } 107 108 void CloudPolicySubsystem::Shutdown() { 109 if (device_management_service_.get()) 110 device_management_service_->Shutdown(); 111 cloud_policy_controller_.reset(); 112 cloud_policy_cache_.reset(); 113 policy_refresh_rate_.Destroy(); 114 prefs_ = NULL; 115 } 116 117 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { 118 return notifier_->state(); 119 } 120 121 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { 122 return notifier_->error_details(); 123 } 124 125 void CloudPolicySubsystem::StopAutoRetry() { 126 cloud_policy_controller_->StopAutoRetry(); 127 device_token_fetcher_->StopAutoRetry(); 128 } 129 130 ConfigurationPolicyProvider* CloudPolicySubsystem::GetManagedPolicyProvider() { 131 if (cloud_policy_cache_.get()) 132 return cloud_policy_cache_->GetManagedPolicyProvider(); 133 134 return NULL; 135 } 136 137 ConfigurationPolicyProvider* 138 CloudPolicySubsystem::GetRecommendedPolicyProvider() { 139 if (cloud_policy_cache_.get()) 140 return cloud_policy_cache_->GetRecommendedPolicyProvider(); 141 142 return NULL; 143 } 144 145 // static 146 void CloudPolicySubsystem::RegisterPrefs(PrefService* pref_service) { 147 pref_service->RegisterIntegerPref(prefs::kPolicyRefreshRate, 148 kDefaultPolicyRefreshRateMs); 149 } 150 151 void CloudPolicySubsystem::UpdatePolicyRefreshRate() { 152 if (cloud_policy_controller_.get()) { 153 // Clamp to sane values. 154 int64 refresh_rate = policy_refresh_rate_.GetValue(); 155 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); 156 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); 157 cloud_policy_controller_->SetRefreshRate(refresh_rate); 158 } 159 } 160 161 void CloudPolicySubsystem::Observe(NotificationType type, 162 const NotificationSource& source, 163 const NotificationDetails& details) { 164 if (type == NotificationType::PREF_CHANGED && 165 policy_refresh_rate_.GetPrefName() == 166 *(Details<std::string>(details).ptr()) && 167 prefs_ == Source<PrefService>(source).ptr()) { 168 UpdatePolicyRefreshRate(); 169 } else { 170 NOTREACHED(); 171 } 172 } 173 174 } // namespace policy 175