Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privet_http_impl.h
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
19 namespace printing {
20 struct PwgRasterSettings;
21 };
22
23 namespace local_discovery {
24
25 class PrivetHTTPClient;
26
27 class PrivetInfoOperationImpl : public PrivetJSONOperation,
28                                 public PrivetURLFetcher::Delegate {
29  public:
30   PrivetInfoOperationImpl(PrivetHTTPClient* privet_client,
31                           const PrivetJSONOperation::ResultCallback& callback);
32   ~PrivetInfoOperationImpl() override;
33
34   void Start() override;
35
36   PrivetHTTPClient* GetHTTPClient() override;
37
38   void OnError(PrivetURLFetcher* fetcher,
39                PrivetURLFetcher::ErrorType error) override;
40   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
50 class 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   ~PrivetRegisterOperationImpl() override;
59
60   void Start() override;
61   void Cancel() override;
62   void CompleteRegistration() override;
63
64   void OnError(PrivetURLFetcher* fetcher,
65                PrivetURLFetcher::ErrorType error) override;
66
67   void OnParsedJson(PrivetURLFetcher* fetcher,
68                     const base::DictionaryValue& value,
69                     bool has_error) override;
70
71   void OnNeedPrivetToken(
72       PrivetURLFetcher* fetcher,
73       const PrivetURLFetcher::TokenCallback& callback) override;
74
75   PrivetHTTPClient* GetHTTPClient() override;
76
77  private:
78   class Cancelation : public PrivetURLFetcher::Delegate {
79    public:
80     Cancelation(PrivetHTTPClient* privet_client, const std::string& user);
81     ~Cancelation() override;
82
83     void OnError(PrivetURLFetcher* fetcher,
84                  PrivetURLFetcher::ErrorType error) override;
85
86     void OnParsedJson(PrivetURLFetcher* fetcher,
87                       const base::DictionaryValue& value,
88                       bool has_error) override;
89
90     void Cleanup();
91
92    private:
93     scoped_ptr<PrivetURLFetcher> url_fetcher_;
94   };
95
96   // Arguments is JSON value from request.
97   typedef base::Callback<void(const base::DictionaryValue&)>
98       ResponseHandler;
99
100   void StartInfoOperation();
101   void OnPrivetInfoDone(const base::DictionaryValue* value);
102
103   void StartResponse(const base::DictionaryValue& value);
104   void GetClaimTokenResponse(const base::DictionaryValue& value);
105   void CompleteResponse(const base::DictionaryValue& value);
106
107   void SendRequest(const std::string& action);
108
109   std::string user_;
110   std::string current_action_;
111   scoped_ptr<PrivetURLFetcher> url_fetcher_;
112   PrivetRegisterOperation::Delegate* delegate_;
113   PrivetHTTPClient* privet_client_;
114   ResponseHandler next_response_handler_;
115   // Required to ensure destroying completed register operations doesn't cause
116   // extraneous cancelations.
117   bool ongoing_;
118
119   scoped_ptr<PrivetJSONOperation> info_operation_;
120   std::string expected_id_;
121 };
122
123 class PrivetJSONOperationImpl : public PrivetJSONOperation,
124                                 public PrivetURLFetcher::Delegate {
125  public:
126   PrivetJSONOperationImpl(PrivetHTTPClient* privet_client,
127                           const std::string& path,
128                           const std::string& query_params,
129                           const PrivetJSONOperation::ResultCallback& callback);
130   ~PrivetJSONOperationImpl() override;
131   void Start() override;
132
133   PrivetHTTPClient* GetHTTPClient() override;
134
135   void OnError(PrivetURLFetcher* fetcher,
136                PrivetURLFetcher::ErrorType error) override;
137   void OnParsedJson(PrivetURLFetcher* fetcher,
138                     const base::DictionaryValue& value,
139                     bool has_error) override;
140   void OnNeedPrivetToken(
141       PrivetURLFetcher* fetcher,
142       const PrivetURLFetcher::TokenCallback& callback) override;
143
144  private:
145   PrivetHTTPClient* privet_client_;
146   std::string path_;
147   std::string query_params_;
148   PrivetJSONOperation::ResultCallback callback_;
149
150   scoped_ptr<PrivetURLFetcher> url_fetcher_;
151 };
152
153 class PrivetDataReadOperationImpl : public PrivetDataReadOperation,
154                                     public PrivetURLFetcher::Delegate {
155  public:
156   PrivetDataReadOperationImpl(
157       PrivetHTTPClient* privet_client,
158       const std::string& path,
159       const std::string& query_params,
160       const PrivetDataReadOperation::ResultCallback& callback);
161   ~PrivetDataReadOperationImpl() override;
162
163   void Start() override;
164
165   void SetDataRange(int range_start, int range_end) override;
166
167   void SaveDataToFile() override;
168
169   PrivetHTTPClient* GetHTTPClient() override;
170
171   void OnError(PrivetURLFetcher* fetcher,
172                PrivetURLFetcher::ErrorType error) override;
173   void OnParsedJson(PrivetURLFetcher* fetcher,
174                     const base::DictionaryValue& value,
175                     bool has_error) override;
176   void OnNeedPrivetToken(
177       PrivetURLFetcher* fetcher,
178       const PrivetURLFetcher::TokenCallback& callback) override;
179   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   PrivetHTTPClient* 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
198 #if defined(ENABLE_PRINT_PREVIEW)
199 class PrivetLocalPrintOperationImpl
200     : public PrivetLocalPrintOperation,
201       public PrivetURLFetcher::Delegate {
202  public:
203   PrivetLocalPrintOperationImpl(PrivetHTTPClient* privet_client,
204                                 PrivetLocalPrintOperation::Delegate* delegate);
205
206   ~PrivetLocalPrintOperationImpl() override;
207   void Start() override;
208
209   void SetData(const scoped_refptr<base::RefCountedBytes>& data) override;
210
211   void SetCapabilities(const std::string& capabilities) override;
212
213   void SetTicket(const std::string& ticket) override;
214
215   void SetUsername(const std::string& user) override;
216
217   void SetJobname(const std::string& jobname) override;
218
219   void SetOffline(bool offline) override;
220
221   void SetPageSize(const gfx::Size& page_size) override;
222
223   void SetPWGRasterConverterForTesting(
224       scoped_ptr<PWGRasterConverter> pwg_raster_converter) override;
225
226   PrivetHTTPClient* GetHTTPClient() override;
227
228   void OnError(PrivetURLFetcher* fetcher,
229                PrivetURLFetcher::ErrorType error) override;
230   void OnParsedJson(PrivetURLFetcher* fetcher,
231                     const base::DictionaryValue& value,
232                     bool has_error) override;
233   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_PRINT_PREVIEW
288
289 class PrivetHTTPClientImpl : public PrivetHTTPClient {
290  public:
291   PrivetHTTPClientImpl(
292       const std::string& name,
293       const net::HostPortPair& host_port,
294       net::URLRequestContextGetter* request_context);
295   ~PrivetHTTPClientImpl() override;
296
297   // PrivetHTTPClient implementation.
298   const std::string& GetName() override;
299   scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
300       const PrivetJSONOperation::ResultCallback& callback) override;
301   scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
302       const GURL& url,
303       net::URLFetcher::RequestType request_type,
304       PrivetURLFetcher::Delegate* delegate) override;
305   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
323 class PrivetV1HTTPClientImpl : public PrivetV1HTTPClient {
324  public:
325   explicit PrivetV1HTTPClientImpl(scoped_ptr<PrivetHTTPClient> info_client);
326   ~PrivetV1HTTPClientImpl() override;
327
328   const std::string& GetName() override;
329   scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
330       const PrivetJSONOperation::ResultCallback& callback) override;
331   scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
332       const std::string& user,
333       PrivetRegisterOperation::Delegate* delegate) override;
334   scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
335       const PrivetJSONOperation::ResultCallback& callback) override;
336   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_