Chromium Code Reviews| 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" | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Can you forward-declare the necessary classes
Tim Song
2014/12/03 01:18:24
You will still have to include 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 // At most one request can be processed concurrently; the error callback will | |
| 26 // be invoked if you make a request when there is another pending. Create | |
| 27 // another CryptAuthClient if you want to make two requests at the same time. | |
| 28 class CryptAuthClient { | |
| 29 public: | |
| 30 typedef base::Callback<void(const std::string&)> ErrorCallback; | |
| 31 | |
| 32 CryptAuthClient(net::URLRequestContextGetter* url_request_context, | |
|
Ilya Sherman
2014/11/18 22:30:44
It looks like this is stored in a scoped_refptr be
Tim Song
2014/12/03 01:18:24
Done.
| |
| 33 CryptAuthAccessTokenFetcher* access_token_fetcher); | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please document, including lifetime expectati
Tim Song
2014/12/03 01:18:24
Done.
| |
| 34 virtual ~CryptAuthClient(); | |
| 35 | |
| 36 // The GetMyDevices API request returns a list of devices that the user has | |
| 37 // registered with CryptAuth. | |
| 38 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> | |
| 39 GetMyDevicesCallback; | |
| 40 void GetMyDevices(bool allow_stale_read, | |
| 41 GetMyDevicesCallback callback, | |
| 42 ErrorCallback error_callback); | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please pass callbacks by const-reference. (A
Tim Song
2014/12/03 01:18:24
Done.
| |
| 43 | |
| 44 // The FindEligibleUnlockDevices API request returns a list of devices | |
| 45 // eligible to be an "unlock key", that is a device capable of unlocking other | |
| 46 // devices. | |
| 47 // The |bluetooth_address| field should contain the Bluetooth address of the | |
| 48 // local device, which is sent to the eligible devices so they can connect to | |
| 49 // the local device. | |
|
Ilya Sherman
2014/11/18 22:30:44
I wonder if it would be better to just document th
Tim Song
2014/12/03 01:18:24
I would prefer the documentation to be here as the
| |
| 50 typedef base::Callback<void( | |
| 51 const cryptauth::FindEligibleUnlockDevicesResponse&)> | |
| 52 FindEligibleUnlockDevicesCallback; | |
| 53 void FindEligibleUnlockDevices(const std::string& bluetooth_address, | |
| 54 FindEligibleUnlockDevicesCallback callback, | |
| 55 ErrorCallback error_callback); | |
| 56 | |
| 57 // The SendDeviceSyncTickle API request tells the server to "tickle" all | |
| 58 // the user's registered devices, so they can sync the latest device state. | |
| 59 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> | |
| 60 SendDeviceSyncTickleCallback; | |
| 61 void SendDeviceSyncTickle(SendDeviceSyncTickleCallback callback, | |
| 62 ErrorCallback error_callback); | |
| 63 | |
| 64 // The ToggleEasyUnlock API request tells the server to designate devices as | |
| 65 // an "unlock key" that can unlock other devices. The |turn_on| argument is | |
| 66 // used to enable or disable the device given by its |public_key|. If | |
| 67 // |apply_to_all| is true, then |public_key| will be ignored, and |turn_on| | |
| 68 // will be applied to all eligible unlock keys. | |
| 69 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> | |
| 70 ToggleEasyUnlockCallback; | |
| 71 void ToggleEasyUnlock(bool turn_on, | |
| 72 bool apply_to_all, | |
| 73 const std::string public_key, | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please pass strings by const-reference too (a
Ilya Sherman
2014/11/18 22:30:44
Since we already reference cryptauth::ToggleEasyUn
Tim Song
2014/12/03 01:18:24
Done.
Tim Song
2014/12/03 01:18:24
Done.
| |
| 74 ToggleEasyUnlockCallback callback, | |
| 75 ErrorCallback error_callback); | |
| 76 | |
| 77 // The SetupEnrollment API request is the first of a two step process to | |
| 78 // enroll the device with CryptAuth. See FinishEnrollment for the last step of | |
| 79 // enrollment. | |
| 80 // You most likely will not use this API directly. Look instead at | |
| 81 // CryptAuthEnroller, which handles the entire enrollment operation. | |
| 82 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> | |
| 83 SetupEnrollmentCallback; | |
| 84 void SetupEnrollment(const std::string application_id, | |
| 85 const std::vector<std::string>& supported_protocols, | |
| 86 SetupEnrollmentCallback callback, | |
| 87 ErrorCallback error_callback); | |
| 88 | |
| 89 // The FinishEnrollment API request is the last of a two step process to | |
| 90 // enroll the device with CryptAuth. See for the SetupEnrollment for the | |
| 91 // first step of enrollment. | |
| 92 // You most likely will not use this API directly. Look instead at | |
| 93 // CryptAuthEnroller, which handles the entire enrollment operation. | |
| 94 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> | |
| 95 FinishEnrollmentCallback; | |
| 96 void FinishEnrollment(const std::string& enrollment_session_id, | |
| 97 const std::string& enrollment_message, | |
| 98 const std::string& device_ephermeral_key, | |
| 99 FinishEnrollmentCallback callback, | |
| 100 ErrorCallback error_callback); | |
| 101 | |
| 102 protected: | |
| 103 // Creates a CryptAuthApiCallFlow object and takes ownership of it. | |
| 104 // Exposed for testing. | |
| 105 virtual CryptAuthApiCallFlow* CreateFlow(GURL request_url); | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please pass by const-reference.
Tim Song
2014/12/03 01:18:24
Done.
| |
| 106 | |
| 107 private: | |
| 108 // Starts a call to the API given by |request_path|, with the templated | |
| 109 // request and response types. The client first fetches the access token and | |
| 110 // then makes the HTTP request. | |
| 111 template <class RequestProto, class ResponseProto> | |
| 112 void MakeApiCall(const std::string& request_path, | |
| 113 const RequestProto& request_proto, | |
| 114 base::Callback<void(const ResponseProto&)> response_callback, | |
| 115 ErrorCallback error_callback); | |
|
Ilya Sherman
2014/11/18 22:30:44
Expanding on a comment above: Why not just expose
Tim Song
2014/12/03 01:18:24
I prefer making all the possible API calls explici
| |
| 116 | |
| 117 // Called when the access token is obtained so the API request can be made. | |
| 118 template <class ResponseProto> | |
| 119 void OnAccessTokenFetched( | |
| 120 std::string serialized_request, | |
| 121 base::Callback<void(const ResponseProto&)> response_callback, | |
| 122 const std::string& access_token); | |
| 123 | |
| 124 // Called with CryptAuthApiCallFlow completes successfully to deserialize and | |
| 125 // return the result. | |
| 126 template <class ResponseProto> | |
| 127 void OnFlowSuccess(base::Callback<void(const ResponseProto&)> result_callback, | |
| 128 const std::string& serialized_response); | |
| 129 // Called when the current API call fails at any step. | |
| 130 void OnApiCallFailed(const std::string& error_message); | |
| 131 | |
| 132 // The context for network requests. | |
| 133 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please leave blank lines between documented v
Tim Song
2014/12/03 01:18:24
Done.
| |
| 134 // Fetchs the access token authorizing the API calls. | |
| 135 CryptAuthAccessTokenFetcher* access_token_fetcher_; | |
|
Ilya Sherman
2014/11/18 22:30:44
nit: Please document lifetime expectations. Alter
Tim Song
2014/12/03 01:18:24
I haven't implemented the access token fetching ye
| |
| 136 | |
| 137 // Handles the current API call. | |
| 138 scoped_ptr<CryptAuthApiCallFlow> flow_; | |
| 139 // URL path of the current request. | |
| 140 std::string request_path_; | |
| 141 // Called when the current request fails. | |
| 142 ErrorCallback error_callback_; | |
| 143 | |
| 144 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); | |
| 147 }; | |
| 148 | |
| 149 } // namespace proximity_auth | |
| 150 | |
| 151 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | |
| OLD | NEW |