Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // Most of this code is copied from: | 5 // Most of this code is copied from: |
| 6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} | 6 // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} |
| 7 | 7 |
| 8 #include "remoting/host/policy_hack/policy_watcher.h" | 8 #include "remoting/host/policy_hack/policy_watcher.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 | 88 |
| 89 policy::PolicyNamespace GetPolicyNamespace() { | 89 policy::PolicyNamespace GetPolicyNamespace() { |
| 90 return policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); | 90 return policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 } // namespace | 93 } // namespace |
| 94 | 94 |
| 95 void PolicyWatcher::StartWatching( | 95 void PolicyWatcher::StartWatching( |
| 96 const PolicyUpdatedCallback& policy_updated_callback, | 96 const PolicyUpdatedCallback& policy_updated_callback, |
| 97 const PolicyErrorCallback& policy_error_callback) { | 97 const PolicyErrorCallback& policy_error_callback) { |
| 98 if (!OnPolicyServiceThread()) { | 98 DCHECK(CalledOnValidThread()); |
| 99 policy_service_task_runner_->PostTask( | 99 DCHECK(!policy_updated_callback.is_null()); |
| 100 FROM_HERE, | 100 DCHECK(!policy_error_callback.is_null()); |
|
Łukasz Anforowicz
2015/01/30 19:50:59
nit: I wonder if we should/could attempt to catch
Sergey Ulanov
2015/01/30 20:06:07
Done.
| |
| 101 base::Bind(&PolicyWatcher::StartWatching, base::Unretained(this), | |
| 102 policy_updated_callback, policy_error_callback)); | |
| 103 return; | |
| 104 } | |
| 105 | 101 |
| 106 policy_updated_callback_ = policy_updated_callback; | 102 policy_updated_callback_ = policy_updated_callback; |
| 107 policy_error_callback_ = policy_error_callback; | 103 policy_error_callback_ = policy_error_callback; |
| 108 | 104 |
| 109 // Listen for future policy changes. | 105 // Listen for future policy changes. |
| 110 policy_service_->AddObserver(policy::POLICY_DOMAIN_CHROME, this); | 106 policy_service_->AddObserver(policy::POLICY_DOMAIN_CHROME, this); |
| 111 | 107 |
| 112 // Process current policy state. | 108 // Process current policy state. |
| 113 if (policy_service_->IsInitializationComplete(policy::POLICY_DOMAIN_CHROME)) { | 109 if (policy_service_->IsInitializationComplete(policy::POLICY_DOMAIN_CHROME)) { |
| 114 OnPolicyServiceInitialized(policy::POLICY_DOMAIN_CHROME); | 110 OnPolicyServiceInitialized(policy::POLICY_DOMAIN_CHROME); |
| 115 } | 111 } |
| 116 } | 112 } |
| 117 | 113 |
| 118 void PolicyWatcher::StopWatching(const base::Closure& stopped_callback) { | |
| 119 policy_service_task_runner_->PostTaskAndReply( | |
| 120 FROM_HERE, base::Bind(&PolicyWatcher::StopWatchingOnPolicyServiceThread, | |
| 121 base::Unretained(this)), | |
| 122 stopped_callback); | |
| 123 } | |
| 124 | |
| 125 void PolicyWatcher::StopWatchingOnPolicyServiceThread() { | |
| 126 policy_service_->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); | |
| 127 policy_updated_callback_.Reset(); | |
| 128 policy_error_callback_.Reset(); | |
| 129 } | |
| 130 | |
| 131 bool PolicyWatcher::OnPolicyServiceThread() const { | |
| 132 return policy_service_task_runner_->BelongsToCurrentThread(); | |
| 133 } | |
| 134 | |
| 135 void PolicyWatcher::UpdatePolicies( | 114 void PolicyWatcher::UpdatePolicies( |
| 136 const base::DictionaryValue* new_policies_raw) { | 115 const base::DictionaryValue* new_policies_raw) { |
| 137 DCHECK(OnPolicyServiceThread()); | 116 DCHECK(CalledOnValidThread()); |
| 138 | 117 |
| 139 transient_policy_error_retry_counter_ = 0; | 118 transient_policy_error_retry_counter_ = 0; |
| 140 | 119 |
| 141 // Use default values for any missing policies. | 120 // Use default values for any missing policies. |
| 142 scoped_ptr<base::DictionaryValue> new_policies = | 121 scoped_ptr<base::DictionaryValue> new_policies = |
| 143 CopyGoodValuesAndAddDefaults( | 122 CopyGoodValuesAndAddDefaults( |
| 144 new_policies_raw, default_values_.get(), bad_type_values_.get()); | 123 new_policies_raw, default_values_.get(), bad_type_values_.get()); |
| 145 | 124 |
| 146 // Find the changed policies. | 125 // Find the changed policies. |
| 147 scoped_ptr<base::DictionaryValue> changed_policies( | 126 scoped_ptr<base::DictionaryValue> changed_policies( |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 172 | 151 |
| 173 void PolicyWatcher::SignalTransientPolicyError() { | 152 void PolicyWatcher::SignalTransientPolicyError() { |
| 174 const int kMaxRetryCount = 5; | 153 const int kMaxRetryCount = 5; |
| 175 transient_policy_error_retry_counter_ += 1; | 154 transient_policy_error_retry_counter_ += 1; |
| 176 if (transient_policy_error_retry_counter_ >= kMaxRetryCount) { | 155 if (transient_policy_error_retry_counter_ >= kMaxRetryCount) { |
| 177 SignalPolicyError(); | 156 SignalPolicyError(); |
| 178 } | 157 } |
| 179 } | 158 } |
| 180 | 159 |
| 181 PolicyWatcher::PolicyWatcher( | 160 PolicyWatcher::PolicyWatcher( |
| 182 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 183 policy_service_task_runner, | |
| 184 policy::PolicyService* policy_service, | 161 policy::PolicyService* policy_service, |
| 185 scoped_ptr<policy::PolicyService> owned_policy_service, | 162 scoped_ptr<policy::PolicyService> owned_policy_service, |
| 186 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, | 163 scoped_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, |
| 187 scoped_ptr<policy::SchemaRegistry> owned_schema_registry) | 164 scoped_ptr<policy::SchemaRegistry> owned_schema_registry) |
| 188 : policy_service_task_runner_(policy_service_task_runner), | 165 : transient_policy_error_retry_counter_(0), |
| 189 transient_policy_error_retry_counter_(0), | |
| 190 old_policies_(new base::DictionaryValue()), | 166 old_policies_(new base::DictionaryValue()), |
| 191 default_values_(new base::DictionaryValue()), | 167 default_values_(new base::DictionaryValue()), |
| 192 policy_service_(policy_service), | 168 policy_service_(policy_service), |
| 193 owned_schema_registry_(owned_schema_registry.Pass()), | 169 owned_schema_registry_(owned_schema_registry.Pass()), |
| 194 owned_policy_provider_(owned_policy_provider.Pass()), | 170 owned_policy_provider_(owned_policy_provider.Pass()), |
| 195 owned_policy_service_(owned_policy_service.Pass()) { | 171 owned_policy_service_(owned_policy_service.Pass()) { |
| 196 // Initialize the default values for each policy. | 172 // Initialize the default values for each policy. |
| 197 default_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, true); | 173 default_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, true); |
| 198 default_values_->SetBoolean(key::kRemoteAccessHostRequireTwoFactor, false); | 174 default_values_->SetBoolean(key::kRemoteAccessHostRequireTwoFactor, false); |
| 199 default_values_->SetBoolean(key::kRemoteAccessHostRequireCurtain, false); | 175 default_values_->SetBoolean(key::kRemoteAccessHostRequireCurtain, false); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 218 | 194 |
| 219 // Initialize the fall-back values to use for unreadable policies. | 195 // Initialize the fall-back values to use for unreadable policies. |
| 220 // For most policies these match the defaults. | 196 // For most policies these match the defaults. |
| 221 bad_type_values_.reset(default_values_->DeepCopy()); | 197 bad_type_values_.reset(default_values_->DeepCopy()); |
| 222 bad_type_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, false); | 198 bad_type_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, false); |
| 223 bad_type_values_->SetBoolean(key::kRemoteAccessHostAllowRelayedConnection, | 199 bad_type_values_->SetBoolean(key::kRemoteAccessHostAllowRelayedConnection, |
| 224 false); | 200 false); |
| 225 } | 201 } |
| 226 | 202 |
| 227 PolicyWatcher::~PolicyWatcher() { | 203 PolicyWatcher::~PolicyWatcher() { |
| 204 // Stop observing |policy_service_| if StartWatching() has been called. | |
| 205 if (!policy_updated_callback_.is_null()) { | |
| 206 policy_service_->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); | |
| 207 } | |
| 208 | |
| 228 if (owned_policy_provider_) { | 209 if (owned_policy_provider_) { |
| 229 owned_policy_provider_->Shutdown(); | 210 owned_policy_provider_->Shutdown(); |
| 230 } | 211 } |
| 231 } | 212 } |
| 232 | 213 |
| 233 void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns, | 214 void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 234 const policy::PolicyMap& previous, | 215 const policy::PolicyMap& previous, |
| 235 const policy::PolicyMap& current) { | 216 const policy::PolicyMap& current) { |
| 236 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); | 217 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); |
| 237 for (auto it = current.begin(); it != current.end(); ++it) { | 218 for (auto it = current.begin(); it != current.end(); ++it) { |
| 238 // TODO(lukasza): Use policy::Schema::Normalize() for schema verification. | 219 // TODO(lukasza): Use policy::Schema::Normalize() for schema verification. |
| 239 policy_dict->Set(it->first, it->second.value->DeepCopy()); | 220 policy_dict->Set(it->first, it->second.value->DeepCopy()); |
| 240 } | 221 } |
| 241 UpdatePolicies(policy_dict.get()); | 222 UpdatePolicies(policy_dict.get()); |
| 242 } | 223 } |
| 243 | 224 |
| 244 void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { | 225 void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { |
| 245 policy::PolicyNamespace ns = GetPolicyNamespace(); | 226 policy::PolicyNamespace ns = GetPolicyNamespace(); |
| 246 const policy::PolicyMap& current = policy_service_->GetPolicies(ns); | 227 const policy::PolicyMap& current = policy_service_->GetPolicies(ns); |
| 247 OnPolicyUpdated(ns, current, current); | 228 OnPolicyUpdated(ns, current, current); |
| 248 } | 229 } |
| 249 | 230 |
| 250 scoped_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( | 231 scoped_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( |
| 251 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 252 policy_service_task_runner, | |
| 253 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader) { | 232 scoped_ptr<policy::AsyncPolicyLoader> async_policy_loader) { |
| 254 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific | 233 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
| 255 // policies (expecting perf and maintanability improvement, but no functional | 234 // policies (expecting perf and maintanability improvement, but no functional |
| 256 // impact). | 235 // impact). |
| 257 policy::Schema schema = policy::Schema::Wrap(policy::GetChromeSchemaData()); | 236 policy::Schema schema = policy::Schema::Wrap(policy::GetChromeSchemaData()); |
| 258 | 237 |
| 259 scoped_ptr<policy::SchemaRegistry> schema_registry( | 238 scoped_ptr<policy::SchemaRegistry> schema_registry( |
| 260 new policy::SchemaRegistry()); | 239 new policy::SchemaRegistry()); |
| 261 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); | 240 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
| 262 | 241 |
| 263 scoped_ptr<policy::AsyncPolicyProvider> policy_provider( | 242 scoped_ptr<policy::AsyncPolicyProvider> policy_provider( |
| 264 new policy::AsyncPolicyProvider(schema_registry.get(), | 243 new policy::AsyncPolicyProvider(schema_registry.get(), |
| 265 async_policy_loader.Pass())); | 244 async_policy_loader.Pass())); |
| 266 policy_provider->Init(schema_registry.get()); | 245 policy_provider->Init(schema_registry.get()); |
| 267 | 246 |
| 268 policy::PolicyServiceImpl::Providers providers; | 247 policy::PolicyServiceImpl::Providers providers; |
| 269 providers.push_back(policy_provider.get()); | 248 providers.push_back(policy_provider.get()); |
| 270 scoped_ptr<policy::PolicyService> policy_service( | 249 scoped_ptr<policy::PolicyService> policy_service( |
| 271 new policy::PolicyServiceImpl(providers)); | 250 new policy::PolicyServiceImpl(providers)); |
| 272 | 251 |
| 273 policy::PolicyService* borrowed_policy_service = policy_service.get(); | 252 policy::PolicyService* borrowed_policy_service = policy_service.get(); |
| 274 return make_scoped_ptr(new PolicyWatcher( | 253 return make_scoped_ptr( |
| 275 policy_service_task_runner, borrowed_policy_service, | 254 new PolicyWatcher(borrowed_policy_service, policy_service.Pass(), |
| 276 policy_service.Pass(), policy_provider.Pass(), schema_registry.Pass())); | 255 policy_provider.Pass(), schema_registry.Pass())); |
| 277 } | 256 } |
| 278 | 257 |
| 279 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( | 258 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( |
| 280 policy::PolicyService* policy_service, | 259 policy::PolicyService* policy_service, |
| 281 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { | 260 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { |
| 282 #if defined(OS_CHROMEOS) | 261 #if defined(OS_CHROMEOS) |
| 262 // On Chrome OS the PolicyService is owned by the browser. | |
| 283 DCHECK(policy_service); | 263 DCHECK(policy_service); |
| 284 return make_scoped_ptr( | 264 return make_scoped_ptr( |
| 285 new PolicyWatcher(content::BrowserThread::GetMessageLoopProxyForThread( | 265 new PolicyWatcher(content::BrowserThread::GetMessageLoopProxyForThread( |
| 286 content::BrowserThread::UI), | 266 content::BrowserThread::UI), |
| 287 policy_service, nullptr, nullptr, nullptr)); | 267 policy_service, nullptr, nullptr, nullptr)); |
| 288 #elif defined(OS_WIN) | 268 #else // !defined(OS_CHROMEOS) |
| 289 DCHECK(!policy_service); | 269 DCHECK(!policy_service); |
| 290 // Always read the Chrome policies (even on Chromium) so that policy | 270 |
| 291 // enforcement can't be bypassed by running Chromium. | 271 // Create platform-specific PolicyLoader. Always read the Chrome policies |
| 292 // Note that this comment applies to all of Win/Mac/Posix branches below. | 272 // (even on Chromium) so that policy enforcement can't be bypassed by running |
| 293 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | 273 // Chromium. |
| 294 return PolicyWatcher::CreateFromPolicyLoader( | 274 scoped_ptr<policy::AsyncPolicyLoader> policy_loader; |
| 295 network_task_runner, | 275 #if defined(OS_WIN) |
| 296 policy::PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); | 276 policy_loader = policy::PolicyLoaderWin::Create( |
| 277 file_task_runner, L"SOFTWARE\\Policies\\Google\\Chrome"); | |
| 297 #elif defined(OS_MACOSX) | 278 #elif defined(OS_MACOSX) |
| 298 CFStringRef bundle_id = CFSTR("com.google.Chrome"); | 279 CFStringRef bundle_id = CFSTR("com.google.Chrome"); |
| 299 DCHECK(!policy_service); | 280 policy_loader.reset(new policy::PolicyLoaderMac( |
| 300 return PolicyWatcher::CreateFromPolicyLoader( | 281 file_task_runner, |
| 301 network_task_runner, | 282 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), |
| 302 make_scoped_ptr(new policy::PolicyLoaderMac( | 283 new MacPreferences(), bundle_id)); |
| 303 network_task_runner, | |
| 304 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), | |
| 305 new MacPreferences(), bundle_id))); | |
| 306 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | 284 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
| 307 DCHECK(!policy_service); | 285 policy_loader.reset(new policy::ConfigDirPolicyLoader( |
| 308 static const base::FilePath::CharType kPolicyDir[] = | 286 file_task_runner, |
| 309 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); | 287 base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies")), |
| 310 return PolicyWatcher::CreateFromPolicyLoader( | 288 policy::POLICY_SCOPE_MACHINE)); |
| 311 network_task_runner, make_scoped_ptr(new policy::ConfigDirPolicyLoader( | |
| 312 network_task_runner, base::FilePath(kPolicyDir), | |
| 313 policy::POLICY_SCOPE_MACHINE))); | |
| 314 #else | 289 #else |
| 315 #error OS that is not yet supported by PolicyWatcher code. | 290 #error OS that is not yet supported by PolicyWatcher code. |
| 316 #endif | 291 #endif |
| 292 | |
| 293 return PolicyWatcher::CreateFromPolicyLoader(policy_loader.Pass()); | |
| 294 #endif // !(OS_CHROMEOS) | |
| 317 } | 295 } |
| 318 | 296 |
| 319 } // namespace policy_hack | 297 } // namespace policy_hack |
| 320 } // namespace remoting | 298 } // namespace remoting |
| OLD | NEW |