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_
10 #include "base/callback.h"
11 #include "base/file_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/local_discovery/privet_http.h"
15 #include "components/cloud_devices/common/cloud_device_description.h"
16 #include "printing/pdf_render_settings.h"
19 struct PwgRasterSettings
;
22 namespace local_discovery
{
24 class PrivetHTTPClientImpl
;
26 class PrivetInfoOperationImpl
: public PrivetJSONOperation
,
27 public PrivetURLFetcher::Delegate
{
29 PrivetInfoOperationImpl(PrivetHTTPClientImpl
* privet_client
,
30 const PrivetJSONOperation::ResultCallback
& callback
);
31 virtual ~PrivetInfoOperationImpl();
33 virtual void Start() OVERRIDE
;
35 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
37 virtual void OnError(PrivetURLFetcher
* fetcher
,
38 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
39 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
40 const base::DictionaryValue
* value
,
41 bool has_error
) OVERRIDE
;
44 PrivetHTTPClientImpl
* privet_client_
;
45 PrivetJSONOperation::ResultCallback callback_
;
46 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
49 class PrivetRegisterOperationImpl
50 : public PrivetRegisterOperation
,
51 public PrivetURLFetcher::Delegate
,
52 public base::SupportsWeakPtr
<PrivetRegisterOperationImpl
> {
54 PrivetRegisterOperationImpl(PrivetHTTPClientImpl
* privet_client
,
55 const std::string
& user
,
56 PrivetRegisterOperation::Delegate
* delegate
);
57 virtual ~PrivetRegisterOperationImpl();
59 virtual void Start() OVERRIDE
;
60 virtual void Cancel() OVERRIDE
;
61 virtual void CompleteRegistration() OVERRIDE
;
63 virtual void OnError(PrivetURLFetcher
* fetcher
,
64 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
66 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
67 const base::DictionaryValue
* value
,
68 bool has_error
) OVERRIDE
;
70 virtual void OnNeedPrivetToken(
71 PrivetURLFetcher
* fetcher
,
72 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
74 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
76 class Cancelation
: public PrivetURLFetcher::Delegate
{
78 Cancelation(PrivetHTTPClientImpl
* privet_client
,
79 const std::string
& user
);
80 virtual ~Cancelation();
82 virtual void OnError(PrivetURLFetcher
* fetcher
,
83 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
85 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
86 const base::DictionaryValue
* value
,
87 bool has_error
) OVERRIDE
;
92 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
95 // Arguments is JSON value from request.
96 typedef base::Callback
<void(const base::DictionaryValue
&)>
99 void StartInfoOperation();
100 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
102 void StartResponse(const base::DictionaryValue
& value
);
103 void GetClaimTokenResponse(const base::DictionaryValue
& value
);
104 void CompleteResponse(const base::DictionaryValue
& value
);
106 void SendRequest(const std::string
& action
);
109 std::string current_action_
;
110 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
111 PrivetRegisterOperation::Delegate
* delegate_
;
112 PrivetHTTPClientImpl
* privet_client_
;
113 ResponseHandler next_response_handler_
;
114 // Required to ensure destroying completed register operations doesn't cause
115 // extraneous cancelations.
118 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
119 std::string expected_id_
;
122 class PrivetJSONOperationImpl
: public PrivetJSONOperation
,
123 public PrivetURLFetcher::Delegate
{
125 PrivetJSONOperationImpl(
126 PrivetHTTPClientImpl
* privet_client
,
127 const std::string
& path
,
128 const std::string
& query_params
,
129 const PrivetJSONOperation::ResultCallback
& callback
);
130 virtual ~PrivetJSONOperationImpl();
131 virtual void Start() OVERRIDE
;
133 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
135 virtual void OnError(PrivetURLFetcher
* fetcher
,
136 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
137 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
138 const base::DictionaryValue
* value
,
139 bool has_error
) OVERRIDE
;
140 virtual void OnNeedPrivetToken(
141 PrivetURLFetcher
* fetcher
,
142 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
145 PrivetHTTPClientImpl
* 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 PrivetHTTPClientImpl
* privet_client
,
158 const std::string
& path
,
159 const std::string
& query_params
,
160 const PrivetDataReadOperation::ResultCallback
& callback
);
161 virtual ~PrivetDataReadOperationImpl();
163 virtual void Start() OVERRIDE
;
165 virtual void SetDataRange(int range_start
, int range_end
) OVERRIDE
;
167 virtual void SaveDataToFile() OVERRIDE
;
169 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
171 virtual void OnError(PrivetURLFetcher
* fetcher
,
172 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
173 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
174 const base::DictionaryValue
* value
,
175 bool has_error
) OVERRIDE
;
176 virtual void OnNeedPrivetToken(
177 PrivetURLFetcher
* fetcher
,
178 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
179 virtual bool OnRawData(PrivetURLFetcher
* fetcher
,
181 const std::string
& data_str
,
182 const base::FilePath
& file_path
) OVERRIDE
;
185 PrivetHTTPClientImpl
* privet_client_
;
187 std::string query_params_
;
190 PrivetDataReadOperation::ResultCallback callback_
;
195 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
198 class PrivetLocalPrintOperationImpl
199 : public PrivetLocalPrintOperation
,
200 public PrivetURLFetcher::Delegate
{
202 PrivetLocalPrintOperationImpl(
203 PrivetHTTPClientImpl
* privet_client
,
204 PrivetLocalPrintOperation::Delegate
* delegate
);
206 virtual ~PrivetLocalPrintOperationImpl();
207 virtual void Start() OVERRIDE
;
209 virtual void SetData(base::RefCountedBytes
* data
) OVERRIDE
;
211 virtual void SetCapabilities(const std::string
& capabilities
) OVERRIDE
;
213 virtual void SetTicket(const std::string
& ticket
) OVERRIDE
;
215 virtual void SetUsername(const std::string
& user
) OVERRIDE
;
217 virtual void SetJobname(const std::string
& jobname
) OVERRIDE
;
219 virtual void SetOffline(bool offline
) OVERRIDE
;
221 virtual void SetPageSize(const gfx::Size
& page_size
) OVERRIDE
;
223 virtual void SetPWGRasterConverterForTesting(
224 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) OVERRIDE
;
226 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
228 virtual void OnError(PrivetURLFetcher
* fetcher
,
229 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
230 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
231 const base::DictionaryValue
* value
,
232 bool has_error
) OVERRIDE
;
233 virtual 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 PrivetHTTPClientImpl
* 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_
;
288 class PrivetHTTPClientImpl
: public PrivetHTTPClient
{
290 PrivetHTTPClientImpl(
291 const std::string
& name
,
292 const net::HostPortPair
& host_port
,
293 net::URLRequestContextGetter
* request_context
);
294 virtual ~PrivetHTTPClientImpl();
296 virtual scoped_ptr
<PrivetRegisterOperation
> CreateRegisterOperation(
297 const std::string
& user
,
298 PrivetRegisterOperation::Delegate
* delegate
) OVERRIDE
;
300 virtual scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
301 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
303 virtual scoped_ptr
<PrivetJSONOperation
> CreateCapabilitiesOperation(
304 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
306 virtual scoped_ptr
<PrivetLocalPrintOperation
> CreateLocalPrintOperation(
307 PrivetLocalPrintOperation::Delegate
* delegate
) OVERRIDE
;
309 virtual scoped_ptr
<PrivetJSONOperation
> CreateStorageListOperation(
310 const std::string
& path
,
311 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
313 virtual scoped_ptr
<PrivetDataReadOperation
> CreateStorageReadOperation(
314 const std::string
& path
,
315 const PrivetDataReadOperation::ResultCallback
& callback
) OVERRIDE
;
317 virtual const std::string
& GetName() OVERRIDE
;
319 scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
321 net::URLFetcher::RequestType request_type
,
322 PrivetURLFetcher::Delegate
* delegate
) const;
324 void RefreshPrivetToken(
325 const PrivetURLFetcher::TokenCallback
& token_callback
);
328 typedef std::vector
<PrivetURLFetcher::TokenCallback
> TokenCallbackVector
;
330 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
333 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
334 net::HostPortPair host_port_
;
336 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
337 TokenCallbackVector token_callbacks_
;
340 } // namespace local_discovery
341 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_