OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/user_policy_signin_service.h" | |
6 | |
7 #include "chrome/browser/browser_process.h" | |
8 #include "chrome/browser/policy/browser_policy_connector.h" | |
9 #include "chrome/browser/policy/cloud_policy_service.h" | |
10 #include "chrome/browser/policy/user_cloud_policy_manager.h" | |
11 #include "chrome/browser/prefs/pref_service.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/signin/signin_manager.h" | |
14 #include "chrome/browser/signin/signin_manager_factory.h" | |
15 #include "chrome/browser/signin/token_service.h" | |
16 #include "chrome/browser/signin/token_service_factory.h" | |
17 #include "chrome/common/chrome_notification_types.h" | |
18 #include "chrome/common/net/gaia/gaia_constants.h" | |
19 #include "chrome/common/net/gaia/gaia_urls.h" | |
20 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
21 #include "chrome/common/pref_names.h" | |
22 #include "content/public/browser/notification_details.h" | |
23 #include "content/public/browser/notification_source.h" | |
24 | |
25 namespace { | |
26 // TODO(atwilson): Move this once we add OAuth token support to TokenService. | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
So we still don't have it? I was under the impress
Andrew T Wilson (Slow)
2012/08/04 00:54:41
There are two separate but related issues:
a) Usi
Mattias Nissler (ping if slow)
2012/08/06 08:33:52
Ah, thanks for the clarification!
| |
27 const char kServiceScopeChromeOSDeviceManagement[] = | |
28 "https://www.googleapis.com/auth/chromeosdevicemanagement"; | |
29 | |
30 // How long to delay before starting device policy network requests. Set to a | |
31 // few seconds to alleviate contention during initial startup. | |
32 const int64 kPolicyServiceInitializationDelayMilliseconds = 2000; | |
33 } // namespace | |
34 | |
35 namespace policy { | |
36 | |
37 UserPolicySigninService::UserPolicySigninService( | |
38 Profile* profile, UserCloudPolicyManager* manager) | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
arguments on separate lines (as per Chromium style
Andrew T Wilson (Slow)
2012/08/04 00:54:41
Done.
| |
39 : profile_(profile), | |
40 manager_(manager) { | |
41 | |
42 // Initialize/shutdown the UserCloudPolicyManager when the user signs in or | |
43 // out. | |
44 registrar_.Add(this, | |
45 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
46 content::Source<Profile>(profile)); | |
47 registrar_.Add(this, | |
48 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
49 content::Source<TokenService>( | |
50 TokenServiceFactory::GetForProfile(profile))); | |
51 | |
52 // The Profile is not yet fully initialized when this object is created, | |
53 // so wait until the initialization has finished to initialize the | |
54 // UserCloudPolicyManager as otherwise various crashes ensue from services | |
55 // trying to access the partially-initialized Profile. | |
56 // TODO(atwilson): Remove this once ProfileImpl::DoFinalInit() goes away and | |
57 // the profile is fully initialized before ProfileKeyedServices are created. | |
58 registrar_.Add(this, | |
59 chrome::NOTIFICATION_PROFILE_ADDED, | |
60 content::Source<Profile>(profile)); | |
61 } | |
62 | |
63 UserPolicySigninService::~UserPolicySigninService() { | |
64 } | |
65 | |
66 void UserPolicySigninService::Observe( | |
67 int type, | |
68 const content::NotificationSource& source, | |
69 const content::NotificationDetails& details) { | |
70 switch (type) { | |
71 case chrome::NOTIFICATION_PROFILE_ADDED: | |
72 // Profile is initialized so it's safe to initialize the | |
73 // UserCloudPolicyManager now. | |
74 ConfigureUserCloudPolicyManager(); | |
75 break; | |
76 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: | |
77 ConfigureUserCloudPolicyManager(); | |
78 break; | |
79 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | |
80 const TokenService::TokenAvailableDetails& token_details = | |
81 *(content::Details<const TokenService::TokenAvailableDetails>( | |
82 details).ptr()); | |
83 if (token_details.service() == | |
84 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { | |
85 // TokenService now has a refresh token, so reconfigure the | |
86 // UserCloudPolicyManager to initiate a DMToken fetch if needed. | |
87 ConfigureUserCloudPolicyManager(); | |
88 } | |
89 break; | |
90 } | |
91 default: | |
92 NOTREACHED(); | |
93 } | |
94 } | |
95 | |
96 | |
97 void UserPolicySigninService::ConfigureUserCloudPolicyManager() { | |
98 // Don't do anything unless cloud policy is enabled. | |
99 if (!profile_->GetPrefs()->GetBoolean(prefs::kLoadCloudPolicyOnSignin)) | |
100 return; | |
101 | |
102 // Either startup or shutdown the UserCloudPolicyManager depending on whether | |
103 // the user is signed in or not. | |
104 if (!manager_) | |
105 return; // Can be null in unit tests. | |
106 | |
107 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile_); | |
108 if (signin_manager->GetAuthenticatedUsername().empty()) { | |
109 manager_->Shutdown(); | |
110 } else { | |
111 if (!manager_->cloud_policy_service()) { | |
112 // Make sure we've initialized the DeviceManagementService. It's OK to | |
113 // call this multiple times so we do it every time we initialize the | |
114 // UserCloudPolicyManager. | |
115 g_browser_process->browser_policy_connector()-> | |
116 ScheduleServiceInitialization( | |
117 kPolicyServiceInitializationDelayMilliseconds); | |
118 // Initialize the UserCloudPolicyManager if it isn't already initialized. | |
119 policy::DeviceManagementService* service = g_browser_process-> | |
120 browser_policy_connector()->device_management_service(); | |
121 manager_->Initialize(g_browser_process->local_state(), | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
I think you want the Profile's PrefService here to
Andrew T Wilson (Slow)
2012/08/04 00:54:41
That pref is only registered as part of browser lo
Mattias Nissler (ping if slow)
2012/08/06 08:33:52
Agree (see also other comment).
| |
122 service, | |
123 policy::USER_AFFILIATION_NONE); | |
124 DCHECK(manager_->cloud_policy_service()); | |
125 } | |
126 | |
127 // Register the CloudPolicyService if needed. | |
128 if (!manager_->IsClientRegistered()) | |
129 RegisterCloudPolicyService(); | |
130 } | |
131 } | |
132 | |
133 void UserPolicySigninService::RegisterCloudPolicyService() { | |
134 // TODO(atwilson): Move the code to mint the devicemanagement token into | |
135 // TokenService. | |
136 std::string token = TokenServiceFactory::GetForProfile(profile_)-> | |
137 GetOAuth2LoginRefreshToken(); | |
138 if (token.empty()) { | |
139 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; | |
140 return; | |
141 } | |
142 | |
143 // Do nothing if already fetching an access token. | |
144 if (oauth2_access_token_fetcher_.get()) | |
145 return; | |
146 | |
147 // Start fetching an OAuth2 access token for the device management service and | |
148 // hand it off to the CloudPolicyClient when done. | |
149 oauth2_access_token_fetcher_.reset( | |
150 new OAuth2AccessTokenFetcher(this, profile_->GetRequestContext())); | |
151 std::vector<std::string> scopes(1, kServiceScopeChromeOSDeviceManagement); | |
152 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
153 oauth2_access_token_fetcher_->Start( | |
154 gaia_urls->oauth2_chrome_client_id(), | |
155 gaia_urls->oauth2_chrome_client_secret(), | |
156 token, | |
157 scopes); | |
158 } | |
159 | |
160 void UserPolicySigninService::OnGetTokenFailure( | |
161 const GoogleServiceAuthError& error) { | |
162 DLOG(WARNING) << "Could not fetch access token for " | |
163 << kServiceScopeChromeOSDeviceManagement; | |
164 oauth2_access_token_fetcher_.reset(); | |
165 manager_->CancelWaitForPolicyFetch(); | |
166 } | |
167 | |
168 void UserPolicySigninService::OnGetTokenSuccess( | |
169 const std::string& access_token, | |
170 const base::Time& expiration_time) { | |
171 // Pass along the new access token to the CloudPolicyClient. | |
172 manager_->RegisterClient(access_token); | |
173 oauth2_access_token_fetcher_.reset(); | |
174 } | |
175 | |
176 } // namespace policy | |
OLD | NEW |