OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/test/chromedriver/server/http_handler.h" | 5 #include "chrome/test/chromedriver/server/http_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
13 #include "base/logging.h" // For CHECK macros. | 14 #include "base/logging.h" // For CHECK macros. |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 url_base_(url_base), | 79 url_base_(url_base), |
79 received_shutdown_(false), | 80 received_shutdown_(false), |
80 weak_ptr_factory_(this) { | 81 weak_ptr_factory_(this) { |
81 #if defined(OS_MACOSX) | 82 #if defined(OS_MACOSX) |
82 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 83 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
83 #endif | 84 #endif |
84 context_getter_ = new URLRequestContextGetter(io_task_runner); | 85 context_getter_ = new URLRequestContextGetter(io_task_runner); |
85 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); | 86 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); |
86 adb_.reset(new AdbImpl(io_task_runner, adb_port)); | 87 adb_.reset(new AdbImpl(io_task_runner, adb_port)); |
87 device_manager_.reset(new DeviceManager(adb_.get())); | 88 device_manager_.reset(new DeviceManager(adb_.get())); |
88 port_server_ = port_server.Pass(); | 89 port_server_ = std::move(port_server); |
89 port_manager_.reset(new PortManager(12000, 13000)); | 90 port_manager_.reset(new PortManager(12000, 13000)); |
90 | 91 |
91 CommandMapping commands[] = { | 92 CommandMapping commands[] = { |
92 CommandMapping( | 93 CommandMapping( |
93 kPost, | 94 kPost, |
94 internal::kNewSessionPathPattern, | 95 internal::kNewSessionPathPattern, |
95 base::Bind(&ExecuteCreateSession, | 96 base::Bind(&ExecuteCreateSession, |
96 &session_thread_map_, | 97 &session_thread_map_, |
97 WrapToCommand( | 98 WrapToCommand( |
98 "InitSession", | 99 "InitSession", |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 CHECK(thread_checker_.CalledOnValidThread()); | 584 CHECK(thread_checker_.CalledOnValidThread()); |
584 | 585 |
585 if (received_shutdown_) | 586 if (received_shutdown_) |
586 return; | 587 return; |
587 | 588 |
588 std::string path = request.path; | 589 std::string path = request.path; |
589 if (!base::StartsWith(path, url_base_, base::CompareCase::SENSITIVE)) { | 590 if (!base::StartsWith(path, url_base_, base::CompareCase::SENSITIVE)) { |
590 scoped_ptr<net::HttpServerResponseInfo> response( | 591 scoped_ptr<net::HttpServerResponseInfo> response( |
591 new net::HttpServerResponseInfo(net::HTTP_BAD_REQUEST)); | 592 new net::HttpServerResponseInfo(net::HTTP_BAD_REQUEST)); |
592 response->SetBody("unhandled request", "text/plain"); | 593 response->SetBody("unhandled request", "text/plain"); |
593 send_response_func.Run(response.Pass()); | 594 send_response_func.Run(std::move(response)); |
594 return; | 595 return; |
595 } | 596 } |
596 | 597 |
597 path.erase(0, url_base_.length()); | 598 path.erase(0, url_base_.length()); |
598 | 599 |
599 HandleCommand(request, path, send_response_func); | 600 HandleCommand(request, path, send_response_func); |
600 | 601 |
601 if (path == kShutdownPath) | 602 if (path == kShutdownPath) |
602 received_shutdown_ = true; | 603 received_shutdown_ = true; |
603 } | 604 } |
(...skipping 26 matching lines...) Expand all Loading... |
630 const std::string& trimmed_path, | 631 const std::string& trimmed_path, |
631 const HttpResponseSenderFunc& send_response_func) { | 632 const HttpResponseSenderFunc& send_response_func) { |
632 base::DictionaryValue params; | 633 base::DictionaryValue params; |
633 std::string session_id; | 634 std::string session_id; |
634 CommandMap::const_iterator iter = command_map_->begin(); | 635 CommandMap::const_iterator iter = command_map_->begin(); |
635 while (true) { | 636 while (true) { |
636 if (iter == command_map_->end()) { | 637 if (iter == command_map_->end()) { |
637 scoped_ptr<net::HttpServerResponseInfo> response( | 638 scoped_ptr<net::HttpServerResponseInfo> response( |
638 new net::HttpServerResponseInfo(net::HTTP_NOT_FOUND)); | 639 new net::HttpServerResponseInfo(net::HTTP_NOT_FOUND)); |
639 response->SetBody("unknown command: " + trimmed_path, "text/plain"); | 640 response->SetBody("unknown command: " + trimmed_path, "text/plain"); |
640 send_response_func.Run(response.Pass()); | 641 send_response_func.Run(std::move(response)); |
641 return; | 642 return; |
642 } | 643 } |
643 if (internal::MatchesCommand( | 644 if (internal::MatchesCommand( |
644 request.method, trimmed_path, *iter, &session_id, ¶ms)) { | 645 request.method, trimmed_path, *iter, &session_id, ¶ms)) { |
645 break; | 646 break; |
646 } | 647 } |
647 ++iter; | 648 ++iter; |
648 } | 649 } |
649 | 650 |
650 if (request.data.length()) { | 651 if (request.data.length()) { |
651 base::DictionaryValue* body_params; | 652 base::DictionaryValue* body_params; |
652 scoped_ptr<base::Value> parsed_body = base::JSONReader::Read(request.data); | 653 scoped_ptr<base::Value> parsed_body = base::JSONReader::Read(request.data); |
653 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) { | 654 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) { |
654 scoped_ptr<net::HttpServerResponseInfo> response( | 655 scoped_ptr<net::HttpServerResponseInfo> response( |
655 new net::HttpServerResponseInfo(net::HTTP_BAD_REQUEST)); | 656 new net::HttpServerResponseInfo(net::HTTP_BAD_REQUEST)); |
656 response->SetBody("missing command parameters", "text/plain"); | 657 response->SetBody("missing command parameters", "text/plain"); |
657 send_response_func.Run(response.Pass()); | 658 send_response_func.Run(std::move(response)); |
658 return; | 659 return; |
659 } | 660 } |
660 params.MergeDictionary(body_params); | 661 params.MergeDictionary(body_params); |
661 } | 662 } |
662 | 663 |
663 iter->command.Run(params, | 664 iter->command.Run(params, |
664 session_id, | 665 session_id, |
665 base::Bind(&HttpHandler::PrepareResponse, | 666 base::Bind(&HttpHandler::PrepareResponse, |
666 weak_ptr_factory_.GetWeakPtr(), | 667 weak_ptr_factory_.GetWeakPtr(), |
667 trimmed_path, | 668 trimmed_path, |
668 send_response_func)); | 669 send_response_func)); |
669 } | 670 } |
670 | 671 |
671 void HttpHandler::PrepareResponse( | 672 void HttpHandler::PrepareResponse( |
672 const std::string& trimmed_path, | 673 const std::string& trimmed_path, |
673 const HttpResponseSenderFunc& send_response_func, | 674 const HttpResponseSenderFunc& send_response_func, |
674 const Status& status, | 675 const Status& status, |
675 scoped_ptr<base::Value> value, | 676 scoped_ptr<base::Value> value, |
676 const std::string& session_id) { | 677 const std::string& session_id) { |
677 CHECK(thread_checker_.CalledOnValidThread()); | 678 CHECK(thread_checker_.CalledOnValidThread()); |
678 scoped_ptr<net::HttpServerResponseInfo> response = | 679 scoped_ptr<net::HttpServerResponseInfo> response = |
679 PrepareResponseHelper(trimmed_path, status, value.Pass(), session_id); | 680 PrepareResponseHelper(trimmed_path, status, std::move(value), session_id); |
680 send_response_func.Run(response.Pass()); | 681 send_response_func.Run(std::move(response)); |
681 if (trimmed_path == kShutdownPath) | 682 if (trimmed_path == kShutdownPath) |
682 quit_func_.Run(); | 683 quit_func_.Run(); |
683 } | 684 } |
684 | 685 |
685 scoped_ptr<net::HttpServerResponseInfo> HttpHandler::PrepareResponseHelper( | 686 scoped_ptr<net::HttpServerResponseInfo> HttpHandler::PrepareResponseHelper( |
686 const std::string& trimmed_path, | 687 const std::string& trimmed_path, |
687 const Status& status, | 688 const Status& status, |
688 scoped_ptr<base::Value> value, | 689 scoped_ptr<base::Value> value, |
689 const std::string& session_id) { | 690 const std::string& session_id) { |
690 if (status.code() == kUnknownCommand) { | 691 if (status.code() == kUnknownCommand) { |
691 scoped_ptr<net::HttpServerResponseInfo> response( | 692 scoped_ptr<net::HttpServerResponseInfo> response( |
692 new net::HttpServerResponseInfo(net::HTTP_NOT_IMPLEMENTED)); | 693 new net::HttpServerResponseInfo(net::HTTP_NOT_IMPLEMENTED)); |
693 response->SetBody("unimplemented command: " + trimmed_path, "text/plain"); | 694 response->SetBody("unimplemented command: " + trimmed_path, "text/plain"); |
694 return response.Pass(); | 695 return response; |
695 } | 696 } |
696 | 697 |
697 if (status.IsError()) { | 698 if (status.IsError()) { |
698 Status full_status(status); | 699 Status full_status(status); |
699 full_status.AddDetails(base::StringPrintf( | 700 full_status.AddDetails(base::StringPrintf( |
700 "Driver info: chromedriver=%s,platform=%s %s %s", | 701 "Driver info: chromedriver=%s,platform=%s %s %s", |
701 kChromeDriverVersion, | 702 kChromeDriverVersion, |
702 base::SysInfo::OperatingSystemName().c_str(), | 703 base::SysInfo::OperatingSystemName().c_str(), |
703 base::SysInfo::OperatingSystemVersion().c_str(), | 704 base::SysInfo::OperatingSystemVersion().c_str(), |
704 base::SysInfo::OperatingSystemArchitecture().c_str())); | 705 base::SysInfo::OperatingSystemArchitecture().c_str())); |
705 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); | 706 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); |
706 error->SetString("message", full_status.message()); | 707 error->SetString("message", full_status.message()); |
707 value.reset(error.release()); | 708 value.reset(error.release()); |
708 } | 709 } |
709 if (!value) | 710 if (!value) |
710 value = base::Value::CreateNullValue(); | 711 value = base::Value::CreateNullValue(); |
711 | 712 |
712 base::DictionaryValue body_params; | 713 base::DictionaryValue body_params; |
713 body_params.SetInteger("status", status.code()); | 714 body_params.SetInteger("status", status.code()); |
714 body_params.Set("value", value.release()); | 715 body_params.Set("value", value.release()); |
715 body_params.SetString("sessionId", session_id); | 716 body_params.SetString("sessionId", session_id); |
716 std::string body; | 717 std::string body; |
717 base::JSONWriter::WriteWithOptions( | 718 base::JSONWriter::WriteWithOptions( |
718 body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 719 body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
719 &body); | 720 &body); |
720 scoped_ptr<net::HttpServerResponseInfo> response( | 721 scoped_ptr<net::HttpServerResponseInfo> response( |
721 new net::HttpServerResponseInfo(net::HTTP_OK)); | 722 new net::HttpServerResponseInfo(net::HTTP_OK)); |
722 response->SetBody(body, "application/json; charset=utf-8"); | 723 response->SetBody(body, "application/json; charset=utf-8"); |
723 return response.Pass(); | 724 return response; |
724 } | 725 } |
725 | 726 |
726 namespace internal { | 727 namespace internal { |
727 | 728 |
728 const char kNewSessionPathPattern[] = "session"; | 729 const char kNewSessionPathPattern[] = "session"; |
729 | 730 |
730 bool MatchesMethod(HttpMethod command_method, const std::string& method) { | 731 bool MatchesMethod(HttpMethod command_method, const std::string& method) { |
731 std::string lower_method = base::ToLowerASCII(method); | 732 std::string lower_method = base::ToLowerASCII(method); |
732 switch (command_method) { | 733 switch (command_method) { |
733 case kGet: | 734 case kGet: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 params.SetString(name, decoded); | 778 params.SetString(name, decoded); |
778 } else if (command_path_parts[i] != path_parts[i]) { | 779 } else if (command_path_parts[i] != path_parts[i]) { |
779 return false; | 780 return false; |
780 } | 781 } |
781 } | 782 } |
782 out_params->MergeDictionary(¶ms); | 783 out_params->MergeDictionary(¶ms); |
783 return true; | 784 return true; |
784 } | 785 } |
785 | 786 |
786 } // namespace internal | 787 } // namespace internal |
OLD | NEW |