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 #include "cloud_print/gcp20/prototype/privet_http_server.h" | 5 #include "cloud_print/gcp20/prototype/privet_http_server.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "cloud_print/gcp20/prototype/gcp20_switches.h" | 14 #include "cloud_print/gcp20/prototype/gcp20_switches.h" |
14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
16 #include "net/base/url_util.h" | 17 #include "net/base/url_util.h" |
17 #include "net/socket/tcp_server_socket.h" | 18 #include "net/socket/tcp_server_socket.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const int kDeviceBusyTimeout = 30; // in seconds | 23 const int kDeviceBusyTimeout = 30; // in seconds |
23 const int kPendingUserActionTimeout = 5; // in seconds | 24 const int kPendingUserActionTimeout = 5; // in seconds |
24 | 25 |
25 const char kPrivetInfo[] = "/privet/info"; | 26 const char kPrivetInfo[] = "/privet/info"; |
26 const char kPrivetRegister[] = "/privet/register"; | 27 const char kPrivetRegister[] = "/privet/register"; |
27 const char kPrivetCapabilities[] = "/privet/capabilities"; | 28 const char kPrivetCapabilities[] = "/privet/capabilities"; |
28 const char kPrivetPrinterCreateJob[] = "/privet/printer/createjob"; | 29 const char kPrivetPrinterCreateJob[] = "/privet/printer/createjob"; |
29 const char kPrivetPrinterSubmitDoc[] = "/privet/printer/submitdoc"; | 30 const char kPrivetPrinterSubmitDoc[] = "/privet/printer/submitdoc"; |
30 const char kPrivetPrinterJobState[] = "/privet/printer/jobstate"; | 31 const char kPrivetPrinterJobState[] = "/privet/printer/jobstate"; |
31 | 32 |
32 // {"error":|error_type|} | 33 // {"error":|error_type|} |
33 scoped_ptr<base::DictionaryValue> CreateError(const std::string& error_type) { | 34 scoped_ptr<base::DictionaryValue> CreateError(const std::string& error_type) { |
34 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue); | 35 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue); |
35 error->SetString("error", error_type); | 36 error->SetString("error", error_type); |
36 | 37 |
37 return error.Pass(); | 38 return std::move(error); |
38 } | 39 } |
39 | 40 |
40 // {"error":|error_type|, "description":|description|} | 41 // {"error":|error_type|, "description":|description|} |
41 scoped_ptr<base::DictionaryValue> CreateErrorWithDescription( | 42 scoped_ptr<base::DictionaryValue> CreateErrorWithDescription( |
42 const std::string& error_type, | 43 const std::string& error_type, |
43 const std::string& description) { | 44 const std::string& description) { |
44 scoped_ptr<base::DictionaryValue> error(CreateError(error_type)); | 45 scoped_ptr<base::DictionaryValue> error(CreateError(error_type)); |
45 error->SetString("description", description); | 46 error->SetString("description", description); |
46 return error.Pass(); | 47 return std::move(error); |
47 } | 48 } |
48 | 49 |
49 // {"error":|error_type|, "timeout":|timeout|} | 50 // {"error":|error_type|, "timeout":|timeout|} |
50 scoped_ptr<base::DictionaryValue> CreateErrorWithTimeout( | 51 scoped_ptr<base::DictionaryValue> CreateErrorWithTimeout( |
51 const std::string& error_type, | 52 const std::string& error_type, |
52 int timeout) { | 53 int timeout) { |
53 scoped_ptr<base::DictionaryValue> error(CreateError(error_type)); | 54 scoped_ptr<base::DictionaryValue> error(CreateError(error_type)); |
54 error->SetInteger("timeout", timeout); | 55 error->SetInteger("timeout", timeout); |
55 return error.Pass(); | 56 return std::move(error); |
56 } | 57 } |
57 | 58 |
58 // Converts state to string. | 59 // Converts state to string. |
59 std::string LocalPrintJobStateToString(LocalPrintJob::State state) { | 60 std::string LocalPrintJobStateToString(LocalPrintJob::State state) { |
60 switch (state) { | 61 switch (state) { |
61 case LocalPrintJob::STATE_DRAFT: | 62 case LocalPrintJob::STATE_DRAFT: |
62 return "draft"; | 63 return "draft"; |
63 break; | 64 break; |
64 case LocalPrintJob::STATE_ABORTED: | 65 case LocalPrintJob::STATE_ABORTED: |
65 return "done"; | 66 return "done"; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 bool PrivetHttpServer::Start(uint16_t port) { | 109 bool PrivetHttpServer::Start(uint16_t port) { |
109 if (server_) | 110 if (server_) |
110 return true; | 111 return true; |
111 | 112 |
112 scoped_ptr<net::ServerSocket> server_socket( | 113 scoped_ptr<net::ServerSocket> server_socket( |
113 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 114 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
114 std::string listen_address = "::"; | 115 std::string listen_address = "::"; |
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableIpv6)) | 116 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableIpv6)) |
116 listen_address = "0.0.0.0"; | 117 listen_address = "0.0.0.0"; |
117 server_socket->ListenWithAddressAndPort(listen_address, port, 1); | 118 server_socket->ListenWithAddressAndPort(listen_address, port, 1); |
118 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 119 server_.reset(new net::HttpServer(std::move(server_socket), this)); |
119 | 120 |
120 net::IPEndPoint address; | 121 net::IPEndPoint address; |
121 if (server_->GetLocalAddress(&address) != net::OK) { | 122 if (server_->GetLocalAddress(&address) != net::OK) { |
122 NOTREACHED() << "Cannot start HTTP server"; | 123 NOTREACHED() << "Cannot start HTTP server"; |
123 return false; | 124 return false; |
124 } | 125 } |
125 | 126 |
126 VLOG(1) << "Address of HTTP server: " << address.ToString(); | 127 VLOG(1) << "Address of HTTP server: " << address.ToString(); |
127 return true; | 128 return true; |
128 } | 129 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 for (size_t i = 0; i < info.api.size(); ++i) | 271 for (size_t i = 0; i < info.api.size(); ++i) |
271 api.AppendString(info.api[i]); | 272 api.AppendString(info.api[i]); |
272 response->Set("api", api.DeepCopy()); | 273 response->Set("api", api.DeepCopy()); |
273 | 274 |
274 base::ListValue type; | 275 base::ListValue type; |
275 for (size_t i = 0; i < info.type.size(); ++i) | 276 for (size_t i = 0; i < info.type.size(); ++i) |
276 type.AppendString(info.type[i]); | 277 type.AppendString(info.type[i]); |
277 response->Set("type", type.DeepCopy()); | 278 response->Set("type", type.DeepCopy()); |
278 | 279 |
279 *status_code = net::HTTP_OK; | 280 *status_code = net::HTTP_OK; |
280 return response.Pass(); | 281 return std::move(response); |
281 } | 282 } |
282 | 283 |
283 scoped_ptr<base::DictionaryValue> PrivetHttpServer::ProcessCapabilities( | 284 scoped_ptr<base::DictionaryValue> PrivetHttpServer::ProcessCapabilities( |
284 net::HttpStatusCode* status_code) const { | 285 net::HttpStatusCode* status_code) const { |
285 if (!delegate_->IsRegistered()) { | 286 if (!delegate_->IsRegistered()) { |
286 *status_code = net::HTTP_NOT_FOUND; | 287 *status_code = net::HTTP_NOT_FOUND; |
287 return scoped_ptr<base::DictionaryValue>(); | 288 return scoped_ptr<base::DictionaryValue>(); |
288 } | 289 } |
289 return make_scoped_ptr(delegate_->GetCapabilities().DeepCopy()); | 290 return make_scoped_ptr(delegate_->GetCapabilities().DeepCopy()); |
290 } | 291 } |
(...skipping 18 matching lines...) Expand all Loading... |
309 delegate_->CreateJob(body, &job_id, &expires_in, | 310 delegate_->CreateJob(body, &job_id, &expires_in, |
310 &error_timeout, &error_description); | 311 &error_timeout, &error_description); |
311 | 312 |
312 scoped_ptr<base::DictionaryValue> response; | 313 scoped_ptr<base::DictionaryValue> response; |
313 *status_code = net::HTTP_OK; | 314 *status_code = net::HTTP_OK; |
314 switch (result) { | 315 switch (result) { |
315 case LocalPrintJob::CREATE_SUCCESS: | 316 case LocalPrintJob::CREATE_SUCCESS: |
316 response.reset(new base::DictionaryValue); | 317 response.reset(new base::DictionaryValue); |
317 response->SetString("job_id", job_id); | 318 response->SetString("job_id", job_id); |
318 response->SetInteger("expires_in", expires_in); | 319 response->SetInteger("expires_in", expires_in); |
319 return response.Pass(); | 320 return std::move(response); |
320 | 321 |
321 case LocalPrintJob::CREATE_INVALID_TICKET: | 322 case LocalPrintJob::CREATE_INVALID_TICKET: |
322 return CreateError("invalid_ticket"); | 323 return CreateError("invalid_ticket"); |
323 case LocalPrintJob::CREATE_PRINTER_BUSY: | 324 case LocalPrintJob::CREATE_PRINTER_BUSY: |
324 return CreateErrorWithTimeout("printer_busy", error_timeout); | 325 return CreateErrorWithTimeout("printer_busy", error_timeout); |
325 case LocalPrintJob::CREATE_PRINTER_ERROR: | 326 case LocalPrintJob::CREATE_PRINTER_ERROR: |
326 return CreateErrorWithDescription("printer_error", error_description); | 327 return CreateErrorWithDescription("printer_error", error_description); |
327 } | 328 } |
328 return scoped_ptr<base::DictionaryValue>(); | 329 return scoped_ptr<base::DictionaryValue>(); |
329 } | 330 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 switch (status) { | 370 switch (status) { |
370 case LocalPrintJob::SAVE_SUCCESS: | 371 case LocalPrintJob::SAVE_SUCCESS: |
371 response->SetString("job_id", job_id); | 372 response->SetString("job_id", job_id); |
372 response->SetInteger("expires_in", expires_in); | 373 response->SetInteger("expires_in", expires_in); |
373 response->SetString("job_type", job.content_type); | 374 response->SetString("job_type", job.content_type); |
374 response->SetString( | 375 response->SetString( |
375 "job_size", | 376 "job_size", |
376 base::StringPrintf("%u", static_cast<uint32_t>(job.content.size()))); | 377 base::StringPrintf("%u", static_cast<uint32_t>(job.content.size()))); |
377 if (job_name_present) | 378 if (job_name_present) |
378 response->SetString("job_name", job.job_name); | 379 response->SetString("job_name", job.job_name); |
379 return response.Pass(); | 380 return std::move(response); |
380 | 381 |
381 case LocalPrintJob::SAVE_INVALID_PRINT_JOB: | 382 case LocalPrintJob::SAVE_INVALID_PRINT_JOB: |
382 return CreateErrorWithTimeout("invalid_print_job", timeout); | 383 return CreateErrorWithTimeout("invalid_print_job", timeout); |
383 case LocalPrintJob::SAVE_INVALID_DOCUMENT_TYPE: | 384 case LocalPrintJob::SAVE_INVALID_DOCUMENT_TYPE: |
384 return CreateError("invalid_document_type"); | 385 return CreateError("invalid_document_type"); |
385 case LocalPrintJob::SAVE_INVALID_DOCUMENT: | 386 case LocalPrintJob::SAVE_INVALID_DOCUMENT: |
386 return CreateError("invalid_document"); | 387 return CreateError("invalid_document"); |
387 case LocalPrintJob::SAVE_DOCUMENT_TOO_LARGE: | 388 case LocalPrintJob::SAVE_DOCUMENT_TOO_LARGE: |
388 return CreateError("document_too_large"); | 389 return CreateError("document_too_large"); |
389 case LocalPrintJob::SAVE_PRINTER_BUSY: | 390 case LocalPrintJob::SAVE_PRINTER_BUSY: |
(...skipping 17 matching lines...) Expand all Loading... |
407 std::string job_id; | 408 std::string job_id; |
408 net::GetValueForKeyInQuery(url, "job_id", &job_id); | 409 net::GetValueForKeyInQuery(url, "job_id", &job_id); |
409 LocalPrintJob::Info info; | 410 LocalPrintJob::Info info; |
410 if (!delegate_->GetJobState(job_id, &info)) | 411 if (!delegate_->GetJobState(job_id, &info)) |
411 return CreateError("invalid_print_job"); | 412 return CreateError("invalid_print_job"); |
412 | 413 |
413 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue); | 414 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue); |
414 response->SetString("job_id", job_id); | 415 response->SetString("job_id", job_id); |
415 response->SetString("state", LocalPrintJobStateToString(info.state)); | 416 response->SetString("state", LocalPrintJobStateToString(info.state)); |
416 response->SetInteger("expires_in", info.expires_in); | 417 response->SetInteger("expires_in", info.expires_in); |
417 return response.Pass(); | 418 return std::move(response); |
418 } | 419 } |
419 | 420 |
420 scoped_ptr<base::DictionaryValue> PrivetHttpServer::ProcessRegister( | 421 scoped_ptr<base::DictionaryValue> PrivetHttpServer::ProcessRegister( |
421 const GURL& url, | 422 const GURL& url, |
422 net::HttpStatusCode* status_code) const { | 423 net::HttpStatusCode* status_code) const { |
423 if (delegate_->IsRegistered()) { | 424 if (delegate_->IsRegistered()) { |
424 *status_code = net::HTTP_NOT_FOUND; | 425 *status_code = net::HTTP_NOT_FOUND; |
425 return scoped_ptr<base::DictionaryValue>(); | 426 return scoped_ptr<base::DictionaryValue>(); |
426 } | 427 } |
427 | 428 |
(...skipping 30 matching lines...) Expand all Loading... |
458 | 459 |
459 if (action == "cancel") | 460 if (action == "cancel") |
460 status = delegate_->RegistrationCancel(user); | 461 status = delegate_->RegistrationCancel(user); |
461 } | 462 } |
462 | 463 |
463 if (status != REG_ERROR_OK) | 464 if (status != REG_ERROR_OK) |
464 response.reset(); | 465 response.reset(); |
465 | 466 |
466 ProcessRegistrationStatus(status, &response); | 467 ProcessRegistrationStatus(status, &response); |
467 *status_code = net::HTTP_OK; | 468 *status_code = net::HTTP_OK; |
468 return response.Pass(); | 469 return std::move(response); |
469 } | 470 } |
470 | 471 |
471 void PrivetHttpServer::ProcessRegistrationStatus( | 472 void PrivetHttpServer::ProcessRegistrationStatus( |
472 RegistrationErrorStatus status, | 473 RegistrationErrorStatus status, |
473 scoped_ptr<base::DictionaryValue>* current_response) const { | 474 scoped_ptr<base::DictionaryValue>* current_response) const { |
474 switch (status) { | 475 switch (status) { |
475 case REG_ERROR_OK: | 476 case REG_ERROR_OK: |
476 DCHECK(*current_response) << "Response shouldn't be empty."; | 477 DCHECK(*current_response) << "Response shouldn't be empty."; |
477 break; | 478 break; |
478 | 479 |
(...skipping 26 matching lines...) Expand all Loading... |
505 delegate_->GetRegistrationServerError(&description); | 506 delegate_->GetRegistrationServerError(&description); |
506 *current_response = CreateErrorWithDescription("server_error", | 507 *current_response = CreateErrorWithDescription("server_error", |
507 description); | 508 description); |
508 break; | 509 break; |
509 } | 510 } |
510 | 511 |
511 default: | 512 default: |
512 NOTREACHED(); | 513 NOTREACHED(); |
513 }; | 514 }; |
514 } | 515 } |
OLD | NEW |