privet_http.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "chrome/browser/local_discovery/privet_url_fetcher.h"
12#include "net/base/host_port_pair.h"
13
14namespace base {
15class RefCountedBytes;
16}
17
18namespace gfx {
19class Size;
20}
21
22namespace printing {
23class PdfRenderSettings;
24}
25
26namespace local_discovery {
27
28class PWGRasterConverter;
29class PrivetHTTPClient;
30
31// Represents a simple request that returns pure JSON.
32class PrivetJSONOperation {
33 public:
34  // If value is null, the operation failed.
35  typedef base::Callback<void(
36      const base::DictionaryValue* /*value*/)> ResultCallback;
37
38  virtual ~PrivetJSONOperation() {}
39
40  virtual void Start() = 0;
41
42  virtual PrivetHTTPClient* GetHTTPClient() = 0;
43};
44
45// Privet HTTP client. Must outlive the operations it creates.
46class PrivetHTTPClient {
47 public:
48  virtual ~PrivetHTTPClient() {}
49
50  // A name for the HTTP client, e.g. the device name for the privet device.
51  virtual const std::string& GetName() = 0;
52
53  // Creates operation to query basic information about local device.
54  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
55      const PrivetJSONOperation::ResultCallback& callback) = 0;
56
57  // Creates a URL fetcher for PrivetV1.
58  virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
59      const GURL& url,
60      net::URLFetcher::RequestType request_type,
61      PrivetURLFetcher::Delegate* delegate) = 0;
62
63  virtual void RefreshPrivetToken(
64      const PrivetURLFetcher::TokenCallback& token_callback) = 0;
65};
66
67class PrivetDataReadOperation {
68 public:
69  enum ResponseType {
70    RESPONSE_TYPE_ERROR,
71    RESPONSE_TYPE_STRING,
72    RESPONSE_TYPE_FILE
73  };
74
75  // If value is null, the operation failed.
76  typedef base::Callback<void(
77      ResponseType /*response_type*/,
78      const std::string& /*response_str*/,
79      const base::FilePath& /*response_file_path*/)> ResultCallback;
80
81  virtual ~PrivetDataReadOperation() {}
82
83  virtual void Start() = 0;
84
85  virtual void SetDataRange(int range_start, int range_end) = 0;
86
87  virtual void SaveDataToFile() = 0;
88
89  virtual PrivetHTTPClient* GetHTTPClient() = 0;
90};
91
92// Represents a full registration flow (/privet/register), normally consisting
93// of calling the start action, the getClaimToken action, and calling the
94// complete action. Some intervention from the caller is required to display the
95// claim URL to the user (noted in OnPrivetRegisterClaimURL).
96class PrivetRegisterOperation {
97 public:
98  enum FailureReason {
99    FAILURE_NETWORK,
100    FAILURE_HTTP_ERROR,
101    FAILURE_JSON_ERROR,
102    FAILURE_MALFORMED_RESPONSE,
103    FAILURE_TOKEN,
104    FAILURE_RETRY
105  };
106
107  class Delegate {
108   public:
109    ~Delegate() {}
110
111    // Called when a user needs to claim the printer by visiting the given URL.
112    virtual void OnPrivetRegisterClaimToken(
113        PrivetRegisterOperation* operation,
114        const std::string& token,
115        const GURL& url) = 0;
116
117    // TODO(noamsml): Remove all unnecessary parameters.
118    // Called in case of an error while registering.  |action| is the
119    // registration action taken during the error. |reason| is the reason for
120    // the failure. |printer_http_code| is the http code returned from the
121    // printer. If it is -1, an internal error occurred while trying to complete
122    // the request. |json| may be null if printer_http_code signifies an error.
123    virtual void OnPrivetRegisterError(PrivetRegisterOperation* operation,
124                                       const std::string& action,
125                                       FailureReason reason,
126                                       int printer_http_code,
127                                       const base::DictionaryValue* json) = 0;
128
129    // Called when the registration is done.
130    virtual void OnPrivetRegisterDone(PrivetRegisterOperation* operation,
131                                      const std::string& device_id) = 0;
132  };
133
134  virtual ~PrivetRegisterOperation() {}
135
136  virtual void Start() = 0;
137  // Owner SHOULD call explicitly before destroying operation.
138  virtual void Cancel() = 0;
139  virtual void CompleteRegistration() = 0;
140
141  virtual PrivetHTTPClient* GetHTTPClient() = 0;
142};
143
144class PrivetLocalPrintOperation {
145 public:
146  class Delegate {
147   public:
148    virtual ~Delegate() {}
149    virtual void OnPrivetPrintingDone(
150        const PrivetLocalPrintOperation* print_operation) = 0;
151    virtual void OnPrivetPrintingError(
152        const PrivetLocalPrintOperation* print_operation, int http_code) = 0;
153  };
154
155  virtual ~PrivetLocalPrintOperation() {}
156
157  virtual void Start() = 0;
158
159
160  // Required print data. MUST be called before calling |Start()|.
161  virtual void SetData(const scoped_refptr<base::RefCountedBytes>& data) = 0;
162
163  // Optional attributes for /submitdoc. Call before calling |Start()|
164  // |ticket| should be in CJT format.
165  virtual void SetTicket(const std::string& ticket) = 0;
166  // |capabilities| should be in CDD format.
167  virtual void SetCapabilities(const std::string& capabilities) = 0;
168  // Username and jobname are for display only.
169  virtual void SetUsername(const std::string& username) = 0;
170  virtual void SetJobname(const std::string& jobname) = 0;
171  // If |offline| is true, we will indicate to the printer not to post the job
172  // to Google Cloud Print.
173  virtual void SetOffline(bool offline) = 0;
174  // Document page size.
175  virtual void SetPageSize(const gfx::Size& page_size) = 0;
176
177  // For testing, inject an alternative PWG raster converter.
178  virtual void SetPWGRasterConverterForTesting(
179      scoped_ptr<PWGRasterConverter> pwg_raster_converter) = 0;
180
181  virtual PrivetHTTPClient* GetHTTPClient() = 0;
182};
183
184// Privet HTTP client. Must outlive the operations it creates.
185class PrivetV1HTTPClient {
186 public:
187  virtual ~PrivetV1HTTPClient() {}
188
189  static scoped_ptr<PrivetV1HTTPClient> CreateDefault(
190      scoped_ptr<PrivetHTTPClient> info_client);
191
192  // A name for the HTTP client, e.g. the device name for the privet device.
193  virtual const std::string& GetName() = 0;
194
195  // Creates operation to query basic information about local device.
196  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
197      const PrivetJSONOperation::ResultCallback& callback) = 0;
198
199  // Creates operation to register local device using Privet v1 protocol.
200  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
201      const std::string& user,
202      PrivetRegisterOperation::Delegate* delegate) = 0;
203
204  // Creates operation to query capabilities of local printer.
205  virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
206      const PrivetJSONOperation::ResultCallback& callback) = 0;
207
208  // Creates operation to submit print job to local printer.
209  virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
210      PrivetLocalPrintOperation::Delegate* delegate) = 0;
211};
212
213}  // namespace local_discovery
214#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
215