Don't preload rarely seen large images
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_client_impl.h
blob787254b4b84b88015cfe73dfe45cb3ec293bc0e4
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 "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h"
13 #include "net/url_request/url_request_context_getter.h"
15 class OAuth2TokenService;
17 namespace proximity_auth {
19 // Implementation of CryptAuthClient.
20 // Note: There is no need to set the |device_classifier| field in request
21 // messages. CryptAuthClient will fill this field for all requests.
22 class CryptAuthClientImpl : public CryptAuthClient {
23 public:
24 typedef base::Callback<void(const std::string&)> ErrorCallback;
26 // Creates the client using |url_request_context| to make the HTTP request
27 // through |api_call_flow|. CryptAuthClientImpl takes ownership of
28 // |access_token_fetcher|, which provides the access token authorizing
29 // CryptAuth requests. The |device_classifier| argument contains basic device
30 // information of the caller (e.g. version and device type).
31 CryptAuthClientImpl(
32 scoped_ptr<CryptAuthApiCallFlow> api_call_flow,
33 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
34 scoped_refptr<net::URLRequestContextGetter> url_request_context,
35 const cryptauth::DeviceClassifier& device_classifier);
36 ~CryptAuthClientImpl() override;
38 // CryptAuthClient:
39 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request,
40 const GetMyDevicesCallback& callback,
41 const ErrorCallback& error_callback) override;
42 void FindEligibleUnlockDevices(
43 const cryptauth::FindEligibleUnlockDevicesRequest& request,
44 const FindEligibleUnlockDevicesCallback& callback,
45 const ErrorCallback& error_callback) override;
46 void SendDeviceSyncTickle(
47 const cryptauth::SendDeviceSyncTickleRequest& request,
48 const SendDeviceSyncTickleCallback& callback,
49 const ErrorCallback& error_callback) override;
50 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request,
51 const ToggleEasyUnlockCallback& callback,
52 const ErrorCallback& error_callback) override;
53 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request,
54 const SetupEnrollmentCallback& callback,
55 const ErrorCallback& error_callback) override;
56 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request,
57 const FinishEnrollmentCallback& callback,
58 const ErrorCallback& error_callback) override;
59 std::string GetAccessTokenUsed() override;
61 private:
62 // Starts a call to the API given by |request_path|, with the templated
63 // request and response types. The client first fetches the access token and
64 // then makes the HTTP request.
65 template <class RequestProto, class ResponseProto>
66 void MakeApiCall(
67 const std::string& request_path,
68 const RequestProto& request_proto,
69 const base::Callback<void(const ResponseProto&)>& response_callback,
70 const ErrorCallback& error_callback);
72 // Called when the access token is obtained so the API request can be made.
73 template <class ResponseProto>
74 void OnAccessTokenFetched(
75 const std::string& serialized_request,
76 const base::Callback<void(const ResponseProto&)>& response_callback,
77 const std::string& access_token);
79 // Called with CryptAuthApiCallFlow completes successfully to deserialize and
80 // return the result.
81 template <class ResponseProto>
82 void OnFlowSuccess(
83 const base::Callback<void(const ResponseProto&)>& result_callback,
84 const std::string& serialized_response);
86 // Called when the current API call fails at any step.
87 void OnApiCallFailed(const std::string& error_message);
89 // Constructs and executes the actual HTTP request.
90 scoped_ptr<CryptAuthApiCallFlow> api_call_flow_;
92 // Fetches the access token authorizing the API calls.
93 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_;
95 // The context for network requests.
96 scoped_refptr<net::URLRequestContextGetter> url_request_context_;
98 // Contains basic device info of the client making the request that is sent to
99 // CryptAuth with each API call.
100 const cryptauth::DeviceClassifier device_classifier_;
102 // True if an API call has been started. Remains true even after the API call
103 // completes.
104 bool has_call_started_;
106 // URL path of the current request.
107 std::string request_path_;
109 // The access token fetched by |access_token_fetcher_|.
110 std::string access_token_used_;
112 // Called when the current request fails.
113 ErrorCallback error_callback_;
115 base::WeakPtrFactory<CryptAuthClientImpl> weak_ptr_factory_;
117 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientImpl);
120 // Implementation of CryptAuthClientFactory.
121 class CryptAuthClientFactoryImpl : public CryptAuthClientFactory {
122 public:
123 // |token_service|: Gets the user's access token.
124 // Not owned, so |token_service| needs to outlive this object.
125 // |account_id|: The account id of the user.
126 // |url_request_context|: The request context to make the HTTP requests.
127 // |device_classifier|: Contains basic device information of the client.
128 CryptAuthClientFactoryImpl(
129 OAuth2TokenService* token_service,
130 const std::string& account_id,
131 scoped_refptr<net::URLRequestContextGetter> url_request_context,
132 const cryptauth::DeviceClassifier& device_classifier);
133 ~CryptAuthClientFactoryImpl() override;
135 // CryptAuthClientFactory:
136 scoped_ptr<CryptAuthClient> CreateInstance() override;
138 private:
139 OAuth2TokenService* token_service_;
140 const std::string account_id_;
141 const scoped_refptr<net::URLRequestContextGetter> url_request_context_;
142 const cryptauth::DeviceClassifier device_classifier_;
144 DISALLOW_COPY_AND_ASSIGN(CryptAuthClientFactoryImpl);
147 } // namespace proximity_auth
149 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CLIENT_IMPL_H