Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / supervised_user / child_accounts / family_info_fetcher.h
blobf9b6a7d7cff97bab77fc57e5df6a9019162b64cc
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_FAMILY_INFO_FETCHER_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_FAMILY_INFO_FETCHER_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "google_apis/gaia/oauth2_token_service.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_fetcher_delegate.h"
18 namespace base {
19 class DictionaryValue;
20 class ListValue;
21 class Time;
24 namespace net {
25 class URLRequestContextGetter;
28 class FamilyInfoFetcher : public OAuth2TokenService::Observer,
29 public OAuth2TokenService::Consumer,
30 public net::URLFetcherDelegate {
31 public:
32 enum ErrorCode {
33 TOKEN_ERROR, // Failed to get OAuth2 token.
34 NETWORK_ERROR, // Network failure.
35 SERVICE_ERROR, // Service returned an error or malformed reply.
37 enum FamilyMemberRole {
38 HEAD_OF_HOUSEHOLD = 0,
39 PARENT,
40 MEMBER,
41 CHILD
43 struct FamilyProfile {
44 FamilyProfile();
45 FamilyProfile(const std::string& id, const std::string& name);
46 ~FamilyProfile();
47 std::string id;
48 std::string name;
50 struct FamilyMember {
51 FamilyMember();
52 FamilyMember(const std::string& obfuscated_gaia_id,
53 FamilyMemberRole role,
54 const std::string& display_name,
55 const std::string& email,
56 const std::string& profile_url,
57 const std::string& profile_image_url);
58 ~FamilyMember();
59 std::string obfuscated_gaia_id;
60 FamilyMemberRole role;
61 // All of the following may be empty.
62 std::string display_name;
63 std::string email;
64 std::string profile_url;
65 std::string profile_image_url;
68 class Consumer {
69 public:
70 virtual void OnGetFamilyProfileSuccess(const FamilyProfile& family) {}
71 virtual void OnGetFamilyMembersSuccess(
72 const std::vector<FamilyMember>& members) {}
73 virtual void OnFailure(ErrorCode error) {}
76 FamilyInfoFetcher(Consumer* consumer,
77 const std::string& account_id,
78 OAuth2TokenService* token_service,
79 net::URLRequestContextGetter* request_context);
80 ~FamilyInfoFetcher() override;
82 // Public so tests can use them.
83 static std::string RoleToString(FamilyMemberRole role);
84 static bool StringToRole(const std::string& str, FamilyMemberRole* role);
86 void StartGetFamilyProfile();
87 void StartGetFamilyMembers();
89 private:
90 // OAuth2TokenService::Observer implementation:
91 void OnRefreshTokenAvailable(const std::string& account_id) override;
92 void OnRefreshTokensLoaded() override;
94 // OAuth2TokenService::Consumer implementation:
95 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
96 const std::string& access_token,
97 const base::Time& expiration_time) override;
98 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
99 const GoogleServiceAuthError& error) override;
101 // net::URLFetcherDelegate implementation.
102 void OnURLFetchComplete(const net::URLFetcher* source) override;
104 static bool ParseMembers(const base::ListValue* list,
105 std::vector<FamilyMember>* members);
106 static bool ParseMember(const base::DictionaryValue* dict,
107 FamilyMember* member);
108 static void ParseProfile(const base::DictionaryValue* dict,
109 FamilyMember* member);
111 void StartFetching();
112 void StartFetchingAccessToken();
113 void FamilyProfileFetched(const std::string& response);
114 void FamilyMembersFetched(const std::string& response);
116 Consumer* consumer_;
117 const std::string account_id_;
118 OAuth2TokenService* token_service_;
119 net::URLRequestContextGetter* request_context_;
121 std::string request_suffix_;
122 net::URLFetcher::RequestType request_type_;
123 scoped_ptr<OAuth2TokenService::Request> access_token_request_;
124 std::string access_token_;
125 bool access_token_expired_;
126 scoped_ptr<net::URLFetcher> url_fetcher_;
128 DISALLOW_COPY_AND_ASSIGN(FamilyInfoFetcher);
131 #endif // CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_FAMILY_INFO_FETCHER_H_