Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/policy/cloud/user_policy_signin_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698