| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GCP20_PROTOTYPE_PRINTER_H_ | 5 #ifndef GCP20_PROTOTYPE_PRINTER_H_ |
| 6 #define GCP20_PROTOTYPE_PRINTER_H_ | 6 #define GCP20_PROTOTYPE_PRINTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "cloud_print/gcp20/prototype/cloud_print_requester.h" | 11 #include "cloud_print/gcp20/prototype/cloud_print_requester.h" |
| 12 #include "cloud_print/gcp20/prototype/dns_sd_server.h" | 12 #include "cloud_print/gcp20/prototype/dns_sd_server.h" |
| 13 #include "cloud_print/gcp20/prototype/printjob_handler.h" |
| 13 #include "cloud_print/gcp20/prototype/privet_http_server.h" | 14 #include "cloud_print/gcp20/prototype/privet_http_server.h" |
| 14 #include "cloud_print/gcp20/prototype/x_privet_token.h" | 15 #include "cloud_print/gcp20/prototype/x_privet_token.h" |
| 15 | 16 |
| 16 extern const char kPrinterStatePath[]; | 17 extern const char kPrinterStatePath[]; |
| 17 | 18 |
| 18 // This class maintain work of DNS-SD server, HTTP server and others. | 19 // This class maintains work of DNS-SD server, HTTP server and others. |
| 19 class Printer : public PrivetHttpServer::Delegate, | 20 class Printer : public PrivetHttpServer::Delegate, |
| 20 public CloudPrintRequester::Delegate { | 21 public CloudPrintRequester::Delegate, |
| 22 public base::SupportsWeakPtr<Printer> { |
| 21 public: | 23 public: |
| 22 // Constructs uninitialized object. | 24 // Constructs uninitialized object. |
| 23 Printer(); | 25 Printer(); |
| 24 | 26 |
| 25 // Destroys the object. | 27 // Destroys the object. |
| 26 virtual ~Printer(); | 28 virtual ~Printer(); |
| 27 | 29 |
| 28 // Starts all servers. | 30 // Starts all servers. |
| 29 bool Start(); | 31 bool Start(); |
| 30 | 32 |
| 31 // Returns true if printer was started. | 33 // Returns true if printer was started. |
| 32 bool IsOnline() const; | 34 bool IsOnline() const; |
| 33 | 35 |
| 36 // Method for trying to reconnecting to server. |
| 37 void WakeUp(); |
| 38 |
| 34 // Stops all servers. | 39 // Stops all servers. |
| 35 void Stop(); | 40 void Stop(); |
| 36 | 41 |
| 37 private: | 42 private: |
| 38 struct RegistrationInfo { | 43 struct RegistrationInfo { |
| 39 enum RegistrationState { | 44 enum RegistrationState { |
| 40 DEV_REG_UNREGISTERED, | 45 DEV_REG_UNREGISTERED, |
| 41 DEV_REG_REGISTRATION_STARTED, | 46 DEV_REG_REGISTRATION_STARTED, |
| 42 DEV_REG_REGISTRATION_CLAIM_TOKEN_READY, | 47 DEV_REG_REGISTRATION_CLAIM_TOKEN_READY, |
| 43 DEV_REG_REGISTRATION_COMPLETING, | 48 DEV_REG_REGISTRATION_COMPLETING, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 67 // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set. | 72 // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set. |
| 68 std::string error_description; | 73 std::string error_description; |
| 69 }; | 74 }; |
| 70 | 75 |
| 71 enum RegistrationAction { | 76 enum RegistrationAction { |
| 72 REG_ACTION_START, | 77 REG_ACTION_START, |
| 73 REG_ACTION_GET_CLAIM_TOKEN, | 78 REG_ACTION_GET_CLAIM_TOKEN, |
| 74 REG_ACTION_COMPLETE, | 79 REG_ACTION_COMPLETE, |
| 75 REG_ACTION_CANCEL | 80 REG_ACTION_CANCEL |
| 76 }; | 81 }; |
| 82 enum ConnectionState { |
| 83 NOT_CONFIGURED, |
| 84 OFFLINE, |
| 85 ONLINE, |
| 86 CONNECTING |
| 87 }; |
| 77 | 88 |
| 78 // PrivetHttpServer::Delegate methods: | 89 // PrivetHttpServer::Delegate methods: |
| 79 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart( | 90 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart( |
| 80 const std::string& user) OVERRIDE; | 91 const std::string& user) OVERRIDE; |
| 81 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken( | 92 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken( |
| 82 const std::string& user, | 93 const std::string& user, |
| 83 std::string* token, | 94 std::string* token, |
| 84 std::string* claim_url) OVERRIDE; | 95 std::string* claim_url) OVERRIDE; |
| 85 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete( | 96 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete( |
| 86 const std::string& user, | 97 const std::string& user, |
| 87 std::string* device_id) OVERRIDE; | 98 std::string* device_id) OVERRIDE; |
| 88 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel( | 99 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel( |
| 89 const std::string& user) OVERRIDE; | 100 const std::string& user) OVERRIDE; |
| 90 virtual void GetRegistrationServerError(std::string* description) OVERRIDE; | 101 virtual void GetRegistrationServerError(std::string* description) OVERRIDE; |
| 91 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; | 102 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; |
| 92 virtual bool IsRegistered() const OVERRIDE; | 103 virtual bool IsRegistered() const OVERRIDE; |
| 93 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; | 104 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; |
| 94 | 105 |
| 95 // CloudRequester::Delegate methods: | 106 // CloudRequester::Delegate methods: |
| 96 virtual void OnRegistrationStartResponseParsed( | 107 virtual void OnRegistrationStartResponseParsed( |
| 97 const std::string& registration_token, | 108 const std::string& registration_token, |
| 98 const std::string& complete_invite_url, | 109 const std::string& complete_invite_url, |
| 99 const std::string& device_id) OVERRIDE; | 110 const std::string& device_id) OVERRIDE; |
| 100 virtual void OnGetAuthCodeResponseParsed( | 111 virtual void OnGetAuthCodeResponseParsed( |
| 101 const std::string& refresh_token) OVERRIDE; | 112 const std::string& refresh_token) OVERRIDE; |
| 102 virtual void OnRegistrationError(const std::string& description) OVERRIDE; | 113 virtual void OnRegistrationError(const std::string& description) OVERRIDE; |
| 114 virtual void OnServerError(const std::string& description) OVERRIDE; |
| 115 virtual void OnNetworkError() OVERRIDE; |
| 116 virtual void OnPrintjobsAvailable( |
| 117 const std::vector<Printjob>& printjobs) OVERRIDE; |
| 118 virtual void OnPrintjobDownloaded(const Printjob& printjob) OVERRIDE; |
| 119 virtual void OnPrintjobDone() OVERRIDE; |
| 103 | 120 |
| 104 // Checks if register call is called correctly (|user| is correct, | 121 // Checks if register call is called correctly (|user| is correct, |
| 105 // error is no set etc). Returns |false| if error status is put into |status|. | 122 // error is no set etc). Returns |false| if error status is put into |status|. |
| 106 // Otherwise no error was occurred. | 123 // Otherwise no error was occurred. |
| 107 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( | 124 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( |
| 108 const std::string& user) const; | 125 const std::string& user) const; |
| 109 | 126 |
| 127 // Checks if confirmation was received. |
| 128 void WaitUserConfirmation(base::Time valid_until); |
| 129 |
| 110 // Generates ProxyId for this device. | 130 // Generates ProxyId for this device. |
| 111 std::string GenerateProxyId() const; | 131 std::string GenerateProxyId() const; |
| 112 | 132 |
| 113 // Checks if confirmation was received. | |
| 114 void WaitUserConfirmation(base::Time valid_until); | |
| 115 | |
| 116 // Creates data for DNS TXT respond. | 133 // Creates data for DNS TXT respond. |
| 117 std::vector<std::string> CreateTxt() const; | 134 std::vector<std::string> CreateTxt() const; |
| 118 | 135 |
| 136 // Ask CloudPrint server for printjobs. |
| 137 void FetchPrintjobs(); |
| 138 |
| 119 // Saving and loading registration info from file. | 139 // Saving and loading registration info from file. |
| 120 void SaveToFile(const std::string& filename) const; | 140 void SaveToFile(const std::string& filename) const; |
| 121 bool LoadFromFile(const std::string& filename); | 141 bool LoadFromFile(const std::string& filename); |
| 122 | 142 |
| 143 // Adds |WakeUp| method to the MessageLoop. |
| 144 void PostWakeUp(); |
| 145 |
| 146 // Adds |WakeUp| method to the MessageLoop with certain |delay|. |
| 147 void PostDelayedWakeUp(const base::TimeDelta& delay); |
| 148 |
| 123 // Converts errors. | 149 // Converts errors. |
| 124 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( | 150 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( |
| 125 RegistrationInfo::ConfirmationState state); | 151 RegistrationInfo::ConfirmationState state); |
| 126 | 152 |
| 153 std::string ConnectionStateToString(ConnectionState state) const; |
| 154 |
| 155 // Changes state and update info in DNS server. |
| 156 bool ChangeState(ConnectionState new_state); |
| 157 |
| 127 RegistrationInfo reg_info_; | 158 RegistrationInfo reg_info_; |
| 128 | 159 |
| 129 // Contains DNS-SD server. | 160 // Contains DNS-SD server. |
| 130 DnsSdServer dns_server_; | 161 DnsSdServer dns_server_; |
| 131 | 162 |
| 132 // Contains Privet HTTP server. | 163 // Contains Privet HTTP server. |
| 133 PrivetHttpServer http_server_; | 164 PrivetHttpServer http_server_; |
| 134 | 165 |
| 135 // Contains Cloud Print client. | 166 // Connection state of device. |
| 167 ConnectionState connection_state_; |
| 168 |
| 169 // Contains CloudPrint client. |
| 136 scoped_ptr<CloudPrintRequester> requester_; | 170 scoped_ptr<CloudPrintRequester> requester_; |
| 137 | 171 |
| 138 XPrivetToken xtoken_; | 172 XPrivetToken xtoken_; |
| 139 | 173 |
| 174 scoped_ptr<PrintjobHandler> printjob_handler_; |
| 175 |
| 140 // Uses for calculating uptime. | 176 // Uses for calculating uptime. |
| 141 base::Time starttime_; | 177 base::Time starttime_; |
| 142 | 178 |
| 143 DISALLOW_COPY_AND_ASSIGN(Printer); | 179 DISALLOW_COPY_AND_ASSIGN(Printer); |
| 144 }; | 180 }; |
| 145 | 181 |
| 146 #endif // GCP20_PROTOTYPE_PRINTER_H_ | 182 #endif // GCP20_PROTOTYPE_PRINTER_H_ |
| 147 | 183 |
| OLD | NEW |