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