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_CRYPT_AUTH_CLIENT_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H |
| 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" |
| 12 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" |
| 13 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 class OAuth2TokenService; |
| 17 |
| 18 namespace proximity_auth { |
| 19 |
| 20 class CryptAuthAccessTokenFetcher; |
| 21 class CryptAuthApiCallFlow; |
| 22 |
| 23 // Use CryptAuthClient to make API requests to the CryptAuth service, which |
| 24 // manages cryptographic credentials (ie. public keys) for a user's devices. |
| 25 // CryptAuthClient only processes one request, so create a new instance for each |
| 26 // request you make. DO NOT REUSE. |
| 27 // For documentation on each API call, see |
| 28 // components/proximity_auth/cryptauth/proto/cryptauth_api.proto |
| 29 class CryptAuthClient { |
| 30 public: |
| 31 typedef base::Callback<void(const std::string&)> ErrorCallback; |
| 32 |
| 33 // Creates the client using |url_request_context| to make the HTTP request. |
| 34 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides |
| 35 // the access token authorizing CryptAuth requests. |
| 36 CryptAuthClient( |
| 37 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, |
| 38 scoped_refptr<net::URLRequestContextGetter> url_request_context); |
| 39 virtual ~CryptAuthClient(); |
| 40 |
| 41 // GetMyDevices |
| 42 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> |
| 43 GetMyDevicesCallback; |
| 44 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, |
| 45 const GetMyDevicesCallback& callback, |
| 46 const ErrorCallback& error_callback); |
| 47 |
| 48 // FindEligibleUnlockDevices |
| 49 typedef base::Callback<void( |
| 50 const cryptauth::FindEligibleUnlockDevicesResponse&)> |
| 51 FindEligibleUnlockDevicesCallback; |
| 52 void FindEligibleUnlockDevices( |
| 53 const cryptauth::FindEligibleUnlockDevicesRequest& request, |
| 54 const FindEligibleUnlockDevicesCallback& callback, |
| 55 const ErrorCallback& error_callback); |
| 56 |
| 57 // SendDeviceSyncTickle |
| 58 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> |
| 59 SendDeviceSyncTickleCallback; |
| 60 void SendDeviceSyncTickle( |
| 61 const cryptauth::SendDeviceSyncTickleRequest& request, |
| 62 const SendDeviceSyncTickleCallback& callback, |
| 63 const ErrorCallback& error_callback); |
| 64 |
| 65 // ToggleEasyUnlock |
| 66 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> |
| 67 ToggleEasyUnlockCallback; |
| 68 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, |
| 69 const ToggleEasyUnlockCallback& callback, |
| 70 const ErrorCallback& error_callback); |
| 71 |
| 72 // SetupEnrollment |
| 73 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> |
| 74 SetupEnrollmentCallback; |
| 75 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, |
| 76 const SetupEnrollmentCallback& callback, |
| 77 const ErrorCallback& error_callback); |
| 78 |
| 79 // FinishEnrollment |
| 80 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> |
| 81 FinishEnrollmentCallback; |
| 82 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, |
| 83 const FinishEnrollmentCallback& callback, |
| 84 const ErrorCallback& error_callback); |
| 85 |
| 86 protected: |
| 87 // Creates a CryptAuthApiCallFlow object. Exposed for testing. |
| 88 virtual scoped_ptr<CryptAuthApiCallFlow> CreateFlow(const GURL& request_url); |
| 89 |
| 90 private: |
| 91 // Starts a call to the API given by |request_path|, with the templated |
| 92 // request and response types. The client first fetches the access token and |
| 93 // then makes the HTTP request. |
| 94 template <class RequestProto, class ResponseProto> |
| 95 void MakeApiCall( |
| 96 const std::string& request_path, |
| 97 const RequestProto& request_proto, |
| 98 const base::Callback<void(const ResponseProto&)>& response_callback, |
| 99 const ErrorCallback& error_callback); |
| 100 |
| 101 // Called when the access token is obtained so the API request can be made. |
| 102 template <class ResponseProto> |
| 103 void OnAccessTokenFetched( |
| 104 const std::string& serialized_request, |
| 105 const base::Callback<void(const ResponseProto&)>& response_callback, |
| 106 const std::string& access_token); |
| 107 |
| 108 // Called with CryptAuthApiCallFlow completes successfully to deserialize and |
| 109 // return the result. |
| 110 template <class ResponseProto> |
| 111 void OnFlowSuccess( |
| 112 const base::Callback<void(const ResponseProto&)>& result_callback, |
| 113 const std::string& serialized_response); |
| 114 |
| 115 // Called when the current API call fails at any step. |
| 116 void OnApiCallFailed(const std::string& error_message); |
| 117 |
| 118 // The context for network requests. |
| 119 scoped_refptr<net::URLRequestContextGetter> url_request_context_; |
| 120 |
| 121 // Fetches the access token authorizing the API calls. |
| 122 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; |
| 123 |
| 124 // Handles the current API call. |
| 125 scoped_ptr<CryptAuthApiCallFlow> flow_; |
| 126 |
| 127 // URL path of the current request. |
| 128 std::string request_path_; |
| 129 |
| 130 // Called when the current request fails. |
| 131 ErrorCallback error_callback_; |
| 132 |
| 133 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); |
| 136 }; |
| 137 |
| 138 } // namespace proximity_auth |
| 139 |
| 140 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H |
OLD | NEW |