Cleanup: Only build extensions renderer code when extensions are enabled.
[chromium-blink-merge.git] / components / ownership / owner_settings_service.cc
blob09c88d372dc89867f0b6ed45f2772211c94e44bd
1 // Copyright 2014 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.
5 #include "components/ownership/owner_settings_service.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/task_runner.h"
14 #include "base/task_runner_util.h"
15 #include "components/ownership/owner_key_util.h"
16 #include "crypto/signature_creator.h"
18 namespace em = enterprise_management;
20 namespace ownership {
22 namespace {
24 std::string AssembleAndSignPolicy(scoped_ptr<em::PolicyData> policy,
25 crypto::RSAPrivateKey* private_key) {
26 // Assemble the policy.
27 em::PolicyFetchResponse policy_response;
28 if (!policy->SerializeToString(policy_response.mutable_policy_data())) {
29 LOG(ERROR) << "Failed to encode policy payload.";
30 return std::string();
33 // Generate the signature.
34 scoped_ptr<crypto::SignatureCreator> signature_creator(
35 crypto::SignatureCreator::Create(private_key));
36 signature_creator->Update(
37 reinterpret_cast<const uint8*>(policy_response.policy_data().c_str()),
38 policy_response.policy_data().size());
39 std::vector<uint8> signature_bytes;
40 std::string policy_blob;
41 if (!signature_creator->Final(&signature_bytes)) {
42 LOG(ERROR) << "Failed to create policy signature.";
43 return std::string();
46 policy_response.mutable_policy_data_signature()->assign(
47 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)),
48 signature_bytes.size());
49 return policy_response.SerializeAsString();
52 } // namepace
54 OwnerSettingsService::OwnerSettingsService(
55 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util)
56 : owner_key_util_(owner_key_util), weak_factory_(this) {
59 OwnerSettingsService::~OwnerSettingsService() {
60 DCHECK(thread_checker_.CalledOnValidThread());
63 bool OwnerSettingsService::IsOwner() {
64 DCHECK(thread_checker_.CalledOnValidThread());
65 return private_key_.get() && private_key_->key();
68 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) {
69 DCHECK(thread_checker_.CalledOnValidThread());
70 if (private_key_.get()) {
71 base::MessageLoop::current()->PostTask(FROM_HERE,
72 base::Bind(callback, IsOwner()));
73 } else {
74 pending_is_owner_callbacks_.push_back(callback);
78 bool OwnerSettingsService::AssembleAndSignPolicyAsync(
79 base::TaskRunner* task_runner,
80 scoped_ptr<em::PolicyData> policy,
81 const AssembleAndSignPolicyAsyncCallback& callback) {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 if (!task_runner || !IsOwner())
84 return false;
85 return base::PostTaskAndReplyWithResult(
86 task_runner,
87 FROM_HERE,
88 base::Bind(
89 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()),
90 callback);
93 void OwnerSettingsService::ReloadKeypair() {
94 ReloadKeypairImpl(
95 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr()));
98 void OwnerSettingsService::OnKeypairLoaded(
99 const scoped_refptr<PublicKey>& public_key,
100 const scoped_refptr<PrivateKey>& private_key) {
101 DCHECK(thread_checker_.CalledOnValidThread());
103 public_key_ = public_key;
104 private_key_ = private_key;
106 const bool is_owner = IsOwner();
107 std::vector<IsOwnerCallback> is_owner_callbacks;
108 is_owner_callbacks.swap(pending_is_owner_callbacks_);
109 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin());
110 it != is_owner_callbacks.end();
111 ++it) {
112 it->Run(is_owner);
115 OnPostKeypairLoadedActions();
118 } // namespace ownership