1 // Copyright 2013 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 CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "chrome/browser/local_discovery/privet_http.h"
16 #include "components/cloud_devices/common/cloud_device_description.h"
17 #include "ui/gfx/geometry/size.h"
19 namespace local_discovery
{
21 class PrivetHTTPClient
;
23 class PrivetInfoOperationImpl
: public PrivetJSONOperation
,
24 public PrivetURLFetcher::Delegate
{
26 PrivetInfoOperationImpl(PrivetHTTPClient
* privet_client
,
27 const PrivetJSONOperation::ResultCallback
& callback
);
28 ~PrivetInfoOperationImpl() override
;
30 void Start() override
;
32 PrivetHTTPClient
* GetHTTPClient() override
;
34 void OnError(PrivetURLFetcher
* fetcher
,
35 PrivetURLFetcher::ErrorType error
) override
;
36 void OnParsedJson(PrivetURLFetcher
* fetcher
,
37 const base::DictionaryValue
& value
,
38 bool has_error
) override
;
41 PrivetHTTPClient
* privet_client_
;
42 PrivetJSONOperation::ResultCallback callback_
;
43 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
46 class PrivetRegisterOperationImpl
47 : public PrivetRegisterOperation
,
48 public PrivetURLFetcher::Delegate
,
49 public base::SupportsWeakPtr
<PrivetRegisterOperationImpl
> {
51 PrivetRegisterOperationImpl(PrivetHTTPClient
* privet_client
,
52 const std::string
& user
,
53 PrivetRegisterOperation::Delegate
* delegate
);
54 ~PrivetRegisterOperationImpl() override
;
56 void Start() override
;
57 void Cancel() override
;
58 void CompleteRegistration() override
;
60 void OnError(PrivetURLFetcher
* fetcher
,
61 PrivetURLFetcher::ErrorType error
) override
;
63 void OnParsedJson(PrivetURLFetcher
* fetcher
,
64 const base::DictionaryValue
& value
,
65 bool has_error
) override
;
67 void OnNeedPrivetToken(
68 PrivetURLFetcher
* fetcher
,
69 const PrivetURLFetcher::TokenCallback
& callback
) override
;
71 PrivetHTTPClient
* GetHTTPClient() override
;
74 class Cancelation
: public PrivetURLFetcher::Delegate
{
76 Cancelation(PrivetHTTPClient
* privet_client
, const std::string
& user
);
77 ~Cancelation() override
;
79 void OnError(PrivetURLFetcher
* fetcher
,
80 PrivetURLFetcher::ErrorType error
) override
;
82 void OnParsedJson(PrivetURLFetcher
* fetcher
,
83 const base::DictionaryValue
& value
,
84 bool has_error
) override
;
89 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
92 // Arguments is JSON value from request.
93 typedef base::Callback
<void(const base::DictionaryValue
&)>
96 void StartInfoOperation();
97 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
99 void StartResponse(const base::DictionaryValue
& value
);
100 void GetClaimTokenResponse(const base::DictionaryValue
& value
);
101 void CompleteResponse(const base::DictionaryValue
& value
);
103 void SendRequest(const std::string
& action
);
106 std::string current_action_
;
107 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
108 PrivetRegisterOperation::Delegate
* delegate_
;
109 PrivetHTTPClient
* privet_client_
;
110 ResponseHandler next_response_handler_
;
111 // Required to ensure destroying completed register operations doesn't cause
112 // extraneous cancelations.
115 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
116 std::string expected_id_
;
119 class PrivetJSONOperationImpl
: public PrivetJSONOperation
,
120 public PrivetURLFetcher::Delegate
{
122 PrivetJSONOperationImpl(PrivetHTTPClient
* privet_client
,
123 const std::string
& path
,
124 const std::string
& query_params
,
125 const PrivetJSONOperation::ResultCallback
& callback
);
126 ~PrivetJSONOperationImpl() override
;
127 void Start() override
;
129 PrivetHTTPClient
* GetHTTPClient() override
;
131 void OnError(PrivetURLFetcher
* fetcher
,
132 PrivetURLFetcher::ErrorType error
) override
;
133 void OnParsedJson(PrivetURLFetcher
* fetcher
,
134 const base::DictionaryValue
& value
,
135 bool has_error
) override
;
136 void OnNeedPrivetToken(
137 PrivetURLFetcher
* fetcher
,
138 const PrivetURLFetcher::TokenCallback
& callback
) override
;
141 PrivetHTTPClient
* privet_client_
;
143 std::string query_params_
;
144 PrivetJSONOperation::ResultCallback callback_
;
146 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
149 #if defined(ENABLE_PRINT_PREVIEW)
150 class PrivetLocalPrintOperationImpl
151 : public PrivetLocalPrintOperation
,
152 public PrivetURLFetcher::Delegate
{
154 PrivetLocalPrintOperationImpl(PrivetHTTPClient
* privet_client
,
155 PrivetLocalPrintOperation::Delegate
* delegate
);
157 ~PrivetLocalPrintOperationImpl() override
;
158 void Start() override
;
160 void SetData(const scoped_refptr
<base::RefCountedBytes
>& data
) override
;
162 void SetCapabilities(const std::string
& capabilities
) override
;
164 void SetTicket(const std::string
& ticket
) override
;
166 void SetUsername(const std::string
& user
) override
;
168 void SetJobname(const std::string
& jobname
) override
;
170 void SetOffline(bool offline
) override
;
172 void SetPageSize(const gfx::Size
& page_size
) override
;
174 void SetPWGRasterConverterForTesting(
175 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) override
;
177 PrivetHTTPClient
* GetHTTPClient() override
;
179 void OnError(PrivetURLFetcher
* fetcher
,
180 PrivetURLFetcher::ErrorType error
) override
;
181 void OnParsedJson(PrivetURLFetcher
* fetcher
,
182 const base::DictionaryValue
& value
,
183 bool has_error
) override
;
184 void OnNeedPrivetToken(
185 PrivetURLFetcher
* fetcher
,
186 const PrivetURLFetcher::TokenCallback
& callback
) override
;
189 typedef base::Callback
<void(bool, const base::DictionaryValue
* value
)>
192 void StartInitialRequest();
196 void StartConvertToPWG();
197 void StartPrinting();
199 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
200 void OnSubmitdocResponse(bool has_error
,
201 const base::DictionaryValue
* value
);
202 void OnCreatejobResponse(bool has_error
,
203 const base::DictionaryValue
* value
);
204 void OnPWGRasterConverted(bool success
, const base::FilePath
& pwg_file_path
);
206 PrivetHTTPClient
* privet_client_
;
207 PrivetLocalPrintOperation::Delegate
* delegate_
;
209 ResponseCallback current_response_
;
211 cloud_devices::CloudDeviceDescription ticket_
;
212 cloud_devices::CloudDeviceDescription capabilities_
;
214 scoped_refptr
<base::RefCountedBytes
> data_
;
215 base::FilePath pwg_file_path_
;
218 bool has_extended_workflow_
;
221 gfx::Size page_size_
;
224 std::string jobname_
;
228 int invalid_job_retries_
;
230 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
231 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
232 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter_
;
234 base::WeakPtrFactory
<PrivetLocalPrintOperationImpl
> weak_factory_
;
236 #endif // ENABLE_PRINT_PREVIEW
238 class PrivetHTTPClientImpl
: public PrivetHTTPClient
{
240 PrivetHTTPClientImpl(
241 const std::string
& name
,
242 const net::HostPortPair
& host_port
,
243 net::URLRequestContextGetter
* request_context
);
244 ~PrivetHTTPClientImpl() override
;
246 // PrivetHTTPClient implementation.
247 const std::string
& GetName() override
;
248 scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
249 const PrivetJSONOperation::ResultCallback
& callback
) override
;
250 scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
252 net::URLFetcher::RequestType request_type
,
253 PrivetURLFetcher::Delegate
* delegate
) override
;
254 void RefreshPrivetToken(
255 const PrivetURLFetcher::TokenCallback
& token_callback
) override
;
258 typedef std::vector
<PrivetURLFetcher::TokenCallback
> TokenCallbackVector
;
260 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
263 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
264 net::HostPortPair host_port_
;
266 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
267 TokenCallbackVector token_callbacks_
;
269 DISALLOW_COPY_AND_ASSIGN(PrivetHTTPClientImpl
);
272 class PrivetV1HTTPClientImpl
: public PrivetV1HTTPClient
{
274 explicit PrivetV1HTTPClientImpl(scoped_ptr
<PrivetHTTPClient
> info_client
);
275 ~PrivetV1HTTPClientImpl() override
;
277 const std::string
& GetName() override
;
278 scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
279 const PrivetJSONOperation::ResultCallback
& callback
) override
;
280 scoped_ptr
<PrivetRegisterOperation
> CreateRegisterOperation(
281 const std::string
& user
,
282 PrivetRegisterOperation::Delegate
* delegate
) override
;
283 scoped_ptr
<PrivetJSONOperation
> CreateCapabilitiesOperation(
284 const PrivetJSONOperation::ResultCallback
& callback
) override
;
285 scoped_ptr
<PrivetLocalPrintOperation
> CreateLocalPrintOperation(
286 PrivetLocalPrintOperation::Delegate
* delegate
) override
;
289 PrivetHTTPClient
* info_client() { return info_client_
.get(); }
291 scoped_ptr
<PrivetHTTPClient
> info_client_
;
293 DISALLOW_COPY_AND_ASSIGN(PrivetV1HTTPClientImpl
);
296 } // namespace local_discovery
297 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_