privet_http.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 "net/base/host_port_pair.h" 13 14namespace local_discovery { 15 16class PrivetHTTPClient; 17 18// Represents a request to /privet/info. Will store a cached response and token 19// in the PrivetHTTPClient that created. 20class PrivetInfoOperation { 21 public: 22 class Delegate { 23 public: 24 virtual ~Delegate() {} 25 26 // In case of non-HTTP errors, |http_code| will be -1. 27 28 // TODO(noamsml): Remove http_code from this delegate; it's unnecessary in 29 // practice 30 virtual void OnPrivetInfoDone( 31 PrivetInfoOperation* operation, 32 int http_code, 33 const base::DictionaryValue* json_value) = 0; 34 }; 35 36 virtual ~PrivetInfoOperation() {} 37 38 virtual void Start() = 0; 39 40 virtual PrivetHTTPClient* GetHTTPClient() = 0; 41}; 42 43// Represents a full registration flow (/privet/register), normally consisting 44// of calling the start action, the getClaimToken action, and calling the 45// complete action. Some intervention from the caller is required to display the 46// claim URL to the user (noted in OnPrivetRegisterClaimURL). 47class PrivetRegisterOperation { 48 public: 49 enum FailureReason { 50 FAILURE_NETWORK, 51 FAILURE_HTTP_ERROR, 52 FAILURE_JSON_ERROR, 53 FAILURE_MALFORMED_RESPONSE 54 }; 55 56 class Delegate { 57 public: 58 ~Delegate() {} 59 60 // Called when a user needs to claim the printer by visiting the given URL. 61 virtual void OnPrivetRegisterClaimToken( 62 PrivetRegisterOperation* operation, 63 const std::string& token, 64 const GURL& url) = 0; 65 66 // TODO(noamsml): Remove all unnecessary parameters. 67 // Called in case of an error while registering. |action| is the 68 // registration action taken during the error. |reason| is the reason for 69 // the failure. |printer_http_code| is the http code returned from the 70 // printer. If it is -1, an internal error occurred while trying to complete 71 // the request. |json| may be null if printer_http_code signifies an error. 72 virtual void OnPrivetRegisterError(PrivetRegisterOperation* operation, 73 const std::string& action, 74 FailureReason reason, 75 int printer_http_code, 76 const DictionaryValue* json) = 0; 77 78 // Called when the registration is done. 79 virtual void OnPrivetRegisterDone(PrivetRegisterOperation* operation, 80 const std::string& device_id) = 0; 81 }; 82 83 virtual ~PrivetRegisterOperation() {} 84 85 virtual void Start() = 0; 86 // Owner SHOULD call explicitly before destroying operation. 87 virtual void Cancel() = 0; 88 virtual void CompleteRegistration() = 0; 89 90 virtual PrivetHTTPClient* GetHTTPClient() = 0; 91}; 92 93class PrivetCapabilitiesOperation { 94 public: 95 class Delegate { 96 public: 97 virtual ~Delegate() {} 98 99 // |capabilities| will be NULL in case of an error. 100 virtual void OnPrivetCapabilities( 101 PrivetCapabilitiesOperation* capabilities_operation, 102 int http_error, 103 const base::DictionaryValue* capabilities) = 0; 104 }; 105 106 virtual ~PrivetCapabilitiesOperation() {} 107 virtual void Start() = 0; 108 109 virtual PrivetHTTPClient* GetHTTPClient() = 0; 110}; 111 112class PrivetLocalPrintOperation { 113 public: 114 class Delegate { 115 public: 116 virtual ~Delegate() {} 117 virtual void OnPrivetPrintingRequestPDF( 118 const PrivetLocalPrintOperation* print_operation) = 0; 119 virtual void OnPrivetPrintingRequestPWGRaster( 120 const PrivetLocalPrintOperation* print_operation) = 0; 121 virtual void OnPrivetPrintingDone( 122 const PrivetLocalPrintOperation* print_operation) = 0; 123 virtual void OnPrivetPrintingError( 124 const PrivetLocalPrintOperation* print_operation, int http_code) = 0; 125 }; 126 127 virtual ~PrivetLocalPrintOperation() {} 128 129 virtual void Start() = 0; 130 131 // Should be called ONLY after |OnPrivetPrintingRequestPDF| or 132 // |OnPrivetPrintingRequestPWGRaster| are called on the delegate. Data should 133 // be in PDF or PWG format depending on what is requested by the local print 134 // operation. 135 virtual void SendData(const std::string& data) = 0; 136 137 // Optional attributes for /submitdoc. Call before calling |Start()| 138 // |ticket| should be in CJT format. 139 virtual void SetTicket(const std::string& ticket) = 0; 140 // Username and jobname are for display only. 141 virtual void SetUsername(const std::string& username) = 0; 142 virtual void SetJobname(const std::string& jobname) = 0; 143 // If |offline| is true, we will indicate to the printer not to post the job 144 // to Google Cloud Print. 145 virtual void SetOffline(bool offline) = 0; 146 147 virtual PrivetHTTPClient* GetHTTPClient() = 0; 148}; 149 150// Privet HTTP client. Must not outlive the operations it creates. 151class PrivetHTTPClient { 152 public: 153 virtual ~PrivetHTTPClient() {} 154 virtual const base::DictionaryValue* GetCachedInfo() const = 0; 155 156 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation( 157 const std::string& user, 158 PrivetRegisterOperation::Delegate* delegate) = 0; 159 virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation( 160 PrivetInfoOperation::Delegate* delegate) = 0; 161 virtual scoped_ptr<PrivetCapabilitiesOperation> CreateCapabilitiesOperation( 162 PrivetCapabilitiesOperation::Delegate* delegate) = 0; 163 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation( 164 PrivetLocalPrintOperation::Delegate* delegate) = 0; 165 166 // A name for the HTTP client, e.g. the device name for the privet device. 167 virtual const std::string& GetName() = 0; 168}; 169 170} // namespace local_discovery 171#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_ 172