privet_http.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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  // |capabilities| should be in CDD format.
145  virtual void SetCapabilities(const std::string& capabilities) = 0;
146  // Username and jobname are for display only.
147  virtual void SetUsername(const std::string& username) = 0;
148  virtual void SetJobname(const std::string& jobname) = 0;
149  // If |offline| is true, we will indicate to the printer not to post the job
150  // to Google Cloud Print.
151  virtual void SetOffline(bool offline) = 0;
152  // Document page size.
153  virtual void SetPageSize(const gfx::Size& page_size) = 0;
154
155  // For testing, inject an alternative PWG raster converter.
156  virtual void SetPWGRasterConverterForTesting(
157      scoped_ptr<PWGRasterConverter> pwg_raster_converter) = 0;
158
159  virtual PrivetHTTPClient* GetHTTPClient() = 0;
160};
161
162// Privet HTTP client. Must not outlive the operations it creates.
163class PrivetHTTPClient {
164 public:
165  virtual ~PrivetHTTPClient() {}
166
167  virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
168      const std::string& user,
169      PrivetRegisterOperation::Delegate* delegate) = 0;
170  virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
171      const PrivetJSONOperation::ResultCallback& callback) = 0;
172  virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation(
173      const PrivetJSONOperation::ResultCallback& callback) = 0;
174  virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
175      PrivetLocalPrintOperation::Delegate* delegate) = 0;
176  virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
177      const std::string& path,
178      const PrivetJSONOperation::ResultCallback& callback) = 0;
179  virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
180      const std::string& path,
181      const PrivetDataReadOperation::ResultCallback& callback) = 0;
182
183  // A name for the HTTP client, e.g. the device name for the privet device.
184  virtual const std::string& GetName() = 0;
185};
186
187}  // namespace local_discovery
188#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
189