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_
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/local_discovery/privet_http.h"
14 namespace local_discovery {
16 class PrivetHTTPClientImpl;
18 class PrivetInfoOperationImpl : public PrivetInfoOperation,
19 public PrivetURLFetcher::Delegate {
21 PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client,
22 PrivetInfoOperation::Delegate* delegate);
23 virtual ~PrivetInfoOperationImpl();
25 virtual void Start() OVERRIDE;
27 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
29 virtual void OnError(PrivetURLFetcher* fetcher,
30 PrivetURLFetcher::ErrorType error) OVERRIDE;
31 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
32 const base::DictionaryValue* value,
33 bool has_error) OVERRIDE;
36 PrivetHTTPClientImpl* privet_client_;
37 PrivetInfoOperation::Delegate* delegate_;
38 scoped_ptr<PrivetURLFetcher> url_fetcher_;
41 class PrivetRegisterOperationImpl
42 : public PrivetRegisterOperation,
43 public PrivetURLFetcher::Delegate,
44 public PrivetInfoOperation::Delegate,
45 public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
47 PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client,
48 const std::string& user,
49 PrivetRegisterOperation::Delegate* delegate);
50 virtual ~PrivetRegisterOperationImpl();
52 virtual void Start() OVERRIDE;
53 virtual void Cancel() OVERRIDE;
54 virtual void CompleteRegistration() OVERRIDE;
56 virtual void OnError(PrivetURLFetcher* fetcher,
57 PrivetURLFetcher::ErrorType error) OVERRIDE;
59 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
60 const base::DictionaryValue* value,
61 bool has_error) OVERRIDE;
63 virtual void OnNeedPrivetToken(
64 PrivetURLFetcher* fetcher,
65 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
67 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
69 const base::DictionaryValue* value) OVERRIDE;
71 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
73 class Cancelation : public PrivetURLFetcher::Delegate {
75 Cancelation(PrivetHTTPClientImpl* privet_client,
76 const std::string& user);
77 virtual ~Cancelation();
79 virtual void OnError(PrivetURLFetcher* fetcher,
80 PrivetURLFetcher::ErrorType error) OVERRIDE;
82 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
83 const base::DictionaryValue* value,
84 bool has_error) OVERRIDE;
89 scoped_ptr<PrivetURLFetcher> url_fetcher_;
92 // Arguments is JSON value from request.
93 typedef base::Callback<void(const base::DictionaryValue&)>
96 void StartInfoOperation();
97 void StartResponse(const base::DictionaryValue& value);
98 void GetClaimTokenResponse(const base::DictionaryValue& value);
99 void CompleteResponse(const base::DictionaryValue& value);
101 void SendRequest(const std::string& action);
104 std::string current_action_;
105 scoped_ptr<PrivetURLFetcher> url_fetcher_;
106 PrivetRegisterOperation::Delegate* delegate_;
107 PrivetHTTPClientImpl* privet_client_;
108 ResponseHandler next_response_handler_;
109 // Required to ensure destroying completed register operations doesn't cause
110 // extraneous cancelations.
113 scoped_ptr<PrivetInfoOperation> info_operation_;
114 std::string expected_id_;
117 // TODO(noamsml): Factor out some of this code into a PrivetBaseOperation
118 class PrivetCapabilitiesOperationImpl : public PrivetCapabilitiesOperation,
119 public PrivetURLFetcher::Delegate {
121 PrivetCapabilitiesOperationImpl(
122 PrivetHTTPClientImpl* privet_client,
123 PrivetCapabilitiesOperation::Delegate* delegate);
124 virtual ~PrivetCapabilitiesOperationImpl();
125 virtual void Start() OVERRIDE;
127 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
129 virtual void OnError(PrivetURLFetcher* fetcher,
130 PrivetURLFetcher::ErrorType error) OVERRIDE;
131 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
132 const base::DictionaryValue* value,
133 bool has_error) OVERRIDE;
134 virtual void OnNeedPrivetToken(
135 PrivetURLFetcher* fetcher,
136 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
139 PrivetHTTPClientImpl* privet_client_;
140 PrivetCapabilitiesOperation::Delegate* delegate_;
142 scoped_ptr<PrivetURLFetcher> url_fetcher_;
143 scoped_ptr<PrivetInfoOperation> info_operation_;
146 class PrivetLocalPrintOperationImpl
147 : public PrivetLocalPrintOperation,
148 public PrivetURLFetcher::Delegate,
149 public PrivetInfoOperation::Delegate {
151 PrivetLocalPrintOperationImpl(
152 PrivetHTTPClientImpl* privet_client,
153 PrivetLocalPrintOperation::Delegate* delegate);
155 virtual ~PrivetLocalPrintOperationImpl();
156 virtual void Start() OVERRIDE;
158 virtual void SendData(const std::string& data) OVERRIDE;
160 virtual void SetTicket(const std::string& ticket) OVERRIDE;
162 virtual void SetUsername(const std::string& user) OVERRIDE;
164 virtual void SetJobname(const std::string& jobname) OVERRIDE;
166 virtual void SetOffline(bool offline) OVERRIDE;
168 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
170 virtual void OnError(PrivetURLFetcher* fetcher,
171 PrivetURLFetcher::ErrorType error) OVERRIDE;
172 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
173 const base::DictionaryValue* value,
174 bool has_error) OVERRIDE;
175 virtual void OnNeedPrivetToken(
176 PrivetURLFetcher* fetcher,
177 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
179 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
181 const base::DictionaryValue* value) OVERRIDE;
183 typedef base::Callback<void(const base::DictionaryValue* value)>
186 void StartCurrentRequest();
188 void StartInitialRequest();
189 void GetCapabilities();
190 // TODO(noamsml): void DoCreatejob();
193 void OnCapabilities(const base::DictionaryValue* value);
194 void OnSubmitdocResponse(const base::DictionaryValue* value);
196 PrivetHTTPClientImpl* privet_client_;
197 PrivetLocalPrintOperation::Delegate* delegate_;
199 base::Closure current_request_;
200 ResponseCallback current_response_;
206 bool has_capabilities_;
207 bool has_extended_workflow_;
212 std::string jobname_;
214 scoped_ptr<PrivetURLFetcher> url_fetcher_;
215 scoped_ptr<PrivetInfoOperation> info_operation_;
218 class PrivetHTTPClientImpl : public PrivetHTTPClient,
219 public PrivetInfoOperation::Delegate {
221 PrivetHTTPClientImpl(
222 const std::string& name,
223 const net::HostPortPair& host_port,
224 net::URLRequestContextGetter* request_context);
225 virtual ~PrivetHTTPClientImpl();
227 virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
229 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
230 const std::string& user,
231 PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
233 virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
234 PrivetInfoOperation::Delegate* delegate) OVERRIDE;
236 virtual scoped_ptr<PrivetCapabilitiesOperation> CreateCapabilitiesOperation(
237 PrivetCapabilitiesOperation::Delegate* delegate) OVERRIDE;
239 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
240 PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
242 virtual const std::string& GetName() OVERRIDE;
244 scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
246 net::URLFetcher::RequestType request_type,
247 PrivetURLFetcher::Delegate* delegate) const;
249 void CacheInfo(const base::DictionaryValue* cached_info);
251 bool HasToken() const;
253 void RefreshPrivetToken(
254 const PrivetURLFetcher::TokenCallback& token_callback);
256 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
258 const base::DictionaryValue* value) OVERRIDE;
261 typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
263 PrivetURLFetcherFactory fetcher_factory_;
264 net::HostPortPair host_port_;
265 scoped_ptr<base::DictionaryValue> cached_info_;
267 scoped_ptr<PrivetInfoOperation> info_operation_;
268 TokenCallbackVector token_callbacks_;
271 } // namespace local_discovery
272 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_