Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/signed_settings_cache.h" | 5 #include "chrome/browser/chromeos/login/signed_settings_cache.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "chrome/browser/chromeos/cros_settings.h" | 11 #include "chrome/browser/chromeos/cros_settings.h" |
| 12 #include "chrome/browser/chromeos/login/ownership_service.h" | |
| 13 #include "chrome/browser/chromeos/login/signed_settings_helper.h" | |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 12 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 17 | 15 |
| 18 using content::BrowserThread; | |
| 19 | |
| 20 namespace em = enterprise_management; | 16 namespace em = enterprise_management; |
| 21 | 17 |
| 22 namespace chromeos { | 18 namespace chromeos { |
| 23 | 19 |
| 24 namespace { | |
| 25 | |
| 26 void OnStorePolicyCompleted(SignedSettings::ReturnCode code) { | |
|
pastarmovj
2012/07/30 13:55:02
2 tests come to my mind to verify that this here w
Mattias Nissler (ping if slow)
2012/08/02 12:01:52
I confirmed that both the cache and migration work
| |
| 27 if (code != SignedSettings::SUCCESS) | |
| 28 LOG(ERROR) << "Couldn't save temp store to the policy blob. code: " << code; | |
| 29 else | |
| 30 CrosSettings::Get()->ReloadProviders(); | |
| 31 } | |
| 32 | |
| 33 void FinishFinalize(PrefService* local_state, | |
| 34 SignedSettings::ReturnCode code, | |
| 35 const em::PolicyFetchResponse& policy) { | |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 37 | |
| 38 if (code != SignedSettings::SUCCESS) { | |
| 39 LOG(ERROR) << "Can't finalize temp store error code:" << code; | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 if (local_state) { | |
| 44 std::string encoded = | |
| 45 local_state->GetString(prefs::kSignedSettingsCache); | |
| 46 std::string policy_string; | |
| 47 if (!base::Base64Decode(encoded, &policy_string)) { | |
| 48 LOG(ERROR) << "Can't decode policy from base64 on finalizing."; | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 em::PolicyData merging_policy_data; | |
| 53 if (!merging_policy_data.ParseFromString(policy_string)) { | |
| 54 LOG(ERROR) << "Can't decode policy from string on finalizing."; | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 em::PolicyFetchResponse policy_envelope = policy; | |
| 59 DCHECK(policy_envelope.has_policy_data()); | |
| 60 em::PolicyData base_policy_data; | |
| 61 base_policy_data.ParseFromString(policy_envelope.policy_data()); | |
| 62 // Merge only the policy value as we should never ever rewrite the other | |
| 63 // fields of the PolicyData protobuf. | |
| 64 base_policy_data.set_policy_value(merging_policy_data.policy_value()); | |
| 65 policy_envelope.set_policy_data(base_policy_data.SerializeAsString()); | |
| 66 DCHECK(base_policy_data.has_username()); | |
| 67 policy_envelope.clear_policy_data_signature(); | |
| 68 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 69 policy_envelope, base::Bind(&OnStorePolicyCompleted)); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // Reload the initial policy blob, and if successful apply the settings from | |
| 74 // temp storage, and write them back the blob in FinishFinalize. | |
| 75 void ReloadSignedSettingsAndFinalize( | |
| 76 PrefService* local_state, | |
| 77 OwnershipService::Status status, | |
| 78 bool current_user_is_owner) { | |
| 79 if (current_user_is_owner) { | |
| 80 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 81 base::Bind(FinishFinalize, local_state)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 namespace signed_settings_cache { | 20 namespace signed_settings_cache { |
| 88 | 21 |
| 89 void RegisterPrefs(PrefService* local_state) { | 22 void RegisterPrefs(PrefService* local_state) { |
| 90 local_state->RegisterStringPref(prefs::kSignedSettingsCache, | 23 local_state->RegisterStringPref(prefs::kSignedSettingsCache, |
| 91 "invalid", | 24 "invalid", |
| 92 PrefService::UNSYNCABLE_PREF); | 25 PrefService::UNSYNCABLE_PREF); |
| 93 } | 26 } |
| 94 | 27 |
| 95 bool Store(const em::PolicyData& policy, PrefService* local_state) { | 28 bool Store(const em::PolicyData& policy, PrefService* local_state) { |
| 96 if (local_state) { | 29 if (local_state) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 114 if (!base::Base64Decode(encoded, &policy_string)) { | 47 if (!base::Base64Decode(encoded, &policy_string)) { |
| 115 // This is normal and happens on first boot. | 48 // This is normal and happens on first boot. |
| 116 VLOG(1) << "Can't decode policy from base64."; | 49 VLOG(1) << "Can't decode policy from base64."; |
| 117 return false; | 50 return false; |
| 118 } | 51 } |
| 119 return policy->ParseFromString(policy_string); | 52 return policy->ParseFromString(policy_string); |
| 120 } | 53 } |
| 121 return false; | 54 return false; |
| 122 } | 55 } |
| 123 | 56 |
| 124 void Finalize(PrefService* local_state) { | |
| 125 // First we have to make sure the owner is really logged in because the key | |
| 126 // notification is generated on every cloud policy key rotation too. | |
| 127 OwnershipService::GetSharedInstance()->GetStatusAsync( | |
| 128 base::Bind(&ReloadSignedSettingsAndFinalize, local_state)); | |
| 129 } | |
| 130 | |
| 131 } // namespace signed_settings_cache | 57 } // namespace signed_settings_cache |
| 132 | 58 |
| 133 } // namespace chromeos | 59 } // namespace chromeos |
| OLD | NEW |