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

Side by Side Diff: components/proximity_auth/cryptauth/cryptauth_client_impl.h

Issue 1066453002: Refactor CryptAuth component to be more testable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cryptauth_securemessage
Patch Set: moved classes to impl files Created 5 years, 8 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
OLDNEW
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" 13
14 class OAuth2TokenService;
15 15
16 namespace proximity_auth { 16 namespace proximity_auth {
17 17
18 class CryptAuthAccessTokenFetcher; 18 // 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 19 // Note: There is no need to set the |device_classifier| field in request
28 // messages. CryptAuthClient will fill this field for all requests. 20 // messages. CryptAuthClient will fill this field for all requests.
29 class CryptAuthClient { 21 class CryptAuthClientImpl : public CryptAuthClient {
30 public: 22 public:
31 typedef base::Callback<void(const std::string&)> ErrorCallback; 23 typedef base::Callback<void(const std::string&)> ErrorCallback;
32 24
33 // Creates the client using |url_request_context| to make the HTTP request. 25 // Creates the client using |url_request_context| to make the HTTP request
34 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides 26 // through |api_call_flow|. CryptAuthClientImpl takes ownership of
35 // the access token authorizing CryptAuth requests. 27 // |access_token_fetcher|, which provides the access token authorizing
36 // The |device_classifier| argument contains basic device information of the 28 // CryptAuth requests. The |device_classifier| argument contains basic device
37 // caller (e.g. version and device type). 29 // information of the caller (e.g. version and device type).
38 CryptAuthClient( 30 CryptAuthClientImpl(
31 scoped_ptr<CryptAuthApiCallFlow> api_call_flow,
39 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, 32 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
40 scoped_refptr<net::URLRequestContextGetter> url_request_context, 33 scoped_refptr<net::URLRequestContextGetter> url_request_context,
41 const cryptauth::DeviceClassifier& device_classifier); 34 const cryptauth::DeviceClassifier& device_classifier);
42 virtual ~CryptAuthClient(); 35 ~CryptAuthClientImpl() override;
43 36
44 // GetMyDevices 37 // CryptAuthClient:
45 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)>
46 GetMyDevicesCallback;
47 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, 38 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request,
48 const GetMyDevicesCallback& callback, 39 const GetMyDevicesCallback& callback,
49 const ErrorCallback& error_callback); 40 const ErrorCallback& error_callback) override;
50
51 // FindEligibleUnlockDevices
52 typedef base::Callback<void(
53 const cryptauth::FindEligibleUnlockDevicesResponse&)>
54 FindEligibleUnlockDevicesCallback;
55 void FindEligibleUnlockDevices( 41 void FindEligibleUnlockDevices(
56 const cryptauth::FindEligibleUnlockDevicesRequest& request, 42 const cryptauth::FindEligibleUnlockDevicesRequest& request,
57 const FindEligibleUnlockDevicesCallback& callback, 43 const FindEligibleUnlockDevicesCallback& callback,
58 const ErrorCallback& error_callback); 44 const ErrorCallback& error_callback) override;
59
60 // SendDeviceSyncTickle
61 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)>
62 SendDeviceSyncTickleCallback;
63 void SendDeviceSyncTickle( 45 void SendDeviceSyncTickle(
64 const cryptauth::SendDeviceSyncTickleRequest& request, 46 const cryptauth::SendDeviceSyncTickleRequest& request,
65 const SendDeviceSyncTickleCallback& callback, 47 const SendDeviceSyncTickleCallback& callback,
66 const ErrorCallback& error_callback); 48 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, 49 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request,
72 const ToggleEasyUnlockCallback& callback, 50 const ToggleEasyUnlockCallback& callback,
73 const ErrorCallback& error_callback); 51 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, 52 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request,
79 const SetupEnrollmentCallback& callback, 53 const SetupEnrollmentCallback& callback,
80 const ErrorCallback& error_callback); 54 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, 55 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request,
86 const FinishEnrollmentCallback& callback, 56 const FinishEnrollmentCallback& callback,
87 const ErrorCallback& error_callback); 57 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 58
93 private: 59 private:
94 // Starts a call to the API given by |request_path|, with the templated 60 // 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 61 // request and response types. The client first fetches the access token and
96 // then makes the HTTP request. 62 // then makes the HTTP request.
97 template <class RequestProto, class ResponseProto> 63 template <class RequestProto, class ResponseProto>
98 void MakeApiCall( 64 void MakeApiCall(
99 const std::string& request_path, 65 const std::string& request_path,
100 const RequestProto& request_proto, 66 const RequestProto& request_proto,
101 const base::Callback<void(const ResponseProto&)>& response_callback, 67 const base::Callback<void(const ResponseProto&)>& response_callback,
102 const ErrorCallback& error_callback); 68 const ErrorCallback& error_callback);
103 69
104 // Called when the access token is obtained so the API request can be made. 70 // Called when the access token is obtained so the API request can be made.
105 template <class ResponseProto> 71 template <class ResponseProto>
106 void OnAccessTokenFetched( 72 void OnAccessTokenFetched(
107 const std::string& serialized_request, 73 const std::string& serialized_request,
108 const base::Callback<void(const ResponseProto&)>& response_callback, 74 const base::Callback<void(const ResponseProto&)>& response_callback,
109 const std::string& access_token); 75 const std::string& access_token);
110 76
111 // Called with CryptAuthApiCallFlow completes successfully to deserialize and 77 // Called with CryptAuthApiCallFlow completes successfully to deserialize and
112 // return the result. 78 // return the result.
113 template <class ResponseProto> 79 template <class ResponseProto>
114 void OnFlowSuccess( 80 void OnFlowSuccess(
115 const base::Callback<void(const ResponseProto&)>& result_callback, 81 const base::Callback<void(const ResponseProto&)>& result_callback,
116 const std::string& serialized_response); 82 const std::string& serialized_response);
117 83
118 // Called when the current API call fails at any step. 84 // Called when the current API call fails at any step.
119 void OnApiCallFailed(const std::string& error_message); 85 void OnApiCallFailed(const std::string& error_message);
120 86
121 // The context for network requests. 87 // Constructs and executes the actual HTTP request.
122 scoped_refptr<net::URLRequestContextGetter> url_request_context_; 88 scoped_ptr<CryptAuthApiCallFlow> api_call_flow_;
123 89
124 // Fetches the access token authorizing the API calls. 90 // Fetches the access token authorizing the API calls.
125 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; 91 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_;
126 92
93 // The context for network requests.
94 scoped_refptr<net::URLRequestContextGetter> url_request_context_;
95
127 // Contains basic device info of the client making the request that is sent to 96 // Contains basic device info of the client making the request that is sent to
128 // CryptAuth with each API call. 97 // CryptAuth with each API call.
129 const cryptauth::DeviceClassifier device_classifier_; 98 const cryptauth::DeviceClassifier device_classifier_;
130 99
131 // Handles the current API call. 100 // True if an API call has been started. Remains true even after the API call
132 scoped_ptr<CryptAuthApiCallFlow> flow_; 101 // completes.
102 bool has_call_started_;
133 103
134 // URL path of the current request. 104 // URL path of the current request.
135 std::string request_path_; 105 std::string request_path_;
136 106
137 // Called when the current request fails. 107 // Called when the current request fails.
138 ErrorCallback error_callback_; 108 ErrorCallback error_callback_;
139 109
140 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; 110 base::WeakPtrFactory<CryptAuthClientImpl> weak_ptr_factory_;
141 111
142 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); 112 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientImpl);
113 };
114
115 // Implementation of CryptAuthClientFactory.
116 class CryptAuthClientFactoryImpl : public CryptAuthClientFactory {
117 public:
118 CryptAuthClientFactoryImpl(
119 OAuth2TokenService* token_service,
Ilya Sherman 2015/04/06 23:48:02 Please document lifetime expectations. Actually,
Tim Song 2015/04/07 02:22:57 Done.
120 const std::string& account_id,
121 scoped_refptr<net::URLRequestContextGetter> url_request_context,
122 const cryptauth::DeviceClassifier& device_classifier);
123 ~CryptAuthClientFactoryImpl() override;
124
125 // CryptAuthClientFactory:
126 scoped_ptr<CryptAuthClient> CreateInstance() override;
127
128 private:
129 OAuth2TokenService* token_service_;
130 const std::string account_id_;
131 const scoped_refptr<net::URLRequestContextGetter> url_request_context_;
132 const cryptauth::DeviceClassifier device_classifier_;
133
134 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientFactoryImpl);
143 }; 135 };
144 136
145 } // namespace proximity_auth 137 } // namespace proximity_auth
146 138
147 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H 139 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_IMPL_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698