privet_http_impl.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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.
4
5#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
7
8#include <string>
9
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"
17
18namespace printing {
19struct PwgRasterSettings;
20};
21
22namespace local_discovery {
23
24class PrivetHTTPClientImpl;
25
26class PrivetInfoOperationImpl : public PrivetJSONOperation,
27                                public PrivetURLFetcher::Delegate {
28 public:
29  PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client,
30                          const PrivetJSONOperation::ResultCallback& callback);
31  virtual ~PrivetInfoOperationImpl();
32
33  virtual void Start() OVERRIDE;
34
35  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
36
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;
42
43 private:
44  PrivetHTTPClientImpl* privet_client_;
45  PrivetJSONOperation::ResultCallback callback_;
46  scoped_ptr<PrivetURLFetcher> url_fetcher_;
47};
48
49class PrivetRegisterOperationImpl
50    : public PrivetRegisterOperation,
51      public PrivetURLFetcher::Delegate,
52      public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
53 public:
54  PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client,
55                              const std::string& user,
56                              PrivetRegisterOperation::Delegate* delegate);
57  virtual ~PrivetRegisterOperationImpl();
58
59  virtual void Start() OVERRIDE;
60  virtual void Cancel() OVERRIDE;
61  virtual void CompleteRegistration() OVERRIDE;
62
63  virtual void OnError(PrivetURLFetcher* fetcher,
64                       PrivetURLFetcher::ErrorType error) OVERRIDE;
65
66  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
67                            const base::DictionaryValue* value,
68                            bool has_error) OVERRIDE;
69
70  virtual void OnNeedPrivetToken(
71      PrivetURLFetcher* fetcher,
72      const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
73
74  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
75 private:
76  class Cancelation : public PrivetURLFetcher::Delegate {
77   public:
78    Cancelation(PrivetHTTPClientImpl* privet_client,
79                const std::string& user);
80    virtual ~Cancelation();
81
82    virtual void OnError(PrivetURLFetcher* fetcher,
83                         PrivetURLFetcher::ErrorType error) OVERRIDE;
84
85    virtual void OnParsedJson(PrivetURLFetcher* fetcher,
86                              const base::DictionaryValue* value,
87                              bool has_error) OVERRIDE;
88
89    void Cleanup();
90
91   private:
92    scoped_ptr<PrivetURLFetcher> url_fetcher_;
93  };
94
95  // Arguments is JSON value from request.
96  typedef base::Callback<void(const base::DictionaryValue&)>
97      ResponseHandler;
98
99  void StartInfoOperation();
100  void OnPrivetInfoDone(const base::DictionaryValue* value);
101
102  void StartResponse(const base::DictionaryValue& value);
103  void GetClaimTokenResponse(const base::DictionaryValue& value);
104  void CompleteResponse(const base::DictionaryValue& value);
105
106  void SendRequest(const std::string& action);
107
108  std::string user_;
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.
116  bool ongoing_;
117
118  scoped_ptr<PrivetJSONOperation> info_operation_;
119  std::string expected_id_;
120};
121
122class PrivetJSONOperationImpl : public PrivetJSONOperation,
123                                public PrivetURLFetcher::Delegate {
124 public:
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;
132
133  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
134
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;
143
144 private:
145  PrivetHTTPClientImpl* privet_client_;
146  std::string path_;
147  std::string query_params_;
148  PrivetJSONOperation::ResultCallback callback_;
149
150  scoped_ptr<PrivetURLFetcher> url_fetcher_;
151};
152
153class PrivetDataReadOperationImpl : public PrivetDataReadOperation,
154                                    public PrivetURLFetcher::Delegate {
155 public:
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();
162
163  virtual void Start() OVERRIDE;
164
165  virtual void SetDataRange(int range_start, int range_end) OVERRIDE;
166
167  virtual void SaveDataToFile() OVERRIDE;
168
169  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
170
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,
180                         bool is_file,
181                         const std::string& data_str,
182                         const base::FilePath& file_path) OVERRIDE;
183
184 private:
185  PrivetHTTPClientImpl* privet_client_;
186  std::string path_;
187  std::string query_params_;
188  int range_start_;
189  int range_end_;
190  PrivetDataReadOperation::ResultCallback callback_;
191
192  bool has_range_;
193  bool save_to_file_;
194
195  scoped_ptr<PrivetURLFetcher> url_fetcher_;
196};
197
198class PrivetLocalPrintOperationImpl
199    : public PrivetLocalPrintOperation,
200      public PrivetURLFetcher::Delegate {
201 public:
202  PrivetLocalPrintOperationImpl(
203      PrivetHTTPClientImpl* privet_client,
204      PrivetLocalPrintOperation::Delegate* delegate);
205
206  virtual ~PrivetLocalPrintOperationImpl();
207  virtual void Start() OVERRIDE;
208
209  virtual void SetData(base::RefCountedBytes* data) OVERRIDE;
210
211  virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
212
213  virtual void SetTicket(const std::string& ticket) OVERRIDE;
214
215  virtual void SetUsername(const std::string& user) OVERRIDE;
216
217  virtual void SetJobname(const std::string& jobname) OVERRIDE;
218
219  virtual void SetOffline(bool offline) OVERRIDE;
220
221  virtual void SetPageSize(const gfx::Size& page_size) OVERRIDE;
222
223  virtual void SetPWGRasterConverterForTesting(
224      scoped_ptr<PWGRasterConverter> pwg_raster_converter) OVERRIDE;
225
226  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
227
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;
236
237 private:
238  typedef base::Callback<void(bool, const base::DictionaryValue* value)>
239      ResponseCallback;
240
241  void StartInitialRequest();
242  void DoCreatejob();
243  void DoSubmitdoc();
244
245  void StartConvertToPWG();
246  void StartPrinting();
247
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);
255
256  PrivetHTTPClientImpl* privet_client_;
257  PrivetLocalPrintOperation::Delegate* delegate_;
258
259  ResponseCallback current_response_;
260
261  cloud_devices::CloudDeviceDescription ticket_;
262  cloud_devices::CloudDeviceDescription capabilities_;
263
264  scoped_refptr<base::RefCountedBytes> data_;
265  base::FilePath pwg_file_path_;
266
267  bool use_pdf_;
268  bool has_extended_workflow_;
269  bool started_;
270  bool offline_;
271  gfx::Size page_size_;
272  int dpi_;
273
274  std::string user_;
275  std::string jobname_;
276
277  std::string jobid_;
278
279  int invalid_job_retries_;
280
281  scoped_ptr<PrivetURLFetcher> url_fetcher_;
282  scoped_ptr<PrivetJSONOperation> info_operation_;
283  scoped_ptr<PWGRasterConverter> pwg_raster_converter_;
284
285  base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_;
286};
287
288class PrivetHTTPClientImpl : public PrivetHTTPClient {
289 public:
290  PrivetHTTPClientImpl(
291      const std::string& name,
292      const net::HostPortPair& host_port,
293      net::URLRequestContextGetter* request_context);
294  virtual ~PrivetHTTPClientImpl();
295
296  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
297      const std::string& user,
298      PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
299
300  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
301      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
302
303  virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
304      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
305
306  virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
307      PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
308
309  virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
310      const std::string& path,
311      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
312
313  virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
314      const std::string& path,
315      const PrivetDataReadOperation::ResultCallback& callback) OVERRIDE;
316
317  virtual const std::string& GetName() OVERRIDE;
318
319  scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
320      const GURL& url,
321      net::URLFetcher::RequestType request_type,
322      PrivetURLFetcher::Delegate* delegate) const;
323
324  void RefreshPrivetToken(
325      const PrivetURLFetcher::TokenCallback& token_callback);
326
327 private:
328  typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
329
330  void OnPrivetInfoDone(const base::DictionaryValue* value);
331
332  std::string name_;
333  scoped_refptr<net::URLRequestContextGetter> request_context_;
334  net::HostPortPair host_port_;
335
336  scoped_ptr<PrivetJSONOperation> info_operation_;
337  TokenCallbackVector token_callbacks_;
338};
339
340}  // namespace local_discovery
341#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
342