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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 } // namespace | 50 } // namespace |
51 | 51 |
52 CommandMapping::CommandMapping(HttpMethod method, | 52 CommandMapping::CommandMapping(HttpMethod method, |
53 const std::string& path_pattern, | 53 const std::string& path_pattern, |
54 const Command& command) | 54 const Command& command) |
55 : method(method), path_pattern(path_pattern), command(command) {} | 55 : method(method), path_pattern(path_pattern), command(command) {} |
56 | 56 |
57 CommandMapping::~CommandMapping() {} | 57 CommandMapping::~CommandMapping() {} |
58 | 58 |
59 HttpHandler::HttpHandler(Log* log, const std::string& url_base) | 59 HttpHandler::HttpHandler(const std::string& url_base) |
60 : log_(log), | 60 : url_base_(url_base), |
61 url_base_(url_base), | |
62 received_shutdown_(false), | 61 received_shutdown_(false), |
63 command_map_(new CommandMap()), | 62 command_map_(new CommandMap()), |
64 weak_ptr_factory_(this) {} | 63 weak_ptr_factory_(this) {} |
65 | 64 |
66 HttpHandler::HttpHandler( | 65 HttpHandler::HttpHandler( |
67 const base::Closure& quit_func, | 66 const base::Closure& quit_func, |
68 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 67 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
69 Log* log, | |
70 const std::string& url_base, | 68 const std::string& url_base, |
71 int adb_port) | 69 int adb_port) |
72 : quit_func_(quit_func), | 70 : quit_func_(quit_func), |
73 log_(log), | |
74 url_base_(url_base), | 71 url_base_(url_base), |
75 received_shutdown_(false), | 72 received_shutdown_(false), |
76 weak_ptr_factory_(this) { | 73 weak_ptr_factory_(this) { |
77 #if defined(OS_MACOSX) | 74 #if defined(OS_MACOSX) |
78 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 75 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
79 #endif | 76 #endif |
80 context_getter_ = new URLRequestContextGetter(io_task_runner); | 77 context_getter_ = new URLRequestContextGetter(io_task_runner); |
81 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); | 78 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); |
82 adb_.reset(new AdbImpl(io_task_runner, log_, adb_port)); | 79 adb_.reset(new AdbImpl(io_task_runner, adb_port)); |
83 device_manager_.reset(new DeviceManager(adb_.get())); | 80 device_manager_.reset(new DeviceManager(adb_.get())); |
84 | 81 |
85 CommandMapping commands[] = { | 82 CommandMapping commands[] = { |
86 CommandMapping(kPost, | 83 CommandMapping(kPost, |
87 internal::kNewSessionPathPattern, | 84 internal::kNewSessionPathPattern, |
88 base::Bind(&ExecuteNewSession, | 85 base::Bind(&ExecuteNewSession, |
89 NewSessionParams(log_, | 86 NewSessionParams(&session_thread_map_, |
90 &session_thread_map_, | |
91 context_getter_, | 87 context_getter_, |
92 socket_factory_, | 88 socket_factory_, |
93 device_manager_.get()))), | 89 device_manager_.get()))), |
94 CommandMapping(kGet, | 90 CommandMapping(kGet, |
95 "session/:sessionId", | 91 "session/:sessionId", |
96 WrapToCommand(base::Bind(&ExecuteGetSessionCapabilities))), | 92 WrapToCommand(base::Bind(&ExecuteGetSessionCapabilities))), |
97 CommandMapping(kDelete, | 93 CommandMapping(kDelete, |
98 "session/:sessionId", | 94 "session/:sessionId", |
99 base::Bind(&ExecuteSessionCommand, | 95 base::Bind(&ExecuteSessionCommand, |
100 &session_thread_map_, | 96 &session_thread_map_, |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 Command HttpHandler::WrapToCommand( | 480 Command HttpHandler::WrapToCommand( |
485 const ElementCommand& element_command) { | 481 const ElementCommand& element_command) { |
486 return WrapToCommand( | 482 return WrapToCommand( |
487 base::Bind(&ExecuteElementCommand, element_command)); | 483 base::Bind(&ExecuteElementCommand, element_command)); |
488 } | 484 } |
489 | 485 |
490 void HttpHandler::HandleCommand( | 486 void HttpHandler::HandleCommand( |
491 const net::HttpServerRequestInfo& request, | 487 const net::HttpServerRequestInfo& request, |
492 const std::string& trimmed_path, | 488 const std::string& trimmed_path, |
493 const HttpResponseSenderFunc& send_response_func) { | 489 const HttpResponseSenderFunc& send_response_func) { |
494 log_->AddEntry(Log::kLog, | 490 if (IsVLogOn(0)) { |
495 base::StringPrintf("handling command: %s %s %s", | 491 VLOG(0) << "Handling command: " << request.method << " " << trimmed_path |
496 request.method.c_str(), | 492 << " " << FormatJsonForDisplay(request.data); |
497 trimmed_path.c_str(), | 493 } |
498 request.data.c_str())); | |
499 | 494 |
500 base::DictionaryValue params; | 495 base::DictionaryValue params; |
501 std::string session_id; | 496 std::string session_id; |
502 CommandMap::const_iterator iter = command_map_->begin(); | 497 CommandMap::const_iterator iter = command_map_->begin(); |
503 while (true) { | 498 while (true) { |
504 if (iter == command_map_->end()) { | 499 if (iter == command_map_->end()) { |
505 scoped_ptr<net::HttpServerResponseInfo> response( | 500 scoped_ptr<net::HttpServerResponseInfo> response( |
506 new net::HttpServerResponseInfo(net::HTTP_NOT_FOUND)); | 501 new net::HttpServerResponseInfo(net::HTTP_NOT_FOUND)); |
507 response->SetBody("unknown command: " + trimmed_path, "text/plain"); | 502 response->SetBody("unknown command: " + trimmed_path, "text/plain"); |
508 send_response_func.Run(response.Pass()); | 503 send_response_func.Run(response.Pass()); |
(...skipping 29 matching lines...) Expand all Loading... |
538 | 533 |
539 void HttpHandler::PrepareResponse( | 534 void HttpHandler::PrepareResponse( |
540 const std::string& trimmed_path, | 535 const std::string& trimmed_path, |
541 const HttpResponseSenderFunc& send_response_func, | 536 const HttpResponseSenderFunc& send_response_func, |
542 const Status& status, | 537 const Status& status, |
543 scoped_ptr<base::Value> value, | 538 scoped_ptr<base::Value> value, |
544 const std::string& session_id) { | 539 const std::string& session_id) { |
545 CHECK(thread_checker_.CalledOnValidThread()); | 540 CHECK(thread_checker_.CalledOnValidThread()); |
546 scoped_ptr<net::HttpServerResponseInfo> response = | 541 scoped_ptr<net::HttpServerResponseInfo> response = |
547 PrepareResponseHelper(trimmed_path, status, value.Pass(), session_id); | 542 PrepareResponseHelper(trimmed_path, status, value.Pass(), session_id); |
548 log_->AddEntry(Log::kLog, | 543 if (IsVLogOn(0)) { |
549 base::StringPrintf("sending response: %d %s", | 544 VLOG(0) << "Sending response: " << response->status_code() << " " |
550 response->status_code(), | 545 << FormatJsonForDisplay(response->body()); |
551 response->body().c_str())); | 546 } |
552 send_response_func.Run(response.Pass()); | 547 send_response_func.Run(response.Pass()); |
553 if (trimmed_path == kShutdownPath) | 548 if (trimmed_path == kShutdownPath) |
554 quit_func_.Run(); | 549 quit_func_.Run(); |
555 } | 550 } |
556 | 551 |
557 scoped_ptr<net::HttpServerResponseInfo> HttpHandler::PrepareResponseHelper( | 552 scoped_ptr<net::HttpServerResponseInfo> HttpHandler::PrepareResponseHelper( |
558 const std::string& trimmed_path, | 553 const std::string& trimmed_path, |
559 const Status& status, | 554 const Status& status, |
560 scoped_ptr<base::Value> value, | 555 scoped_ptr<base::Value> value, |
561 const std::string& session_id) { | 556 const std::string& session_id) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 params.SetString(name, path_parts[i]); | 643 params.SetString(name, path_parts[i]); |
649 } else if (command_path_parts[i] != path_parts[i]) { | 644 } else if (command_path_parts[i] != path_parts[i]) { |
650 return false; | 645 return false; |
651 } | 646 } |
652 } | 647 } |
653 out_params->MergeDictionary(¶ms); | 648 out_params->MergeDictionary(¶ms); |
654 return true; | 649 return true; |
655 } | 650 } |
656 | 651 |
657 } // namespace internal | 652 } // namespace internal |
OLD | NEW |