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/policy/device_policy_identity_strategy.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/chromeos/cros/cros_library.h"
9#include "chrome/browser/chromeos/login/ownership_service.h"
10#include "chrome/browser/chromeos/login/user_manager.h"
11#include "chrome/browser/chromeos/system_access.h"
12#include "chrome/browser/net/gaia/token_service.h"
13#include "chrome/browser/policy/proto/device_management_constants.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/profiles/profile_manager.h"
16#include "chrome/common/guid.h"
17#include "chrome/common/net/gaia/gaia_constants.h"
18#include "content/common/notification_service.h"
19#include "content/common/notification_type.h"
20
21// MachineInfo key names.
22static const char kMachineInfoSystemHwqual[] = "hardware_class";
23static const char kMachineInfoSerialNumber[] = "serial_number";
24
25namespace policy {
26
27DevicePolicyIdentityStrategy::DevicePolicyIdentityStrategy() {
28  chromeos::SystemAccess* sys_lib = chromeos::SystemAccess::GetInstance();
29
30  if (!sys_lib->GetMachineStatistic(kMachineInfoSystemHwqual,
31                                    &machine_model_)) {
32    LOG(ERROR) << "Failed to get machine model.";
33  }
34  if (!sys_lib->GetMachineStatistic(kMachineInfoSerialNumber,
35                                    &machine_id_)) {
36    LOG(ERROR) << "Failed to get machine serial number.";
37  }
38}
39
40DevicePolicyIdentityStrategy::~DevicePolicyIdentityStrategy() {
41}
42
43std::string DevicePolicyIdentityStrategy::GetDeviceToken() {
44  return device_token_;
45}
46
47std::string DevicePolicyIdentityStrategy::GetDeviceID() {
48  return device_id_;
49}
50
51std::string DevicePolicyIdentityStrategy::GetMachineID() {
52  return machine_id_;
53}
54
55std::string DevicePolicyIdentityStrategy::GetMachineModel() {
56  return machine_model_;
57}
58
59em::DeviceRegisterRequest_Type
60DevicePolicyIdentityStrategy::GetPolicyRegisterType() {
61  return em::DeviceRegisterRequest::DEVICE;
62}
63
64std::string DevicePolicyIdentityStrategy::GetPolicyType() {
65  return kChromeDevicePolicyType;
66}
67
68void DevicePolicyIdentityStrategy::SetAuthCredentials(
69    const std::string& username,
70    const std::string& auth_token) {
71  username_ = username;
72  auth_token_ = auth_token;
73  device_id_ = guid::GenerateGUID();
74  NotifyAuthChanged();
75}
76
77void DevicePolicyIdentityStrategy::SetDeviceManagementCredentials(
78    const std::string& owner_email,
79    const std::string& device_id,
80    const std::string& device_token) {
81  username_ = owner_email;
82  device_id_ = device_id;
83  device_token_ = device_token;
84  NotifyDeviceTokenChanged();
85}
86
87void DevicePolicyIdentityStrategy::FetchPolicy() {
88  DCHECK(!device_token_.empty());
89  NotifyDeviceTokenChanged();
90}
91
92bool DevicePolicyIdentityStrategy::GetCredentials(std::string* username,
93                                                  std::string* auth_token) {
94  *username = username_;
95  *auth_token = auth_token_;
96
97  return !username->empty() && !auth_token->empty();
98}
99
100void DevicePolicyIdentityStrategy::OnDeviceTokenAvailable(
101    const std::string& token) {
102  device_token_ = token;
103}
104
105}  // namespace policy
106