Chromium Code Reviews| Index: chrome/browser/policy/device_token_fetcher.cc | 
| diff --git a/chrome/browser/policy/device_token_fetcher.cc b/chrome/browser/policy/device_token_fetcher.cc | 
| index 1952747394c6fbb36c3c29e0147414f002e2f15f..ec58dd3143aeabfaa06b345ab63ba67eccc5c268 100644 | 
| --- a/chrome/browser/policy/device_token_fetcher.cc | 
| +++ b/chrome/browser/policy/device_token_fetcher.cc | 
| @@ -7,15 +7,42 @@ | 
| #include "base/file_util.h" | 
| #include "base/path_service.h" | 
| #include "base/singleton.h" | 
| +#include "base/string_util.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/browser/profile.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" | 
| +#include "chrome/common/pref_names.h" | 
| + | 
| +#if defined(OS_CHROMEOS) | 
| +#include "chrome/browser/chromeos/login/user_manager.h" | 
| +#else | 
| +#include "chrome/browser/prefs/pref_service.h" | 
| +#include "chrome/browser/sync/signin_manager.h" | 
| +#endif | 
| + | 
| +class PrefService; | 
| + | 
| +namespace { | 
| + | 
| +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.
 
 | 
| + | 
| +int kNumNonDasherDomains = 2; | 
| 
 
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
arraysize()?
 
gfeher
2010/11/23 13:47:51
Done.
 
 | 
| + | 
| +// Domain names that are known not to support Dasher. | 
| +// We don't register the device when such a user logs in. | 
| +const char* kNonDasherDomains[] = { | 
| + "@googlemail.com", | 
| + "@gmail.com" | 
| +}; | 
| + | 
| +} // namespace | 
| namespace policy { | 
| @@ -23,21 +50,33 @@ namespace em = enterprise_management; | 
| DeviceTokenFetcher::DeviceTokenFetcher( | 
| DeviceManagementBackend* backend, | 
| - TokenService* token_service, | 
| + Profile* profile, | 
| const FilePath& token_path) | 
| - : token_path_(token_path), | 
| + : profile_(profile), | 
| + token_path_(token_path), | 
| backend_(backend), | 
| - token_service_(token_service), | 
| state_(kStateNotStarted), | 
| device_token_load_complete_event_(true, false) { | 
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| + 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.
 
 | 
| auth_token_ = token_service_->GetTokenForService( | 
| GaiaConstants::kDeviceManagementService); | 
| registrar_.Add(this, | 
| NotificationType::TOKEN_AVAILABLE, | 
| Source<TokenService>(token_service_)); | 
| + // Register for the event of user login. The device management token won't | 
| + // be fetched until we know the domain of the currently logged in user. | 
| + #if defined(OS_CHROMEOS) | 
| 
 
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
indentation
 
gfeher
2010/11/23 13:47:51
Done.
 
 | 
| + registrar_.Add(this, | 
| + NotificationType::LOGIN_USER_CHANGED, | 
| + NotificationService::AllSources()); | 
| + #else | 
| + registrar_.Add(this, | 
| + NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, | 
| + Source<Profile>(profile_)); | 
| + #endif | 
| } | 
| void DeviceTokenFetcher::Observe(NotificationType type, | 
| @@ -55,11 +94,43 @@ void DeviceTokenFetcher::Observe(NotificationType type, | 
| } | 
| } | 
| } | 
| + #if defined(OS_CHROMEOS) | 
| 
 
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
indentation
 
gfeher
2010/11/23 13:47:51
Done.
 
 | 
| + } else if (type == NotificationType::LOGIN_USER_CHANGED) { | 
| + SendServerRequestIfPossible(); | 
| + #else | 
| + } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { | 
| + if (profile_ == Source<Profile>(source).ptr()) { | 
| + SendServerRequestIfPossible(); | 
| + } | 
| + #endif | 
| } else { | 
| NOTREACHED(); | 
| } | 
| } | 
| +std::string DeviceTokenFetcher::GetCurrentUser() { | 
| +#if defined(OS_CHROMEOS) | 
| + return chromeos::UserManager::Get()->logged_in_user().email(); | 
| +#else | 
| + return profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | 
| +#endif | 
| +} | 
| + | 
| +bool DeviceTokenFetcher::CanCurrentUserBeDasher() { | 
| + std::string username = GetCurrentUser(); | 
| + if (username.empty()) { | 
| + // This means incognito user in case of ChromiumOS and | 
| + // no logged-in user in case of Chromium (SigninService). | 
| + return false; | 
| + } | 
| + for (int i = 0; i < kNumNonDasherDomains; i++) { | 
| + if (EndsWith(username, kNonDasherDomains[i], true)) { | 
| + return false; | 
| + } | 
| + } | 
| + return true; | 
| +} | 
| + | 
| void DeviceTokenFetcher::HandleRegisterResponse( | 
| const em::DeviceRegisterResponse& response) { | 
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| @@ -149,7 +220,8 @@ void DeviceTokenFetcher::SendServerRequestIfPossible() { | 
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| if (state_ == kStateReadyToRequestDeviceTokenFromServer | 
| && HasAuthToken() | 
| - && backend_) { | 
| + && backend_ | 
| + && CanCurrentUserBeDasher()) { | 
| em::DeviceRegisterRequest register_request; | 
| SetState(kStateRequestingDeviceTokenFromServer); | 
| backend_->ProcessRegisterRequest(auth_token_, |