Chromium Code Reviews| 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 support Dasher. | |
| 31 // We don't register the device when such a user logs in. | |
| 32 const char* kNonDasherDomains[] = { | |
|
danno
2010/11/23 17:04:49
See comment below about Dasher.
gfeher
2010/11/24 19:38:40
Done.
| |
| 33 "@googlemail.com", | |
|
Nikita (slow)
2010/11/25 12:18:00
Just a note, that I've been able to login with Goo
| |
| 34 "@gmail.com" | |
| 35 }; | |
| 36 | |
| 37 // Checks the domain part of the given username against the list of known | |
| 38 // non-dasher domain names. Returns false if |username| is empty or its | |
| 39 // in a domain known not to be supported by dasher. | |
| 40 bool CanBeInDasherDomain(const std::string& username) { | |
|
danno
2010/11/23 17:04:49
Is it possible to login with a user name that has
gfeher
2010/11/24 19:38:40
I've made an experiment with ChromeOS. I could add
| |
| 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(kNonDasherDomains); i++) { | |
| 47 if (EndsWith(username, kNonDasherDomains[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 27 matching lines...) Expand all Loading... | |
| 100 // FILE thread. | 165 // FILE thread. |
| 101 BrowserThread::PostTask( | 166 BrowserThread::PostTask( |
| 102 BrowserThread::FILE, | 167 BrowserThread::FILE, |
| 103 FROM_HERE, | 168 FROM_HERE, |
| 104 NewRunnableMethod(this, | 169 NewRunnableMethod(this, |
| 105 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); | 170 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); |
| 106 } | 171 } |
| 107 } | 172 } |
| 108 | 173 |
| 109 void DeviceTokenFetcher::Shutdown() { | 174 void DeviceTokenFetcher::Shutdown() { |
| 110 token_service_ = NULL; | 175 profile_ = NULL; |
| 111 backend_ = NULL; | 176 backend_ = NULL; |
| 112 } | 177 } |
| 113 | 178 |
| 114 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { | 179 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 116 if (file_util::PathExists(token_path_)) { | 181 if (file_util::PathExists(token_path_)) { |
| 117 std::string data; | 182 std::string data; |
| 118 em::DeviceCredentials device_credentials; | 183 em::DeviceCredentials device_credentials; |
| 119 if (file_util::ReadFileToString(token_path_, &data) && | 184 if (file_util::ReadFileToString(token_path_, &data) && |
| 120 device_credentials.ParseFromArray(data.c_str(), data.size())) { | 185 device_credentials.ParseFromArray(data.c_str(), data.size())) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 140 } | 205 } |
| 141 | 206 |
| 142 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { | 207 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 SetState(kStateReadyToRequestDeviceTokenFromServer); | 209 SetState(kStateReadyToRequestDeviceTokenFromServer); |
| 145 SendServerRequestIfPossible(); | 210 SendServerRequestIfPossible(); |
| 146 } | 211 } |
| 147 | 212 |
| 148 void DeviceTokenFetcher::SendServerRequestIfPossible() { | 213 void DeviceTokenFetcher::SendServerRequestIfPossible() { |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 215 std::string username = GetCurrentUser(); | |
| 150 if (state_ == kStateReadyToRequestDeviceTokenFromServer | 216 if (state_ == kStateReadyToRequestDeviceTokenFromServer |
| 151 && HasAuthToken() | 217 && HasAuthToken() |
| 152 && backend_) { | 218 && backend_ |
| 153 em::DeviceRegisterRequest register_request; | 219 && !username.empty()) { |
| 154 SetState(kStateRequestingDeviceTokenFromServer); | 220 if (CanBeInDasherDomain(username)) { |
|
danno
2010/11/23 17:04:49
Don't use Dasher, use something like "IsGoogleAcco
gfeher
2010/11/24 19:38:40
How about simply saying "non-managed"?
| |
| 155 backend_->ProcessRegisterRequest(auth_token_, | 221 em::DeviceRegisterRequest register_request; |
| 156 GetDeviceID(), | 222 SetState(kStateRequestingDeviceTokenFromServer); |
| 157 register_request, | 223 backend_->ProcessRegisterRequest(auth_token_, |
| 158 this); | 224 GetDeviceID(), |
| 225 register_request, | |
| 226 this); | |
| 227 } else { | |
| 228 OnError(DeviceManagementBackend::kErrorServiceManagementNotSupported); | |
|
danno
2010/11/23 17:04:49
There is a new method on the delegate for this, On
gfeher
2010/11/24 19:38:40
Done.
| |
| 229 } | |
| 159 } | 230 } |
| 160 } | 231 } |
| 161 | 232 |
| 162 bool DeviceTokenFetcher::IsTokenPending() { | 233 bool DeviceTokenFetcher::IsTokenPending() { |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 return !device_token_load_complete_event_.IsSignaled(); | 235 return !device_token_load_complete_event_.IsSignaled(); |
| 165 } | 236 } |
| 166 | 237 |
| 167 std::string DeviceTokenFetcher::GetDeviceToken() { | 238 std::string DeviceTokenFetcher::GetDeviceToken() { |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 DCHECK(no_error); | 289 DCHECK(no_error); |
| 219 file_util::WriteFile(path, data.c_str(), data.length()); | 290 file_util::WriteFile(path, data.c_str(), data.length()); |
| 220 } | 291 } |
| 221 | 292 |
| 222 // static | 293 // static |
| 223 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 294 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
| 224 return guid::GenerateGUID(); | 295 return guid::GenerateGUID(); |
| 225 } | 296 } |
| 226 | 297 |
| 227 } // namespace policy | 298 } // namespace policy |
| OLD | NEW |