OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/login/signed_settings_cache.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/login/ownership_service.h" |
| 11 #include "chrome/browser/chromeos/login/signed_settings_helper.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 namespace { |
| 21 |
| 22 void OnStorePolicyCompleted(SignedSettings::ReturnCode code) { |
| 23 if (code != SignedSettings::SUCCESS) |
| 24 LOG(ERROR) << "Couldn't save temp store to the policy blob. code: " << code; |
| 25 } |
| 26 |
| 27 void FinishFinalize(PrefService* local_state, |
| 28 SignedSettings::ReturnCode code, |
| 29 const em::PolicyFetchResponse& policy) { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 31 |
| 32 if (code != SignedSettings::SUCCESS) { |
| 33 LOG(ERROR) << "Can't finalize temp store error code:" << code; |
| 34 return; |
| 35 } |
| 36 |
| 37 if (local_state) { |
| 38 std::string encoded = |
| 39 local_state->GetString(prefs::kSignedSettingsCache); |
| 40 std::string policy_string; |
| 41 if (!base::Base64Decode(encoded, &policy_string)) { |
| 42 LOG(WARNING) << "Can't decode policy from base64 on finalizing."; |
| 43 return; |
| 44 } |
| 45 |
| 46 em::PolicyData merging_policy_data; |
| 47 if (!merging_policy_data.ParseFromString(policy_string)) { |
| 48 LOG(WARNING) << "Can't decode policy from string on finalizing."; |
| 49 return; |
| 50 } |
| 51 |
| 52 em::PolicyFetchResponse policy_envelope = policy; |
| 53 DCHECK(policy_envelope.has_policy_data()); |
| 54 em::PolicyData base_policy_data; |
| 55 base_policy_data.ParseFromString(policy_envelope.policy_data()); |
| 56 // Merge only the policy value as we should never ever rewrite the other |
| 57 // fields of the PolicyData protobuf. |
| 58 base_policy_data.set_policy_value(merging_policy_data.policy_value()); |
| 59 policy_envelope.set_policy_data(base_policy_data.SerializeAsString()); |
| 60 DCHECK(base_policy_data.has_username()); |
| 61 policy_envelope.clear_policy_data_signature(); |
| 62 SignedSettingsHelper::Get()->StartStorePolicyOp( |
| 63 policy_envelope, base::Bind(&OnStorePolicyCompleted)); |
| 64 } |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 69 namespace signed_settings_cache { |
| 70 |
| 71 void RegisterPrefs(PrefService* local_state) { |
| 72 local_state->RegisterStringPref(prefs::kSignedSettingsCache, |
| 73 "invalid", |
| 74 PrefService::UNSYNCABLE_PREF); |
| 75 } |
| 76 |
| 77 bool Store(const em::PolicyData& policy, PrefService* local_state) { |
| 78 if (local_state) { |
| 79 std::string policy_string = policy.SerializeAsString(); |
| 80 std::string encoded; |
| 81 if (!base::Base64Encode(policy_string, &encoded)) { |
| 82 LOG(WARNING) << "Can't encode policy in base64."; |
| 83 return false; |
| 84 } |
| 85 local_state->SetString(prefs::kSignedSettingsCache, encoded); |
| 86 return true; |
| 87 } |
| 88 return false; |
| 89 } |
| 90 |
| 91 bool Retrieve(em::PolicyData *policy, PrefService* local_state) { |
| 92 if (local_state) { |
| 93 std::string encoded = |
| 94 local_state->GetString(prefs::kSignedSettingsCache); |
| 95 std::string policy_string; |
| 96 if (!base::Base64Decode(encoded, &policy_string)) { |
| 97 LOG(WARNING) << "Can't decode policy from base64."; |
| 98 return false; |
| 99 } |
| 100 return policy->ParseFromString(policy_string); |
| 101 } |
| 102 return false; |
| 103 } |
| 104 |
| 105 void Finalize(PrefService* local_state) { |
| 106 // Reload the initial policy blob, apply settings from temp storage, |
| 107 // and write back the blob. |
| 108 SignedSettingsHelper::Get()->StartRetrievePolicyOp( |
| 109 base::Bind(FinishFinalize, local_state)); |
| 110 } |
| 111 |
| 112 } // namespace signed_settings_cache |
| 113 |
| 114 } // namespace chromeos |
OLD | NEW |