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" |
| 21 #include "chrome/common/pref_names.h" | |
| 22 | |
| 23 #if defined(OS_CHROMEOS) | |
| 24 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 25 #else | |
| 26 #include "chrome/browser/prefs/pref_service.h" | |
| 27 #include "chrome/browser/sync/signin_manager.h" | |
| 28 #endif | |
| 29 | |
| 30 class PrefService; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 static const char kPlaceholderDeviceID[] = "placeholder_device_id"; | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
What's this?
gfeher
2010/11/23 13:47:51
Done.
| |
| 35 | |
| 36 int kNumNonDasherDomains = 2; | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
arraysize()?
gfeher
2010/11/23 13:47:51
Done.
| |
| 37 | |
| 38 // Domain names that are known not to support Dasher. | |
| 39 // We don't register the device when such a user logs in. | |
| 40 const char* kNonDasherDomains[] = { | |
| 41 "@googlemail.com", | |
| 42 "@gmail.com" | |
| 43 }; | |
| 44 | |
| 45 } // namespace | |
| 19 | 46 |
| 20 namespace policy { | 47 namespace policy { |
| 21 | 48 |
| 22 namespace em = enterprise_management; | 49 namespace em = enterprise_management; |
| 23 | 50 |
| 24 DeviceTokenFetcher::DeviceTokenFetcher( | 51 DeviceTokenFetcher::DeviceTokenFetcher( |
| 25 DeviceManagementBackend* backend, | 52 DeviceManagementBackend* backend, |
| 26 TokenService* token_service, | 53 Profile* profile, |
| 27 const FilePath& token_path) | 54 const FilePath& token_path) |
| 28 : token_path_(token_path), | 55 : profile_(profile), |
| 56 token_path_(token_path), | |
| 29 backend_(backend), | 57 backend_(backend), |
| 30 token_service_(token_service), | |
| 31 state_(kStateNotStarted), | 58 state_(kStateNotStarted), |
| 32 device_token_load_complete_event_(true, false) { | 59 device_token_load_complete_event_(true, false) { |
| 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 34 | 61 |
| 62 token_service_ = profile_->GetTokenService(); | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
If you store the profile, you can always do this w
gfeher
2010/11/23 13:47:51
Done.
| |
| 35 auth_token_ = token_service_->GetTokenForService( | 63 auth_token_ = token_service_->GetTokenForService( |
| 36 GaiaConstants::kDeviceManagementService); | 64 GaiaConstants::kDeviceManagementService); |
| 37 | 65 |
| 38 registrar_.Add(this, | 66 registrar_.Add(this, |
| 39 NotificationType::TOKEN_AVAILABLE, | 67 NotificationType::TOKEN_AVAILABLE, |
| 40 Source<TokenService>(token_service_)); | 68 Source<TokenService>(token_service_)); |
| 69 // Register for the event of user login. The device management token won't | |
| 70 // be fetched until we know the domain of the currently logged in user. | |
| 71 #if defined(OS_CHROMEOS) | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
indentation
gfeher
2010/11/23 13:47:51
Done.
| |
| 72 registrar_.Add(this, | |
| 73 NotificationType::LOGIN_USER_CHANGED, | |
| 74 NotificationService::AllSources()); | |
| 75 #else | |
| 76 registrar_.Add(this, | |
| 77 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, | |
| 78 Source<Profile>(profile_)); | |
| 79 #endif | |
| 41 } | 80 } |
| 42 | 81 |
| 43 void DeviceTokenFetcher::Observe(NotificationType type, | 82 void DeviceTokenFetcher::Observe(NotificationType type, |
| 44 const NotificationSource& source, | 83 const NotificationSource& source, |
| 45 const NotificationDetails& details) { | 84 const NotificationDetails& details) { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 if (type == NotificationType::TOKEN_AVAILABLE) { | 86 if (type == NotificationType::TOKEN_AVAILABLE) { |
| 48 if (Source<TokenService>(source).ptr() == token_service_) { | 87 if (Source<TokenService>(source).ptr() == token_service_) { |
| 49 const TokenService::TokenAvailableDetails* token_details = | 88 const TokenService::TokenAvailableDetails* token_details = |
| 50 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | 89 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
| 51 if (token_details->service() == GaiaConstants::kDeviceManagementService) { | 90 if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
| 52 if (!HasAuthToken()) { | 91 if (!HasAuthToken()) { |
| 53 auth_token_ = token_details->token(); | 92 auth_token_ = token_details->token(); |
| 54 SendServerRequestIfPossible(); | 93 SendServerRequestIfPossible(); |
| 55 } | 94 } |
| 56 } | 95 } |
| 57 } | 96 } |
| 97 #if defined(OS_CHROMEOS) | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
indentation
gfeher
2010/11/23 13:47:51
Done.
| |
| 98 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | |
| 99 SendServerRequestIfPossible(); | |
| 100 #else | |
| 101 } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { | |
| 102 if (profile_ == Source<Profile>(source).ptr()) { | |
| 103 SendServerRequestIfPossible(); | |
| 104 } | |
| 105 #endif | |
| 58 } else { | 106 } else { |
| 59 NOTREACHED(); | 107 NOTREACHED(); |
| 60 } | 108 } |
| 61 } | 109 } |
| 62 | 110 |
| 111 std::string DeviceTokenFetcher::GetCurrentUser() { | |
| 112 #if defined(OS_CHROMEOS) | |
| 113 return chromeos::UserManager::Get()->logged_in_user().email(); | |
| 114 #else | |
| 115 return profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | |
| 116 #endif | |
| 117 } | |
| 118 | |
| 119 bool DeviceTokenFetcher::CanCurrentUserBeDasher() { | |
| 120 std::string username = GetCurrentUser(); | |
| 121 if (username.empty()) { | |
| 122 // This means incognito user in case of ChromiumOS and | |
| 123 // no logged-in user in case of Chromium (SigninService). | |
| 124 return false; | |
| 125 } | |
| 126 for (int i = 0; i < kNumNonDasherDomains; i++) { | |
| 127 if (EndsWith(username, kNonDasherDomains[i], true)) { | |
| 128 return false; | |
| 129 } | |
| 130 } | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 63 void DeviceTokenFetcher::HandleRegisterResponse( | 134 void DeviceTokenFetcher::HandleRegisterResponse( |
| 64 const em::DeviceRegisterResponse& response) { | 135 const em::DeviceRegisterResponse& response) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 DCHECK_EQ(kStateRequestingDeviceTokenFromServer, state_); | 137 DCHECK_EQ(kStateRequestingDeviceTokenFromServer, state_); |
| 67 if (response.has_device_management_token()) { | 138 if (response.has_device_management_token()) { |
| 68 device_token_ = response.device_management_token(); | 139 device_token_ = response.device_management_token(); |
| 69 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
| 70 BrowserThread::FILE, | 141 BrowserThread::FILE, |
| 71 FROM_HERE, | 142 FROM_HERE, |
| 72 NewRunnableFunction(&WriteDeviceTokenToDisk, | 143 NewRunnableFunction(&WriteDeviceTokenToDisk, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { | 213 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 SetState(kStateReadyToRequestDeviceTokenFromServer); | 215 SetState(kStateReadyToRequestDeviceTokenFromServer); |
| 145 SendServerRequestIfPossible(); | 216 SendServerRequestIfPossible(); |
| 146 } | 217 } |
| 147 | 218 |
| 148 void DeviceTokenFetcher::SendServerRequestIfPossible() { | 219 void DeviceTokenFetcher::SendServerRequestIfPossible() { |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 150 if (state_ == kStateReadyToRequestDeviceTokenFromServer | 221 if (state_ == kStateReadyToRequestDeviceTokenFromServer |
| 151 && HasAuthToken() | 222 && HasAuthToken() |
| 152 && backend_) { | 223 && backend_ |
| 224 && CanCurrentUserBeDasher()) { | |
| 153 em::DeviceRegisterRequest register_request; | 225 em::DeviceRegisterRequest register_request; |
| 154 SetState(kStateRequestingDeviceTokenFromServer); | 226 SetState(kStateRequestingDeviceTokenFromServer); |
| 155 backend_->ProcessRegisterRequest(auth_token_, | 227 backend_->ProcessRegisterRequest(auth_token_, |
| 156 GetDeviceID(), | 228 GetDeviceID(), |
| 157 register_request, | 229 register_request, |
| 158 this); | 230 this); |
| 159 } | 231 } |
| 160 } | 232 } |
| 161 | 233 |
| 162 bool DeviceTokenFetcher::IsTokenPending() { | 234 bool DeviceTokenFetcher::IsTokenPending() { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 DCHECK(no_error); | 290 DCHECK(no_error); |
| 219 file_util::WriteFile(path, data.c_str(), data.length()); | 291 file_util::WriteFile(path, data.c_str(), data.length()); |
| 220 } | 292 } |
| 221 | 293 |
| 222 // static | 294 // static |
| 223 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 295 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
| 224 return guid::GenerateGUID(); | 296 return guid::GenerateGUID(); |
| 225 } | 297 } |
| 226 | 298 |
| 227 } // namespace policy | 299 } // namespace policy |
| OLD | NEW |