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 // Privet HTTP client. Must outlive the operations it creates.
46 class PrivetHTTPClient
{
48 virtual ~PrivetHTTPClient() {}
50 // A name for the HTTP client, e.g. the device name for the privet device.
51 virtual const std::string
& GetName() = 0;
53 // Creates operation to query basic information about local device.
54 virtual scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
55 const PrivetJSONOperation::ResultCallback
& callback
) = 0;
57 // Creates a URL fetcher for PrivetV1.
58 virtual scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
60 net::URLFetcher::RequestType request_type
,
61 PrivetURLFetcher::Delegate
* delegate
) = 0;
63 virtual void RefreshPrivetToken(
64 const PrivetURLFetcher::TokenCallback
& token_callback
) = 0;
67 class PrivetDataReadOperation
{
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
;
81 virtual ~PrivetDataReadOperation() {}
83 virtual void Start() = 0;
85 virtual void SetDataRange(int range_start
, int range_end
) = 0;
87 virtual void SaveDataToFile() = 0;
89 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
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).
96 class PrivetRegisterOperation
{
102 FAILURE_MALFORMED_RESPONSE
,
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;
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;
129 // Called when the registration is done.
130 virtual void OnPrivetRegisterDone(PrivetRegisterOperation
* operation
,
131 const std::string
& device_id
) = 0;
134 virtual ~PrivetRegisterOperation() {}
136 virtual void Start() = 0;
137 // Owner SHOULD call explicitly before destroying operation.
138 virtual void Cancel() = 0;
139 virtual void CompleteRegistration() = 0;
141 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
144 class PrivetLocalPrintOperation
{
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;
155 virtual ~PrivetLocalPrintOperation() {}
157 virtual void Start() = 0;
160 // Required print data. MUST be called before calling |Start()|.
161 virtual void SetData(base::RefCountedBytes
* data
) = 0;
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;
177 // For testing, inject an alternative PWG raster converter.
178 virtual void SetPWGRasterConverterForTesting(
179 scoped_ptr
<PWGRasterConverter
> pwg_raster_converter
) = 0;
181 virtual PrivetHTTPClient
* GetHTTPClient() = 0;
184 // Privet HTTP client. Must outlive the operations it creates.
185 class PrivetV1HTTPClient
{
187 virtual ~PrivetV1HTTPClient() {}
189 static scoped_ptr
<PrivetV1HTTPClient
> CreateDefault(
190 scoped_ptr
<PrivetHTTPClient
> info_client
);
192 // A name for the HTTP client, e.g. the device name for the privet device.
193 virtual const std::string
& GetName() = 0;
195 // Creates operation to query basic information about local device.
196 virtual scoped_ptr
<PrivetJSONOperation
> CreateInfoOperation(
197 const PrivetJSONOperation::ResultCallback
& callback
) = 0;
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;
204 // Creates operation to query capabilities of local printer.
205 virtual scoped_ptr
<PrivetJSONOperation
> CreateCapabilitiesOperation(
206 const PrivetJSONOperation::ResultCallback
& callback
) = 0;
208 // Creates operation to submit print job to local printer.
209 virtual scoped_ptr
<PrivetLocalPrintOperation
> CreateLocalPrintOperation(
210 PrivetLocalPrintOperation::Delegate
* delegate
) = 0;
212 // Creates operation to list files on local Privet storage.
213 virtual scoped_ptr
<PrivetJSONOperation
> CreateStorageListOperation(
214 const std::string
& path
,
215 const PrivetJSONOperation::ResultCallback
& callback
) = 0;
217 // Creates operation to read data from local Privet storage.
218 virtual scoped_ptr
<PrivetDataReadOperation
> CreateStorageReadOperation(
219 const std::string
& path
,
220 const PrivetDataReadOperation::ResultCallback
& callback
) = 0;
223 } // namespace local_discovery
224 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_