| Index: chrome/browser/policy/cloud_policy_data.cc
|
| diff --git a/chrome/browser/policy/cloud_policy_data.cc b/chrome/browser/policy/cloud_policy_data.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c40f4661273e06a3ed44070c87768704f1086395
|
| --- /dev/null
|
| +++ b/chrome/browser/policy/cloud_policy_data.cc
|
| @@ -0,0 +1,176 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/policy/cloud_policy_data.h"
|
| +
|
| +#include "base/string_util.h"
|
| +#include "chrome/browser/policy/proto/device_management_backend.pb.h"
|
| +#include "chrome/browser/policy/proto/device_management_constants.h"
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| +#include "chrome/browser/chromeos/cros/cros_library.h"
|
| +#include "chrome/browser/chromeos/login/ownership_service.h"
|
| +#include "chrome/browser/chromeos/login/user_manager.h"
|
| +#include "chrome/browser/chromeos/system_access.h"
|
| +#endif
|
| +
|
| +namespace {
|
| +
|
| +// MachineInfo key names.
|
| +static const char kMachineInfoSystemHwqual[] = "hardware_class";
|
| +static const char kMachineInfoSerialNumber[] = "serial_number";
|
| +
|
| +} // namespace
|
| +
|
| +namespace policy {
|
| +
|
| +namespace em = enterprise_management;
|
| +
|
| +// static
|
| +CloudPolicyData* CloudPolicyData::CreateForUserPolicies() {
|
| + return new CloudPolicyData(em::DeviceRegisterRequest::USER,
|
| + kChromeUserPolicyType,
|
| + "", "");
|
| +}
|
| +
|
| +// static
|
| +CloudPolicyData* CloudPolicyData::CreateForDevicePolicies() {
|
| + std::string machine_model;
|
| + std::string machine_id;
|
| +#if defined(OS_CHROMEOS)
|
| + chromeos::SystemAccess* sys_lib = chromeos::SystemAccess::GetInstance();
|
| + if (!sys_lib->GetMachineStatistic(kMachineInfoSystemHwqual,
|
| + &machine_model)) {
|
| + LOG(ERROR) << "Failed to get machine model.";
|
| + }
|
| + if (!sys_lib->GetMachineStatistic(kMachineInfoSerialNumber,
|
| + &machine_id)) {
|
| + LOG(ERROR) << "Failed to get machine serial number.";
|
| + }
|
| +#endif
|
| + return new CloudPolicyData(em::DeviceRegisterRequest::DEVICE,
|
| + kChromeDevicePolicyType,
|
| + machine_model,
|
| + machine_id);
|
| +}
|
| +
|
| +CloudPolicyData::CloudPolicyData(
|
| + const em::DeviceRegisterRequest_Type policy_register_type,
|
| + const std::string& policy_type,
|
| + const std::string& machine_model,
|
| + const std::string& machine_id)
|
| + : policy_register_type_(policy_register_type),
|
| + policy_type_(policy_type),
|
| + machine_model_(machine_model),
|
| + machine_id_(machine_id),
|
| + token_cache_loaded_(false) {}
|
| +
|
| +CloudPolicyData::~CloudPolicyData() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnPolicyDataGoingAway());
|
| +}
|
| +
|
| +void CloudPolicyData::SetDeviceToken(const std::string& device_token,
|
| + bool from_cache) {
|
| + DCHECK(token_cache_loaded_ != from_cache);
|
| + if (!token_cache_loaded_) {
|
| + // The cache should be the first to set the token. (It may be "")
|
| + DCHECK(from_cache);
|
| + token_cache_loaded_ = true;
|
| + } else {
|
| + // The cache should never set the token later.
|
| + DCHECK(!from_cache);
|
| + }
|
| + device_token_ = device_token;
|
| + token_cache_loaded_ = true;
|
| + NotifyDeviceTokenChanged();
|
| +}
|
| +
|
| +void CloudPolicyData::SetGaiaToken(const std::string& gaia_token) {
|
| + DCHECK(!user_name_.empty());
|
| + gaia_token_ = gaia_token;
|
| + NotifyCredentialsChanged();
|
| +}
|
| +
|
| +void CloudPolicyData::Reset() {
|
| + user_name_ = "";
|
| + gaia_token_ = "";
|
| + device_id_ = "";
|
| + device_token_ = "";
|
| +}
|
| +
|
| +void CloudPolicyData::SetupForTesting(const std::string& device_token,
|
| + const std::string& device_id,
|
| + const std::string& user_name,
|
| + const std::string& gaia_token,
|
| + bool token_cache_loaded) {
|
| + device_id_ = device_id;
|
| + user_name_ = user_name;
|
| + gaia_token_ = gaia_token;
|
| + device_token_ = device_token;
|
| + token_cache_loaded_ = token_cache_loaded;
|
| +}
|
| +
|
| +void CloudPolicyData::set_device_id(const std::string& device_id) {
|
| + device_id_ = device_id;
|
| +}
|
| +
|
| +std::string CloudPolicyData::device_id() const {
|
| + return device_id_;
|
| +}
|
| +
|
| +void CloudPolicyData::set_user_name(const std::string& user_name) {
|
| + user_name_ = user_name;
|
| +}
|
| +
|
| +std::string CloudPolicyData::device_token() const {
|
| + return device_token_;
|
| +}
|
| +
|
| +std::string CloudPolicyData::gaia_token() const {
|
| + return gaia_token_;
|
| +}
|
| +
|
| +std::string CloudPolicyData::machine_id() const {
|
| + return machine_id_;
|
| +}
|
| +
|
| +std::string CloudPolicyData::machine_model() const {
|
| + return machine_model_;
|
| +}
|
| +
|
| +em::DeviceRegisterRequest_Type
|
| +CloudPolicyData::policy_register_type() const {
|
| + return policy_register_type_;
|
| +}
|
| +
|
| +std::string CloudPolicyData::policy_type() const {
|
| + return policy_type_;
|
| +}
|
| +
|
| +bool CloudPolicyData::token_cache_loaded() const {
|
| + return token_cache_loaded_;
|
| +}
|
| +
|
| +std::string CloudPolicyData::user_name() const {
|
| + return user_name_;
|
| +}
|
| +
|
| +void CloudPolicyData::AddObserver(CloudPolicyData::Observer* observer) {
|
| + observer_list_.AddObserver(observer);
|
| +}
|
| +
|
| +void CloudPolicyData::RemoveObserver(CloudPolicyData::Observer* observer) {
|
| + observer_list_.RemoveObserver(observer);
|
| +}
|
| +
|
| +void CloudPolicyData::NotifyCredentialsChanged() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnCredentialsChanged());
|
| +}
|
| +
|
| +void CloudPolicyData::NotifyDeviceTokenChanged() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenChanged());
|
| +}
|
| +
|
| +
|
| +} // namespace policy
|
|
|