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_CRYPTAUTH_CLIENT_IMPL_H
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H
8 #include "base/memory/weak_ptr.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"
12 #include "net/url_request/url_request_context_getter.h"
14 class OAuth2TokenService
;
16 namespace proximity_auth
{
18 // Implementation of CryptAuthClient.
19 // Note: There is no need to set the |device_classifier| field in request
20 // messages. CryptAuthClient will fill this field for all requests.
21 class CryptAuthClientImpl
: public CryptAuthClient
{
23 typedef base::Callback
<void(const std::string
&)> ErrorCallback
;
25 // Creates the client using |url_request_context| to make the HTTP request
26 // through |api_call_flow|. CryptAuthClientImpl takes ownership of
27 // |access_token_fetcher|, which provides the access token authorizing
28 // CryptAuth requests. The |device_classifier| argument contains basic device
29 // information of the caller (e.g. version and device type).
31 scoped_ptr
<CryptAuthApiCallFlow
> api_call_flow
,
32 scoped_ptr
<CryptAuthAccessTokenFetcher
> access_token_fetcher
,
33 scoped_refptr
<net::URLRequestContextGetter
> url_request_context
,
34 const cryptauth::DeviceClassifier
& device_classifier
);
35 ~CryptAuthClientImpl() override
;
38 void GetMyDevices(const cryptauth::GetMyDevicesRequest
& request
,
39 const GetMyDevicesCallback
& callback
,
40 const ErrorCallback
& error_callback
) override
;
41 void FindEligibleUnlockDevices(
42 const cryptauth::FindEligibleUnlockDevicesRequest
& request
,
43 const FindEligibleUnlockDevicesCallback
& callback
,
44 const ErrorCallback
& error_callback
) override
;
45 void SendDeviceSyncTickle(
46 const cryptauth::SendDeviceSyncTickleRequest
& request
,
47 const SendDeviceSyncTickleCallback
& callback
,
48 const ErrorCallback
& error_callback
) override
;
49 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest
& request
,
50 const ToggleEasyUnlockCallback
& callback
,
51 const ErrorCallback
& error_callback
) override
;
52 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest
& request
,
53 const SetupEnrollmentCallback
& callback
,
54 const ErrorCallback
& error_callback
) override
;
55 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest
& request
,
56 const FinishEnrollmentCallback
& callback
,
57 const ErrorCallback
& error_callback
) override
;
60 // Starts a call to the API given by |request_path|, with the templated
61 // request and response types. The client first fetches the access token and
62 // then makes the HTTP request.
63 template <class RequestProto
, class ResponseProto
>
65 const std::string
& request_path
,
66 const RequestProto
& request_proto
,
67 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
68 const ErrorCallback
& error_callback
);
70 // Called when the access token is obtained so the API request can be made.
71 template <class ResponseProto
>
72 void OnAccessTokenFetched(
73 const std::string
& serialized_request
,
74 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
75 const std::string
& access_token
);
77 // Called with CryptAuthApiCallFlow completes successfully to deserialize and
79 template <class ResponseProto
>
81 const base::Callback
<void(const ResponseProto
&)>& result_callback
,
82 const std::string
& serialized_response
);
84 // Called when the current API call fails at any step.
85 void OnApiCallFailed(const std::string
& error_message
);
87 // Constructs and executes the actual HTTP request.
88 scoped_ptr
<CryptAuthApiCallFlow
> api_call_flow_
;
90 // Fetches the access token authorizing the API calls.
91 scoped_ptr
<CryptAuthAccessTokenFetcher
> access_token_fetcher_
;
93 // The context for network requests.
94 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_
;
96 // Contains basic device info of the client making the request that is sent to
97 // CryptAuth with each API call.
98 const cryptauth::DeviceClassifier device_classifier_
;
100 // True if an API call has been started. Remains true even after the API call
102 bool has_call_started_
;
104 // URL path of the current request.
105 std::string request_path_
;
107 // Called when the current request fails.
108 ErrorCallback error_callback_
;
110 base::WeakPtrFactory
<CryptAuthClientImpl
> weak_ptr_factory_
;
112 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientImpl
);
115 // Implementation of CryptAuthClientFactory.
116 class CryptAuthClientFactoryImpl
: public CryptAuthClientFactory
{
118 // |token_service|: Gets the user's access token.
119 // Not owned, so |token_service| needs to outlive this object.
120 // |account_id|: The account id of the user.
121 // |url_request_context|: The request context to make the HTTP requests.
122 // |device_classifier|: Contains basic device information of the client.
123 CryptAuthClientFactoryImpl(
124 OAuth2TokenService
* token_service
,
125 const std::string
& account_id
,
126 scoped_refptr
<net::URLRequestContextGetter
> url_request_context
,
127 const cryptauth::DeviceClassifier
& device_classifier
);
128 ~CryptAuthClientFactoryImpl() override
;
130 // CryptAuthClientFactory:
131 scoped_ptr
<CryptAuthClient
> CreateInstance() override
;
134 OAuth2TokenService
* token_service_
;
135 const std::string account_id_
;
136 const scoped_refptr
<net::URLRequestContextGetter
> url_request_context_
;
137 const cryptauth::DeviceClassifier device_classifier_
;
139 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientFactoryImpl
);
142 } // namespace proximity_auth
144 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H