OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_IMPL_H |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_IMPL_H |
7 | 7 |
8 #include "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
12 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | 9 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" |
10 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | |
11 #include "components/proximity_auth/cryptauth/cryptauth_client.h" | |
13 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
14 #include "url/gurl.h" | |
15 | 13 |
16 namespace proximity_auth { | 14 namespace proximity_auth { |
17 | 15 |
18 class CryptAuthAccessTokenFetcher; | 16 // Implementation of CryptAuthClient. |
19 class CryptAuthApiCallFlow; | |
20 | |
21 // Use CryptAuthClient to make API requests to the CryptAuth service, which | |
22 // manages cryptographic credentials (ie. public keys) for a user's devices. | |
23 // CryptAuthClient only processes one request, so create a new instance for each | |
24 // request you make. DO NOT REUSE. | |
25 // For documentation on each API call, see | |
26 // components/proximity_auth/cryptauth/proto/cryptauth_api.proto | |
27 // Note: There is no need to set the |device_classifier| field in request | 17 // Note: There is no need to set the |device_classifier| field in request |
28 // messages. CryptAuthClient will fill this field for all requests. | 18 // messages. CryptAuthClient will fill this field for all requests. |
29 class CryptAuthClient { | 19 class CryptAuthClientImpl : public CryptAuthClient { |
30 public: | 20 public: |
31 typedef base::Callback<void(const std::string&)> ErrorCallback; | 21 typedef base::Callback<void(const std::string&)> ErrorCallback; |
32 | 22 |
33 // Creates the client using |url_request_context| to make the HTTP request. | 23 // Creates the client using |url_request_context| to make the HTTP request |
34 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides | 24 // through |api_call_flow|. CryptAuthClientImpl takes ownership of |
35 // the access token authorizing CryptAuth requests. | 25 // |access_token_fetcher|, which provides the access token authorizing |
36 // The |device_classifier| argument contains basic device information of the | 26 // CryptAuth requests. The |device_classifier| argument contains basic device |
37 // caller (e.g. version and device type). | 27 // information of the caller (e.g. version and device type). |
38 CryptAuthClient( | 28 CryptAuthClientImpl( |
29 scoped_ptr<CryptAuthApiCallFlow> api_call_flow, | |
39 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, | 30 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, |
40 scoped_refptr<net::URLRequestContextGetter> url_request_context, | 31 scoped_refptr<net::URLRequestContextGetter> url_request_context, |
41 const cryptauth::DeviceClassifier& device_classifier); | 32 const cryptauth::DeviceClassifier& device_classifier); |
42 virtual ~CryptAuthClient(); | 33 ~CryptAuthClientImpl() override; |
43 | 34 |
44 // GetMyDevices | 35 // CryptAuthClient: |
45 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> | |
46 GetMyDevicesCallback; | |
47 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, | 36 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, |
48 const GetMyDevicesCallback& callback, | 37 const GetMyDevicesCallback& callback, |
49 const ErrorCallback& error_callback); | 38 const ErrorCallback& error_callback) override; |
50 | |
51 // FindEligibleUnlockDevices | |
52 typedef base::Callback<void( | |
53 const cryptauth::FindEligibleUnlockDevicesResponse&)> | |
54 FindEligibleUnlockDevicesCallback; | |
55 void FindEligibleUnlockDevices( | 39 void FindEligibleUnlockDevices( |
56 const cryptauth::FindEligibleUnlockDevicesRequest& request, | 40 const cryptauth::FindEligibleUnlockDevicesRequest& request, |
57 const FindEligibleUnlockDevicesCallback& callback, | 41 const FindEligibleUnlockDevicesCallback& callback, |
58 const ErrorCallback& error_callback); | 42 const ErrorCallback& error_callback) override; |
59 | |
60 // SendDeviceSyncTickle | |
61 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> | |
62 SendDeviceSyncTickleCallback; | |
63 void SendDeviceSyncTickle( | 43 void SendDeviceSyncTickle( |
64 const cryptauth::SendDeviceSyncTickleRequest& request, | 44 const cryptauth::SendDeviceSyncTickleRequest& request, |
65 const SendDeviceSyncTickleCallback& callback, | 45 const SendDeviceSyncTickleCallback& callback, |
66 const ErrorCallback& error_callback); | 46 const ErrorCallback& error_callback) override; |
67 | |
68 // ToggleEasyUnlock | |
69 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> | |
70 ToggleEasyUnlockCallback; | |
71 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, | 47 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, |
72 const ToggleEasyUnlockCallback& callback, | 48 const ToggleEasyUnlockCallback& callback, |
73 const ErrorCallback& error_callback); | 49 const ErrorCallback& error_callback) override; |
74 | |
75 // SetupEnrollment | |
76 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> | |
77 SetupEnrollmentCallback; | |
78 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, | 50 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, |
79 const SetupEnrollmentCallback& callback, | 51 const SetupEnrollmentCallback& callback, |
80 const ErrorCallback& error_callback); | 52 const ErrorCallback& error_callback) override; |
81 | |
82 // FinishEnrollment | |
83 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> | |
84 FinishEnrollmentCallback; | |
85 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, | 53 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, |
86 const FinishEnrollmentCallback& callback, | 54 const FinishEnrollmentCallback& callback, |
87 const ErrorCallback& error_callback); | 55 const ErrorCallback& error_callback) override; |
88 | |
89 protected: | |
90 // Creates a CryptAuthApiCallFlow object. Exposed for testing. | |
91 virtual scoped_ptr<CryptAuthApiCallFlow> CreateFlow(const GURL& request_url); | |
92 | 56 |
93 private: | 57 private: |
94 // Starts a call to the API given by |request_path|, with the templated | 58 // Starts a call to the API given by |request_path|, with the templated |
95 // request and response types. The client first fetches the access token and | 59 // request and response types. The client first fetches the access token and |
96 // then makes the HTTP request. | 60 // then makes the HTTP request. |
97 template <class RequestProto, class ResponseProto> | 61 template <class RequestProto, class ResponseProto> |
98 void MakeApiCall( | 62 void MakeApiCall( |
99 const std::string& request_path, | 63 const std::string& request_path, |
100 const RequestProto& request_proto, | 64 const RequestProto& request_proto, |
101 const base::Callback<void(const ResponseProto&)>& response_callback, | 65 const base::Callback<void(const ResponseProto&)>& response_callback, |
102 const ErrorCallback& error_callback); | 66 const ErrorCallback& error_callback); |
103 | 67 |
104 // Called when the access token is obtained so the API request can be made. | 68 // Called when the access token is obtained so the API request can be made. |
105 template <class ResponseProto> | 69 template <class ResponseProto> |
106 void OnAccessTokenFetched( | 70 void OnAccessTokenFetched( |
107 const std::string& serialized_request, | 71 const std::string& serialized_request, |
108 const base::Callback<void(const ResponseProto&)>& response_callback, | 72 const base::Callback<void(const ResponseProto&)>& response_callback, |
109 const std::string& access_token); | 73 const std::string& access_token); |
110 | 74 |
111 // Called with CryptAuthApiCallFlow completes successfully to deserialize and | 75 // Called with CryptAuthApiCallFlow completes successfully to deserialize and |
112 // return the result. | 76 // return the result. |
113 template <class ResponseProto> | 77 template <class ResponseProto> |
114 void OnFlowSuccess( | 78 void OnFlowSuccess( |
115 const base::Callback<void(const ResponseProto&)>& result_callback, | 79 const base::Callback<void(const ResponseProto&)>& result_callback, |
116 const std::string& serialized_response); | 80 const std::string& serialized_response); |
117 | 81 |
118 // Called when the current API call fails at any step. | 82 // Called when the current API call fails at any step. |
119 void OnApiCallFailed(const std::string& error_message); | 83 void OnApiCallFailed(const std::string& error_message); |
120 | 84 |
121 // The context for network requests. | 85 // Constructs and executes the actual HTTP request. |
122 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | 86 scoped_ptr<CryptAuthApiCallFlow> api_call_flow_; |
123 | 87 |
124 // Fetches the access token authorizing the API calls. | 88 // Fetches the access token authorizing the API calls. |
125 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; | 89 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; |
126 | 90 |
91 // The context for network requests. | |
92 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
93 | |
127 // Contains basic device info of the client making the request that is sent to | 94 // Contains basic device info of the client making the request that is sent to |
128 // CryptAuth with each API call. | 95 // CryptAuth with each API call. |
129 const cryptauth::DeviceClassifier device_classifier_; | 96 const cryptauth::DeviceClassifier device_classifier_; |
130 | 97 |
131 // Handles the current API call. | 98 // True if an API call has been started. Remains true even after the API call |
132 scoped_ptr<CryptAuthApiCallFlow> flow_; | 99 // completes. |
100 bool call_started_; | |
Ilya Sherman
2015/04/06 18:53:34
nit: I'd add a "has_" prefix, to emphasize that th
Tim Song
2015/04/06 23:21:17
Done.
| |
133 | 101 |
134 // URL path of the current request. | 102 // URL path of the current request. |
135 std::string request_path_; | 103 std::string request_path_; |
136 | 104 |
137 // Called when the current request fails. | 105 // Called when the current request fails. |
138 ErrorCallback error_callback_; | 106 ErrorCallback error_callback_; |
139 | 107 |
140 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; | 108 base::WeakPtrFactory<CryptAuthClientImpl> weak_ptr_factory_; |
141 | 109 |
142 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); | 110 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientImpl); |
143 }; | 111 }; |
144 | 112 |
145 } // namespace proximity_auth | 113 } // namespace proximity_auth |
146 | 114 |
147 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 115 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_IMPL_H |
OLD | NEW |