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