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

Side by Side Diff: components/signin/public/interfaces/account_id_traits.h

Issue 2617763003: Add a mojo interface for AccountId (Closed)
Patch Set: rebase Created 3 years, 11 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
« no previous file with comments | « components/signin/public/interfaces/account_id.typemap ('k') | components/typemaps.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #ifndef COMPONENTS_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
6 #define COMPONENTS_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
7
8 #include <string>
9
10 #include "components/signin/core/account_id/account_id.h"
11 #include "components/signin/public/interfaces/account_id.mojom.h"
12
13 namespace mojo {
14
15 template <>
16 struct EnumTraits<signin::mojom::AccountType, AccountType> {
17 static signin::mojom::AccountType ToMojom(AccountType input) {
18 switch (input) {
19 case AccountType::UNKNOWN:
20 return signin::mojom::AccountType::UNKNOWN;
21 case AccountType::GOOGLE:
22 return signin::mojom::AccountType::GOOGLE;
23 case AccountType::ACTIVE_DIRECTORY:
24 return signin::mojom::AccountType::ACTIVE_DIRECTORY;
25 }
26 NOTREACHED();
27 return signin::mojom::AccountType::UNKNOWN;
28 }
29
30 static bool FromMojom(signin::mojom::AccountType input,
31 AccountType* out) {
32 switch (input) {
33 case signin::mojom::AccountType::UNKNOWN:
34 *out = AccountType::UNKNOWN;
35 return true;
36 case signin::mojom::AccountType::GOOGLE:
37 *out = AccountType::GOOGLE;
38 return true;
39 case signin::mojom::AccountType::ACTIVE_DIRECTORY:
40 *out = AccountType::ACTIVE_DIRECTORY;
41 return true;
42 }
43 NOTREACHED();
44 return false;
45 }
46 };
47
48
49 template <>
50 struct StructTraits<signin::mojom::AccountIdDataView, AccountId> {
51 static AccountType account_type(const AccountId& r) {
52 return r.GetAccountType();
53 }
54 static std::string id(const AccountId& r) {
55 switch (r.GetAccountType()) {
56 case AccountType::GOOGLE:
57 return r.GetGaiaId();
58 case AccountType::ACTIVE_DIRECTORY:
59 return r.GetObjGuid();
60 case AccountType::UNKNOWN:
61 // UNKNOWN type is used for users that have only email (e.g. in tests
62 // or legacy users that have not run through migration code).
63 // Return an empty string for such accounts.
64 return std::string();
65 }
66 NOTREACHED();
67 return std::string();
68 }
69 static std::string user_email(const AccountId& r) {
70 return r.GetUserEmail();
71 }
72
73 static bool Read(signin::mojom::AccountIdDataView data, AccountId* out) {
74 AccountType account_type;
75 std::string id;
76 std::string user_email;
77 if (!data.ReadAccountType(&account_type) ||
78 !data.ReadId(&id) ||
79 !data.ReadUserEmail(&user_email)) {
80 return false;
81 }
82
83 switch (account_type) {
84 case AccountType::GOOGLE:
85 *out = AccountId::FromUserEmailGaiaId(user_email, id);
86 break;
87 case AccountType::ACTIVE_DIRECTORY:
88 *out = AccountId::AdFromUserEmailObjGuid(user_email, id);
89 break;
90 case AccountType::UNKNOWN:
91 // UNKNOWN type is used for users that have only email (e.g. in tests
92 // or legacy users that have not run through migration code).
93 // Bail if there is no user email.
94 if (user_email.empty())
95 return false;
96
97 *out = AccountId::FromUserEmail(user_email);
98 break;
99 }
100
101 return out->is_valid();
102 }
103 };
104
105 } // namespace mojo
106
107 #endif // COMPONENTS_SIGNIN_PUBLIC_INTERFACES_ACCOUNT_ID_TRAITS_H_
OLDNEW
« no previous file with comments | « components/signin/public/interfaces/account_id.typemap ('k') | components/typemaps.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698