Chromium Code Reviews| 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 } // namespace | |
| 28 | |
| 29 // static | |
| 30 void SignedSettingsCache::RegisterPrefs(PrefService* local_state) { | |
| 31 local_state->RegisterStringPref(prefs::kSignedSettingsCache, | |
| 32 "invalid", | |
| 33 PrefService::UNSYNCABLE_PREF); | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 bool SignedSettingsCache::Store(const em::PolicyData& policy, | |
| 38 PrefService* local_state) { | |
| 39 if (local_state) { | |
| 40 std::string policy_string = policy.SerializeAsString(); | |
| 41 std::string encoded; | |
| 42 if (!base::Base64Encode(policy_string, &encoded)) { | |
|
Mattias Nissler (ping if slow)
2011/11/30 11:22:44
No need to encode stuff, JSON can handle binary st
pastarmovj
2011/11/30 17:21:16
Done.
| |
| 43 LOG(WARNING) << "Can't encode policy in base64."; | |
| 44 return false; | |
| 45 } | |
| 46 local_state->SetString(prefs::kSignedSettingsCache, encoded); | |
| 47 return true; | |
| 48 } | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 // static | |
| 53 bool SignedSettingsCache::Retrieve(em::PolicyData *policy, | |
| 54 PrefService* local_state) { | |
| 55 if (local_state) { | |
| 56 std::string encoded = | |
| 57 local_state->GetString(prefs::kSignedSettingsCache); | |
| 58 std::string policy_string; | |
| 59 if (!base::Base64Decode(encoded, &policy_string)) { | |
| 60 LOG(WARNING) << "Can't decode policy from base64."; | |
| 61 return false; | |
| 62 } | |
| 63 return policy->ParseFromString(policy_string); | |
| 64 } | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 // static | |
| 69 void SignedSettingsCache::Finalize(PrefService* local_state, | |
| 70 const em::PolicyFetchResponse& policy) { | |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 72 if (local_state) { | |
| 73 std::string encoded = | |
| 74 local_state->GetString(prefs::kSignedSettingsCache); | |
| 75 std::string policy_string; | |
| 76 if (!base::Base64Decode(encoded, &policy_string)) { | |
| 77 LOG(WARNING) << "Can't decode policy from base64 on finalizing."; | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 em::PolicyData merging_policy_data; | |
| 82 if (!merging_policy_data.ParseFromString(policy_string)) { | |
| 83 LOG(WARNING) << "Can't decode policy from string on finalizing."; | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 em::PolicyFetchResponse policy_envelope = policy; | |
| 88 DCHECK(policy_envelope.has_policy_data()); | |
| 89 em::PolicyData base_policy_data; | |
| 90 base_policy_data.ParseFromString(policy_envelope.policy_data()); | |
| 91 // Merge only the policy value as we should never ever rewrite the other | |
| 92 // fields of the PolicyData protobuf. | |
| 93 base_policy_data.set_policy_value(merging_policy_data.policy_value()); | |
| 94 policy_envelope.set_policy_data(base_policy_data.SerializeAsString()); | |
| 95 DCHECK(base_policy_data.has_username()); | |
| 96 policy_envelope.clear_policy_data_signature(); | |
| 97 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 98 policy_envelope, base::Bind(&OnStorePolicyCompleted)); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 } // namespace chromeos | |
| OLD | NEW |