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 class CryptAuthClient { | |
28 public: | |
29 typedef base::Callback<void(const std::string&)> ErrorCallback; | |
30 | |
31 // Creates the client using |url_request_context| to make the HTTP request. | |
32 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides | |
33 // the access token authorizing CryptAuth requests. | |
34 CryptAuthClient( | |
35 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, | |
36 scoped_refptr<net::URLRequestContextGetter> url_request_context); | |
37 virtual ~CryptAuthClient(); | |
38 | |
39 // The GetMyDevices API request returns a list of devices that the user has | |
40 // registered with CryptAuth. | |
41 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> | |
42 GetMyDevicesCallback; | |
43 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, | |
44 const GetMyDevicesCallback& callback, | |
45 const ErrorCallback& error_callback); | |
46 | |
47 // The FindEligibleUnlockDevices API request returns a list of devices | |
48 // eligible to be an "unlock key", that is a device capable of unlocking other | |
49 // devices. | |
50 // The |bluetooth_address| field should contain the Bluetooth address of the | |
Ilya Sherman
2014/12/03 03:16:37
I think it's really weird to refer to |bluetooth_a
Tim Song
2014/12/05 00:00:36
I get what you're saying. I removed all the docume
| |
51 // local device, which is sent to the eligible devices so they can connect to | |
52 // the local device. | |
53 typedef base::Callback<void( | |
54 const cryptauth::FindEligibleUnlockDevicesResponse&)> | |
55 FindEligibleUnlockDevicesCallback; | |
56 void FindEligibleUnlockDevices( | |
57 const cryptauth::FindEligibleUnlockDevicesRequest& request, | |
58 const FindEligibleUnlockDevicesCallback& callback, | |
59 const ErrorCallback& error_callback); | |
60 | |
61 // The SendDeviceSyncTickle API request tells the server to "tickle" all | |
62 // the user's registered devices, so they can sync the latest device state. | |
63 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> | |
64 SendDeviceSyncTickleCallback; | |
65 void SendDeviceSyncTickle( | |
66 const cryptauth::SendDeviceSyncTickleRequest& request, | |
67 const SendDeviceSyncTickleCallback& callback, | |
68 const ErrorCallback& error_callback); | |
69 | |
70 // The ToggleEasyUnlock API request tells the server to designate devices as | |
71 // an "unlock key" that can unlock other devices. The |turn_on| argument is | |
72 // used to enable or disable the device given by its |public_key|. If | |
73 // |apply_to_all| is true, then |public_key| will be ignored, and |turn_on| | |
74 // will be applied to all eligible unlock keys. | |
75 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> | |
76 ToggleEasyUnlockCallback; | |
77 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, | |
78 const ToggleEasyUnlockCallback& callback, | |
79 const ErrorCallback& error_callback); | |
80 | |
81 // The SetupEnrollment API request is the first of a two step process to | |
82 // enroll the device with CryptAuth. See FinishEnrollment for the last step of | |
83 // enrollment. | |
84 // You most likely will not use this API directly. Look instead at | |
85 // CryptAuthEnroller, which handles the entire enrollment operation. | |
86 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> | |
87 SetupEnrollmentCallback; | |
88 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, | |
89 const SetupEnrollmentCallback& callback, | |
90 const ErrorCallback& error_callback); | |
91 | |
92 // The FinishEnrollment API request is the last of a two step process to | |
93 // enroll the device with CryptAuth. See for the SetupEnrollment for the | |
Ilya Sherman
2014/12/03 03:16:37
nit: "See for the" -> "See"
Tim Song
2014/12/05 00:00:36
Done.
| |
94 // first step of enrollment. | |
95 // You most likely will not use this API directly. Look instead at | |
96 // CryptAuthEnroller, which handles the entire enrollment operation. | |
97 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> | |
98 FinishEnrollmentCallback; | |
99 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, | |
100 const FinishEnrollmentCallback& callback, | |
101 const ErrorCallback& error_callback); | |
102 | |
103 protected: | |
104 // Creates a CryptAuthApiCallFlow object and takes ownership of it. | |
Ilya Sherman
2014/12/03 03:16:37
nit: I'm not sure what "and takes ownership of it"
Tim Song
2014/12/05 00:00:36
Because the function is protected, the caller will
| |
105 // Exposed for testing. | |
106 virtual CryptAuthApiCallFlow* CreateFlow(const GURL& request_url); | |
Ilya Sherman
2014/12/03 03:16:37
Please return a scoped_ptr here.
Tim Song
2014/12/05 00:00:36
Done.
| |
107 | |
108 private: | |
109 // Starts a call to the API given by |request_path|, with the templated | |
110 // request and response types. The client first fetches the access token and | |
111 // then makes the HTTP request. | |
112 template <class RequestProto, class ResponseProto> | |
113 void MakeApiCall( | |
114 const std::string& request_path, | |
115 const RequestProto& request_proto, | |
116 const base::Callback<void(const ResponseProto&)>& response_callback, | |
117 const ErrorCallback& error_callback); | |
118 | |
119 // Called when the access token is obtained so the API request can be made. | |
120 template <class ResponseProto> | |
121 void OnAccessTokenFetched( | |
122 const std::string& serialized_request, | |
123 const base::Callback<void(const ResponseProto&)>& response_callback, | |
124 const std::string& access_token); | |
125 | |
126 // Called with CryptAuthApiCallFlow completes successfully to deserialize and | |
127 // return the result. | |
128 template <class ResponseProto> | |
129 void OnFlowSuccess( | |
130 const base::Callback<void(const ResponseProto&)>& result_callback, | |
131 const std::string& serialized_response); | |
132 | |
133 // Called when the current API call fails at any step. | |
134 void OnApiCallFailed(const std::string& error_message); | |
135 | |
136 // The context for network requests. | |
137 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
138 | |
139 // Fetchs the access token authorizing the API calls. | |
Ilya Sherman
2014/12/03 03:16:37
nit: "Fetchs" -> "Fetches"
Tim Song
2014/12/05 00:00:36
Done.
| |
140 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; | |
141 | |
142 // Handles the current API call. | |
143 scoped_ptr<CryptAuthApiCallFlow> flow_; | |
144 | |
145 // URL path of the current request. | |
146 std::string request_path_; | |
147 | |
148 // Called when the current request fails. | |
149 ErrorCallback error_callback_; | |
150 | |
151 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); | |
154 }; | |
155 | |
156 } // namespace proximity_auth | |
157 | |
158 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | |
OLD | NEW |