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 "printing/pdf_render_settings.h"
17 namespace local_discovery
{
19 class PrivetHTTPClientImpl
;
21 class PrivetInfoOperationImpl
: public PrivetJSONOperation
,
22 public PrivetURLFetcher::Delegate
{
24 PrivetInfoOperationImpl(PrivetHTTPClientImpl
* privet_client
,
25 const PrivetJSONOperation::ResultCallback
& callback
);
26 virtual ~PrivetInfoOperationImpl();
28 virtual void Start() OVERRIDE
;
30 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
32 virtual void OnError(PrivetURLFetcher
* fetcher
,
33 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
34 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
35 const base::DictionaryValue
* value
,
36 bool has_error
) OVERRIDE
;
39 PrivetHTTPClientImpl
* privet_client_
;
40 PrivetJSONOperation::ResultCallback callback_
;
41 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
44 class PrivetRegisterOperationImpl
45 : public PrivetRegisterOperation
,
46 public PrivetURLFetcher::Delegate
,
47 public base::SupportsWeakPtr
<PrivetRegisterOperationImpl
> {
49 PrivetRegisterOperationImpl(PrivetHTTPClientImpl
* privet_client
,
50 const std::string
& user
,
51 PrivetRegisterOperation::Delegate
* delegate
);
52 virtual ~PrivetRegisterOperationImpl();
54 virtual void Start() OVERRIDE
;
55 virtual void Cancel() OVERRIDE
;
56 virtual void CompleteRegistration() OVERRIDE
;
58 virtual void OnError(PrivetURLFetcher
* fetcher
,
59 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
61 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
62 const base::DictionaryValue
* value
,
63 bool has_error
) OVERRIDE
;
65 virtual void OnNeedPrivetToken(
66 PrivetURLFetcher
* fetcher
,
67 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
69 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
71 class Cancelation
: public PrivetURLFetcher::Delegate
{
73 Cancelation(PrivetHTTPClientImpl
* privet_client
,
74 const std::string
& user
);
75 virtual ~Cancelation();
77 virtual void OnError(PrivetURLFetcher
* fetcher
,
78 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
80 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
81 const base::DictionaryValue
* value
,
82 bool has_error
) OVERRIDE
;
87 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
90 // Arguments is JSON value from request.
91 typedef base::Callback
<void(const base::DictionaryValue
&)>
94 void StartInfoOperation();
95 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
98 void StartResponse(const base::DictionaryValue
& value
);
99 void GetClaimTokenResponse(const base::DictionaryValue
& value
);
100 void CompleteResponse(const base::DictionaryValue
& value
);
102 void SendRequest(const std::string
& action
);
105 std::string current_action_
;
106 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
107 PrivetRegisterOperation::Delegate
* delegate_
;
108 PrivetHTTPClientImpl
* privet_client_
;
109 ResponseHandler next_response_handler_
;
110 // Required to ensure destroying completed register operations doesn't cause
111 // extraneous cancelations.
114 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
115 std::string expected_id_
;
118 class PrivetJSONOperationImpl
: public PrivetJSONOperation
,
119 public PrivetURLFetcher::Delegate
{
121 PrivetJSONOperationImpl(
122 PrivetHTTPClientImpl
* privet_client
,
123 const std::string
& path
,
124 const std::string
& query_params
,
125 const PrivetJSONOperation::ResultCallback
& callback
);
126 virtual ~PrivetJSONOperationImpl();
127 virtual void Start() OVERRIDE
;
129 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
131 virtual void OnError(PrivetURLFetcher
* fetcher
,
132 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
133 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
134 const base::DictionaryValue
* value
,
135 bool has_error
) OVERRIDE
;
136 virtual void OnNeedPrivetToken(
137 PrivetURLFetcher
* fetcher
,
138 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
141 PrivetHTTPClientImpl
* privet_client_
;
143 std::string query_params_
;
144 PrivetJSONOperation::ResultCallback callback_
;
146 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
149 class PrivetLocalPrintOperationImpl
150 : public PrivetLocalPrintOperation
,
151 public PrivetURLFetcher::Delegate
{
153 PrivetLocalPrintOperationImpl(
154 PrivetHTTPClientImpl
* privet_client
,
155 PrivetLocalPrintOperation::Delegate
* delegate
);
157 virtual ~PrivetLocalPrintOperationImpl();
158 virtual void Start() OVERRIDE
;
160 virtual void SetData(base::RefCountedBytes
* data
) OVERRIDE
;
162 virtual void SetTicket(const std::string
& ticket
) OVERRIDE
;
164 virtual void SetUsername(const std::string
& user
) OVERRIDE
;
166 virtual void SetJobname(const std::string
& jobname
) OVERRIDE
;
168 virtual void SetOffline(bool offline
) OVERRIDE
;
170 virtual void SetConversionSettings(
171 const printing::PdfRenderSettings
& conversion_settings
) OVERRIDE
;
173 virtual void SetPWGRasterConverterForTesting(
174 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) OVERRIDE
;
176 virtual PrivetHTTPClient
* GetHTTPClient() OVERRIDE
;
178 virtual void OnError(PrivetURLFetcher
* fetcher
,
179 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
180 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
181 const base::DictionaryValue
* value
,
182 bool has_error
) OVERRIDE
;
183 virtual void OnNeedPrivetToken(
184 PrivetURLFetcher
* fetcher
,
185 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
188 typedef base::Callback
<void(bool, const base::DictionaryValue
* value
)>
191 void StartInitialRequest();
192 void GetCapabilities();
196 void StartConvertToPWG();
197 void StartPrinting();
199 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
200 void OnCapabilitiesResponse(bool has_error
,
201 const base::DictionaryValue
* value
);
202 void OnSubmitdocResponse(bool has_error
,
203 const base::DictionaryValue
* value
);
204 void OnCreatejobResponse(bool has_error
,
205 const base::DictionaryValue
* value
);
206 void OnPWGRasterConverted(bool success
, const base::FilePath
& pwg_file_path
);
208 PrivetHTTPClientImpl
* privet_client_
;
209 PrivetLocalPrintOperation::Delegate
* delegate_
;
211 ResponseCallback current_response_
;
214 scoped_refptr
<base::RefCountedBytes
> data_
;
215 base::FilePath pwg_file_path_
;
218 bool has_capabilities_
;
219 bool has_extended_workflow_
;
222 printing::PdfRenderSettings conversion_settings_
;
225 std::string jobname_
;
229 int invalid_job_retries_
;
231 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
232 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
233 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter_
;
235 base::WeakPtrFactory
<PrivetLocalPrintOperationImpl
> weak_factory_
;
238 class PrivetHTTPClientImpl
: public PrivetHTTPClient
{
240 PrivetHTTPClientImpl(
241 const std::string
& name
,
242 const net::HostPortPair
& host_port
,
243 net::URLRequestContextGetter
* request_context
);
244 virtual ~PrivetHTTPClientImpl();
246 virtual const base::DictionaryValue
* GetCachedInfo() const OVERRIDE
;
248 virtual scoped_ptr
<PrivetRegisterOperation
> CreateRegisterOperation(
249 const std::string
& user
,
250 PrivetRegisterOperation::Delegate
* delegate
) OVERRIDE
;
252 virtual scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
253 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
255 virtual scoped_ptr
<PrivetJSONOperation
> CreateCapabilitiesOperation(
256 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
258 virtual scoped_ptr
<PrivetLocalPrintOperation
> CreateLocalPrintOperation(
259 PrivetLocalPrintOperation::Delegate
* delegate
) OVERRIDE
;
261 virtual scoped_ptr
<PrivetJSONOperation
> CreateStorageListOperation(
262 const std::string
& path
,
263 const PrivetJSONOperation::ResultCallback
& callback
) OVERRIDE
;
265 virtual const std::string
& GetName() OVERRIDE
;
267 scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
269 net::URLFetcher::RequestType request_type
,
270 PrivetURLFetcher::Delegate
* delegate
) const;
272 void CacheInfo(const base::DictionaryValue
* cached_info
);
274 bool HasToken() const;
276 void RefreshPrivetToken(
277 const PrivetURLFetcher::TokenCallback
& token_callback
);
280 typedef std::vector
<PrivetURLFetcher::TokenCallback
> TokenCallbackVector
;
282 void OnPrivetInfoDone(const base::DictionaryValue
* value
);
285 PrivetURLFetcherFactory fetcher_factory_
;
286 net::HostPortPair host_port_
;
287 scoped_ptr
<base::DictionaryValue
> cached_info_
;
289 scoped_ptr
<PrivetJSONOperation
> info_operation_
;
290 TokenCallbackVector token_callbacks_
;
293 } // namespace local_discovery
294 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_