5c720e2f4b443ac2f5d2971f76f2215c19533d3b
[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
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
17 namespace local_discovery {
18
19 class PrivetHTTPClientImpl;
20
21 class 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
44 class 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
118 class 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
149 class PrivetLocalPrintOperationImpl
150     : public PrivetLocalPrintOperation,
151       public PrivetURLFetcher::Delegate {
152  public:
153   PrivetLocalPrintOperationImpl(
154       PrivetHTTPClientImpl* privet_client,
155       PrivetLocalPrintOperation::Delegate* delegate);
156
157   virtual ~PrivetLocalPrintOperationImpl();
158   virtual void Start() OVERRIDE;
159
160   virtual void SetData(base::RefCountedBytes* data) OVERRIDE;
161
162   virtual void SetTicket(const std::string& ticket) OVERRIDE;
163
164   virtual void SetUsername(const std::string& user) OVERRIDE;
165
166   virtual void SetJobname(const std::string& jobname) OVERRIDE;
167
168   virtual void SetOffline(bool offline) OVERRIDE;
169
170   virtual void SetConversionSettings(
171       const printing::PdfRenderSettings& conversion_settings) OVERRIDE;
172
173   virtual void SetPWGRasterConverterForTesting(
174       scoped_ptr<PWGRasterConverter> pwg_raster_converter) OVERRIDE;
175
176   virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
177
178   virtual void OnError(PrivetURLFetcher* fetcher,
179                        PrivetURLFetcher::ErrorType error) OVERRIDE;
180   virtual void OnParsedJson(PrivetURLFetcher* fetcher,
181                             const base::DictionaryValue* value,
182                             bool has_error) OVERRIDE;
183   virtual void OnNeedPrivetToken(
184       PrivetURLFetcher* fetcher,
185       const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
186
187  private:
188   typedef base::Callback<void(bool, const base::DictionaryValue* value)>
189       ResponseCallback;
190
191   void StartInitialRequest();
192   void GetCapabilities();
193   void DoCreatejob();
194   void DoSubmitdoc();
195
196   void StartConvertToPWG();
197   void StartPrinting();
198
199   void OnPrivetInfoDone(const base::DictionaryValue* value);
200   void OnCapabilitiesResponse(bool has_error,
201                               const base::DictionaryValue* value);
202   void OnSubmitdocResponse(bool has_error,
203                            const base::DictionaryValue* value);
204   void OnCreatejobResponse(bool has_error,
205                            const base::DictionaryValue* value);
206   void OnPWGRasterConverted(bool success, const base::FilePath& pwg_file_path);
207
208   PrivetHTTPClientImpl* privet_client_;
209   PrivetLocalPrintOperation::Delegate* delegate_;
210
211   ResponseCallback current_response_;
212
213   std::string ticket_;
214   scoped_refptr<base::RefCountedBytes> data_;
215   base::FilePath pwg_file_path_;
216
217   bool use_pdf_;
218   bool has_capabilities_;
219   bool has_extended_workflow_;
220   bool started_;
221   bool offline_;
222   printing::PdfRenderSettings conversion_settings_;
223
224   std::string user_;
225   std::string jobname_;
226
227   std::string jobid_;
228
229   int invalid_job_retries_;
230
231   scoped_ptr<PrivetURLFetcher> url_fetcher_;
232   scoped_ptr<PrivetJSONOperation> info_operation_;
233   scoped_ptr<PWGRasterConverter> pwg_raster_converter_;
234
235   base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_;
236 };
237
238 class PrivetHTTPClientImpl : public PrivetHTTPClient {
239  public:
240   PrivetHTTPClientImpl(
241       const std::string& name,
242       const net::HostPortPair& host_port,
243       net::URLRequestContextGetter* request_context);
244   virtual ~PrivetHTTPClientImpl();
245
246   virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
247
248   virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
249       const std::string& user,
250       PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
251
252   virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
253       const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
254
255   virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
256       const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
257
258   virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
259       PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
260
261   virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
262       const std::string& path,
263       const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
264
265   virtual const std::string& GetName() OVERRIDE;
266
267   scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
268       const GURL& url,
269       net::URLFetcher::RequestType request_type,
270       PrivetURLFetcher::Delegate* delegate) const;
271
272   void CacheInfo(const base::DictionaryValue* cached_info);
273
274   bool HasToken() const;
275
276   void RefreshPrivetToken(
277       const PrivetURLFetcher::TokenCallback& token_callback);
278
279  private:
280   typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
281
282   void OnPrivetInfoDone(const base::DictionaryValue* value);
283
284   std::string name_;
285   PrivetURLFetcherFactory fetcher_factory_;
286   net::HostPortPair host_port_;
287   scoped_ptr<base::DictionaryValue> cached_info_;
288
289   scoped_ptr<PrivetJSONOperation> info_operation_;
290   TokenCallbackVector token_callbacks_;
291 };
292
293 }  // namespace local_discovery
294 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_