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