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/cloud/user_policy_signin_service.h" | 5 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" | 13 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" |
14 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 14 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
17 #include "chrome/browser/signin/signin_manager.h" | 19 #include "chrome/browser/signin/signin_manager.h" |
18 #include "chrome/browser/signin/signin_manager_factory.h" | |
19 #include "chrome/browser/signin/token_service.h" | |
20 #include "chrome/browser/signin/token_service_factory.h" | |
21 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
22 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
23 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
24 #include "google_apis/gaia/gaia_constants.h" | 23 #include "google_apis/gaia/gaia_constants.h" |
25 | 24 |
26 namespace policy { | 25 namespace policy { |
27 | 26 |
28 UserPolicySigninService::UserPolicySigninService( | 27 UserPolicySigninService::UserPolicySigninService( |
29 Profile* profile, | 28 Profile* profile, |
30 PrefService* local_state, | 29 PrefService* local_state, |
31 DeviceManagementService* device_management_service) | 30 DeviceManagementService* device_management_service, |
| 31 ProfileOAuth2TokenService* token_service) |
32 : UserPolicySigninServiceBase(profile, | 32 : UserPolicySigninServiceBase(profile, |
33 local_state, | 33 local_state, |
34 device_management_service) { | 34 device_management_service), |
| 35 oauth2_token_service_(token_service) { |
35 if (profile->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) | 36 if (profile->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) |
36 return; | 37 return; |
37 | 38 |
| 39 // ProfileOAuth2TokenService should not yet have loaded its tokens since this |
| 40 // happens in the background after PKS initialization - so this service |
| 41 // should always be created before the oauth token is available. |
| 42 DCHECK(!oauth2_token_service_->RefreshTokenIsAvailable()); |
| 43 |
38 // Listen for an OAuth token to become available so we can register a client | 44 // Listen for an OAuth token to become available so we can register a client |
39 // if for some reason the client is not already registered (for example, if | 45 // if for some reason the client is not already registered (for example, if |
40 // the policy load failed during initial signin). | 46 // the policy load failed during initial signin). |
41 registrar()->Add(this, | 47 oauth2_token_service_->AddObserver(this); |
42 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
43 content::Source<TokenService>( | |
44 TokenServiceFactory::GetForProfile(profile))); | |
45 | |
46 // TokenService should not yet have loaded its tokens since this happens in | |
47 // the background after PKS initialization - so this service should always be | |
48 // created before the oauth token is available. | |
49 DCHECK(!TokenServiceFactory::GetForProfile(profile)->HasOAuthLoginToken()); | |
50 } | 48 } |
51 | 49 |
52 UserPolicySigninService::~UserPolicySigninService() {} | 50 UserPolicySigninService::~UserPolicySigninService() { |
| 51 } |
53 | 52 |
54 void UserPolicySigninService::Shutdown() { | 53 void UserPolicySigninService::PrepareForUserCloudPolicyManagerShutdown() { |
55 // Stop any pending registration helper activity. We do this here instead of | 54 // Stop any pending registration helper activity. We do this here instead of |
56 // in the destructor because we want to shutdown the registration helper | 55 // in the destructor because we want to shutdown the registration helper |
57 // before UserCloudPolicyManager shuts down the CloudPolicyClient. | 56 // before UserCloudPolicyManager shuts down the CloudPolicyClient. |
58 registration_helper_.reset(); | 57 registration_helper_.reset(); |
| 58 |
| 59 UserPolicySigninServiceBase::PrepareForUserCloudPolicyManagerShutdown(); |
| 60 } |
| 61 |
| 62 void UserPolicySigninService::Shutdown() { |
59 UserPolicySigninServiceBase::Shutdown(); | 63 UserPolicySigninServiceBase::Shutdown(); |
| 64 oauth2_token_service_->RemoveObserver(this); |
60 } | 65 } |
61 | 66 |
62 void UserPolicySigninService::RegisterPolicyClient( | 67 void UserPolicySigninService::RegisterPolicyClient( |
63 const std::string& username, | 68 const std::string& username, |
64 const std::string& oauth2_refresh_token, | 69 const std::string& oauth2_refresh_token, |
65 const PolicyRegistrationCallback& callback) { | 70 const PolicyRegistrationCallback& callback) { |
66 DCHECK(!oauth2_refresh_token.empty()); | 71 DCHECK(!oauth2_refresh_token.empty()); |
67 | 72 |
68 // Create a new CloudPolicyClient for fetching the DMToken. | 73 // Create a new CloudPolicyClient for fetching the DMToken. |
69 scoped_ptr<CloudPolicyClient> policy_client = PrepareToRegister(username); | 74 scoped_ptr<CloudPolicyClient> policy_client = PrepareToRegister(username); |
(...skipping 21 matching lines...) Expand all Loading... |
91 scoped_ptr<CloudPolicyClient> client, | 96 scoped_ptr<CloudPolicyClient> client, |
92 PolicyRegistrationCallback callback) { | 97 PolicyRegistrationCallback callback) { |
93 registration_helper_.reset(); | 98 registration_helper_.reset(); |
94 if (!client->is_registered()) { | 99 if (!client->is_registered()) { |
95 // Registration failed, so free the client and pass NULL to the callback. | 100 // Registration failed, so free the client and pass NULL to the callback. |
96 client.reset(); | 101 client.reset(); |
97 } | 102 } |
98 callback.Run(client.Pass()); | 103 callback.Run(client.Pass()); |
99 } | 104 } |
100 | 105 |
101 void UserPolicySigninService::Observe( | 106 void UserPolicySigninService::OnRefreshTokenAvailable( |
102 int type, | 107 const std::string& account_id) { |
103 const content::NotificationSource& source, | 108 // If using a TestingProfile with no UserCloudPolicyManager, skip |
104 const content::NotificationDetails& details) { | 109 // initialization. |
105 | 110 if (!GetManager()) { |
106 if (profile()->IsManaged()) { | |
107 registrar()->RemoveAll(); | |
108 return; | |
109 } | |
110 | |
111 // If using a TestingProfile with no SigninManager or UserCloudPolicyManager, | |
112 // skip initialization. | |
113 if (!GetManager() || !SigninManagerFactory::GetForProfile(profile())) { | |
114 DVLOG(1) << "Skipping initialization for tests due to missing components."; | 111 DVLOG(1) << "Skipping initialization for tests due to missing components."; |
115 return; | 112 return; |
116 } | 113 } |
117 | 114 |
118 switch (type) { | 115 std::string username = GetSigninManager()->GetAuthenticatedUsername(); |
119 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | 116 // Should not have OAuth tokens if the user isn't signed in. |
120 const TokenService::TokenAvailableDetails& token_details = | 117 DCHECK(!username.empty()); |
121 *(content::Details<const TokenService::TokenAvailableDetails>( | 118 // ProfileOAuth2TokenService now has a refresh token so initialize the |
122 details).ptr()); | 119 // UserCloudPolicyManager. |
123 if (token_details.service() == | 120 InitializeForSignedInUser(username); |
124 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { | |
125 SigninManager* signin_manager = | |
126 SigninManagerFactory::GetForProfile(profile()); | |
127 std::string username = signin_manager->GetAuthenticatedUsername(); | |
128 // Should not have GAIA tokens if the user isn't signed in. | |
129 DCHECK(!username.empty()); | |
130 // TokenService now has a refresh token (implying that the user is | |
131 // signed in) so initialize the UserCloudPolicyManager. | |
132 InitializeForSignedInUser(username); | |
133 } | |
134 break; | |
135 } | |
136 default: | |
137 UserPolicySigninServiceBase::Observe(type, source, details); | |
138 } | |
139 } | 121 } |
140 | 122 |
141 void UserPolicySigninService::InitializeUserCloudPolicyManager( | 123 void UserPolicySigninService::InitializeUserCloudPolicyManager( |
142 scoped_ptr<CloudPolicyClient> client) { | 124 scoped_ptr<CloudPolicyClient> client) { |
143 UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(client.Pass()); | 125 UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(client.Pass()); |
144 ProhibitSignoutIfNeeded(); | 126 ProhibitSignoutIfNeeded(); |
145 } | 127 } |
146 | 128 |
147 void UserPolicySigninService::ShutdownUserCloudPolicyManager() { | 129 void UserPolicySigninService::ShutdownUserCloudPolicyManager() { |
148 UserCloudPolicyManager* manager = GetManager(); | 130 UserCloudPolicyManager* manager = GetManager(); |
149 if (manager) { | 131 // Allow the user to signout again. |
150 // Allow the user to signout again. | 132 if (manager) |
151 SigninManagerFactory::GetForProfile(profile())->ProhibitSignout(false); | 133 GetSigninManager()->ProhibitSignout(false); |
152 } | |
153 UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager(); | 134 UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager(); |
154 } | 135 } |
155 | 136 |
156 void UserPolicySigninService::OnInitializationCompleted( | 137 void UserPolicySigninService::OnInitializationCompleted( |
157 CloudPolicyService* service) { | 138 CloudPolicyService* service) { |
158 UserCloudPolicyManager* manager = GetManager(); | 139 UserCloudPolicyManager* manager = GetManager(); |
159 DCHECK_EQ(service, manager->core()->service()); | 140 DCHECK_EQ(service, manager->core()->service()); |
160 DCHECK(service->IsInitializationComplete()); | 141 DCHECK(service->IsInitializationComplete()); |
161 // The service is now initialized - if the client is not yet registered, then | 142 // The service is now initialized - if the client is not yet registered, then |
162 // it means that there is no cached policy and so we need to initiate a new | 143 // it means that there is no cached policy and so we need to initiate a new |
163 // client registration. | 144 // client registration. |
164 DVLOG_IF(1, manager->IsClientRegistered()) | 145 DVLOG_IF(1, manager->IsClientRegistered()) |
165 << "Client already registered - not fetching DMToken"; | 146 << "Client already registered - not fetching DMToken"; |
166 if (!manager->IsClientRegistered()) { | 147 if (!manager->IsClientRegistered()) { |
167 std::string token = TokenServiceFactory::GetForProfile(profile())-> | 148 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { |
168 GetOAuth2LoginRefreshToken(); | 149 // No token yet - this class listens for OnRefreshTokenAvailable() |
169 if (token.empty()) { | |
170 // No token yet - this class listens for NOTIFICATION_TOKEN_AVAILABLE | |
171 // and will re-attempt registration once the token is available. | 150 // and will re-attempt registration once the token is available. |
172 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; | 151 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; |
173 return; | 152 return; |
174 } | 153 } |
175 RegisterCloudPolicyService(token); | 154 RegisterCloudPolicyService(); |
176 } | 155 } |
177 // If client is registered now, prohibit signout. | 156 // If client is registered now, prohibit signout. |
178 ProhibitSignoutIfNeeded(); | 157 ProhibitSignoutIfNeeded(); |
179 } | 158 } |
180 | 159 |
181 void UserPolicySigninService::RegisterCloudPolicyService( | 160 void UserPolicySigninService::RegisterCloudPolicyService() { |
182 const std::string& login_token) { | |
183 DCHECK(!GetManager()->IsClientRegistered()); | 161 DCHECK(!GetManager()->IsClientRegistered()); |
184 DVLOG(1) << "Fetching new DM Token"; | 162 DVLOG(1) << "Fetching new DM Token"; |
185 // Do nothing if already starting the registration process. | 163 // Do nothing if already starting the registration process. |
186 if (registration_helper_) | 164 if (registration_helper_) |
187 return; | 165 return; |
188 | 166 |
189 // Start the process of registering the CloudPolicyClient. Once it completes, | 167 // Start the process of registering the CloudPolicyClient. Once it completes, |
190 // policy fetch will automatically happen. | 168 // policy fetch will automatically happen. |
191 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( | 169 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( |
192 profile()->GetRequestContext(), | 170 profile()->GetRequestContext(), |
193 GetManager()->core()->client(), | 171 GetManager()->core()->client(), |
194 ShouldForceLoadPolicy(), | 172 ShouldForceLoadPolicy(), |
195 enterprise_management::DeviceRegisterRequest::BROWSER)); | 173 enterprise_management::DeviceRegisterRequest::BROWSER)); |
196 registration_helper_->StartRegistrationWithLoginToken( | 174 registration_helper_->StartRegistration( |
197 login_token, | 175 oauth2_token_service_, |
| 176 GetSigninManager()->GetAuthenticatedUsername(), |
198 base::Bind(&UserPolicySigninService::OnRegistrationComplete, | 177 base::Bind(&UserPolicySigninService::OnRegistrationComplete, |
199 base::Unretained(this))); | 178 base::Unretained(this))); |
200 } | 179 } |
201 | 180 |
202 void UserPolicySigninService::OnRegistrationComplete() { | 181 void UserPolicySigninService::OnRegistrationComplete() { |
203 ProhibitSignoutIfNeeded(); | 182 ProhibitSignoutIfNeeded(); |
204 registration_helper_.reset(); | 183 registration_helper_.reset(); |
205 } | 184 } |
206 | 185 |
207 void UserPolicySigninService::ProhibitSignoutIfNeeded() { | 186 void UserPolicySigninService::ProhibitSignoutIfNeeded() { |
208 if (GetManager()->IsClientRegistered()) { | 187 if (GetManager()->IsClientRegistered()) { |
209 DVLOG(1) << "User is registered for policy - prohibiting signout"; | 188 DVLOG(1) << "User is registered for policy - prohibiting signout"; |
210 SigninManager* signin_manager = | 189 GetSigninManager()->ProhibitSignout(true); |
211 SigninManagerFactory::GetForProfile(profile()); | |
212 signin_manager->ProhibitSignout(true); | |
213 } | 190 } |
214 } | 191 } |
215 | 192 |
216 } // namespace policy | 193 } // namespace policy |
OLD | NEW |