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 28 matching lines...) Expand all Loading... |
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 }; |
81 | 87 |
| 88 enum ConnectionState { |
| 89 NOT_CONFIGURED, |
| 90 OFFLINE, |
| 91 ONLINE, |
| 92 CONNECTING |
| 93 }; |
| 94 |
82 // PrivetHttpServer::Delegate methods: | 95 // PrivetHttpServer::Delegate methods: |
83 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart( | 96 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart( |
84 const std::string& user) OVERRIDE; | 97 const std::string& user) OVERRIDE; |
85 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken( | 98 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken( |
86 const std::string& user, | 99 const std::string& user, |
87 std::string* token, | 100 std::string* token, |
88 std::string* claim_url) OVERRIDE; | 101 std::string* claim_url) OVERRIDE; |
89 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete( | 102 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete( |
90 const std::string& user, | 103 const std::string& user, |
91 std::string* device_id) OVERRIDE; | 104 std::string* device_id) OVERRIDE; |
92 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel( | 105 virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel( |
93 const std::string& user) OVERRIDE; | 106 const std::string& user) OVERRIDE; |
94 virtual void GetRegistrationServerError(std::string* description) OVERRIDE; | 107 virtual void GetRegistrationServerError(std::string* description) OVERRIDE; |
95 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; | 108 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; |
96 virtual bool IsRegistered() const OVERRIDE; | 109 virtual bool IsRegistered() const OVERRIDE; |
97 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; | 110 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; |
98 | 111 |
99 // CloudRequester::Delegate methods: | 112 // CloudRequester::Delegate methods: |
100 virtual void OnRegistrationStartResponseParsed( | 113 virtual void OnRegistrationStartResponseParsed( |
101 const std::string& registration_token, | 114 const std::string& registration_token, |
102 const std::string& complete_invite_url, | 115 const std::string& complete_invite_url, |
103 const std::string& device_id) OVERRIDE; | 116 const std::string& device_id) OVERRIDE; |
104 virtual void OnGetAuthCodeResponseParsed( | 117 virtual void OnGetAuthCodeResponseParsed( |
105 const std::string& refresh_token) OVERRIDE; | 118 const std::string& refresh_token) OVERRIDE; |
106 virtual void OnRegistrationError(const std::string& description) OVERRIDE; | 119 virtual void OnRegistrationError(const std::string& description) OVERRIDE; |
| 120 virtual void OnServerError(const std::string& description) OVERRIDE; |
| 121 virtual void OnNetworkError() OVERRIDE; |
| 122 virtual void OnPrintJobsAvailable( |
| 123 const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE; |
| 124 virtual void OnPrintJobDownloaded( |
| 125 const cloud_print_response_parser::Job& job) OVERRIDE; |
| 126 virtual void OnPrintJobDone() OVERRIDE; |
107 | 127 |
108 // Checks if register call is called correctly (|user| is correct, | 128 // 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|. | 129 // error is no set etc). Returns |false| if error status is put into |status|. |
110 // Otherwise no error was occurred. | 130 // Otherwise no error was occurred. |
111 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( | 131 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( |
112 const std::string& user) const; | 132 const std::string& user) const; |
113 | 133 |
| 134 // Checks if confirmation was received. |
| 135 void WaitUserConfirmation(base::Time valid_until); |
| 136 |
114 // Generates ProxyId for this device. | 137 // Generates ProxyId for this device. |
115 std::string GenerateProxyId() const; | 138 std::string GenerateProxyId() const; |
116 | 139 |
117 // Checks if confirmation was received. | |
118 void WaitUserConfirmation(base::Time valid_until); | |
119 | |
120 // Creates data for DNS TXT respond. | 140 // Creates data for DNS TXT respond. |
121 std::vector<std::string> CreateTxt() const; | 141 std::vector<std::string> CreateTxt() const; |
122 | 142 |
| 143 // Ask CloudPrint server for printjobs. |
| 144 void FetchPrintJobs(); |
| 145 |
123 // Saving and loading registration info from file. | 146 // Saving and loading registration info from file. |
124 void SaveToFile(const std::string& filename) const; | 147 void SaveToFile(const std::string& filename) const; |
125 bool LoadFromFile(const std::string& filename); | 148 bool LoadFromFile(const std::string& filename); |
126 | 149 |
| 150 // Adds |WakeUp| method to the MessageLoop. |
| 151 void PostWakeUp(); |
| 152 |
| 153 // Adds |WakeUp| method to the MessageLoop with certain |delay|. |
| 154 void PostDelayedWakeUp(const base::TimeDelta& delay); |
| 155 |
127 // Converts errors. | 156 // Converts errors. |
128 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( | 157 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( |
129 RegistrationInfo::ConfirmationState state); | 158 RegistrationInfo::ConfirmationState state); |
130 | 159 |
| 160 std::string ConnectionStateToString(ConnectionState state) const; |
| 161 |
| 162 // Changes state and update info in DNS server. |
| 163 bool ChangeState(ConnectionState new_state); |
| 164 |
131 RegistrationInfo reg_info_; | 165 RegistrationInfo reg_info_; |
132 | 166 |
133 // Contains DNS-SD server. | 167 // Contains DNS-SD server. |
134 DnsSdServer dns_server_; | 168 DnsSdServer dns_server_; |
135 | 169 |
136 // Contains Privet HTTP server. | 170 // Contains Privet HTTP server. |
137 PrivetHttpServer http_server_; | 171 PrivetHttpServer http_server_; |
138 | 172 |
139 // Contains Cloud Print client. | 173 // Connection state of device. |
| 174 ConnectionState connection_state_; |
| 175 |
| 176 // Contains CloudPrint client. |
140 scoped_ptr<CloudPrintRequester> requester_; | 177 scoped_ptr<CloudPrintRequester> requester_; |
141 | 178 |
142 XPrivetToken xtoken_; | 179 XPrivetToken xtoken_; |
143 | 180 |
| 181 scoped_ptr<PrintJobHandler> print_job_handler_; |
| 182 |
144 // Uses for calculating uptime. | 183 // Uses for calculating uptime. |
145 base::Time starttime_; | 184 base::Time starttime_; |
146 | 185 |
147 DISALLOW_COPY_AND_ASSIGN(Printer); | 186 DISALLOW_COPY_AND_ASSIGN(Printer); |
148 }; | 187 }; |
149 | 188 |
150 #endif // GCP20_PROTOTYPE_PRINTER_H_ | 189 #endif // GCP20_PROTOTYPE_PRINTER_H_ |
151 | 190 |
OLD | NEW |