| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/user_policy_signin_service.h" | 5 #include "chrome/browser/policy/user_policy_signin_service.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/policy/browser_policy_connector.h" | 8 #include "chrome/browser/policy/browser_policy_connector.h" |
| 9 #include "chrome/browser/policy/cloud_policy_service.h" | 9 #include "chrome/browser/policy/cloud_policy_service.h" |
| 10 #include "chrome/browser/policy/user_cloud_policy_manager.h" | 10 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
| 11 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/signin/signin_manager.h" | 14 #include "chrome/browser/signin/signin_manager.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/signin/token_service.h" | 16 #include "chrome/browser/signin/token_service.h" |
| 16 #include "chrome/browser/signin/token_service_factory.h" | 17 #include "chrome/browser/signin/token_service_factory.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 21 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
| 23 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 24 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 // TODO(atwilson): Move this once we add OAuth token support to TokenService. | 27 // TODO(atwilson): Move this once we add OAuth token support to TokenService. |
| 27 const char kServiceScopeChromeOSDeviceManagement[] = | 28 const char kServiceScopeChromeOSDeviceManagement[] = |
| 28 "https://www.googleapis.com/auth/chromeosdevicemanagement"; | 29 "https://www.googleapis.com/auth/chromeosdevicemanagement"; |
| 29 | 30 |
| 30 // How long to delay before starting device policy network requests. Set to a | 31 // How long to delay before starting device policy network requests. Set to a |
| 31 // few seconds to alleviate contention during initial startup. | 32 // few seconds to alleviate contention during initial startup. |
| 32 const int64 kPolicyServiceInitializationDelayMilliseconds = 2000; | 33 const int64 kPolicyServiceInitializationDelayMilliseconds = 2000; |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 namespace policy { | 36 namespace policy { |
| 36 | 37 |
| 37 UserPolicySigninService::UserPolicySigninService( | 38 UserPolicySigninService::UserPolicySigninService( |
| 38 Profile* profile, | 39 Profile* profile) |
| 39 UserCloudPolicyManager* manager) | 40 : profile_(profile) { |
| 40 : profile_(profile), | |
| 41 manager_(manager) { | |
| 42 | 41 |
| 43 // Initialize/shutdown the UserCloudPolicyManager when the user signs in or | 42 // Initialize/shutdown the UserCloudPolicyManager when the user signs in or |
| 44 // out. | 43 // out. |
| 45 registrar_.Add(this, | 44 registrar_.Add(this, |
| 46 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 45 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| 47 content::Source<Profile>(profile)); | 46 content::Source<Profile>(profile)); |
| 48 registrar_.Add(this, | 47 registrar_.Add(this, |
| 49 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 48 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 50 content::Source<TokenService>( | 49 content::Source<TokenService>( |
| 51 TokenServiceFactory::GetForProfile(profile))); | 50 TokenServiceFactory::GetForProfile(profile))); |
| 52 | 51 |
| 53 // The Profile is not yet fully initialized when this object is created, | 52 // The Profile is not yet fully initialized when this object is created, |
| 54 // so wait until the initialization has finished to initialize the | 53 // so wait until the initialization has finished to initialize the |
| 55 // UserCloudPolicyManager as otherwise various crashes ensue from services | 54 // UserCloudPolicyManager as otherwise various crashes ensue from services |
| 56 // trying to access the partially-initialized Profile. | 55 // trying to access the partially-initialized Profile. |
| 57 // TODO(atwilson): Remove this once ProfileImpl::DoFinalInit() goes away and | 56 // TODO(atwilson): Remove this once ProfileImpl::DoFinalInit() goes away and |
| 58 // the profile is fully initialized before ProfileKeyedServices are created. | 57 // the profile is fully initialized before ProfileKeyedServices are created. |
| 59 registrar_.Add(this, | 58 registrar_.Add(this, |
| 60 chrome::NOTIFICATION_PROFILE_ADDED, | 59 chrome::NOTIFICATION_PROFILE_ADDED, |
| 61 content::Source<Profile>(profile)); | 60 content::Source<Profile>(profile)); |
| 62 } | 61 } |
| 63 | 62 |
| 64 UserPolicySigninService::~UserPolicySigninService() { | 63 UserPolicySigninService::~UserPolicySigninService() {} |
| 65 StopObserving(); | |
| 66 } | |
| 67 | 64 |
| 68 void UserPolicySigninService::StopObserving() { | 65 void UserPolicySigninService::StopObserving() { |
| 69 if (manager_ && manager_->cloud_policy_service()) | 66 UserCloudPolicyManager* manager = GetManager(); |
| 70 manager_->cloud_policy_service()->RemoveObserver(this); | 67 if (manager && manager->cloud_policy_service()) |
| 68 manager->cloud_policy_service()->RemoveObserver(this); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void UserPolicySigninService::Observe( | 71 void UserPolicySigninService::Observe( |
| 74 int type, | 72 int type, |
| 75 const content::NotificationSource& source, | 73 const content::NotificationSource& source, |
| 76 const content::NotificationDetails& details) { | 74 const content::NotificationDetails& details) { |
| 77 switch (type) { | 75 switch (type) { |
| 78 case chrome::NOTIFICATION_PROFILE_ADDED: | 76 case chrome::NOTIFICATION_PROFILE_ADDED: |
| 79 // Profile is initialized so it's safe to initialize the | 77 // Profile is initialized so it's safe to initialize the |
| 80 // UserCloudPolicyManager now. | 78 // UserCloudPolicyManager now. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 101 } | 99 } |
| 102 | 100 |
| 103 | 101 |
| 104 void UserPolicySigninService::ConfigureUserCloudPolicyManager() { | 102 void UserPolicySigninService::ConfigureUserCloudPolicyManager() { |
| 105 // Don't do anything unless cloud policy is enabled. | 103 // Don't do anything unless cloud policy is enabled. |
| 106 if (!profile_->GetPrefs()->GetBoolean(prefs::kLoadCloudPolicyOnSignin)) | 104 if (!profile_->GetPrefs()->GetBoolean(prefs::kLoadCloudPolicyOnSignin)) |
| 107 return; | 105 return; |
| 108 | 106 |
| 109 // Either startup or shutdown the UserCloudPolicyManager depending on whether | 107 // Either startup or shutdown the UserCloudPolicyManager depending on whether |
| 110 // the user is signed in or not. | 108 // the user is signed in or not. |
| 111 if (!manager_) | 109 UserCloudPolicyManager* manager = GetManager(); |
| 110 if (!manager) |
| 112 return; // Can be null in unit tests. | 111 return; // Can be null in unit tests. |
| 113 | 112 |
| 114 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile_); | 113 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile_); |
| 115 if (signin_manager->GetAuthenticatedUsername().empty()) { | 114 if (signin_manager->GetAuthenticatedUsername().empty()) { |
| 116 // User has signed out - remove existing policy. | 115 // User has signed out - remove existing policy. |
| 117 StopObserving(); | 116 StopObserving(); |
| 118 manager_->ShutdownAndRemovePolicy(); | 117 manager->ShutdownAndRemovePolicy(); |
| 119 } else { | 118 } else { |
| 120 // Initialize the UserCloudPolicyManager if it isn't already initialized. | 119 // Initialize the UserCloudPolicyManager if it isn't already initialized. |
| 121 if (!manager_->cloud_policy_service()) { | 120 if (!manager->cloud_policy_service()) { |
| 122 // Make sure we've initialized the DeviceManagementService. It's OK to | 121 // Make sure we've initialized the DeviceManagementService. It's OK to |
| 123 // call this multiple times so we do it every time we initialize the | 122 // call this multiple times so we do it every time we initialize the |
| 124 // UserCloudPolicyManager. | 123 // UserCloudPolicyManager. |
| 125 g_browser_process->browser_policy_connector()-> | 124 g_browser_process->browser_policy_connector()-> |
| 126 ScheduleServiceInitialization( | 125 ScheduleServiceInitialization( |
| 127 kPolicyServiceInitializationDelayMilliseconds); | 126 kPolicyServiceInitializationDelayMilliseconds); |
| 128 // If there is no cached DMToken then we can detect this below (or when | 127 // If there is no cached DMToken then we can detect this below (or when |
| 129 // the OnInitializationCompleted() callback is invoked. | 128 // the OnInitializationCompleted() callback is invoked. |
| 130 policy::DeviceManagementService* service = g_browser_process-> | 129 policy::DeviceManagementService* service = g_browser_process-> |
| 131 browser_policy_connector()->device_management_service(); | 130 browser_policy_connector()->device_management_service(); |
| 132 manager_->Initialize(g_browser_process->local_state(), | 131 manager->Initialize(g_browser_process->local_state(), service); |
| 133 service, | 132 DCHECK(manager->cloud_policy_service()); |
| 134 policy::USER_AFFILIATION_NONE); | 133 manager->cloud_policy_service()->AddObserver(this); |
| 135 DCHECK(manager_->cloud_policy_service()); | |
| 136 manager_->cloud_policy_service()->AddObserver(this); | |
| 137 } | 134 } |
| 138 | 135 |
| 139 // If the CloudPolicyService is initialized, but the CloudPolicyClient still | 136 // If the CloudPolicyService is initialized, but the CloudPolicyClient still |
| 140 // needs to be registered, kick off registration. | 137 // needs to be registered, kick off registration. |
| 141 if (manager_->cloud_policy_service()->IsInitializationComplete() && | 138 if (manager->cloud_policy_service()->IsInitializationComplete() && |
| 142 !manager_->IsClientRegistered()) { | 139 !manager->IsClientRegistered()) { |
| 143 RegisterCloudPolicyService(); | 140 RegisterCloudPolicyService(); |
| 144 } | 141 } |
| 145 } | 142 } |
| 146 } | 143 } |
| 147 | 144 |
| 148 void UserPolicySigninService::OnInitializationCompleted( | 145 void UserPolicySigninService::OnInitializationCompleted( |
| 149 CloudPolicyService* service) { | 146 CloudPolicyService* service) { |
| 150 DCHECK_EQ(service, manager_->cloud_policy_service()); | 147 UserCloudPolicyManager* manager = GetManager(); |
| 148 DCHECK_EQ(service, manager->cloud_policy_service()); |
| 151 DCHECK(service->IsInitializationComplete()); | 149 DCHECK(service->IsInitializationComplete()); |
| 152 // The service is now initialized - if the client is not yet registered, then | 150 // The service is now initialized - if the client is not yet registered, then |
| 153 // it means that there is no cached policy and so we need to initiate a new | 151 // it means that there is no cached policy and so we need to initiate a new |
| 154 // client registration. | 152 // client registration. |
| 155 DVLOG_IF(1, manager_->IsClientRegistered()) | 153 DVLOG_IF(1, manager->IsClientRegistered()) |
| 156 << "Client already registered - not fetching DMToken"; | 154 << "Client already registered - not fetching DMToken"; |
| 157 if (!manager_->IsClientRegistered()) | 155 if (!manager->IsClientRegistered()) |
| 158 RegisterCloudPolicyService(); | 156 RegisterCloudPolicyService(); |
| 159 } | 157 } |
| 160 | 158 |
| 161 void UserPolicySigninService::RegisterCloudPolicyService() { | 159 void UserPolicySigninService::RegisterCloudPolicyService() { |
| 162 DVLOG(1) << "Fetching new DM Token"; | 160 DVLOG(1) << "Fetching new DM Token"; |
| 163 // TODO(atwilson): Move the code to mint the devicemanagement token into | 161 // TODO(atwilson): Move the code to mint the devicemanagement token into |
| 164 // TokenService. | 162 // TokenService. |
| 165 std::string token = TokenServiceFactory::GetForProfile(profile_)-> | 163 std::string token = TokenServiceFactory::GetForProfile(profile_)-> |
| 166 GetOAuth2LoginRefreshToken(); | 164 GetOAuth2LoginRefreshToken(); |
| 167 if (token.empty()) { | 165 if (token.empty()) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 184 gaia_urls->oauth2_chrome_client_secret(), | 182 gaia_urls->oauth2_chrome_client_secret(), |
| 185 token, | 183 token, |
| 186 scopes); | 184 scopes); |
| 187 } | 185 } |
| 188 | 186 |
| 189 void UserPolicySigninService::OnGetTokenFailure( | 187 void UserPolicySigninService::OnGetTokenFailure( |
| 190 const GoogleServiceAuthError& error) { | 188 const GoogleServiceAuthError& error) { |
| 191 DLOG(WARNING) << "Could not fetch access token for " | 189 DLOG(WARNING) << "Could not fetch access token for " |
| 192 << kServiceScopeChromeOSDeviceManagement; | 190 << kServiceScopeChromeOSDeviceManagement; |
| 193 oauth2_access_token_fetcher_.reset(); | 191 oauth2_access_token_fetcher_.reset(); |
| 194 manager_->CancelWaitForPolicyFetch(); | |
| 195 } | 192 } |
| 196 | 193 |
| 197 void UserPolicySigninService::OnGetTokenSuccess( | 194 void UserPolicySigninService::OnGetTokenSuccess( |
| 198 const std::string& access_token, | 195 const std::string& access_token, |
| 199 const base::Time& expiration_time) { | 196 const base::Time& expiration_time) { |
| 197 UserCloudPolicyManager* manager = GetManager(); |
| 200 // Pass along the new access token to the CloudPolicyClient. | 198 // Pass along the new access token to the CloudPolicyClient. |
| 201 DVLOG(1) << "Fetched new scoped OAuth token:" << access_token; | 199 DVLOG(1) << "Fetched new scoped OAuth token:" << access_token; |
| 202 manager_->RegisterClient(access_token); | 200 manager->RegisterClient(access_token); |
| 203 oauth2_access_token_fetcher_.reset(); | 201 oauth2_access_token_fetcher_.reset(); |
| 204 } | 202 } |
| 205 | 203 |
| 204 void UserPolicySigninService::Shutdown() { |
| 205 StopObserving(); |
| 206 } |
| 207 |
| 208 UserCloudPolicyManager* UserPolicySigninService::GetManager() { |
| 209 return UserCloudPolicyManagerFactory::GetForProfile(profile_); |
| 210 } |
| 211 |
| 206 } // namespace policy | 212 } // namespace policy |
| OLD | NEW |