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