1// Copyright 2013 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 "components/policy/core/common/cloud/user_cloud_policy_store_base.h"
6
7#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
8#include "components/policy/core/common/cloud/cloud_policy_constants.h"
9#include "components/policy/core/common/policy_map.h"
10#include "policy/proto/cloud_policy.pb.h"
11
12namespace policy {
13
14// Decodes a CloudPolicySettings object into a policy map. The implementation is
15// generated code in policy/cloud_policy_generated.cc.
16void DecodePolicy(const enterprise_management::CloudPolicySettings& policy,
17                  base::WeakPtr<CloudExternalDataManager> external_data_manager,
18                  PolicyMap* policies);
19
20UserCloudPolicyStoreBase::UserCloudPolicyStoreBase(
21    scoped_refptr<base::SequencedTaskRunner> background_task_runner)
22    : background_task_runner_(background_task_runner) {}
23
24UserCloudPolicyStoreBase::~UserCloudPolicyStoreBase() {
25}
26
27scoped_ptr<UserCloudPolicyValidator> UserCloudPolicyStoreBase::CreateValidator(
28    scoped_ptr<enterprise_management::PolicyFetchResponse> policy,
29    CloudPolicyValidatorBase::ValidateTimestampOption timestamp_option) {
30  // Configure the validator.
31  UserCloudPolicyValidator* validator =
32      UserCloudPolicyValidator::Create(policy.Pass(), background_task_runner_);
33  validator->ValidatePolicyType(GetChromeUserPolicyType());
34  validator->ValidateAgainstCurrentPolicy(
35      policy_.get(),
36      timestamp_option,
37      CloudPolicyValidatorBase::DM_TOKEN_REQUIRED);
38  validator->ValidatePayload();
39  return scoped_ptr<UserCloudPolicyValidator>(validator);
40}
41
42void UserCloudPolicyStoreBase::InstallPolicy(
43    scoped_ptr<enterprise_management::PolicyData> policy_data,
44    scoped_ptr<enterprise_management::CloudPolicySettings> payload) {
45  // Decode the payload.
46  policy_map_.Clear();
47  DecodePolicy(*payload, external_data_manager(), &policy_map_);
48  policy_ = policy_data.Pass();
49}
50
51}  // namespace policy
52