OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" | |
11 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | |
12 #include "components/proximity_auth/cryptauth/cryptauth_client.h" | |
13 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | |
14 #include "net/url_request/url_request_context_getter.h" | |
15 | |
16 class OAuth2TokenService; | |
17 | |
18 namespace proximity_auth { | |
19 | |
20 // Implementation of CryptAuthClient. | |
21 // Note: There is no need to set the |device_classifier| field in request | |
22 // messages. CryptAuthClient will fill this field for all requests. | |
23 class CryptAuthClientImpl : public CryptAuthClient { | |
24 public: | |
25 typedef base::Callback<void(const std::string&)> ErrorCallback; | |
26 | |
27 // Creates the client using |url_request_context| to make the HTTP request | |
28 // through |api_call_flow|. CryptAuthClientImpl takes ownership of | |
29 // |access_token_fetcher|, which provides the access token authorizing | |
30 // CryptAuth requests. The |device_classifier| argument contains basic device | |
31 // information of the caller (e.g. version and device type). | |
32 CryptAuthClientImpl( | |
33 std::unique_ptr<CryptAuthApiCallFlow> api_call_flow, | |
34 std::unique_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, | |
35 scoped_refptr<net::URLRequestContextGetter> url_request_context, | |
36 const cryptauth::DeviceClassifier& device_classifier); | |
37 ~CryptAuthClientImpl() override; | |
38 | |
39 // CryptAuthClient: | |
40 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, | |
41 const GetMyDevicesCallback& callback, | |
42 const ErrorCallback& error_callback) override; | |
43 void FindEligibleUnlockDevices( | |
44 const cryptauth::FindEligibleUnlockDevicesRequest& request, | |
45 const FindEligibleUnlockDevicesCallback& callback, | |
46 const ErrorCallback& error_callback) override; | |
47 void SendDeviceSyncTickle( | |
48 const cryptauth::SendDeviceSyncTickleRequest& request, | |
49 const SendDeviceSyncTickleCallback& callback, | |
50 const ErrorCallback& error_callback) override; | |
51 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, | |
52 const ToggleEasyUnlockCallback& callback, | |
53 const ErrorCallback& error_callback) override; | |
54 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, | |
55 const SetupEnrollmentCallback& callback, | |
56 const ErrorCallback& error_callback) override; | |
57 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, | |
58 const FinishEnrollmentCallback& callback, | |
59 const ErrorCallback& error_callback) override; | |
60 std::string GetAccessTokenUsed() override; | |
61 | |
62 private: | |
63 // Starts a call to the API given by |request_path|, with the templated | |
64 // request and response types. The client first fetches the access token and | |
65 // then makes the HTTP request. | |
66 template <class RequestProto, class ResponseProto> | |
67 void MakeApiCall( | |
68 const std::string& request_path, | |
69 const RequestProto& request_proto, | |
70 const base::Callback<void(const ResponseProto&)>& response_callback, | |
71 const ErrorCallback& error_callback); | |
72 | |
73 // Called when the access token is obtained so the API request can be made. | |
74 template <class ResponseProto> | |
75 void OnAccessTokenFetched( | |
76 const std::string& serialized_request, | |
77 const base::Callback<void(const ResponseProto&)>& response_callback, | |
78 const std::string& access_token); | |
79 | |
80 // Called with CryptAuthApiCallFlow completes successfully to deserialize and | |
81 // return the result. | |
82 template <class ResponseProto> | |
83 void OnFlowSuccess( | |
84 const base::Callback<void(const ResponseProto&)>& result_callback, | |
85 const std::string& serialized_response); | |
86 | |
87 // Called when the current API call fails at any step. | |
88 void OnApiCallFailed(const std::string& error_message); | |
89 | |
90 // Constructs and executes the actual HTTP request. | |
91 std::unique_ptr<CryptAuthApiCallFlow> api_call_flow_; | |
92 | |
93 // Fetches the access token authorizing the API calls. | |
94 std::unique_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; | |
95 | |
96 // The context for network requests. | |
97 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
98 | |
99 // Contains basic device info of the client making the request that is sent to | |
100 // CryptAuth with each API call. | |
101 const cryptauth::DeviceClassifier device_classifier_; | |
102 | |
103 // True if an API call has been started. Remains true even after the API call | |
104 // completes. | |
105 bool has_call_started_; | |
106 | |
107 // URL path of the current request. | |
108 std::string request_path_; | |
109 | |
110 // The access token fetched by |access_token_fetcher_|. | |
111 std::string access_token_used_; | |
112 | |
113 // Called when the current request fails. | |
114 ErrorCallback error_callback_; | |
115 | |
116 base::WeakPtrFactory<CryptAuthClientImpl> weak_ptr_factory_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientImpl); | |
119 }; | |
120 | |
121 // Implementation of CryptAuthClientFactory. | |
122 class CryptAuthClientFactoryImpl : public CryptAuthClientFactory { | |
123 public: | |
124 // |token_service|: Gets the user's access token. | |
125 // Not owned, so |token_service| needs to outlive this object. | |
126 // |account_id|: The account id of the user. | |
127 // |url_request_context|: The request context to make the HTTP requests. | |
128 // |device_classifier|: Contains basic device information of the client. | |
129 CryptAuthClientFactoryImpl( | |
130 OAuth2TokenService* token_service, | |
131 const std::string& account_id, | |
132 scoped_refptr<net::URLRequestContextGetter> url_request_context, | |
133 const cryptauth::DeviceClassifier& device_classifier); | |
134 ~CryptAuthClientFactoryImpl() override; | |
135 | |
136 // CryptAuthClientFactory: | |
137 std::unique_ptr<CryptAuthClient> CreateInstance() override; | |
138 | |
139 private: | |
140 OAuth2TokenService* token_service_; | |
141 const std::string account_id_; | |
142 const scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
143 const cryptauth::DeviceClassifier device_classifier_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientFactoryImpl); | |
146 }; | |
147 | |
148 } // namespace proximity_auth | |
149 | |
150 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H | |
OLD | NEW |