[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_http_impl.h
blob1fcde73cd50b64993c5130d69c439ef86385a3de
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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
8 #include <string>
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"
18 namespace printing {
19 struct PwgRasterSettings;
22 namespace local_discovery {
24 class PrivetHTTPClientImpl;
26 class PrivetInfoOperationImpl : public PrivetJSONOperation,
27 public PrivetURLFetcher::Delegate {
28 public:
29 PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client,
30 const PrivetJSONOperation::ResultCallback& callback);
31 virtual ~PrivetInfoOperationImpl();
33 virtual void Start() OVERRIDE;
35 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
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;
43 private:
44 PrivetHTTPClientImpl* privet_client_;
45 PrivetJSONOperation::ResultCallback callback_;
46 scoped_ptr<PrivetURLFetcher> url_fetcher_;
49 class 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();
59 virtual void Start() OVERRIDE;
60 virtual void Cancel() OVERRIDE;
61 virtual void CompleteRegistration() OVERRIDE;
63 virtual void OnError(PrivetURLFetcher* fetcher,
64 PrivetURLFetcher::ErrorType error) OVERRIDE;
66 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
67 const base::DictionaryValue* value,
68 bool has_error) OVERRIDE;
70 virtual void OnNeedPrivetToken(
71 PrivetURLFetcher* fetcher,
72 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
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();
82 virtual void OnError(PrivetURLFetcher* fetcher,
83 PrivetURLFetcher::ErrorType error) OVERRIDE;
85 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
86 const base::DictionaryValue* value,
87 bool has_error) OVERRIDE;
89 void Cleanup();
91 private:
92 scoped_ptr<PrivetURLFetcher> url_fetcher_;
95 // Arguments is JSON value from request.
96 typedef base::Callback<void(const base::DictionaryValue&)>
97 ResponseHandler;
99 void StartInfoOperation();
100 void OnPrivetInfoDone(const base::DictionaryValue* value);
102 void StartResponse(const base::DictionaryValue& value);
103 void GetClaimTokenResponse(const base::DictionaryValue& value);
104 void CompleteResponse(const base::DictionaryValue& value);
106 void SendRequest(const std::string& action);
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_;
118 scoped_ptr<PrivetJSONOperation> info_operation_;
119 std::string expected_id_;
122 class 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;
133 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
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;
144 private:
145 PrivetHTTPClientImpl* privet_client_;
146 std::string path_;
147 std::string query_params_;
148 PrivetJSONOperation::ResultCallback callback_;
150 scoped_ptr<PrivetURLFetcher> url_fetcher_;
153 class 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();
163 virtual void Start() OVERRIDE;
165 virtual void SetDataRange(int range_start, int range_end) OVERRIDE;
167 virtual void SaveDataToFile() OVERRIDE;
169 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
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;
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_;
192 bool has_range_;
193 bool save_to_file_;
195 scoped_ptr<PrivetURLFetcher> url_fetcher_;
198 class PrivetLocalPrintOperationImpl
199 : public PrivetLocalPrintOperation,
200 public PrivetURLFetcher::Delegate {
201 public:
202 PrivetLocalPrintOperationImpl(
203 PrivetHTTPClientImpl* privet_client,
204 PrivetLocalPrintOperation::Delegate* delegate);
206 virtual ~PrivetLocalPrintOperationImpl();
207 virtual void Start() OVERRIDE;
209 virtual void SetData(base::RefCountedBytes* data) OVERRIDE;
211 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
213 virtual void SetTicket(const std::string& ticket) OVERRIDE;
215 virtual void SetUsername(const std::string& user) OVERRIDE;
217 virtual void SetJobname(const std::string& jobname) OVERRIDE;
219 virtual void SetOffline(bool offline) OVERRIDE;
221 virtual void SetPageSize(const gfx::Size& page_size) OVERRIDE;
223 virtual void SetPWGRasterConverterForTesting(
224 scoped_ptr<PWGRasterConverter> pwg_raster_converter) OVERRIDE;
226 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
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;
237 private:
238 typedef base::Callback<void(bool, const base::DictionaryValue* value)>
239 ResponseCallback;
241 void StartInitialRequest();
242 void DoCreatejob();
243 void DoSubmitdoc();
245 void StartConvertToPWG();
246 void StartPrinting();
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);
256 PrivetHTTPClientImpl* privet_client_;
257 PrivetLocalPrintOperation::Delegate* delegate_;
259 ResponseCallback current_response_;
261 cloud_devices::CloudDeviceDescription ticket_;
262 cloud_devices::CloudDeviceDescription capabilities_;
264 scoped_refptr<base::RefCountedBytes> data_;
265 base::FilePath pwg_file_path_;
267 bool use_pdf_;
268 bool has_extended_workflow_;
269 bool started_;
270 bool offline_;
271 gfx::Size page_size_;
272 int dpi_;
274 std::string user_;
275 std::string jobname_;
277 std::string jobid_;
279 int invalid_job_retries_;
281 scoped_ptr<PrivetURLFetcher> url_fetcher_;
282 scoped_ptr<PrivetJSONOperation> info_operation_;
283 scoped_ptr<PWGRasterConverter> pwg_raster_converter_;
285 base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_;
288 class 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();
296 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
297 const std::string& user,
298 PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
300 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
301 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
303 virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
304 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
306 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
307 PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
309 virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
310 const std::string& path,
311 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
313 virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
314 const std::string& path,
315 const PrivetDataReadOperation::ResultCallback& callback) OVERRIDE;
317 virtual const std::string& GetName() OVERRIDE;
319 scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
320 const GURL& url,
321 net::URLFetcher::RequestType request_type,
322 PrivetURLFetcher::Delegate* delegate) const;
324 void RefreshPrivetToken(
325 const PrivetURLFetcher::TokenCallback& token_callback);
327 private:
328 typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
330 void OnPrivetInfoDone(const base::DictionaryValue* value);
332 std::string name_;
333 scoped_refptr<net::URLRequestContextGetter> request_context_;
334 net::HostPortPair host_port_;
336 scoped_ptr<PrivetJSONOperation> info_operation_;
337 TokenCallbackVector token_callbacks_;
340 } // namespace local_discovery
341 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_