summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/device_policy_identity_strategy.cc
blob: 26be2a9402d47affaaa4b8b5f00f0b2249fa741f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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/device_policy_identity_strategy.h"

#include "chrome/browser/browser_process.h"
#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"
#include "chrome/browser/net/gaia/token_service.h"
#include "chrome/browser/policy/proto/device_management_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/guid.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "content/common/notification_service.h"
#include "content/common/notification_type.h"

// MachineInfo key names.
static const char kMachineInfoSystemHwqual[] = "hardware_class";
static const char kMachineInfoSerialNumber[] = "serial_number";

namespace policy {

DevicePolicyIdentityStrategy::DevicePolicyIdentityStrategy() {
  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.";
  }
}

DevicePolicyIdentityStrategy::~DevicePolicyIdentityStrategy() {
}

std::string DevicePolicyIdentityStrategy::GetDeviceToken() {
  return device_token_;
}

std::string DevicePolicyIdentityStrategy::GetDeviceID() {
  return device_id_;
}

std::string DevicePolicyIdentityStrategy::GetMachineID() {
  return machine_id_;
}

std::string DevicePolicyIdentityStrategy::GetMachineModel() {
  return machine_model_;
}

em::DeviceRegisterRequest_Type
DevicePolicyIdentityStrategy::GetPolicyRegisterType() {
  return em::DeviceRegisterRequest::DEVICE;
}

std::string DevicePolicyIdentityStrategy::GetPolicyType() {
  return kChromeDevicePolicyType;
}

void DevicePolicyIdentityStrategy::SetAuthCredentials(
    const std::string& username,
    const std::string& auth_token) {
  username_ = username;
  auth_token_ = auth_token;
  device_id_ = guid::GenerateGUID();
  NotifyAuthChanged();
}

void DevicePolicyIdentityStrategy::SetDeviceManagementCredentials(
    const std::string& owner_email,
    const std::string& device_id,
    const std::string& device_token) {
  username_ = owner_email;
  device_id_ = device_id;
  device_token_ = device_token;
  NotifyDeviceTokenChanged();
}

bool DevicePolicyIdentityStrategy::GetCredentials(std::string* username,
                                                  std::string* auth_token) {
  *username = username_;
  *auth_token = auth_token_;

  return !username->empty() && !auth_token->empty();
}

void DevicePolicyIdentityStrategy::OnDeviceTokenAvailable(
    const std::string& token) {
  DCHECK(!machine_id_.empty());

  device_token_ = token;
  NotifyDeviceTokenChanged();
}

}  // namespace policy