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_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
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"
16 class RefCountedBytes
;
24 class PdfRenderSettings
;
27 namespace local_discovery
{
29 class PrivetHTTPClient
;
31 // Represents a simple request that returns pure JSON.
32 class PrivetJSONOperation
{
34 // If value is null, the operation failed.
35 typedef base::Callback
<void(
36 const base::DictionaryValue
* /*value*/)> ResultCallback
;
38 virtual ~PrivetJSONOperation() {}
40 virtual void Start() = 0;
42 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
45 class PrivetDataReadOperation
{
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
;
59 virtual ~PrivetDataReadOperation() {}
61 virtual void Start() = 0;
63 virtual void SetDataRange(int range_start
, int range_end
) = 0;
65 virtual void SaveDataToFile() = 0;
67 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
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).
74 class PrivetRegisterOperation
{
80 FAILURE_MALFORMED_RESPONSE
,
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
,
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;
107 // Called when the registration is done.
108 virtual void OnPrivetRegisterDone(PrivetRegisterOperation
* operation
,
109 const std::string
& device_id
) = 0;
112 virtual ~PrivetRegisterOperation() {}
114 virtual void Start() = 0;
115 // Owner SHOULD call explicitly before destroying operation.
116 virtual void Cancel() = 0;
117 virtual void CompleteRegistration() = 0;
119 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
122 class PrivetLocalPrintOperation
{
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;
133 virtual ~PrivetLocalPrintOperation() {}
135 virtual void Start() = 0;
138 // Required print data. MUST be called before calling |Start()|.
139 virtual void SetData(base::RefCountedBytes
* data
) = 0;
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;
155 // For testing, inject an alternative PWG raster converter.
156 virtual void SetPWGRasterConverterForTesting(
157 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) = 0;
159 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
162 // Privet HTTP client. Must not outlive the operations it creates.
163 class PrivetHTTPClient
{
165 virtual ~PrivetHTTPClient() {}
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;
183 // A name for the HTTP client, e.g. the device name for the privet device.
184 virtual const std::string
& GetName() = 0;
187 } // namespace local_discovery
188 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_