| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "components/policy/core/common/cloud/component_cloud_policy_service.h" | 5 #include "components/policy/core/common/cloud/component_cloud_policy_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 core_->AddObserver(this); | 293 core_->AddObserver(this); |
| 294 client->AddObserver(this); | 294 client->AddObserver(this); |
| 295 | 295 |
| 296 // Register the supported policy domain for being downloaded in future policy | 296 // Register the supported policy domain for being downloaded in future policy |
| 297 // fetches. | 297 // fetches. |
| 298 client->AddPolicyTypeToFetch(policy_type_, | 298 client->AddPolicyTypeToFetch(policy_type_, |
| 299 std::string() /* settings_entity_id */); | 299 std::string() /* settings_entity_id */); |
| 300 } | 300 } |
| 301 | 301 |
| 302 ComponentCloudPolicyService::~ComponentCloudPolicyService() { | 302 ComponentCloudPolicyService::~ComponentCloudPolicyService() { |
| 303 DCHECK(CalledOnValidThread()); | 303 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 304 | 304 |
| 305 schema_registry_->RemoveObserver(this); | 305 schema_registry_->RemoveObserver(this); |
| 306 core_->store()->RemoveObserver(this); | 306 core_->store()->RemoveObserver(this); |
| 307 core_->RemoveObserver(this); | 307 core_->RemoveObserver(this); |
| 308 if (core_->client()) | 308 if (core_->client()) |
| 309 Disconnect(); | 309 Disconnect(); |
| 310 | 310 |
| 311 io_task_runner_->DeleteSoon(FROM_HERE, | 311 io_task_runner_->DeleteSoon(FROM_HERE, |
| 312 external_policy_data_fetcher_backend_.release()); | 312 external_policy_data_fetcher_backend_.release()); |
| 313 backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release()); | 313 backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release()); |
| 314 } | 314 } |
| 315 | 315 |
| 316 // static | 316 // static |
| 317 bool ComponentCloudPolicyService::SupportsDomain(PolicyDomain domain) { | 317 bool ComponentCloudPolicyService::SupportsDomain(PolicyDomain domain) { |
| 318 return ComponentCloudPolicyStore::SupportsDomain(domain); | 318 return ComponentCloudPolicyStore::SupportsDomain(domain); |
| 319 } | 319 } |
| 320 | 320 |
| 321 void ComponentCloudPolicyService::ClearCache() { | 321 void ComponentCloudPolicyService::ClearCache() { |
| 322 DCHECK(CalledOnValidThread()); | 322 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 323 backend_task_runner_->PostTask( | 323 backend_task_runner_->PostTask( |
| 324 FROM_HERE, | 324 FROM_HERE, |
| 325 base::Bind(&Backend::ClearCache, base::Unretained(backend_.get()))); | 325 base::Bind(&Backend::ClearCache, base::Unretained(backend_.get()))); |
| 326 } | 326 } |
| 327 | 327 |
| 328 void ComponentCloudPolicyService::OnSchemaRegistryReady() { | 328 void ComponentCloudPolicyService::OnSchemaRegistryReady() { |
| 329 DCHECK(CalledOnValidThread()); | 329 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 330 UpdateFromSchemaRegistry(); | 330 UpdateFromSchemaRegistry(); |
| 331 } | 331 } |
| 332 | 332 |
| 333 void ComponentCloudPolicyService::OnSchemaRegistryUpdated( | 333 void ComponentCloudPolicyService::OnSchemaRegistryUpdated( |
| 334 bool has_new_schemas) { | 334 bool has_new_schemas) { |
| 335 DCHECK(CalledOnValidThread()); | 335 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 336 UpdateFromSchemaRegistry(); | 336 UpdateFromSchemaRegistry(); |
| 337 } | 337 } |
| 338 | 338 |
| 339 void ComponentCloudPolicyService::OnCoreConnected(CloudPolicyCore* core) { | 339 void ComponentCloudPolicyService::OnCoreConnected(CloudPolicyCore* core) { |
| 340 DCHECK(CalledOnValidThread()); | 340 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 341 DCHECK_EQ(core_, core); | 341 DCHECK_EQ(core_, core); |
| 342 // Immediately update with any PolicyFetchResponses that the client may | 342 // Immediately update with any PolicyFetchResponses that the client may |
| 343 // already have. | 343 // already have. |
| 344 UpdateFromClient(); | 344 UpdateFromClient(); |
| 345 } | 345 } |
| 346 | 346 |
| 347 void ComponentCloudPolicyService::OnCoreDisconnecting(CloudPolicyCore* core) { | 347 void ComponentCloudPolicyService::OnCoreDisconnecting(CloudPolicyCore* core) { |
| 348 DCHECK(CalledOnValidThread()); | 348 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 349 DCHECK_EQ(core_, core); | 349 DCHECK_EQ(core_, core); |
| 350 Disconnect(); | 350 Disconnect(); |
| 351 } | 351 } |
| 352 | 352 |
| 353 void ComponentCloudPolicyService::OnRefreshSchedulerStarted( | 353 void ComponentCloudPolicyService::OnRefreshSchedulerStarted( |
| 354 CloudPolicyCore* core) { | 354 CloudPolicyCore* core) { |
| 355 // Ignored. | 355 // Ignored. |
| 356 } | 356 } |
| 357 | 357 |
| 358 void ComponentCloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | 358 void ComponentCloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) { |
| 359 DCHECK(CalledOnValidThread()); | 359 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 360 DCHECK_EQ(core_->store(), store); | 360 DCHECK_EQ(core_->store(), store); |
| 361 UpdateFromSuperiorStore(); | 361 UpdateFromSuperiorStore(); |
| 362 } | 362 } |
| 363 | 363 |
| 364 void ComponentCloudPolicyService::OnStoreError(CloudPolicyStore* store) { | 364 void ComponentCloudPolicyService::OnStoreError(CloudPolicyStore* store) { |
| 365 DCHECK(CalledOnValidThread()); | 365 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 366 DCHECK_EQ(core_->store(), store); | 366 DCHECK_EQ(core_->store(), store); |
| 367 UpdateFromSuperiorStore(); | 367 UpdateFromSuperiorStore(); |
| 368 } | 368 } |
| 369 | 369 |
| 370 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 370 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
| 371 DCHECK(CalledOnValidThread()); | 371 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 372 DCHECK_EQ(core_->client(), client); | 372 DCHECK_EQ(core_->client(), client); |
| 373 UpdateFromClient(); | 373 UpdateFromClient(); |
| 374 } | 374 } |
| 375 | 375 |
| 376 void ComponentCloudPolicyService::OnRegistrationStateChanged( | 376 void ComponentCloudPolicyService::OnRegistrationStateChanged( |
| 377 CloudPolicyClient* client) { | 377 CloudPolicyClient* client) { |
| 378 DCHECK(CalledOnValidThread()); | 378 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 379 // Ignored; the registration state is tracked by looking at the | 379 // Ignored; the registration state is tracked by looking at the |
| 380 // CloudPolicyStore instead. | 380 // CloudPolicyStore instead. |
| 381 } | 381 } |
| 382 | 382 |
| 383 void ComponentCloudPolicyService::OnClientError(CloudPolicyClient* client) { | 383 void ComponentCloudPolicyService::OnClientError(CloudPolicyClient* client) { |
| 384 DCHECK(CalledOnValidThread()); | 384 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 385 // Ignored. | 385 // Ignored. |
| 386 } | 386 } |
| 387 | 387 |
| 388 void ComponentCloudPolicyService::UpdateFromSuperiorStore() { | 388 void ComponentCloudPolicyService::UpdateFromSuperiorStore() { |
| 389 DCHECK(CalledOnValidThread()); | 389 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 390 | 390 |
| 391 DVLOG(2) << "Obtaining credentials from the superior policy store"; | 391 DVLOG(2) << "Obtaining credentials from the superior policy store"; |
| 392 | 392 |
| 393 const em::PolicyData* policy = core_->store()->policy(); | 393 const em::PolicyData* policy = core_->store()->policy(); |
| 394 if (!policy || !policy->has_username() || !policy->has_request_token()) { | 394 if (!policy || !policy->has_username() || !policy->has_request_token()) { |
| 395 // Clear the cache in case there is no policy or there are no credentials - | 395 // Clear the cache in case there is no policy or there are no credentials - |
| 396 // e.g. when the user signs out. | 396 // e.g. when the user signs out. |
| 397 backend_task_runner_->PostTask( | 397 backend_task_runner_->PostTask( |
| 398 FROM_HERE, | 398 FROM_HERE, |
| 399 base::Bind(&Backend::ClearCache, base::Unretained(backend_.get()))); | 399 base::Bind(&Backend::ClearCache, base::Unretained(backend_.get()))); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 416 } | 416 } |
| 417 | 417 |
| 418 // Initialize the backend to load the initial policy if not done yet, | 418 // Initialize the backend to load the initial policy if not done yet, |
| 419 // regardless of the signin state. | 419 // regardless of the signin state. |
| 420 backend_task_runner_->PostTask( | 420 backend_task_runner_->PostTask( |
| 421 FROM_HERE, | 421 FROM_HERE, |
| 422 base::Bind(&Backend::InitIfNeeded, base::Unretained(backend_.get()))); | 422 base::Bind(&Backend::InitIfNeeded, base::Unretained(backend_.get()))); |
| 423 } | 423 } |
| 424 | 424 |
| 425 void ComponentCloudPolicyService::UpdateFromClient() { | 425 void ComponentCloudPolicyService::UpdateFromClient() { |
| 426 DCHECK(CalledOnValidThread()); | 426 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 427 | 427 |
| 428 if (core_->client()->responses().empty()) { | 428 if (core_->client()->responses().empty()) { |
| 429 // The client's responses will be empty if it hasn't fetched policy from the | 429 // The client's responses will be empty if it hasn't fetched policy from the |
| 430 // DMServer yet. Make sure we don't purge the caches in this case. | 430 // DMServer yet. Make sure we don't purge the caches in this case. |
| 431 return; | 431 return; |
| 432 } | 432 } |
| 433 | 433 |
| 434 DVLOG(2) << "Obtaining fetched policies from the policy client"; | 434 DVLOG(2) << "Obtaining fetched policies from the policy client"; |
| 435 | 435 |
| 436 std::unique_ptr<ScopedResponseMap> valid_responses = | 436 std::unique_ptr<ScopedResponseMap> valid_responses = |
| 437 base::MakeUnique<ScopedResponseMap>(); | 437 base::MakeUnique<ScopedResponseMap>(); |
| 438 for (const auto& response : core_->client()->responses()) { | 438 for (const auto& response : core_->client()->responses()) { |
| 439 PolicyNamespace ns; | 439 PolicyNamespace ns; |
| 440 if (!ToPolicyNamespace(response.first, &ns)) { | 440 if (!ToPolicyNamespace(response.first, &ns)) { |
| 441 DVLOG(1) << "Ignored policy with type = " << response.first.first; | 441 DVLOG(1) << "Ignored policy with type = " << response.first.first; |
| 442 continue; | 442 continue; |
| 443 } | 443 } |
| 444 (*valid_responses)[ns] = | 444 (*valid_responses)[ns] = |
| 445 base::MakeUnique<em::PolicyFetchResponse>(*response.second); | 445 base::MakeUnique<em::PolicyFetchResponse>(*response.second); |
| 446 } | 446 } |
| 447 | 447 |
| 448 backend_task_runner_->PostTask( | 448 backend_task_runner_->PostTask( |
| 449 FROM_HERE, | 449 FROM_HERE, |
| 450 base::Bind(&Backend::SetFetchedPolicy, base::Unretained(backend_.get()), | 450 base::Bind(&Backend::SetFetchedPolicy, base::Unretained(backend_.get()), |
| 451 base::Passed(&valid_responses))); | 451 base::Passed(&valid_responses))); |
| 452 } | 452 } |
| 453 | 453 |
| 454 void ComponentCloudPolicyService::UpdateFromSchemaRegistry() { | 454 void ComponentCloudPolicyService::UpdateFromSchemaRegistry() { |
| 455 DCHECK(CalledOnValidThread()); | 455 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 456 if (!schema_registry_->IsReady()) { | 456 if (!schema_registry_->IsReady()) { |
| 457 // Ignore notifications from the registry which is not ready yet. | 457 // Ignore notifications from the registry which is not ready yet. |
| 458 return; | 458 return; |
| 459 } | 459 } |
| 460 DVLOG(2) << "Updating schema map"; | 460 DVLOG(2) << "Updating schema map"; |
| 461 current_schema_map_ = schema_registry_->schema_map(); | 461 current_schema_map_ = schema_registry_->schema_map(); |
| 462 FilterAndInstallPolicy(); | 462 FilterAndInstallPolicy(); |
| 463 } | 463 } |
| 464 | 464 |
| 465 void ComponentCloudPolicyService::Disconnect() { | 465 void ComponentCloudPolicyService::Disconnect() { |
| 466 DCHECK(CalledOnValidThread()); | 466 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 467 | 467 |
| 468 core_->client()->RemoveObserver(this); | 468 core_->client()->RemoveObserver(this); |
| 469 | 469 |
| 470 // Unregister the policy domain from being downloaded in the future policy | 470 // Unregister the policy domain from being downloaded in the future policy |
| 471 // fetches. | 471 // fetches. |
| 472 core_->client()->RemovePolicyTypeToFetch( | 472 core_->client()->RemovePolicyTypeToFetch( |
| 473 policy_type_, std::string() /* settings_entity_id */); | 473 policy_type_, std::string() /* settings_entity_id */); |
| 474 } | 474 } |
| 475 | 475 |
| 476 void ComponentCloudPolicyService::SetPolicy( | 476 void ComponentCloudPolicyService::SetPolicy( |
| 477 std::unique_ptr<PolicyBundle> policy) { | 477 std::unique_ptr<PolicyBundle> policy) { |
| 478 DCHECK(CalledOnValidThread()); | 478 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 479 | 479 |
| 480 // Store the current unfiltered policies. | 480 // Store the current unfiltered policies. |
| 481 unfiltered_policy_ = std::move(policy); | 481 unfiltered_policy_ = std::move(policy); |
| 482 | 482 |
| 483 FilterAndInstallPolicy(); | 483 FilterAndInstallPolicy(); |
| 484 } | 484 } |
| 485 | 485 |
| 486 void ComponentCloudPolicyService::FilterAndInstallPolicy() { | 486 void ComponentCloudPolicyService::FilterAndInstallPolicy() { |
| 487 DCHECK(CalledOnValidThread()); | 487 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 488 | 488 |
| 489 if (!unfiltered_policy_ || !current_schema_map_) | 489 if (!unfiltered_policy_ || !current_schema_map_) |
| 490 return; | 490 return; |
| 491 | 491 |
| 492 // Make a copy in |policy_| and filter it and validate against the schemas; | 492 // Make a copy in |policy_| and filter it and validate against the schemas; |
| 493 // this is what's passed to the outside world. | 493 // this is what's passed to the outside world. |
| 494 policy_.CopyFrom(*unfiltered_policy_); | 494 policy_.CopyFrom(*unfiltered_policy_); |
| 495 current_schema_map_->FilterBundle(&policy_); | 495 current_schema_map_->FilterBundle(&policy_); |
| 496 | 496 |
| 497 policy_installed_ = true; | 497 policy_installed_ = true; |
| 498 DVLOG(1) << "Installed policy (count = " | 498 DVLOG(1) << "Installed policy (count = " |
| 499 << std::distance(policy_.begin(), policy_.end()) << ")"; | 499 << std::distance(policy_.begin(), policy_.end()) << ")"; |
| 500 delegate_->OnComponentCloudPolicyUpdated(); | 500 delegate_->OnComponentCloudPolicyUpdated(); |
| 501 } | 501 } |
| 502 | 502 |
| 503 } // namespace policy | 503 } // namespace policy |
| OLD | NEW |