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