Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_http_impl.h
blob1814aff6869727ea88f7ce9846b63096715d3b07
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>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/files/file_path.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 "ui/gfx/geometry/size.h"
19 namespace local_discovery {
21 class PrivetHTTPClient;
23 class PrivetInfoOperationImpl : public PrivetJSONOperation,
24 public PrivetURLFetcher::Delegate {
25 public:
26 PrivetInfoOperationImpl(PrivetHTTPClient* privet_client,
27 const PrivetJSONOperation::ResultCallback& callback);
28 ~PrivetInfoOperationImpl() override;
30 void Start() override;
32 PrivetHTTPClient* GetHTTPClient() override;
34 void OnError(PrivetURLFetcher* fetcher,
35 PrivetURLFetcher::ErrorType error) override;
36 void OnParsedJson(PrivetURLFetcher* fetcher,
37 const base::DictionaryValue& value,
38 bool has_error) override;
40 private:
41 PrivetHTTPClient* privet_client_;
42 PrivetJSONOperation::ResultCallback callback_;
43 scoped_ptr<PrivetURLFetcher> url_fetcher_;
46 class PrivetRegisterOperationImpl
47 : public PrivetRegisterOperation,
48 public PrivetURLFetcher::Delegate,
49 public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
50 public:
51 PrivetRegisterOperationImpl(PrivetHTTPClient* privet_client,
52 const std::string& user,
53 PrivetRegisterOperation::Delegate* delegate);
54 ~PrivetRegisterOperationImpl() override;
56 void Start() override;
57 void Cancel() override;
58 void CompleteRegistration() override;
60 void OnError(PrivetURLFetcher* fetcher,
61 PrivetURLFetcher::ErrorType error) override;
63 void OnParsedJson(PrivetURLFetcher* fetcher,
64 const base::DictionaryValue& value,
65 bool has_error) override;
67 void OnNeedPrivetToken(
68 PrivetURLFetcher* fetcher,
69 const PrivetURLFetcher::TokenCallback& callback) override;
71 PrivetHTTPClient* GetHTTPClient() override;
73 private:
74 class Cancelation : public PrivetURLFetcher::Delegate {
75 public:
76 Cancelation(PrivetHTTPClient* privet_client, const std::string& user);
77 ~Cancelation() override;
79 void OnError(PrivetURLFetcher* fetcher,
80 PrivetURLFetcher::ErrorType error) override;
82 void OnParsedJson(PrivetURLFetcher* fetcher,
83 const base::DictionaryValue& value,
84 bool has_error) override;
86 void Cleanup();
88 private:
89 scoped_ptr<PrivetURLFetcher> url_fetcher_;
92 // Arguments is JSON value from request.
93 typedef base::Callback<void(const base::DictionaryValue&)>
94 ResponseHandler;
96 void StartInfoOperation();
97 void OnPrivetInfoDone(const base::DictionaryValue* value);
99 void StartResponse(const base::DictionaryValue& value);
100 void GetClaimTokenResponse(const base::DictionaryValue& value);
101 void CompleteResponse(const base::DictionaryValue& value);
103 void SendRequest(const std::string& action);
105 std::string user_;
106 std::string current_action_;
107 scoped_ptr<PrivetURLFetcher> url_fetcher_;
108 PrivetRegisterOperation::Delegate* delegate_;
109 PrivetHTTPClient* privet_client_;
110 ResponseHandler next_response_handler_;
111 // Required to ensure destroying completed register operations doesn't cause
112 // extraneous cancelations.
113 bool ongoing_;
115 scoped_ptr<PrivetJSONOperation> info_operation_;
116 std::string expected_id_;
119 class PrivetJSONOperationImpl : public PrivetJSONOperation,
120 public PrivetURLFetcher::Delegate {
121 public:
122 PrivetJSONOperationImpl(PrivetHTTPClient* privet_client,
123 const std::string& path,
124 const std::string& query_params,
125 const PrivetJSONOperation::ResultCallback& callback);
126 ~PrivetJSONOperationImpl() override;
127 void Start() override;
129 PrivetHTTPClient* GetHTTPClient() override;
131 void OnError(PrivetURLFetcher* fetcher,
132 PrivetURLFetcher::ErrorType error) override;
133 void OnParsedJson(PrivetURLFetcher* fetcher,
134 const base::DictionaryValue& value,
135 bool has_error) override;
136 void OnNeedPrivetToken(
137 PrivetURLFetcher* fetcher,
138 const PrivetURLFetcher::TokenCallback& callback) override;
140 private:
141 PrivetHTTPClient* privet_client_;
142 std::string path_;
143 std::string query_params_;
144 PrivetJSONOperation::ResultCallback callback_;
146 scoped_ptr<PrivetURLFetcher> url_fetcher_;
149 #if defined(ENABLE_PRINT_PREVIEW)
150 class PrivetLocalPrintOperationImpl
151 : public PrivetLocalPrintOperation,
152 public PrivetURLFetcher::Delegate {
153 public:
154 PrivetLocalPrintOperationImpl(PrivetHTTPClient* privet_client,
155 PrivetLocalPrintOperation::Delegate* delegate);
157 ~PrivetLocalPrintOperationImpl() override;
158 void Start() override;
160 void SetData(const scoped_refptr<base::RefCountedBytes>& data) override;
162 void SetCapabilities(const std::string& capabilities) override;
164 void SetTicket(const std::string& ticket) override;
166 void SetUsername(const std::string& user) override;
168 void SetJobname(const std::string& jobname) override;
170 void SetOffline(bool offline) override;
172 void SetPageSize(const gfx::Size& page_size) override;
174 void SetPWGRasterConverterForTesting(
175 scoped_ptr<PWGRasterConverter> pwg_raster_converter) override;
177 PrivetHTTPClient* GetHTTPClient() override;
179 void OnError(PrivetURLFetcher* fetcher,
180 PrivetURLFetcher::ErrorType error) override;
181 void OnParsedJson(PrivetURLFetcher* fetcher,
182 const base::DictionaryValue& value,
183 bool has_error) override;
184 void OnNeedPrivetToken(
185 PrivetURLFetcher* fetcher,
186 const PrivetURLFetcher::TokenCallback& callback) override;
188 private:
189 typedef base::Callback<void(bool, const base::DictionaryValue* value)>
190 ResponseCallback;
192 void StartInitialRequest();
193 void DoCreatejob();
194 void DoSubmitdoc();
196 void StartConvertToPWG();
197 void StartPrinting();
199 void OnPrivetInfoDone(const base::DictionaryValue* value);
200 void OnSubmitdocResponse(bool has_error,
201 const base::DictionaryValue* value);
202 void OnCreatejobResponse(bool has_error,
203 const base::DictionaryValue* value);
204 void OnPWGRasterConverted(bool success, const base::FilePath& pwg_file_path);
206 PrivetHTTPClient* privet_client_;
207 PrivetLocalPrintOperation::Delegate* delegate_;
209 ResponseCallback current_response_;
211 cloud_devices::CloudDeviceDescription ticket_;
212 cloud_devices::CloudDeviceDescription capabilities_;
214 scoped_refptr<base::RefCountedBytes> data_;
215 base::FilePath pwg_file_path_;
217 bool use_pdf_;
218 bool has_extended_workflow_;
219 bool started_;
220 bool offline_;
221 gfx::Size page_size_;
223 std::string user_;
224 std::string jobname_;
226 std::string jobid_;
228 int invalid_job_retries_;
230 scoped_ptr<PrivetURLFetcher> url_fetcher_;
231 scoped_ptr<PrivetJSONOperation> info_operation_;
232 scoped_ptr<PWGRasterConverter> pwg_raster_converter_;
234 base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_;
236 #endif // ENABLE_PRINT_PREVIEW
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 ~PrivetHTTPClientImpl() override;
246 // PrivetHTTPClient implementation.
247 const std::string& GetName() override;
248 scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
249 const PrivetJSONOperation::ResultCallback& callback) override;
250 scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
251 const GURL& url,
252 net::URLFetcher::RequestType request_type,
253 PrivetURLFetcher::Delegate* delegate) override;
254 void RefreshPrivetToken(
255 const PrivetURLFetcher::TokenCallback& token_callback) override;
257 private:
258 typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
260 void OnPrivetInfoDone(const base::DictionaryValue* value);
262 std::string name_;
263 scoped_refptr<net::URLRequestContextGetter> request_context_;
264 net::HostPortPair host_port_;
266 scoped_ptr<PrivetJSONOperation> info_operation_;
267 TokenCallbackVector token_callbacks_;
269 DISALLOW_COPY_AND_ASSIGN(PrivetHTTPClientImpl);
272 class PrivetV1HTTPClientImpl : public PrivetV1HTTPClient {
273 public:
274 explicit PrivetV1HTTPClientImpl(scoped_ptr<PrivetHTTPClient> info_client);
275 ~PrivetV1HTTPClientImpl() override;
277 const std::string& GetName() override;
278 scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
279 const PrivetJSONOperation::ResultCallback& callback) override;
280 scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
281 const std::string& user,
282 PrivetRegisterOperation::Delegate* delegate) override;
283 scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
284 const PrivetJSONOperation::ResultCallback& callback) override;
285 scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
286 PrivetLocalPrintOperation::Delegate* delegate) override;
288 private:
289 PrivetHTTPClient* info_client() { return info_client_.get(); }
291 scoped_ptr<PrivetHTTPClient> info_client_;
293 DISALLOW_COPY_AND_ASSIGN(PrivetV1HTTPClientImpl);
296 } // namespace local_discovery
297 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_