OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/device_token_fetcher.h" | 5 #include "chrome/browser/policy/device_token_fetcher.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
10 #include "base/string_util.h" | |
10 #include "chrome/browser/guid.h" | 11 #include "chrome/browser/guid.h" |
11 #include "chrome/browser/net/gaia/token_service.h" | 12 #include "chrome/browser/net/gaia/token_service.h" |
12 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 13 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
14 #include "chrome/browser/profile.h" | |
13 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
14 #include "chrome/common/net/gaia/gaia_constants.h" | 16 #include "chrome/common/net/gaia/gaia_constants.h" |
15 #include "chrome/common/notification_details.h" | 17 #include "chrome/common/notification_details.h" |
16 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
17 #include "chrome/common/notification_source.h" | 19 #include "chrome/common/notification_source.h" |
18 #include "chrome/common/notification_type.h" | 20 #include "chrome/common/notification_type.h" |
19 | 21 |
22 #if defined(OS_CHROMEOS) | |
23 #include "chrome/browser/chromeos/login/user_manager.h" | |
24 #else | |
25 #include "chrome/browser/browser_signin.h" | |
26 #endif | |
27 | |
28 namespace { | |
29 | |
30 // Domain names that are known not to be managed. | |
31 // We don't register the device when such a user logs in. | |
32 const char* kNonManagedDomains[] = { | |
33 "@googlemail.com", | |
34 "@gmail.com" | |
35 }; | |
36 | |
37 // Checks the domain part of the given username against the list of known | |
38 // non-managed domain names. Returns false if |username| is empty or its | |
39 // in a domain known not to be managed. | |
40 bool CanBeInManagedDomain(const std::string& username) { | |
41 if (username.empty()) { | |
42 // This means incognito user in case of ChromiumOS and | |
43 // no logged-in user in case of Chromium (SigninService). | |
44 return false; | |
45 } | |
46 for (size_t i = 0; i < arraysize(kNonManagedDomains); i++) { | |
47 if (EndsWith(username, kNonManagedDomains[i], true)) { | |
48 return false; | |
49 } | |
50 } | |
51 return true; | |
52 } | |
53 | |
54 } // namespace | |
55 | |
20 namespace policy { | 56 namespace policy { |
21 | 57 |
22 namespace em = enterprise_management; | 58 namespace em = enterprise_management; |
23 | 59 |
24 DeviceTokenFetcher::DeviceTokenFetcher( | 60 DeviceTokenFetcher::DeviceTokenFetcher( |
25 DeviceManagementBackend* backend, | 61 DeviceManagementBackend* backend, |
26 TokenService* token_service, | 62 Profile* profile, |
27 const FilePath& token_path) | 63 const FilePath& token_path) |
28 : token_path_(token_path), | 64 : profile_(profile), |
65 token_path_(token_path), | |
29 backend_(backend), | 66 backend_(backend), |
30 token_service_(token_service), | |
31 state_(kStateNotStarted), | 67 state_(kStateNotStarted), |
32 device_token_load_complete_event_(true, false) { | 68 device_token_load_complete_event_(true, false) { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 | 70 |
35 auth_token_ = token_service_->GetTokenForService( | 71 TokenService* token_service = profile_->GetTokenService(); |
72 auth_token_ = token_service->GetTokenForService( | |
36 GaiaConstants::kDeviceManagementService); | 73 GaiaConstants::kDeviceManagementService); |
37 | 74 |
38 registrar_.Add(this, | 75 registrar_.Add(this, |
39 NotificationType::TOKEN_AVAILABLE, | 76 NotificationType::TOKEN_AVAILABLE, |
40 Source<TokenService>(token_service_)); | 77 Source<TokenService>(token_service)); |
78 // Register for the event of user login. The device management token won't | |
79 // be fetched until we know the domain of the currently logged in user. | |
80 #if defined(OS_CHROMEOS) | |
81 registrar_.Add(this, | |
82 NotificationType::LOGIN_USER_CHANGED, | |
83 NotificationService::AllSources()); | |
84 #else | |
85 registrar_.Add(this, | |
86 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, | |
87 Source<Profile>(profile_)); | |
88 #endif | |
41 } | 89 } |
42 | 90 |
43 void DeviceTokenFetcher::Observe(NotificationType type, | 91 void DeviceTokenFetcher::Observe(NotificationType type, |
44 const NotificationSource& source, | 92 const NotificationSource& source, |
45 const NotificationDetails& details) { | 93 const NotificationDetails& details) { |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
47 if (type == NotificationType::TOKEN_AVAILABLE) { | 95 if (type == NotificationType::TOKEN_AVAILABLE) { |
48 if (Source<TokenService>(source).ptr() == token_service_) { | 96 if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) { |
49 const TokenService::TokenAvailableDetails* token_details = | 97 const TokenService::TokenAvailableDetails* token_details = |
50 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | 98 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
51 if (token_details->service() == GaiaConstants::kDeviceManagementService) { | 99 if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
52 if (!HasAuthToken()) { | 100 if (!HasAuthToken()) { |
53 auth_token_ = token_details->token(); | 101 auth_token_ = token_details->token(); |
54 SendServerRequestIfPossible(); | 102 SendServerRequestIfPossible(); |
55 } | 103 } |
56 } | 104 } |
57 } | 105 } |
106 #if defined(OS_CHROMEOS) | |
107 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | |
108 SendServerRequestIfPossible(); | |
109 #else | |
110 } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { | |
111 if (profile_ == Source<Profile>(source).ptr()) { | |
112 SendServerRequestIfPossible(); | |
113 } | |
114 #endif | |
58 } else { | 115 } else { |
59 NOTREACHED(); | 116 NOTREACHED(); |
60 } | 117 } |
61 } | 118 } |
62 | 119 |
120 std::string DeviceTokenFetcher::GetCurrentUser() { | |
121 #if defined(OS_CHROMEOS) | |
122 return chromeos::UserManager::Get()->logged_in_user().email(); | |
123 #else | |
124 return profile_->GetBrowserSignin()->GetSignedInUsername(); | |
125 #endif | |
126 } | |
127 | |
63 void DeviceTokenFetcher::HandleRegisterResponse( | 128 void DeviceTokenFetcher::HandleRegisterResponse( |
64 const em::DeviceRegisterResponse& response) { | 129 const em::DeviceRegisterResponse& response) { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 DCHECK_EQ(kStateRequestingDeviceTokenFromServer, state_); | 131 DCHECK_EQ(kStateRequestingDeviceTokenFromServer, state_); |
67 if (response.has_device_management_token()) { | 132 if (response.has_device_management_token()) { |
68 device_token_ = response.device_management_token(); | 133 device_token_ = response.device_management_token(); |
69 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
70 BrowserThread::FILE, | 135 BrowserThread::FILE, |
71 FROM_HERE, | 136 FROM_HERE, |
72 NewRunnableFunction(&WriteDeviceTokenToDisk, | 137 NewRunnableFunction(&WriteDeviceTokenToDisk, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 // FILE thread. | 179 // FILE thread. |
115 BrowserThread::PostTask( | 180 BrowserThread::PostTask( |
116 BrowserThread::FILE, | 181 BrowserThread::FILE, |
117 FROM_HERE, | 182 FROM_HERE, |
118 NewRunnableMethod(this, | 183 NewRunnableMethod(this, |
119 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); | 184 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); |
120 } | 185 } |
121 } | 186 } |
122 | 187 |
123 void DeviceTokenFetcher::Shutdown() { | 188 void DeviceTokenFetcher::Shutdown() { |
124 token_service_ = NULL; | 189 profile_ = NULL; |
125 backend_ = NULL; | 190 backend_ = NULL; |
126 } | 191 } |
127 | 192 |
128 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { | 193 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { |
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
130 if (file_util::PathExists(token_path_)) { | 195 if (file_util::PathExists(token_path_)) { |
131 std::string data; | 196 std::string data; |
132 em::DeviceCredentials device_credentials; | 197 em::DeviceCredentials device_credentials; |
133 if (file_util::ReadFileToString(token_path_, &data) && | 198 if (file_util::ReadFileToString(token_path_, &data) && |
134 device_credentials.ParseFromArray(data.c_str(), data.size())) { | 199 device_credentials.ParseFromArray(data.c_str(), data.size())) { |
(...skipping 19 matching lines...) Expand all Loading... | |
154 } | 219 } |
155 | 220 |
156 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { | 221 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { |
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
158 SetState(kStateReadyToRequestDeviceTokenFromServer); | 223 SetState(kStateReadyToRequestDeviceTokenFromServer); |
159 SendServerRequestIfPossible(); | 224 SendServerRequestIfPossible(); |
160 } | 225 } |
161 | 226 |
162 void DeviceTokenFetcher::SendServerRequestIfPossible() { | 227 void DeviceTokenFetcher::SendServerRequestIfPossible() { |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
229 std::string username = GetCurrentUser(); | |
164 if (state_ == kStateReadyToRequestDeviceTokenFromServer | 230 if (state_ == kStateReadyToRequestDeviceTokenFromServer |
165 && HasAuthToken() | 231 && HasAuthToken() |
166 && backend_) { | 232 && backend_ |
167 em::DeviceRegisterRequest register_request; | 233 && !username.empty()) { |
Jakob Kummerow
2010/11/27 10:56:29
I strongly suggest to remove this line.
The case
| |
168 SetState(kStateRequestingDeviceTokenFromServer); | 234 if (CanBeInManagedDomain(username)) { |
169 backend_->ProcessRegisterRequest(auth_token_, | 235 em::DeviceRegisterRequest register_request; |
170 GetDeviceID(), | 236 SetState(kStateRequestingDeviceTokenFromServer); |
171 register_request, | 237 backend_->ProcessRegisterRequest(auth_token_, |
172 this); | 238 GetDeviceID(), |
239 register_request, | |
240 this); | |
241 } else { | |
242 SetState(kStateNotManaged); | |
243 } | |
173 } | 244 } |
174 } | 245 } |
175 | 246 |
176 bool DeviceTokenFetcher::IsTokenPending() { | 247 bool DeviceTokenFetcher::IsTokenPending() { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 return !device_token_load_complete_event_.IsSignaled(); | 249 return !device_token_load_complete_event_.IsSignaled(); |
179 } | 250 } |
180 | 251 |
181 std::string DeviceTokenFetcher::GetDeviceToken() { | 252 std::string DeviceTokenFetcher::GetDeviceToken() { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 DCHECK(no_error); | 304 DCHECK(no_error); |
234 file_util::WriteFile(path, data.c_str(), data.length()); | 305 file_util::WriteFile(path, data.c_str(), data.length()); |
235 } | 306 } |
236 | 307 |
237 // static | 308 // static |
238 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 309 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
239 return guid::GenerateGUID(); | 310 return guid::GenerateGUID(); |
240 } | 311 } |
241 | 312 |
242 } // namespace policy | 313 } // namespace policy |
OLD | NEW |