device_settings_cache.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/settings/device_settings_cache.h"
6
7#include <string>
8
9#include "base/base64.h"
10#include "base/bind.h"
11#include "base/prefs/pref_registry_simple.h"
12#include "base/prefs/pref_service.h"
13#include "chrome/browser/chromeos/settings/cros_settings.h"
14#include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
15#include "chrome/common/pref_names.h"
16
17namespace em = enterprise_management;
18
19namespace chromeos {
20
21namespace device_settings_cache {
22
23void RegisterPrefs(PrefRegistrySimple* registry) {
24  registry->RegisterStringPref(prefs::kDeviceSettingsCache, "invalid");
25}
26
27bool Store(const em::PolicyData& policy, PrefService* local_state) {
28  if (local_state) {
29    std::string policy_string = policy.SerializeAsString();
30    std::string encoded;
31    if (!base::Base64Encode(policy_string, &encoded)) {
32      LOG(ERROR) << "Can't encode policy in base64.";
33      return false;
34    }
35    local_state->SetString(prefs::kDeviceSettingsCache, encoded);
36    return true;
37  }
38  return false;
39}
40
41bool Retrieve(em::PolicyData *policy, PrefService* local_state) {
42  if (local_state) {
43    std::string encoded =
44        local_state->GetString(prefs::kDeviceSettingsCache);
45    std::string policy_string;
46    if (!base::Base64Decode(encoded, &policy_string)) {
47      // This is normal and happens on first boot.
48      VLOG(1) << "Can't decode policy from base64.";
49      return false;
50    }
51    return policy->ParseFromString(policy_string);
52  }
53  return false;
54}
55
56}  // namespace device_settings_cache
57
58}  // namespace chromeos
59