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

Side by Side Diff: chrome/browser/extensions/api/identity/identity_api.cc

Issue 274853002: Identity API: add chrome.identity.getAccounts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add IDENTITY_GETACCOUNTS to histograms.xml Created 6 years, 7 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/extensions/api/identity/identity_api.h" 5 #include "chrome/browser/extensions/api/identity/identity_api.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_global_error.h" 23 #include "chrome/browser/signin/signin_global_error.h"
24 #include "chrome/browser/signin/signin_manager_factory.h" 24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/common/extensions/api/identity.h" 25 #include "chrome/common/extensions/api/identity.h"
26 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" 26 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
27 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
28 #include "chrome/common/url_constants.h" 28 #include "chrome/common/url_constants.h"
29 #include "components/signin/core/browser/profile_oauth2_token_service.h" 29 #include "components/signin/core/browser/profile_oauth2_token_service.h"
30 #include "components/signin/core/browser/signin_manager.h" 30 #include "components/signin/core/browser/signin_manager.h"
31 #include "components/signin/core/common/profile_management_switches.h"
31 #include "extensions/browser/event_router.h" 32 #include "extensions/browser/event_router.h"
32 #include "extensions/browser/extension_function_dispatcher.h" 33 #include "extensions/browser/extension_function_dispatcher.h"
33 #include "extensions/common/extension.h" 34 #include "extensions/common/extension.h"
34 #include "google_apis/gaia/gaia_urls.h" 35 #include "google_apis/gaia/gaia_urls.h"
35 #include "url/gurl.h" 36 #include "url/gurl.h"
36 37
37 #if defined(OS_CHROMEOS) 38 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/login/user_manager.h" 39 #include "chrome/browser/chromeos/login/user_manager.h"
39 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 40 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
40 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" 41 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 162
162 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken( 163 const IdentityTokenCacheValue& IdentityAPI::GetCachedToken(
163 const ExtensionTokenKey& key) { 164 const ExtensionTokenKey& key) {
164 return token_cache_[key]; 165 return token_cache_[key];
165 } 166 }
166 167
167 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { 168 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() {
168 return token_cache_; 169 return token_cache_;
169 } 170 }
170 171
172 std::vector<std::string> IdentityAPI::GetAccounts() const {
173 const std::vector<AccountIds> ids = account_tracker_.GetAccounts();
174 std::vector<std::string> gaia_ids;
175
176 if (switches::IsExtensionsMultiAccount()) {
177 for (std::vector<AccountIds>::const_iterator it = ids.begin();
178 it != ids.end();
179 ++it) {
180 gaia_ids.push_back(it->gaia);
181 }
182 } else if (ids.size() >= 1) {
183 gaia_ids.push_back(ids[0].gaia);
184 }
185
186 return gaia_ids;
187 }
188
171 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { 189 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) {
172 account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_), 190 account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_),
173 error); 191 error);
174 } 192 }
175 193
176 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { 194 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const {
177 return account_tracker_.GetAuthStatus(); 195 return account_tracker_.GetAuthStatus();
178 } 196 }
179 197
180 void IdentityAPI::Shutdown() { 198 void IdentityAPI::Shutdown() {
181 FOR_EACH_OBSERVER(ShutdownObserver, shutdown_observer_list_, OnShutdown()); 199 FOR_EACH_OBSERVER(ShutdownObserver, shutdown_observer_list_, OnShutdown());
182 account_tracker_.RemoveObserver(this); 200 account_tracker_.RemoveObserver(this);
183 account_tracker_.Shutdown(); 201 account_tracker_.Shutdown();
184 } 202 }
185 203
186 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > 204 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> >
187 g_factory = LAZY_INSTANCE_INITIALIZER; 205 g_factory = LAZY_INSTANCE_INITIALIZER;
188 206
189 // static 207 // static
190 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { 208 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() {
191 return g_factory.Pointer(); 209 return g_factory.Pointer();
192 } 210 }
193 211
194 void IdentityAPI::OnAccountAdded(const AccountIds& ids) {} 212 void IdentityAPI::OnAccountAdded(const AccountIds& ids) {}
195 213
196 void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {} 214 void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {}
197 215
198 void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids, 216 void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids,
199 bool is_signed_in) { 217 bool is_signed_in) {
218 const std::string primary_account_id = GetPrimaryAccountId(browser_context_);
219 if (primary_account_id != ids.account_key &&
220 !switches::IsExtensionsMultiAccount()) {
221 return;
222 }
223
200 api::identity::AccountInfo account_info; 224 api::identity::AccountInfo account_info;
201 account_info.id = ids.gaia; 225 account_info.id = ids.gaia;
202 226
203 scoped_ptr<base::ListValue> args = 227 scoped_ptr<base::ListValue> args =
204 api::identity::OnSignInChanged::Create(account_info, is_signed_in); 228 api::identity::OnSignInChanged::Create(account_info, is_signed_in);
205 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName, 229 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName,
206 args.Pass(), 230 args.Pass(),
207 browser_context_)); 231 browser_context_));
208 232
209 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 233 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
210 } 234 }
211 235
212 void IdentityAPI::AddShutdownObserver(ShutdownObserver* observer) { 236 void IdentityAPI::AddShutdownObserver(ShutdownObserver* observer) {
213 shutdown_observer_list_.AddObserver(observer); 237 shutdown_observer_list_.AddObserver(observer);
214 } 238 }
215 239
216 void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) { 240 void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) {
217 shutdown_observer_list_.RemoveObserver(observer); 241 shutdown_observer_list_.RemoveObserver(observer);
218 } 242 }
219 243
244 void IdentityAPI::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
245 account_tracker_.SetAccountStateForTest(ids, is_signed_in);
246 }
247
220 template <> 248 template <>
221 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { 249 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() {
222 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); 250 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
223 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); 251 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
224 } 252 }
225 253
254 IdentityGetAccountsFunction::IdentityGetAccountsFunction() {
255 }
256
257 IdentityGetAccountsFunction::
258 ~IdentityGetAccountsFunction() {
Roger Tawa OOO till Jul 10th 2014/05/13 14:13:12 nit: I don't think you need to wrap line.
Michael Courage 2014/05/13 17:02:06 Done. clang-formatted the whole class definition.
259 }
260
261 ExtensionFunction::ResponseAction IdentityGetAccountsFunction::Run() {
262 if (GetProfile()->IsOffTheRecord()) {
263 return RespondNow(Error(identity_constants::kOffTheRecord));
264 }
265
266 std::vector<std::string> gaia_ids =
267 IdentityAPI::GetFactoryInstance()->Get(GetProfile())->GetAccounts();
268 DCHECK(gaia_ids.size() < 2 || switches::IsExtensionsMultiAccount());
269
270 base::ListValue* infos = new base::ListValue();
271
272 for (std::vector<std::string>::const_iterator it = gaia_ids.begin();
273 it != gaia_ids.end(); ++it) {
274 api::identity::AccountInfo account_info;
275 account_info.id = *it;
276 infos->Append(account_info.ToValue().release());
277 }
278
279 return RespondNow(MultipleArguments(infos));
280 }
281
226 IdentityGetAuthTokenFunction::IdentityGetAuthTokenFunction() 282 IdentityGetAuthTokenFunction::IdentityGetAuthTokenFunction()
227 : OAuth2TokenService::Consumer("extensions_identity_api"), 283 : OAuth2TokenService::Consumer("extensions_identity_api"),
228 should_prompt_for_scopes_(false), 284 should_prompt_for_scopes_(false),
229 should_prompt_for_signin_(false) {} 285 should_prompt_for_signin_(false) {}
230 286
231 IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {} 287 IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {}
232 288
233 bool IdentityGetAuthTokenFunction::RunAsync() { 289 bool IdentityGetAuthTokenFunction::RunAsync() {
234 if (GetProfile()->IsOffTheRecord()) { 290 if (GetProfile()->IsOffTheRecord()) {
235 error_ = identity_constants::kOffTheRecord; 291 error_ = identity_constants::kOffTheRecord;
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( 829 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange(
774 const GURL& redirect_url) { 830 const GURL& redirect_url) {
775 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { 831 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) {
776 SetResult(new base::StringValue(redirect_url.spec())); 832 SetResult(new base::StringValue(redirect_url.spec()));
777 SendResponse(true); 833 SendResponse(true);
778 Release(); // Balanced in RunAsync. 834 Release(); // Balanced in RunAsync.
779 } 835 }
780 } 836 }
781 837
782 } // namespace extensions 838 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698