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 GCP20_PROTOTYPE_PRINTER_H_ 6 #define GCP20_PROTOTYPE_PRINTER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/files/file_path.h" 12 #include "base/memory/weak_ptr.h" 13 #include "cloud_print/gcp20/prototype/cloud_print_requester.h" 14 #include "cloud_print/gcp20/prototype/cloud_print_xmpp_listener.h" 15 #include "cloud_print/gcp20/prototype/dns_sd_server.h" 16 #include "cloud_print/gcp20/prototype/print_job_handler.h" 17 #include "cloud_print/gcp20/prototype/printer_state.h" 18 #include "cloud_print/gcp20/prototype/privet_http_server.h" 19 #include "cloud_print/gcp20/prototype/x_privet_token.h" 20 21 extern const base::FilePath::CharType kPrinterStatePath[]; 22 23 // This class maintains work of DNS-SD server, HTTP server and others. 24 class Printer : public base::SupportsWeakPtr<Printer>, 25 public PrivetHttpServer::Delegate, 26 public CloudPrintRequester::Delegate, 27 public CloudPrintXmppListener::Delegate { 28 public: 29 // Constructs uninitialized object. 30 Printer(); 31 32 // Destroys the object. 33 virtual ~Printer(); 34 35 // Starts all servers. 36 bool Start(); 37 38 // Returns true if printer was started. 39 bool IsRunning() const; 40 41 // Stops all servers. 42 void Stop(); 43 44 private: 45 FRIEND_TEST_ALL_PREFIXES(Printer, ValidateCapabilities); 46 47 enum ConnectionState { 48 NOT_CONFIGURED, 49 OFFLINE, 50 ONLINE, 51 CONNECTING 52 }; 53 54 std::string GetRawCdd(); 55 56 // PrivetHttpServer::Delegate methods: 57 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart( 58 const std::string& user) OVERRIDE; 59 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken( 60 const std::string& user, 61 std::string* token, 62 std::string* claim_url) OVERRIDE; 63 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete( 64 const std::string& user, 65 std::string* device_id) OVERRIDE; 66 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel( 67 const std::string& user) OVERRIDE; 68 virtual void GetRegistrationServerError(std::string* description) OVERRIDE; 69 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; 70 virtual bool IsRegistered() const OVERRIDE; 71 virtual bool IsLocalPrintingAllowed() const OVERRIDE; 72 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; 73 virtual const base::DictionaryValue& GetCapabilities() OVERRIDE; 74 virtual LocalPrintJob::CreateResult CreateJob( 75 const std::string& ticket, 76 std::string* job_id, 77 int* expires_in, 78 int* error_timeout, 79 std::string* error_description) OVERRIDE; 80 virtual LocalPrintJob::SaveResult SubmitDoc( 81 const LocalPrintJob& job, 82 std::string* job_id, 83 int* expires_in, 84 std::string* error_description, 85 int* timeout) OVERRIDE; 86 virtual LocalPrintJob::SaveResult SubmitDocWithId( 87 const LocalPrintJob& job, 88 const std::string& job_id, 89 int* expires_in, 90 std::string* error_description, 91 int* timeout) OVERRIDE; 92 virtual bool GetJobState(const std::string& id, 93 LocalPrintJob::Info* info) OVERRIDE; 94 95 // CloudRequester::Delegate methods: 96 virtual void OnRegistrationStartResponseParsed( 97 const std::string& registration_token, 98 const std::string& complete_invite_url, 99 const std::string& device_id) OVERRIDE; 100 virtual void OnRegistrationFinished( 101 const std::string& refresh_token, 102 const std::string& access_token, 103 int access_token_expires_in_seconds) OVERRIDE; 104 virtual void OnXmppJidReceived(const std::string& xmpp_jid) OVERRIDE; 105 virtual void OnAccesstokenReceviced(const std::string& access_token, 106 int expires_in_seconds) OVERRIDE; 107 virtual void OnRegistrationError(const std::string& description) OVERRIDE; 108 virtual void OnNetworkError() OVERRIDE; 109 virtual void OnServerError(const std::string& description) OVERRIDE; 110 virtual void OnAuthError() OVERRIDE; 111 virtual std::string GetAccessToken() OVERRIDE; 112 virtual void OnPrintJobsAvailable( 113 const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE; 114 virtual void OnPrintJobDownloaded( 115 const cloud_print_response_parser::Job& job) OVERRIDE; 116 virtual void OnPrintJobDone() OVERRIDE; 117 virtual void OnLocalSettingsReceived( 118 LocalSettings::State state, 119 const LocalSettings& settings) OVERRIDE; 120 virtual void OnLocalSettingsUpdated() OVERRIDE; 121 122 // CloudPrintXmppListener::Delegate methods: 123 virtual void OnXmppConnected() OVERRIDE; 124 virtual void OnXmppAuthError() OVERRIDE; 125 virtual void OnXmppNetworkError() OVERRIDE; 126 virtual void OnXmppNewPrintJob(const std::string& device_id) OVERRIDE; 127 virtual void OnXmppNewLocalSettings(const std::string& device_id) OVERRIDE; 128 virtual void OnXmppDeleteNotification(const std::string& device_id) OVERRIDE; 129 130 // Method for trying to reconnecting to server on start or after network fail. 131 void TryConnect(); 132 133 // Connects XMPP. 134 void ConnectXmpp(); 135 136 // Method to handle pending events. 137 // Do *NOT* call this method instantly. Only with |PostOnIdle|. 138 void OnIdle(); 139 140 // Ask Cloud Print server for printjobs. 141 void FetchPrintJobs(); 142 143 // Ask Cloud Print server for new local sendings. 144 void GetLocalSettings(); 145 146 // Applies new local settings to printer. 147 void ApplyLocalSettings(const LocalSettings& settings); 148 149 // Used for erasing all printer info. 150 void OnPrinterDeleted(); 151 152 // Saves |access_token| and calculates time for next update. 153 void RememberAccessToken(const std::string& access_token, 154 int expires_in_seconds); 155 156 // Sets registration state to error and adds description. 157 void SetRegistrationError(const std::string& description); 158 159 // Checks if register call is called correctly (|user| is correct, 160 // error is no set etc). Returns |false| if error status is put into |status|. 161 // Otherwise no error was occurred. 162 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( 163 const std::string& user); 164 165 // Checks if confirmation was received. 166 void WaitUserConfirmation(base::Time valid_until); 167 168 // Generates ProxyId for this device. 169 std::string GenerateProxyId() const; 170 171 // Creates data for DNS TXT respond. 172 std::vector<std::string> CreateTxt() const; 173 174 // Saving and loading registration info from file. 175 void SaveToFile(); 176 bool LoadFromFile(); 177 178 // Adds |OnIdle| method to the MessageLoop. 179 void PostOnIdle(); 180 181 // Registration timeout. 182 void CheckRegistrationExpiration(); 183 184 // Delays expiration after user action. 185 void UpdateRegistrationExpiration(); 186 187 // Deletes registration expiration at all. 188 void InvalidateRegistrationExpiration(); 189 190 // Methods to start HTTP and DNS-SD servers. Return |true| if servers 191 // were started. If failed neither HTTP nor DNS-SD server will be running. 192 bool StartLocalDiscoveryServers(); 193 194 // Methods to start HTTP and DNS-SD servers. Return |true| if servers 195 // were started. 196 bool StartDnsServer(); 197 bool StartHttpServer(); 198 199 // Converts errors. 200 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( 201 PrinterState::ConfirmationState state); 202 203 std::string ConnectionStateToString(ConnectionState state) const; 204 205 // Changes state to OFFLINE and posts TryReconnect. 206 // For registration reconnect is instant every time. 207 void FallOffline(bool instant_reconnect); 208 209 // Changes state and update info in DNS server. Returns |true| if state 210 // was changed (otherwise state was the same). 211 bool ChangeState(ConnectionState new_state); 212 213 // Contains printers workflow info. 214 PrinterState state_; 215 216 // Connection state of device. 217 ConnectionState connection_state_; 218 219 // Contains DNS-SD server. 220 DnsSdServer dns_server_; 221 222 // Contains Privet HTTP server. 223 PrivetHttpServer http_server_; 224 225 // Contains CloudPrint client. 226 scoped_ptr<CloudPrintRequester> requester_; 227 228 // XMPP Listener. 229 scoped_ptr<CloudPrintXmppListener> xmpp_listener_; 230 231 XPrivetToken xtoken_; 232 233 scoped_ptr<PrintJobHandler> print_job_handler_; 234 235 // Uses for calculating uptime. 236 base::Time starttime_; 237 238 // Uses to validate registration timeout. 239 base::Time registration_expiration_; 240 241 // Used for preventing two and more OnIdle posted in message loop. 242 bool on_idle_posted_; 243 244 // Contains |true| if Printer has to check pending local settings. 245 bool pending_local_settings_check_; 246 247 // Contains |true| if Printer has to check available printjobs. 248 bool pending_print_jobs_check_; 249 250 // Contains |true| if Printer has to be deleted. 251 bool pending_deletion_; 252 253 DISALLOW_COPY_AND_ASSIGN(Printer); 254 }; 255 256 #endif // GCP20_PROTOTYPE_PRINTER_H_ 257 258