privet_http_impl.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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/memory/weak_ptr.h"
12#include "chrome/browser/local_discovery/privet_http.h"
13
14namespace local_discovery {
15
16class PrivetHTTPClientImpl;
17
18class PrivetInfoOperationImpl : public PrivetInfoOperation,
19                                public PrivetURLFetcher::Delegate {
20 public:
21  PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client,
22                          PrivetInfoOperation::Delegate* delegate);
23  virtual ~PrivetInfoOperationImpl();
24
25  virtual void Start() OVERRIDE;
26
27  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
28
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;
34
35 private:
36  PrivetHTTPClientImpl* privet_client_;
37  PrivetInfoOperation::Delegate* delegate_;
38  scoped_ptr<PrivetURLFetcher> url_fetcher_;
39};
40
41class PrivetRegisterOperationImpl
42    : public PrivetRegisterOperation,
43      public PrivetURLFetcher::Delegate,
44      public PrivetInfoOperation::Delegate,
45      public base::SupportsWeakPtr<PrivetRegisterOperationImpl> {
46 public:
47  PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client,
48                              const std::string& user,
49                              PrivetRegisterOperation::Delegate* delegate);
50  virtual ~PrivetRegisterOperationImpl();
51
52  virtual void Start() OVERRIDE;
53  virtual void Cancel() OVERRIDE;
54  virtual void CompleteRegistration() OVERRIDE;
55
56  virtual void OnError(PrivetURLFetcher* fetcher,
57                       PrivetURLFetcher::ErrorType error) OVERRIDE;
58
59  virtual void OnParsedJson(PrivetURLFetcher* fetcher,
60                            const base::DictionaryValue* value,
61                            bool has_error) OVERRIDE;
62
63  virtual void OnNeedPrivetToken(
64      PrivetURLFetcher* fetcher,
65      const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
66
67  virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
68                                int http_code,
69                                const base::DictionaryValue* value) OVERRIDE;
70
71  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
72 private:
73  class Cancelation : public PrivetURLFetcher::Delegate {
74   public:
75    Cancelation(PrivetHTTPClientImpl* privet_client,
76                const std::string& user);
77    virtual ~Cancelation();
78
79    virtual void OnError(PrivetURLFetcher* fetcher,
80                         PrivetURLFetcher::ErrorType error) OVERRIDE;
81
82    virtual void OnParsedJson(PrivetURLFetcher* fetcher,
83                              const base::DictionaryValue* value,
84                              bool has_error) OVERRIDE;
85
86    void Cleanup();
87
88   private:
89    scoped_ptr<PrivetURLFetcher> url_fetcher_;
90  };
91
92  // Arguments is JSON value from request.
93  typedef base::Callback<void(const base::DictionaryValue&)>
94      ResponseHandler;
95
96  void StartInfoOperation();
97  void StartResponse(const base::DictionaryValue& value);
98  void GetClaimTokenResponse(const base::DictionaryValue& value);
99  void CompleteResponse(const base::DictionaryValue& value);
100
101  void SendRequest(const std::string& action);
102
103  std::string user_;
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.
111  bool ongoing_;
112
113  scoped_ptr<PrivetInfoOperation> info_operation_;
114  std::string expected_id_;
115};
116
117// TODO(noamsml): Factor out some of this code into a PrivetBaseOperation
118class PrivetCapabilitiesOperationImpl : public PrivetCapabilitiesOperation,
119                                        public PrivetURLFetcher::Delegate {
120 public:
121  PrivetCapabilitiesOperationImpl(
122      PrivetHTTPClientImpl* privet_client,
123      PrivetCapabilitiesOperation::Delegate* delegate);
124  virtual ~PrivetCapabilitiesOperationImpl();
125  virtual void Start() OVERRIDE;
126
127  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
128
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;
137
138 private:
139  PrivetHTTPClientImpl* privet_client_;
140  PrivetCapabilitiesOperation::Delegate* delegate_;
141
142  scoped_ptr<PrivetURLFetcher> url_fetcher_;
143  scoped_ptr<PrivetInfoOperation> info_operation_;
144};
145
146class PrivetLocalPrintOperationImpl
147    : public PrivetLocalPrintOperation,
148      public PrivetURLFetcher::Delegate,
149      public PrivetInfoOperation::Delegate {
150 public:
151  PrivetLocalPrintOperationImpl(
152      PrivetHTTPClientImpl* privet_client,
153      PrivetLocalPrintOperation::Delegate* delegate);
154
155  virtual ~PrivetLocalPrintOperationImpl();
156  virtual void Start() OVERRIDE;
157
158  virtual void SendData(const std::string& data) OVERRIDE;
159
160  virtual void SetTicket(const std::string& ticket) OVERRIDE;
161
162  virtual void SetUsername(const std::string& user) OVERRIDE;
163
164  virtual void SetJobname(const std::string& jobname) OVERRIDE;
165
166  virtual  void SetOffline(bool offline) OVERRIDE;
167
168  virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
169
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;
178
179  virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
180                                int http_code,
181                                const base::DictionaryValue* value) OVERRIDE;
182 private:
183  typedef base::Callback<void(const base::DictionaryValue* value)>
184      ResponseCallback;
185
186  void StartCurrentRequest();
187
188  void StartInitialRequest();
189  void GetCapabilities();
190  // TODO(noamsml): void DoCreatejob();
191  void DoSubmitdoc();
192
193  void OnCapabilities(const base::DictionaryValue* value);
194  void OnSubmitdocResponse(const base::DictionaryValue* value);
195
196  PrivetHTTPClientImpl* privet_client_;
197  PrivetLocalPrintOperation::Delegate* delegate_;
198
199  base::Closure current_request_;
200  ResponseCallback current_response_;
201
202  std::string ticket_;
203  std::string data_;
204
205  bool use_pdf_;
206  bool has_capabilities_;
207  bool has_extended_workflow_;
208  bool started_;
209  bool offline_;
210
211  std::string user_;
212  std::string jobname_;
213
214  scoped_ptr<PrivetURLFetcher> url_fetcher_;
215  scoped_ptr<PrivetInfoOperation> info_operation_;
216};
217
218class PrivetHTTPClientImpl : public PrivetHTTPClient,
219                             public PrivetInfoOperation::Delegate {
220 public:
221  PrivetHTTPClientImpl(
222      const std::string& name,
223      const net::HostPortPair& host_port,
224      net::URLRequestContextGetter* request_context);
225  virtual ~PrivetHTTPClientImpl();
226
227  virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE;
228
229  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
230      const std::string& user,
231      PrivetRegisterOperation::Delegate* delegate) OVERRIDE;
232
233  virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
234      PrivetInfoOperation::Delegate* delegate) OVERRIDE;
235
236  virtual scoped_ptr<PrivetCapabilitiesOperation> CreateCapabilitiesOperation(
237      PrivetCapabilitiesOperation::Delegate* delegate) OVERRIDE;
238
239  virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
240      PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE;
241
242  virtual const std::string& GetName() OVERRIDE;
243
244  scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
245      const GURL& url,
246      net::URLFetcher::RequestType request_type,
247      PrivetURLFetcher::Delegate* delegate) const;
248
249  void CacheInfo(const base::DictionaryValue* cached_info);
250
251  bool HasToken() const;
252
253  void RefreshPrivetToken(
254      const PrivetURLFetcher::TokenCallback& token_callback);
255
256  virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
257                                int http_code,
258                                const base::DictionaryValue* value) OVERRIDE;
259
260 private:
261  typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector;
262  std::string name_;
263  PrivetURLFetcherFactory fetcher_factory_;
264  net::HostPortPair host_port_;
265  scoped_ptr<base::DictionaryValue> cached_info_;
266
267  scoped_ptr<PrivetInfoOperation> info_operation_;
268  TokenCallbackVector token_callbacks_;
269};
270
271}  // namespace local_discovery
272#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_
273