device_settings_cache.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
19e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com// Copyright (c) 2012 The Chromium Authors. All rights reserved.
29e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com// Use of this source code is governed by a BSD-style license that can be
39e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com// found in the LICENSE file.
49e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com
59e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com#include "chrome/browser/chromeos/settings/device_settings_cache.h"
69e49fb63d355446b91d20ff78ad78b297e89a50dcaryclark@google.com
7d88e0894d0156f4d427b812fec69bfba3eec7a8dcaryclark@google.com#include <string>
878e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com
903f970652e07c6832cae41fa374cb68ca80d472ccaryclark@google.com#include "base/base64.h"
1078e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com#include "base/bind.h"
11cd4421df5012b75c792c6c8bf2c5ee0410921c15caryclark@google.com#include "base/prefs/pref_registry_simple.h"
1259823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com#include "base/prefs/pref_service.h"
13198e054b33051a6cd5f606ccbc8d539cefc5631fcaryclark@google.com#include "chrome/browser/chromeos/settings/cros_settings.h"
14198e054b33051a6cd5f606ccbc8d539cefc5631fcaryclark@google.com#include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h"
15be584d782020fe0d413a9ab4e9a57a13b1ac1032reed@google.com#include "chrome/common/pref_names.h"
16cd4421df5012b75c792c6c8bf2c5ee0410921c15caryclark@google.com
17752b60e633a349c5b9f7bcc6a28b8064fc77bb41caryclark@google.comnamespace em = enterprise_management;
182e7f4c810dc717383df42d27bdba862514ab6d51caryclark@google.com
192e7f4c810dc717383df42d27bdba862514ab6d51caryclark@google.comnamespace chromeos {
20198e054b33051a6cd5f606ccbc8d539cefc5631fcaryclark@google.com
21be584d782020fe0d413a9ab4e9a57a13b1ac1032reed@google.comnamespace device_settings_cache {
2224bec79d6f3d71ff97b50db72461a3892bd4f6b5caryclark@google.com
2359823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.comvoid RegisterPrefs(PrefRegistrySimple* registry) {
248dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com  registry->RegisterStringPref(prefs::kDeviceSettingsCache, "invalid");
2578e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com}
2678e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com
2778e17130f396d8b2157116c2504e357192f87ed1caryclark@google.combool Store(const em::PolicyData& policy, PrefService* local_state) {
282ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com  if (local_state) {
292ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com    std::string policy_string = policy.SerializeAsString();
302ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com    std::string encoded;
312ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com    if (!base::Base64Encode(policy_string, &encoded)) {
322ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com      LOG(ERROR) << "Can't encode policy in base64.";
332ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com      return false;
342ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com    }
352ddff9388694263c7be9347de7eb768cd0847997caryclark@google.com    local_state->SetString(prefs::kDeviceSettingsCache, encoded);
3678e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com    return true;
3778e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com  }
3878e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com  return false;
3959823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com}
4027c449af06cd1d05db441593d08b84f3530fba52caryclark@google.com
4147580694fbe974a065caf7c39c3d2075708c2018caryclark@google.combool Retrieve(em::PolicyData *policy, PrefService* local_state) {
42d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com  if (local_state) {
4378e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com    std::string encoded =
4478e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com        local_state->GetString(prefs::kDeviceSettingsCache);
4578e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com    std::string policy_string;
4678e17130f396d8b2157116c2504e357192f87ed1caryclark@google.com    if (!base::Base64Decode(encoded, &policy_string)) {
4759823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com      // This is normal and happens on first boot.
4859823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com      VLOG(1) << "Can't decode policy from base64.";
4924bec79d6f3d71ff97b50db72461a3892bd4f6b5caryclark@google.com      return false;
5024bec79d6f3d71ff97b50db72461a3892bd4f6b5caryclark@google.com    }
5159823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com    return policy->ParseFromString(policy_string);
5259823f7f3ba43c7c6bc1fa8c600b093ecb4236aacaryclark@google.com  }
53  return false;
54}
55
56}  // namespace device_settings_cache
57
58}  // namespace chromeos
59