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#include <vector>
10
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"
18
19namespace printing {
20struct PwgRasterSettings;
21};
22
23namespace local_discovery {
24
25class PrivetHTTPClient;
26
27class PrivetInfoOperationImpl : public PrivetJSONOperation,
28                                public PrivetURLFetcher::Delegate {
29 public:
30  PrivetInfoOperationImpl(PrivetHTTPClient* privet_client,
31                          const PrivetJSONOperation::ResultCallback& callback);
32  virtual ~PrivetInfoOperationImpl();
33
34  virtual void Start() OVERRIDE;
35
36  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
37
38  virtual void OnError(PrivetURLFetcher* fetcher,
39                       PrivetURLFetcher::ErrorType error) OVERRIDE;
40  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
41                            const base::DictionaryValue& value,
42                            bool has_error) OVERRIDE;
43
44 private:
45  PrivetHTTPClient* privet_client_;
46  PrivetJSONOperation::ResultCallback callback_;
47  scoped_ptr<PrivetURLFetcher> url_fetcher_;
48};
49
50class PrivetRegisterOperationImpl
51    : public PrivetRegisterOperation,
52      public PrivetURLFetcher::Delegate,
53      public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
54 public:
55  PrivetRegisterOperationImpl(PrivetHTTPClient* privet_client,
56                              const std::string& user,
57                              PrivetRegisterOperation::Delegate* delegate);
58  virtual ~PrivetRegisterOperationImpl();
59
60  virtual void Start() OVERRIDE;
61  virtual void Cancel() OVERRIDE;
62  virtual void CompleteRegistration() OVERRIDE;
63
64  virtual void OnError(PrivetURLFetcher* fetcher,
65                       PrivetURLFetcher::ErrorType error) OVERRIDE;
66
67  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
68                            const base::DictionaryValue& value,
69                            bool has_error) OVERRIDE;
70
71  virtual void OnNeedPrivetToken(
72      PrivetURLFetcher* fetcher,
73      const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
74
75  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
76 private:
77  class Cancelation : public PrivetURLFetcher::Delegate {
78   public:
79    Cancelation(PrivetHTTPClient* privet_client, 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  PrivetHTTPClient* 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(PrivetHTTPClient* privet_client,
126                          const std::string& path,
127                          const std::string& query_params,
128                          const PrivetJSONOperation::ResultCallback& callback);
129  virtual ~PrivetJSONOperationImpl();
130  virtual void Start() OVERRIDE;
131
132  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
133
134  virtual void OnError(PrivetURLFetcher* fetcher,
135                       PrivetURLFetcher::ErrorType error) OVERRIDE;
136  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
137                            const base::DictionaryValue& value,
138                            bool has_error) OVERRIDE;
139  virtual void OnNeedPrivetToken(
140      PrivetURLFetcher* fetcher,
141      const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
142
143 private:
144  PrivetHTTPClient* privet_client_;
145  std::string path_;
146  std::string query_params_;
147  PrivetJSONOperation::ResultCallback callback_;
148
149  scoped_ptr<PrivetURLFetcher> url_fetcher_;
150};
151
152class PrivetDataReadOperationImpl : public PrivetDataReadOperation,
153                                    public PrivetURLFetcher::Delegate {
154 public:
155  PrivetDataReadOperationImpl(
156      PrivetHTTPClient* privet_client,
157      const std::string& path,
158      const std::string& query_params,
159      const PrivetDataReadOperation::ResultCallback& callback);
160  virtual ~PrivetDataReadOperationImpl();
161
162  virtual void Start() OVERRIDE;
163
164  virtual void SetDataRange(int range_start, int range_end) OVERRIDE;
165
166  virtual void SaveDataToFile() OVERRIDE;
167
168  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
169
170  virtual void OnError(PrivetURLFetcher* fetcher,
171                       PrivetURLFetcher::ErrorType error) OVERRIDE;
172  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
173                            const base::DictionaryValue& value,
174                            bool has_error) OVERRIDE;
175  virtual void OnNeedPrivetToken(
176      PrivetURLFetcher* fetcher,
177      const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
178  virtual bool OnRawData(PrivetURLFetcher* fetcher,
179                         bool is_file,
180                         const std::string& data_str,
181                         const base::FilePath& file_path) OVERRIDE;
182
183 private:
184  PrivetHTTPClient* privet_client_;
185  std::string path_;
186  std::string query_params_;
187  int range_start_;
188  int range_end_;
189  PrivetDataReadOperation::ResultCallback callback_;
190
191  bool has_range_;
192  bool save_to_file_;
193
194  scoped_ptr<PrivetURLFetcher> url_fetcher_;
195};
196
197#if defined(ENABLE_FULL_PRINTING)
198class PrivetLocalPrintOperationImpl
199    : public PrivetLocalPrintOperation,
200      public PrivetURLFetcher::Delegate {
201 public:
202  PrivetLocalPrintOperationImpl(PrivetHTTPClient* privet_client,
203                                PrivetLocalPrintOperation::Delegate* delegate);
204
205  virtual ~PrivetLocalPrintOperationImpl();
206  virtual void Start() OVERRIDE;
207
208  virtual void SetData(
209      const scoped_refptr<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  PrivetHTTPClient* 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#endif  // ENABLE_FULL_PRINTING
288
289class PrivetHTTPClientImpl : public PrivetHTTPClient {
290 public:
291  PrivetHTTPClientImpl(
292      const std::string& name,
293      const net::HostPortPair& host_port,
294      net::URLRequestContextGetter* request_context);
295  virtual ~PrivetHTTPClientImpl();
296
297  // PrivetHTTPClient implementation.
298  virtual const std::string& GetName() OVERRIDE;
299  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
300      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
301  virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
302      const GURL& url,
303      net::URLFetcher::RequestType request_type,
304      PrivetURLFetcher::Delegate* delegate) OVERRIDE;
305  virtual void RefreshPrivetToken(
306      const PrivetURLFetcher::TokenCallback& token_callback) OVERRIDE;
307
308 private:
309  typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
310
311  void OnPrivetInfoDone(const base::DictionaryValue* value);
312
313  std::string name_;
314  scoped_refptr<net::URLRequestContextGetter> request_context_;
315  net::HostPortPair host_port_;
316
317  scoped_ptr<PrivetJSONOperation> info_operation_;
318  TokenCallbackVector token_callbacks_;
319
320  DISALLOW_COPY_AND_ASSIGN(PrivetHTTPClientImpl);
321};
322
323class PrivetV1HTTPClientImpl : public PrivetV1HTTPClient {
324 public:
325  explicit PrivetV1HTTPClientImpl(scoped_ptr<PrivetHTTPClient> info_client);
326  virtual ~PrivetV1HTTPClientImpl();
327
328  virtual const std::string& GetName() OVERRIDE;
329  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
330      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
331  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
332      const std::string& user,
333      PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
334  virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
335      const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
336  virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
337      PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
338
339 private:
340  PrivetHTTPClient* info_client() { return info_client_.get(); }
341
342  scoped_ptr<PrivetHTTPClient> info_client_;
343
344  DISALLOW_COPY_AND_ASSIGN(PrivetV1HTTPClientImpl);
345};
346
347}  // namespace local_discovery
348#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
349