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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2010 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_token_fetcher.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/singleton.h"
#include "chrome/browser/guid.h"
#include "chrome/browser/net/gaia/token_service.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"
namespace policy {
namespace em = enterprise_management;
DeviceTokenFetcher::DeviceTokenFetcher(
DeviceManagementBackend* backend,
const FilePath& token_path)
: token_path_(token_path),
backend_(backend),
state_(kStateNotStarted),
device_token_load_complete_event_(true, false) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// The token fetcher gets initialized AuthTokens for the device management
// server are available. Install a notification observer to ensure that the
// device management token gets fetched after the AuthTokens are available.
registrar_.Add(this,
NotificationType::TOKEN_AVAILABLE,
NotificationService::AllSources());
}
void DeviceTokenFetcher::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (type == NotificationType::TOKEN_AVAILABLE) {
const Source<TokenService> token_service(source);
const TokenService::TokenAvailableDetails* token_details =
Details<const TokenService::TokenAvailableDetails>(details).ptr();
if (token_details->service() == GaiaConstants::kDeviceManagementService) {
if (!HasAuthToken()) {
auth_token_ = token_details->token();
SendServerRequestIfPossible();
}
}
} else {
NOTREACHED();
}
}
void DeviceTokenFetcher::HandleRegisterResponse(
const em::DeviceRegisterResponse& response) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(kStateRequestingDeviceTokenFromServer, state_);
if (response.has_device_management_token()) {
device_token_ = response.device_management_token();
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
NewRunnableFunction(&WriteDeviceTokenToDisk,
token_path_,
device_token_,
device_id_));
SetState(kStateHasDeviceToken);
} else {
NOTREACHED();
SetState(kStateFailure);
}
}
void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
SetState(kStateFailure);
}
void DeviceTokenFetcher::StartFetching() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ == kStateNotStarted) {
SetState(kStateLoadDeviceTokenFromDisk);
// The file calls for loading the persisted token must be deferred to the
// FILE thread.
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
NewRunnableMethod(this,
&DeviceTokenFetcher::AttemptTokenLoadFromDisk));
}
}
void DeviceTokenFetcher::AttemptTokenLoadFromDisk() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (file_util::PathExists(token_path_)) {
std::string data;
em::DeviceCredentials device_credentials;
if (file_util::ReadFileToString(token_path_, &data) &&
device_credentials.ParseFromArray(data.c_str(), data.size())) {
device_token_ = device_credentials.device_token();
device_id_ = device_credentials.device_id();
if (!device_token_.empty() && !device_id_.empty()) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
NewRunnableMethod(this,
&DeviceTokenFetcher::SetState,
kStateHasDeviceToken));
return;
}
}
}
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
NewRunnableMethod(this,
&DeviceTokenFetcher::MakeReadyToRequestDeviceToken));
}
void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
SetState(kStateReadyToRequestDeviceTokenFromServer);
SendServerRequestIfPossible();
}
void DeviceTokenFetcher::SendServerRequestIfPossible() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ == kStateReadyToRequestDeviceTokenFromServer
&& HasAuthToken()) {
em::DeviceRegisterRequest register_request;
SetState(kStateRequestingDeviceTokenFromServer);
backend_->ProcessRegisterRequest(auth_token_,
GetDeviceID(),
register_request,
this);
}
}
bool DeviceTokenFetcher::IsTokenPending() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return !device_token_load_complete_event_.IsSignaled();
}
std::string DeviceTokenFetcher::GetDeviceToken() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
device_token_load_complete_event_.Wait();
return device_token_;
}
std::string DeviceTokenFetcher::GetDeviceID() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// As long as access to this is only allowed from the UI thread, no explicit
// locking is necessary to prevent the ID from being generated twice.
if (device_id_.empty())
device_id_ = GenerateNewDeviceID();
return device_id_;
}
void DeviceTokenFetcher::SetState(FetcherState state) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state_ == state)
return;
state_ = state;
if (state == kStateFailure) {
device_token_load_complete_event_.Signal();
} else if (state == kStateHasDeviceToken) {
device_token_load_complete_event_.Signal();
NotificationService::current()->Notify(
NotificationType::DEVICE_TOKEN_AVAILABLE,
Source<DeviceTokenFetcher>(this),
NotificationService::NoDetails());
}
}
void DeviceTokenFetcher::GetDeviceTokenPath(FilePath* token_path) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
*token_path = token_path_;
}
bool DeviceTokenFetcher::IsTokenValid() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return state_ == kStateHasDeviceToken;
}
// static
void DeviceTokenFetcher::WriteDeviceTokenToDisk(
const FilePath& path,
const std::string& device_token,
const std::string& device_id) {
em::DeviceCredentials device_credentials;
device_credentials.set_device_token(device_token);
device_credentials.set_device_id(device_id);
std::string data;
DCHECK(device_credentials.SerializeToString(&data));
file_util::WriteFile(path, data.c_str(), data.length());
}
// static
std::string DeviceTokenFetcher::GenerateNewDeviceID() {
return guid::GenerateGUID();
}
}
|