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