Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_client.h
blob0e77e5025b857f37379a82fc4cdf8657d64c64d7
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 COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H
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"
16 namespace proximity_auth {
18 class CryptAuthAccessTokenFetcher;
19 class CryptAuthApiCallFlow;
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
28 // messages. CryptAuthClient will fill this field for all requests.
29 class CryptAuthClient {
30 public:
31 typedef base::Callback<void(const std::string&)> ErrorCallback;
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 // The |device_classifier| argument contains basic device information of the
37 // caller (e.g. version and device type).
38 CryptAuthClient(
39 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
40 scoped_refptr<net::URLRequestContextGetter> url_request_context,
41 const cryptauth::DeviceClassifier& device_classifier);
42 virtual ~CryptAuthClient();
44 // GetMyDevices
45 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)>
46 GetMyDevicesCallback;
47 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request,
48 const GetMyDevicesCallback& callback,
49 const ErrorCallback& error_callback);
51 // FindEligibleUnlockDevices
52 typedef base::Callback<void(
53 const cryptauth::FindEligibleUnlockDevicesResponse&)>
54 FindEligibleUnlockDevicesCallback;
55 void FindEligibleUnlockDevices(
56 const cryptauth::FindEligibleUnlockDevicesRequest& request,
57 const FindEligibleUnlockDevicesCallback& callback,
58 const ErrorCallback& error_callback);
60 // SendDeviceSyncTickle
61 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)>
62 SendDeviceSyncTickleCallback;
63 void SendDeviceSyncTickle(
64 const cryptauth::SendDeviceSyncTickleRequest& request,
65 const SendDeviceSyncTickleCallback& callback,
66 const ErrorCallback& error_callback);
68 // ToggleEasyUnlock
69 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)>
70 ToggleEasyUnlockCallback;
71 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request,
72 const ToggleEasyUnlockCallback& callback,
73 const ErrorCallback& error_callback);
75 // SetupEnrollment
76 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)>
77 SetupEnrollmentCallback;
78 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request,
79 const SetupEnrollmentCallback& callback,
80 const ErrorCallback& error_callback);
82 // FinishEnrollment
83 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)>
84 FinishEnrollmentCallback;
85 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request,
86 const FinishEnrollmentCallback& callback,
87 const ErrorCallback& error_callback);
89 protected:
90 // Creates a CryptAuthApiCallFlow object. Exposed for testing.
91 virtual scoped_ptr<CryptAuthApiCallFlow> CreateFlow(const GURL& request_url);
93 private:
94 // 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
96 // then makes the HTTP request.
97 template <class RequestProto, class ResponseProto>
98 void MakeApiCall(
99 const std::string& request_path,
100 const RequestProto& request_proto,
101 const base::Callback<void(const ResponseProto&)>& response_callback,
102 const ErrorCallback& error_callback);
104 // Called when the access token is obtained so the API request can be made.
105 template <class ResponseProto>
106 void OnAccessTokenFetched(
107 const std::string& serialized_request,
108 const base::Callback<void(const ResponseProto&)>& response_callback,
109 const std::string& access_token);
111 // Called with CryptAuthApiCallFlow completes successfully to deserialize and
112 // return the result.
113 template <class ResponseProto>
114 void OnFlowSuccess(
115 const base::Callback<void(const ResponseProto&)>& result_callback,
116 const std::string& serialized_response);
118 // Called when the current API call fails at any step.
119 void OnApiCallFailed(const std::string& error_message);
121 // The context for network requests.
122 scoped_refptr<net::URLRequestContextGetter> url_request_context_;
124 // Fetches the access token authorizing the API calls.
125 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_;
127 // Contains basic device info of the client making the request that is sent to
128 // CryptAuth with each API call.
129 const cryptauth::DeviceClassifier device_classifier_;
131 // Handles the current API call.
132 scoped_ptr<CryptAuthApiCallFlow> flow_;
134 // URL path of the current request.
135 std::string request_path_;
137 // Called when the current request fails.
138 ErrorCallback error_callback_;
140 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_;
142 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient);
145 } // namespace proximity_auth
147 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H