1 // Copyright 2017 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/extensions/api/identity/identity_get_profile_user_info_function.h"
6 
7 #include "base/notreached.h"
8 #include "chrome/browser/extensions/api/identity/identity_constants.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/identity_manager_factory.h"
11 #include "chrome/common/extensions/api/identity.h"
12 #include "components/signin/public/identity_manager/account_info.h"
13 #include "components/signin/public/identity_manager/consent_level.h"
14 #include "components/signin/public/identity_manager/identity_manager.h"
15 #include "content/public/browser/browser_context.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/permissions/permissions_data.h"
18 
19 namespace extensions {
20 
21 namespace {
GetConsentLevelFromProfileDetails(const api::identity::ProfileDetails * details)22 signin::ConsentLevel GetConsentLevelFromProfileDetails(
23     const api::identity::ProfileDetails* details) {
24   api::identity::AccountStatus account_status =
25       details ? details->account_status : api::identity::ACCOUNT_STATUS_NONE;
26 
27   switch (account_status) {
28     case api::identity::ACCOUNT_STATUS_ANY:
29       return signin::ConsentLevel::kNotRequired;
30     case api::identity::ACCOUNT_STATUS_NONE:
31     case api::identity::ACCOUNT_STATUS_SYNC:
32       return signin::ConsentLevel::kSync;
33   }
34 
35   NOTREACHED() << "Unexpected value for account_status: " << account_status;
36   return signin::ConsentLevel::kSync;
37 }
38 }  // namespace
39 
40 IdentityGetProfileUserInfoFunction::IdentityGetProfileUserInfoFunction() =
41     default;
42 
43 IdentityGetProfileUserInfoFunction::~IdentityGetProfileUserInfoFunction() =
44     default;
45 
Run()46 ExtensionFunction::ResponseAction IdentityGetProfileUserInfoFunction::Run() {
47   if (browser_context()->IsOffTheRecord()) {
48     return RespondNow(Error(identity_constants::kOffTheRecord));
49   }
50 
51   std::unique_ptr<api::identity::GetProfileUserInfo::Params> params(
52       api::identity::GetProfileUserInfo::Params::Create(*args_));
53   EXTENSION_FUNCTION_VALIDATE(params.get());
54 
55   api::identity::ProfileUserInfo profile_user_info;
56 
57   if (extension()->permissions_data()->HasAPIPermission(
58           APIPermission::kIdentityEmail)) {
59     signin::ConsentLevel consent_level =
60         GetConsentLevelFromProfileDetails(params->details.get());
61     auto account_info = IdentityManagerFactory::GetForProfile(
62                             Profile::FromBrowserContext(browser_context()))
63                             ->GetPrimaryAccountInfo(consent_level);
64     profile_user_info.email = account_info.email;
65     profile_user_info.id = account_info.gaia;
66   }
67 
68   return RespondNow(OneArgument(
69       base::Value::FromUniquePtrValue(profile_user_info.ToValue())));
70 }
71 
72 }  // namespace extensions
73