This source file includes following definitions.
- NotInSchemaMap
- ToPolicyNamespaceKey
- ToPolicyNamespace
- store_
- SetCredentials
- Init
- UpdateExternalPolicy
- OnComponentCloudPolicyStoreUpdated
- OnSchemasUpdated
- weak_ptr_factory_
- SupportsDomain
- ClearCache
- OnSchemaRegistryReady
- OnSchemaRegistryUpdated
- OnCoreConnected
- OnCoreDisconnecting
- OnRefreshSchedulerStarted
- OnStoreLoaded
- OnStoreError
- OnPolicyFetched
- OnRegistrationStateChanged
- OnClientError
- InitializeIfReady
- OnBackendInitialized
- SetCurrentSchema
- OnPolicyUpdated
#include "components/policy/core/common/cloud/component_cloud_policy_service.h"
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/sequenced_task_runner.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
#include "components/policy/core/common/cloud/component_cloud_policy_store.h"
#include "components/policy/core/common/cloud/component_cloud_policy_updater.h"
#include "components/policy/core/common/cloud/external_policy_data_fetcher.h"
#include "components/policy/core/common/cloud/resource_cache.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/core/common/schema_map.h"
#include "net/url_request/url_request_context_getter.h"
#include "policy/proto/device_management_backend.pb.h"
namespace em = enterprise_management;
namespace policy {
namespace {
bool NotInSchemaMap(const scoped_refptr<SchemaMap> schema_map,
PolicyDomain domain,
const std::string& component_id) {
return schema_map->GetSchema(PolicyNamespace(domain, component_id)) == NULL;
}
bool ToPolicyNamespaceKey(const PolicyNamespace& ns, PolicyNamespaceKey* key) {
if (!ComponentCloudPolicyStore::GetPolicyType(ns.domain, &key->first))
return false;
key->second = ns.component_id;
return true;
}
bool ToPolicyNamespace(const PolicyNamespaceKey& key, PolicyNamespace* ns) {
if (!ComponentCloudPolicyStore::GetPolicyDomain(key.first, &ns->domain))
return false;
ns->component_id = key.second;
return true;
}
}
ComponentCloudPolicyService::Delegate::~Delegate() {}
class ComponentCloudPolicyService::Backend
: public ComponentCloudPolicyStore::Delegate {
public:
Backend(base::WeakPtr<ComponentCloudPolicyService> service,
scoped_refptr<base::SequencedTaskRunner> task_runner,
scoped_refptr<base::SequencedTaskRunner> service_task_runner,
scoped_ptr<ResourceCache> cache,
scoped_ptr<ExternalPolicyDataFetcher> external_policy_data_fetcher);
virtual ~Backend();
void SetCredentials(const std::string& username, const std::string& dm_token);
void Init(scoped_refptr<SchemaMap> schema_map);
void UpdateExternalPolicy(scoped_ptr<em::PolicyFetchResponse> response);
virtual void OnComponentCloudPolicyStoreUpdated() OVERRIDE;
void OnSchemasUpdated(scoped_refptr<SchemaMap> schema_map,
scoped_ptr<PolicyNamespaceList> removed);
private:
base::WeakPtr<ComponentCloudPolicyService> service_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
scoped_refptr<base::SequencedTaskRunner> service_task_runner_;
scoped_ptr<ResourceCache> cache_;
scoped_ptr<ExternalPolicyDataFetcher> external_policy_data_fetcher_;
ComponentCloudPolicyStore store_;
scoped_ptr<ComponentCloudPolicyUpdater> updater_;
scoped_refptr<SchemaMap> schema_map_;
DISALLOW_COPY_AND_ASSIGN(Backend);
};
ComponentCloudPolicyService::Backend::Backend(
base::WeakPtr<ComponentCloudPolicyService> service,
scoped_refptr<base::SequencedTaskRunner> task_runner,
scoped_refptr<base::SequencedTaskRunner> service_task_runner,
scoped_ptr<ResourceCache> cache,
scoped_ptr<ExternalPolicyDataFetcher> external_policy_data_fetcher)
: service_(service),
task_runner_(task_runner),
service_task_runner_(service_task_runner),
cache_(cache.Pass()),
external_policy_data_fetcher_(external_policy_data_fetcher.Pass()),
store_(this, cache_.get()) {}
ComponentCloudPolicyService::Backend::~Backend() {}
void ComponentCloudPolicyService::Backend::SetCredentials(
const std::string& username,
const std::string& dm_token) {
if (username.empty() || dm_token.empty()) {
store_.Clear();
} else {
store_.SetCredentials(username, dm_token);
}
}
void ComponentCloudPolicyService::Backend::Init(
scoped_refptr<SchemaMap> schema_map) {
DCHECK(!schema_map_);
OnSchemasUpdated(schema_map, scoped_ptr<PolicyNamespaceList>());
store_.Load();
scoped_ptr<PolicyBundle> bundle(new PolicyBundle);
bundle->CopyFrom(store_.policy());
schema_map_->FilterBundle(bundle.get());
updater_.reset(new ComponentCloudPolicyUpdater(
task_runner_, external_policy_data_fetcher_.Pass(), &store_));
service_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ComponentCloudPolicyService::OnBackendInitialized,
service_,
base::Passed(&bundle)));
}
void ComponentCloudPolicyService::Backend::UpdateExternalPolicy(
scoped_ptr<em::PolicyFetchResponse> response) {
updater_->UpdateExternalPolicy(response.Pass());
}
void ComponentCloudPolicyService::Backend::
OnComponentCloudPolicyStoreUpdated() {
if (!schema_map_) {
return;
}
scoped_ptr<PolicyBundle> bundle(new PolicyBundle);
bundle->CopyFrom(store_.policy());
schema_map_->FilterBundle(bundle.get());
service_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ComponentCloudPolicyService::OnPolicyUpdated,
service_,
base::Passed(&bundle)));
}
void ComponentCloudPolicyService::Backend::OnSchemasUpdated(
scoped_refptr<SchemaMap> schema_map,
scoped_ptr<PolicyNamespaceList> removed) {
const DomainMap& domains = schema_map->GetDomains();
for (DomainMap::const_iterator domain = domains.begin();
domain != domains.end(); ++domain) {
store_.Purge(domain->first,
base::Bind(&NotInSchemaMap, schema_map, domain->first));
}
schema_map_ = schema_map;
if (removed) {
for (size_t i = 0; i < removed->size(); ++i)
updater_->CancelUpdate((*removed)[i]);
}
}
ComponentCloudPolicyService::ComponentCloudPolicyService(
Delegate* delegate,
SchemaRegistry* schema_registry,
CloudPolicyCore* core,
scoped_ptr<ResourceCache> cache,
scoped_refptr<net::URLRequestContextGetter> request_context,
scoped_refptr<base::SequencedTaskRunner> backend_task_runner,
scoped_refptr<base::SequencedTaskRunner> io_task_runner)
: delegate_(delegate),
schema_registry_(schema_registry),
core_(core),
request_context_(request_context),
backend_task_runner_(backend_task_runner),
io_task_runner_(io_task_runner),
current_schema_map_(new SchemaMap),
started_loading_initial_policy_(false),
loaded_initial_policy_(false),
is_registered_for_cloud_policy_(false),
weak_ptr_factory_(this) {
external_policy_data_fetcher_backend_.reset(
new ExternalPolicyDataFetcherBackend(io_task_runner_, request_context));
backend_.reset(
new Backend(weak_ptr_factory_.GetWeakPtr(),
backend_task_runner_,
base::MessageLoopProxy::current(),
cache.Pass(),
external_policy_data_fetcher_backend_->CreateFrontend(
backend_task_runner_)));
schema_registry_->AddObserver(this);
core_->store()->AddObserver(this);
if (core_->store()->is_initialized())
OnStoreLoaded(core_->store());
}
ComponentCloudPolicyService::~ComponentCloudPolicyService() {
DCHECK(CalledOnValidThread());
schema_registry_->RemoveObserver(this);
core_->store()->RemoveObserver(this);
core_->RemoveObserver(this);
if (core_->client())
OnCoreDisconnecting(core_);
io_task_runner_->DeleteSoon(FROM_HERE,
external_policy_data_fetcher_backend_.release());
backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release());
}
bool ComponentCloudPolicyService::SupportsDomain(PolicyDomain domain) {
return ComponentCloudPolicyStore::SupportsDomain(domain);
}
void ComponentCloudPolicyService::ClearCache() {
DCHECK(CalledOnValidThread());
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&Backend::SetCredentials,
base::Unretained(backend_.get()),
std::string(), std::string()));
}
void ComponentCloudPolicyService::OnSchemaRegistryReady() {
DCHECK(CalledOnValidThread());
InitializeIfReady();
}
void ComponentCloudPolicyService::OnSchemaRegistryUpdated(
bool has_new_schemas) {
DCHECK(CalledOnValidThread());
if (!loaded_initial_policy_)
return;
SetCurrentSchema();
}
void ComponentCloudPolicyService::OnCoreConnected(CloudPolicyCore* core) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(core_, core);
core_->client()->AddObserver(this);
OnPolicyFetched(core_->client());
current_schema_map_ = new SchemaMap();
SetCurrentSchema();
}
void ComponentCloudPolicyService::OnCoreDisconnecting(CloudPolicyCore* core) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(core_, core);
core_->client()->RemoveObserver(this);
scoped_refptr<SchemaMap> empty = new SchemaMap();
PolicyNamespaceList removed;
PolicyNamespaceList added;
empty->GetChanges(current_schema_map_, &removed, &added);
for (size_t i = 0; i < removed.size(); ++i) {
PolicyNamespaceKey key;
if (ToPolicyNamespaceKey(removed[i], &key))
core_->client()->RemoveNamespaceToFetch(key);
}
}
void ComponentCloudPolicyService::OnRefreshSchedulerStarted(
CloudPolicyCore* core) {
}
void ComponentCloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(core_->store(), store);
const bool was_registered_before = is_registered_for_cloud_policy_;
const em::PolicyData* policy = core_->store()->policy();
std::string username;
std::string request_token;
if (policy && policy->has_username() && policy->has_request_token()) {
is_registered_for_cloud_policy_ = true;
username = policy->username();
request_token = policy->request_token();
} else {
is_registered_for_cloud_policy_ = false;
}
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&Backend::SetCredentials,
base::Unretained(backend_.get()),
username,
request_token));
if (!loaded_initial_policy_) {
InitializeIfReady();
} else if (!was_registered_before && is_registered_for_cloud_policy_) {
if (core_->client())
OnPolicyFetched(core_->client());
}
}
void ComponentCloudPolicyService::OnStoreError(CloudPolicyStore* store) {
DCHECK(CalledOnValidThread());
OnStoreLoaded(store);
}
void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(core_->client(), client);
if (!is_registered_for_cloud_policy_) {
return;
}
const CloudPolicyClient::ResponseMap& responses =
core_->client()->responses();
for (CloudPolicyClient::ResponseMap::const_iterator it = responses.begin();
it != responses.end(); ++it) {
PolicyNamespace ns;
if (ToPolicyNamespace(it->first, &ns) &&
current_schema_map_->GetSchema(ns)) {
scoped_ptr<em::PolicyFetchResponse> response(
new em::PolicyFetchResponse(*it->second));
backend_task_runner_->PostTask(
FROM_HERE,
base::Bind(&Backend::UpdateExternalPolicy,
base::Unretained(backend_.get()),
base::Passed(&response)));
}
}
}
void ComponentCloudPolicyService::OnRegistrationStateChanged(
CloudPolicyClient* client) {
DCHECK(CalledOnValidThread());
}
void ComponentCloudPolicyService::OnClientError(CloudPolicyClient* client) {
DCHECK(CalledOnValidThread());
}
void ComponentCloudPolicyService::InitializeIfReady() {
DCHECK(CalledOnValidThread());
if (started_loading_initial_policy_ || !schema_registry_->IsReady() ||
!core_->store()->is_initialized()) {
return;
}
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&Backend::Init,
base::Unretained(backend_.get()),
schema_registry_->schema_map()));
started_loading_initial_policy_ = true;
}
void ComponentCloudPolicyService::OnBackendInitialized(
scoped_ptr<PolicyBundle> initial_policy) {
DCHECK(CalledOnValidThread());
DCHECK(!loaded_initial_policy_);
loaded_initial_policy_ = true;
OnPolicyUpdated(initial_policy.Pass());
core_->AddObserver(this);
if (core_->client()) {
OnCoreConnected(core_);
} else {
SetCurrentSchema();
}
}
void ComponentCloudPolicyService::SetCurrentSchema() {
DCHECK(CalledOnValidThread());
scoped_ptr<PolicyNamespaceList> removed(new PolicyNamespaceList);
PolicyNamespaceList added;
const scoped_refptr<SchemaMap>& new_schema_map =
schema_registry_->schema_map();
new_schema_map->GetChanges(current_schema_map_, removed.get(), &added);
current_schema_map_ = new_schema_map;
if (core_->client()) {
for (size_t i = 0; i < removed->size(); ++i) {
PolicyNamespaceKey key;
if (ToPolicyNamespaceKey((*removed)[i], &key))
core_->client()->RemoveNamespaceToFetch(key);
}
bool added_namespaces_to_client = false;
for (size_t i = 0; i < added.size(); ++i) {
PolicyNamespaceKey key;
if (ToPolicyNamespaceKey(added[i], &key)) {
core_->client()->AddNamespaceToFetch(key);
added_namespaces_to_client = true;
}
}
if (added_namespaces_to_client)
core_->RefreshSoon();
}
backend_task_runner_->PostTask(FROM_HERE,
base::Bind(&Backend::OnSchemasUpdated,
base::Unretained(backend_.get()),
current_schema_map_,
base::Passed(&removed)));
}
void ComponentCloudPolicyService::OnPolicyUpdated(
scoped_ptr<PolicyBundle> policy) {
DCHECK(CalledOnValidThread());
policy_.Swap(policy.get());
delegate_->OnComponentCloudPolicyUpdated();
}
}