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_util.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 "printing/pdf_render_settings.h"
20 struct PwgRasterSettings
;
23 namespace local_discovery
{
25 class PrivetHTTPClient
;
27 class PrivetInfoOperationImpl
: public PrivetJSONOperation
,
28 public PrivetURLFetcher::Delegate
{
30 PrivetInfoOperationImpl(PrivetHTTPClient
* privet_client
,
31 const PrivetJSONOperation::ResultCallback
& callback
);
32 ~PrivetInfoOperationImpl() override
;
34 void Start() override
;
36 PrivetHTTPClient
* GetHTTPClient() override
;
38 void OnError(PrivetURLFetcher
* fetcher
,
39 PrivetURLFetcher::ErrorType error
) override
;
40 void OnParsedJson(PrivetURLFetcher
* fetcher
,
41 const base::DictionaryValue
& value
,
42 bool has_error
) override
;
45 PrivetHTTPClient
* privet_client_
;
46 PrivetJSONOperation::ResultCallback callback_
;
47 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
50 class PrivetRegisterOperationImpl
51 : public PrivetRegisterOperation
,
52 public PrivetURLFetcher::Delegate
,
53 public base::SupportsWeakPtr
<PrivetRegisterOperationImpl
> {
55 PrivetRegisterOperationImpl(PrivetHTTPClient
* privet_client
,
56 const std::string
& user
,
57 PrivetRegisterOperation::Delegate
* delegate
);
58 ~PrivetRegisterOperationImpl() override
;
60 void Start() override
;
61 void Cancel() override
;
62 void CompleteRegistration() override
;
64 void OnError(PrivetURLFetcher
* fetcher
,
65 PrivetURLFetcher::ErrorType error
) override
;
67 void OnParsedJson(PrivetURLFetcher
* fetcher
,
68 const base::DictionaryValue
& value
,
69 bool has_error
) override
;
71 void OnNeedPrivetToken(
72 PrivetURLFetcher
* fetcher
,
73 const PrivetURLFetcher::TokenCallback
& callback
) override
;
75 PrivetHTTPClient
* GetHTTPClient() override
;
78 class Cancelation
: public PrivetURLFetcher::Delegate
{
80 Cancelation(PrivetHTTPClient
* privet_client
, const std::string
& user
);
81 ~Cancelation() override
;
83 void OnError(PrivetURLFetcher
* fetcher
,
84 PrivetURLFetcher::ErrorType error
) override
;
86 void OnParsedJson(PrivetURLFetcher
* fetcher
,
87 const base::DictionaryValue
& value
,
88 bool has_error
) override
;
93 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
96 // Arguments is JSON value from request.
97 typedef base::Callback
<void(const base::DictionaryValue
&)>
100 void StartInfoOperation();
101 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
103 void StartResponse(const base::DictionaryValue
& value
);
104 void GetClaimTokenResponse(const base::DictionaryValue
& value
);
105 void CompleteResponse(const base::DictionaryValue
& value
);
107 void SendRequest(const std::string
& action
);
110 std::string current_action_
;
111 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
112 PrivetRegisterOperation::Delegate
* delegate_
;
113 PrivetHTTPClient
* privet_client_
;
114 ResponseHandler next_response_handler_
;
115 // Required to ensure destroying completed register operations doesn't cause
116 // extraneous cancelations.
119 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
120 std::string expected_id_
;
123 class PrivetJSONOperationImpl
: public PrivetJSONOperation
,
124 public PrivetURLFetcher::Delegate
{
126 PrivetJSONOperationImpl(PrivetHTTPClient
* privet_client
,
127 const std::string
& path
,
128 const std::string
& query_params
,
129 const PrivetJSONOperation::ResultCallback
& callback
);
130 ~PrivetJSONOperationImpl() override
;
131 void Start() override
;
133 PrivetHTTPClient
* GetHTTPClient() override
;
135 void OnError(PrivetURLFetcher
* fetcher
,
136 PrivetURLFetcher::ErrorType error
) override
;
137 void OnParsedJson(PrivetURLFetcher
* fetcher
,
138 const base::DictionaryValue
& value
,
139 bool has_error
) override
;
140 void OnNeedPrivetToken(
141 PrivetURLFetcher
* fetcher
,
142 const PrivetURLFetcher::TokenCallback
& callback
) override
;
145 PrivetHTTPClient
* privet_client_
;
147 std::string query_params_
;
148 PrivetJSONOperation::ResultCallback callback_
;
150 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
153 class PrivetDataReadOperationImpl
: public PrivetDataReadOperation
,
154 public PrivetURLFetcher::Delegate
{
156 PrivetDataReadOperationImpl(
157 PrivetHTTPClient
* privet_client
,
158 const std::string
& path
,
159 const std::string
& query_params
,
160 const PrivetDataReadOperation::ResultCallback
& callback
);
161 ~PrivetDataReadOperationImpl() override
;
163 void Start() override
;
165 void SetDataRange(int range_start
, int range_end
) override
;
167 void SaveDataToFile() override
;
169 PrivetHTTPClient
* GetHTTPClient() override
;
171 void OnError(PrivetURLFetcher
* fetcher
,
172 PrivetURLFetcher::ErrorType error
) override
;
173 void OnParsedJson(PrivetURLFetcher
* fetcher
,
174 const base::DictionaryValue
& value
,
175 bool has_error
) override
;
176 void OnNeedPrivetToken(
177 PrivetURLFetcher
* fetcher
,
178 const PrivetURLFetcher::TokenCallback
& callback
) override
;
179 bool OnRawData(PrivetURLFetcher
* fetcher
,
181 const std::string
& data_str
,
182 const base::FilePath
& file_path
) override
;
185 PrivetHTTPClient
* privet_client_
;
187 std::string query_params_
;
190 PrivetDataReadOperation::ResultCallback callback_
;
195 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
198 #if defined(ENABLE_PRINT_PREVIEW)
199 class PrivetLocalPrintOperationImpl
200 : public PrivetLocalPrintOperation
,
201 public PrivetURLFetcher::Delegate
{
203 PrivetLocalPrintOperationImpl(PrivetHTTPClient
* privet_client
,
204 PrivetLocalPrintOperation::Delegate
* delegate
);
206 ~PrivetLocalPrintOperationImpl() override
;
207 void Start() override
;
209 void SetData(const scoped_refptr
<base::RefCountedBytes
>& data
) override
;
211 void SetCapabilities(const std::string
& capabilities
) override
;
213 void SetTicket(const std::string
& ticket
) override
;
215 void SetUsername(const std::string
& user
) override
;
217 void SetJobname(const std::string
& jobname
) override
;
219 void SetOffline(bool offline
) override
;
221 void SetPageSize(const gfx::Size
& page_size
) override
;
223 void SetPWGRasterConverterForTesting(
224 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) override
;
226 PrivetHTTPClient
* GetHTTPClient() override
;
228 void OnError(PrivetURLFetcher
* fetcher
,
229 PrivetURLFetcher::ErrorType error
) override
;
230 void OnParsedJson(PrivetURLFetcher
* fetcher
,
231 const base::DictionaryValue
& value
,
232 bool has_error
) override
;
233 void OnNeedPrivetToken(
234 PrivetURLFetcher
* fetcher
,
235 const PrivetURLFetcher::TokenCallback
& callback
) override
;
238 typedef base::Callback
<void(bool, const base::DictionaryValue
* value
)>
241 void StartInitialRequest();
245 void StartConvertToPWG();
246 void StartPrinting();
248 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
249 void OnSubmitdocResponse(bool has_error
,
250 const base::DictionaryValue
* value
);
251 void OnCreatejobResponse(bool has_error
,
252 const base::DictionaryValue
* value
);
253 void OnPWGRasterConverted(bool success
, const base::FilePath
& pwg_file_path
);
254 void FillPwgRasterSettings(printing::PwgRasterSettings
* transfrom_settings
);
256 PrivetHTTPClient
* privet_client_
;
257 PrivetLocalPrintOperation::Delegate
* delegate_
;
259 ResponseCallback current_response_
;
261 cloud_devices::CloudDeviceDescription ticket_
;
262 cloud_devices::CloudDeviceDescription capabilities_
;
264 scoped_refptr
<base::RefCountedBytes
> data_
;
265 base::FilePath pwg_file_path_
;
268 bool has_extended_workflow_
;
271 gfx::Size page_size_
;
275 std::string jobname_
;
279 int invalid_job_retries_
;
281 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
282 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
283 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter_
;
285 base::WeakPtrFactory
<PrivetLocalPrintOperationImpl
> weak_factory_
;
287 #endif // ENABLE_PRINT_PREVIEW
289 class PrivetHTTPClientImpl
: public PrivetHTTPClient
{
291 PrivetHTTPClientImpl(
292 const std::string
& name
,
293 const net::HostPortPair
& host_port
,
294 net::URLRequestContextGetter
* request_context
);
295 ~PrivetHTTPClientImpl() override
;
297 // PrivetHTTPClient implementation.
298 const std::string
& GetName() override
;
299 scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
300 const PrivetJSONOperation::ResultCallback
& callback
) override
;
301 scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
303 net::URLFetcher::RequestType request_type
,
304 PrivetURLFetcher::Delegate
* delegate
) override
;
305 void RefreshPrivetToken(
306 const PrivetURLFetcher::TokenCallback
& token_callback
) override
;
309 typedef std::vector
<PrivetURLFetcher::TokenCallback
> TokenCallbackVector
;
311 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
314 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
315 net::HostPortPair host_port_
;
317 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
318 TokenCallbackVector token_callbacks_
;
320 DISALLOW_COPY_AND_ASSIGN(PrivetHTTPClientImpl
);
323 class PrivetV1HTTPClientImpl
: public PrivetV1HTTPClient
{
325 explicit PrivetV1HTTPClientImpl(scoped_ptr
<PrivetHTTPClient
> info_client
);
326 ~PrivetV1HTTPClientImpl() override
;
328 const std::string
& GetName() override
;
329 scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
330 const PrivetJSONOperation::ResultCallback
& callback
) override
;
331 scoped_ptr
<PrivetRegisterOperation
> CreateRegisterOperation(
332 const std::string
& user
,
333 PrivetRegisterOperation::Delegate
* delegate
) override
;
334 scoped_ptr
<PrivetJSONOperation
> CreateCapabilitiesOperation(
335 const PrivetJSONOperation::ResultCallback
& callback
) override
;
336 scoped_ptr
<PrivetLocalPrintOperation
> CreateLocalPrintOperation(
337 PrivetLocalPrintOperation::Delegate
* delegate
) override
;
340 PrivetHTTPClient
* info_client() { return info_client_
.get(); }
342 scoped_ptr
<PrivetHTTPClient
> info_client_
;
344 DISALLOW_COPY_AND_ASSIGN(PrivetV1HTTPClientImpl
);
347 } // namespace local_discovery
348 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_