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 PrivetDataReadOperationImpl : public PrivetDataReadOperation,
150 public PrivetURLFetcher::Delegate {
152 PrivetDataReadOperationImpl(
153 PrivetHTTPClientImpl* privet_client,
154 const std::string& path,
155 const std::string& query_params,
156 const PrivetDataReadOperation::ResultCallback& callback);
157 virtual ~PrivetDataReadOperationImpl();
159 virtual void Start() OVERRIDE;
161 virtual void SetDataRange(int range_start, int range_end) OVERRIDE;
163 virtual void SaveDataToFile() OVERRIDE;
165 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
167 virtual void OnError(PrivetURLFetcher* fetcher,
168 PrivetURLFetcher::ErrorType error) OVERRIDE;
169 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
170 const base::DictionaryValue* value,
171 bool has_error) OVERRIDE;
172 virtual void OnNeedPrivetToken(
173 PrivetURLFetcher* fetcher,
174 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
175 virtual bool OnRawData(PrivetURLFetcher* fetcher,
177 const std::string& data_str,
178 const base::FilePath& file_path) OVERRIDE;
181 PrivetHTTPClientImpl* privet_client_;
183 std::string query_params_;
186 PrivetDataReadOperation::ResultCallback callback_;
191 scoped_ptr<PrivetURLFetcher> url_fetcher_;
194 class PrivetLocalPrintOperationImpl
195 : public PrivetLocalPrintOperation,
196 public PrivetURLFetcher::Delegate {
198 PrivetLocalPrintOperationImpl(
199 PrivetHTTPClientImpl* privet_client,
200 PrivetLocalPrintOperation::Delegate* delegate);
202 virtual ~PrivetLocalPrintOperationImpl();
203 virtual void Start() OVERRIDE;
205 virtual void SetData(base::RefCountedBytes* data) OVERRIDE;
207 virtual void SetTicket(const std::string& ticket) OVERRIDE;
209 virtual void SetUsername(const std::string& user) OVERRIDE;
211 virtual void SetJobname(const std::string& jobname) OVERRIDE;
213 virtual void SetOffline(bool offline) OVERRIDE;
215 virtual void SetPageSize(const gfx::Size& page_size) OVERRIDE;
217 virtual void SetPWGRasterConverterForTesting(
218 scoped_ptr<PWGRasterConverter> pwg_raster_converter) OVERRIDE;
220 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
222 virtual void OnError(PrivetURLFetcher* fetcher,
223 PrivetURLFetcher::ErrorType error) OVERRIDE;
224 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
225 const base::DictionaryValue* value,
226 bool has_error) OVERRIDE;
227 virtual void OnNeedPrivetToken(
228 PrivetURLFetcher* fetcher,
229 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
232 typedef base::Callback<void(bool, const base::DictionaryValue* value)>
235 void StartInitialRequest();
236 void GetCapabilities();
240 void StartConvertToPWG();
241 void StartPrinting();
243 void OnPrivetInfoDone(const base::DictionaryValue* value);
244 void OnCapabilitiesResponse(bool has_error,
245 const base::DictionaryValue* value);
246 void OnSubmitdocResponse(bool has_error,
247 const base::DictionaryValue* value);
248 void OnCreatejobResponse(bool has_error,
249 const base::DictionaryValue* value);
250 void OnPWGRasterConverted(bool success, const base::FilePath& pwg_file_path);
252 PrivetHTTPClientImpl* privet_client_;
253 PrivetLocalPrintOperation::Delegate* delegate_;
255 ResponseCallback current_response_;
258 scoped_refptr<base::RefCountedBytes> data_;
259 base::FilePath pwg_file_path_;
262 bool has_capabilities_;
263 bool has_extended_workflow_;
266 gfx::Size page_size_;
270 std::string jobname_;
274 int invalid_job_retries_;
276 scoped_ptr<PrivetURLFetcher> url_fetcher_;
277 scoped_ptr<PrivetJSONOperation> info_operation_;
278 scoped_ptr<PWGRasterConverter> pwg_raster_converter_;
280 base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_;
283 class PrivetHTTPClientImpl : public PrivetHTTPClient {
285 PrivetHTTPClientImpl(
286 const std::string& name,
287 const net::HostPortPair& host_port,
288 net::URLRequestContextGetter* request_context);
289 virtual ~PrivetHTTPClientImpl();
291 virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
293 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
294 const std::string& user,
295 PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
297 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
298 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
300 virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
301 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
303 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
304 PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
306 virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
307 const std::string& path,
308 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
310 virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
311 const std::string& path,
312 const PrivetDataReadOperation::ResultCallback& callback) OVERRIDE;
314 virtual const std::string& GetName() OVERRIDE;
316 scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
318 net::URLFetcher::RequestType request_type,
319 PrivetURLFetcher::Delegate* delegate) const;
321 void CacheInfo(const base::DictionaryValue* cached_info);
323 bool HasToken() const;
325 void RefreshPrivetToken(
326 const PrivetURLFetcher::TokenCallback& token_callback);
329 typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
331 void OnPrivetInfoDone(const base::DictionaryValue* value);
334 PrivetURLFetcherFactory fetcher_factory_;
335 net::HostPortPair host_port_;
336 scoped_ptr<base::DictionaryValue> cached_info_;
338 scoped_ptr<PrivetJSONOperation> info_operation_;
339 TokenCallbackVector token_callbacks_;
342 } // namespace local_discovery
343 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_