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.
5 #ifndef CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_
6 #define CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_
11 #include "base/basictypes.h"
12 #include "base/values.h"
13 #include "cloud_print/gcp20/prototype/local_print_job.h"
14 #include "net/server/http_server.h"
15 #include "net/server/http_server_request_info.h"
19 // HTTP server for receiving .
20 class PrivetHttpServer: public net::HttpServer::Delegate {
22 // TODO(maksymb): Move this enum to some namespace instead of this class.
23 enum RegistrationErrorStatus {
26 REG_ERROR_INVALID_PARAMS,
27 REG_ERROR_DEVICE_BUSY,
28 REG_ERROR_PENDING_USER_ACTION,
29 REG_ERROR_USER_CANCEL,
30 REG_ERROR_CONFIRMATION_TIMEOUT,
31 REG_ERROR_INVALID_ACTION,
33 REG_ERROR_SERVER_ERROR
36 // TODO(maksymb): Move this struct to some namespace instead of this class.
43 std::string description;
46 std::string device_state;
47 std::string connection_state;
48 std::string manufacturer;
50 std::string serial_number;
53 std::string x_privet_token;
55 std::vector<std::string> api;
56 std::vector<std::string> type;
61 virtual ~Delegate() {}
63 // Invoked when registration is starting.
64 virtual RegistrationErrorStatus RegistrationStart(
65 const std::string& user) = 0;
67 // Invoked when claimtoken is needed.
68 virtual RegistrationErrorStatus RegistrationGetClaimToken(
69 const std::string& user,
71 std::string* claim_url) = 0;
73 // Invoked when registration is going to be completed.
74 virtual RegistrationErrorStatus RegistrationComplete(
75 const std::string& user,
76 std::string* device_id) = 0;
78 // Invoked when client asked for cancelling the registration.
79 virtual RegistrationErrorStatus RegistrationCancel(
80 const std::string& user) = 0;
82 // Invoked for receiving server error details.
83 virtual void GetRegistrationServerError(std::string* description) = 0;
85 // Invoked when /privet/info is called.
86 virtual void CreateInfo(DeviceInfo* info) = 0;
88 // Invoked for checking wether /privet/register should be exposed.
89 virtual bool IsRegistered() const = 0;
91 // Invoked for checking wether /privet/printer/* should be exposed.
92 virtual bool IsLocalPrintingAllowed() const = 0;
94 // Invoked when XPrivetToken has to be checked.
95 virtual bool CheckXPrivetTokenHeader(const std::string& token) const = 0;
97 // Invoked for getting capabilities.
98 virtual const base::DictionaryValue& GetCapabilities() = 0;
100 // Invoked for creating a job.
101 virtual LocalPrintJob::CreateResult CreateJob(
102 const std::string& ticket,
105 // TODO(maksymb): Use base::TimeDelta for timeouts
107 std::string* error_description) = 0;
109 // Invoked for simple local printing.
110 virtual LocalPrintJob::SaveResult SubmitDoc(
111 const LocalPrintJob& job,
114 std::string* error_description,
117 // Invoked for advanced local printing.
118 virtual LocalPrintJob::SaveResult SubmitDocWithId(
119 const LocalPrintJob& job,
120 const std::string& job_id,
122 std::string* error_description,
125 // Invoked for getting job status.
126 virtual bool GetJobState(const std::string& job_id,
127 LocalPrintJob::Info* info) = 0;
130 // Constructor doesn't start server.
131 explicit PrivetHttpServer(Delegate* delegate);
133 // Destroys the object.
134 virtual ~PrivetHttpServer();
136 // Starts HTTP server: start listening port |port| for HTTP requests.
137 bool Start(uint16 port);
139 // Stops HTTP server.
143 // net::HttpServer::Delegate methods:
144 virtual void OnConnect(int connection_id) OVERRIDE {}
145 virtual void OnHttpRequest(
147 const net::HttpServerRequestInfo& info) OVERRIDE;
148 virtual void OnWebSocketRequest(
150 const net::HttpServerRequestInfo& info) OVERRIDE;
151 virtual void OnWebSocketMessage(int connection_id,
152 const std::string& data) OVERRIDE;
153 virtual void OnClose(int connection_id) OVERRIDE;
155 // Sends error as response. Invoked when request method is invalid.
156 void ReportInvalidMethod(int connection_id);
158 // Returns |true| if |request| should be done with correct |method|.
159 // Otherwise sends |Invalid method| error.
160 // Also checks support of |request| by this server.
161 bool ValidateRequestMethod(int connection_id,
162 const std::string& request,
163 const std::string& method);
165 // Processes http request after all preparations (XPrivetHeader check,
166 // data handling etc.)
167 net::HttpStatusCode ProcessHttpRequest(
169 const net::HttpServerRequestInfo& info,
170 std::string* response);
172 // Pivet API methods. Return reference to NULL if output should be empty.
173 scoped_ptr<base::DictionaryValue> ProcessInfo(
174 net::HttpStatusCode* status_code) const;
176 scoped_ptr<base::DictionaryValue> ProcessCapabilities(
177 net::HttpStatusCode* status_code) const;
179 scoped_ptr<base::DictionaryValue> ProcessCreateJob(
181 const std::string& body,
182 net::HttpStatusCode* status_code) const;
184 scoped_ptr<base::DictionaryValue> ProcessSubmitDoc(
186 const net::HttpServerRequestInfo& info,
187 net::HttpStatusCode* status_code) const;
189 scoped_ptr<base::DictionaryValue> ProcessJobState(
191 net::HttpStatusCode* status_code) const;
193 scoped_ptr<base::DictionaryValue> ProcessRegister(
195 net::HttpStatusCode* status_code) const;
197 // Processes current status and depending on it replaces (or not)
198 // |current_response| with error or empty response.
199 void ProcessRegistrationStatus(
200 RegistrationErrorStatus status,
201 scoped_ptr<base::DictionaryValue>* current_response) const;
203 // Port for listening.
207 // Contains encapsulated object for listening for requests.
208 scoped_ptr<net::HttpServer> server_;
212 DISALLOW_COPY_AND_ASSIGN(PrivetHttpServer);
215 #endif // CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_