OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/location.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "components/devtools_discovery/devtools_discovery_manager.h" | 21 #include "components/devtools_discovery/devtools_discovery_manager.h" |
21 #include "components/devtools_http_handler/devtools_http_handler.h" | 22 #include "components/devtools_http_handler/devtools_http_handler.h" |
22 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | 23 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" |
23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, | 238 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, |
238 const base::FilePath& output_directory, | 239 const base::FilePath& output_directory, |
239 const base::FilePath& frontend_dir, | 240 const base::FilePath& frontend_dir, |
240 bool bundles_resources) { | 241 bool bundles_resources) { |
241 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 242 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
242 scoped_ptr<base::Thread> thread(new base::Thread(kDevToolsHandlerThreadName)); | 243 scoped_ptr<base::Thread> thread(new base::Thread(kDevToolsHandlerThreadName)); |
243 base::Thread::Options options; | 244 base::Thread::Options options; |
244 options.message_loop_type = base::MessageLoop::TYPE_IO; | 245 options.message_loop_type = base::MessageLoop::TYPE_IO; |
245 if (thread->StartWithOptions(options)) { | 246 if (thread->StartWithOptions(options)) { |
246 base::MessageLoop* message_loop = thread->message_loop(); | 247 base::MessageLoop* message_loop = thread->message_loop(); |
247 message_loop->PostTask(FROM_HERE, | 248 message_loop->task_runner()->PostTask( |
248 base::Bind(&StartServerOnHandlerThread, | 249 FROM_HERE, |
249 handler, | 250 base::Bind(&StartServerOnHandlerThread, handler, |
250 base::Unretained(thread.release()), | 251 base::Unretained(thread.release()), server_socket_factory, |
251 server_socket_factory, | 252 output_directory, frontend_dir, bundles_resources)); |
252 output_directory, | |
253 frontend_dir, | |
254 bundles_resources)); | |
255 } | 253 } |
256 } | 254 } |
257 | 255 |
258 // DevToolsAgentHostClientImpl ----------------------------------------------- | 256 // DevToolsAgentHostClientImpl ----------------------------------------------- |
259 // An internal implementation of DevToolsAgentHostClient that delegates | 257 // An internal implementation of DevToolsAgentHostClient that delegates |
260 // messages sent to a DebuggerShell instance. | 258 // messages sent to a DebuggerShell instance. |
261 class DevToolsAgentHostClientImpl : public DevToolsAgentHostClient { | 259 class DevToolsAgentHostClientImpl : public DevToolsAgentHostClient { |
262 public: | 260 public: |
263 DevToolsAgentHostClientImpl(base::MessageLoop* message_loop, | 261 DevToolsAgentHostClientImpl(base::MessageLoop* message_loop, |
264 ServerWrapper* server_wrapper, | 262 ServerWrapper* server_wrapper, |
(...skipping 19 matching lines...) Expand all Loading... |
284 DCHECK(agent_host == agent_host_.get()); | 282 DCHECK(agent_host == agent_host_.get()); |
285 | 283 |
286 std::string message = base::StringPrintf( | 284 std::string message = base::StringPrintf( |
287 "{ \"method\": \"Inspector.detached\", " | 285 "{ \"method\": \"Inspector.detached\", " |
288 "\"params\": { \"reason\": \"%s\"} }", | 286 "\"params\": { \"reason\": \"%s\"} }", |
289 replaced_with_another_client ? | 287 replaced_with_another_client ? |
290 "replaced_with_devtools" : "target_closed"); | 288 "replaced_with_devtools" : "target_closed"); |
291 DispatchProtocolMessage(agent_host, message); | 289 DispatchProtocolMessage(agent_host, message); |
292 | 290 |
293 agent_host_ = nullptr; | 291 agent_host_ = nullptr; |
294 message_loop_->PostTask( | 292 message_loop_->task_runner()->PostTask( |
295 FROM_HERE, | 293 FROM_HERE, |
296 base::Bind(&ServerWrapper::Close, | 294 base::Bind(&ServerWrapper::Close, base::Unretained(server_wrapper_), |
297 base::Unretained(server_wrapper_), | |
298 connection_id_)); | 295 connection_id_)); |
299 } | 296 } |
300 | 297 |
301 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 298 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
302 const std::string& message) override { | 299 const std::string& message) override { |
303 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 300 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
304 DCHECK(agent_host == agent_host_.get()); | 301 DCHECK(agent_host == agent_host_.get()); |
305 message_loop_->PostTask( | 302 message_loop_->task_runner()->PostTask( |
306 FROM_HERE, | 303 FROM_HERE, |
307 base::Bind(&ServerWrapper::SendOverWebSocket, | 304 base::Bind(&ServerWrapper::SendOverWebSocket, |
308 base::Unretained(server_wrapper_), | 305 base::Unretained(server_wrapper_), connection_id_, message)); |
309 connection_id_, | |
310 message)); | |
311 } | 306 } |
312 | 307 |
313 void OnMessage(const std::string& message) { | 308 void OnMessage(const std::string& message) { |
314 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 309 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
315 if (agent_host_.get()) | 310 if (agent_host_.get()) |
316 agent_host_->DispatchProtocolMessage(message); | 311 agent_host_->DispatchProtocolMessage(message); |
317 } | 312 } |
318 | 313 |
319 private: | 314 private: |
320 base::MessageLoop* const message_loop_; | 315 base::MessageLoop* const message_loop_; |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 int connection_id, | 680 int connection_id, |
686 const net::HttpServerRequestInfo& request) { | 681 const net::HttpServerRequestInfo& request) { |
687 if (!thread_) | 682 if (!thread_) |
688 return; | 683 return; |
689 | 684 |
690 std::string browser_prefix = "/devtools/browser"; | 685 std::string browser_prefix = "/devtools/browser"; |
691 size_t browser_pos = request.path.find(browser_prefix); | 686 size_t browser_pos = request.path.find(browser_prefix); |
692 if (browser_pos == 0) { | 687 if (browser_pos == 0) { |
693 scoped_refptr<DevToolsAgentHost> browser_agent = | 688 scoped_refptr<DevToolsAgentHost> browser_agent = |
694 DevToolsAgentHost::CreateForBrowser( | 689 DevToolsAgentHost::CreateForBrowser( |
695 thread_->message_loop_proxy(), | 690 thread_->task_runner(), |
696 base::Bind(&ServerSocketFactory::CreateForTethering, | 691 base::Bind(&ServerSocketFactory::CreateForTethering, |
697 base::Unretained(socket_factory_))); | 692 base::Unretained(socket_factory_))); |
698 connection_to_client_[connection_id] = new DevToolsAgentHostClientImpl( | 693 connection_to_client_[connection_id] = new DevToolsAgentHostClientImpl( |
699 thread_->message_loop(), server_wrapper_, connection_id, browser_agent); | 694 thread_->message_loop(), server_wrapper_, connection_id, browser_agent); |
700 AcceptWebSocket(connection_id, request); | 695 AcceptWebSocket(connection_id, request); |
701 return; | 696 return; |
702 } | 697 } |
703 | 698 |
704 size_t pos = request.path.find(kPageUrlPrefix); | 699 size_t pos = request.path.find(kPageUrlPrefix); |
705 if (pos != 0) { | 700 if (pos != 0) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 if (value) { | 815 if (value) { |
821 base::JSONWriter::WriteWithOptions( | 816 base::JSONWriter::WriteWithOptions( |
822 *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_value); | 817 *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_value); |
823 } | 818 } |
824 std::string json_message; | 819 std::string json_message; |
825 base::JSONWriter::Write(base::StringValue(message), &json_message); | 820 base::JSONWriter::Write(base::StringValue(message), &json_message); |
826 | 821 |
827 net::HttpServerResponseInfo response(status_code); | 822 net::HttpServerResponseInfo response(status_code); |
828 response.SetBody(json_value + message, "application/json; charset=UTF-8"); | 823 response.SetBody(json_value + message, "application/json; charset=UTF-8"); |
829 | 824 |
830 thread_->message_loop()->PostTask( | 825 thread_->task_runner()->PostTask( |
831 FROM_HERE, | 826 FROM_HERE, |
832 base::Bind(&ServerWrapper::SendResponse, | 827 base::Bind(&ServerWrapper::SendResponse, |
833 base::Unretained(server_wrapper_), | 828 base::Unretained(server_wrapper_), connection_id, response)); |
834 connection_id, | |
835 response)); | |
836 } | 829 } |
837 | 830 |
838 void DevToolsHttpHandler::Send200(int connection_id, | 831 void DevToolsHttpHandler::Send200(int connection_id, |
839 const std::string& data, | 832 const std::string& data, |
840 const std::string& mime_type) { | 833 const std::string& mime_type) { |
841 if (!thread_) | 834 if (!thread_) |
842 return; | 835 return; |
843 thread_->message_loop()->PostTask( | 836 thread_->task_runner()->PostTask( |
844 FROM_HERE, | 837 FROM_HERE, |
845 base::Bind(&ServerWrapper::Send200, | 838 base::Bind(&ServerWrapper::Send200, base::Unretained(server_wrapper_), |
846 base::Unretained(server_wrapper_), | 839 connection_id, data, mime_type)); |
847 connection_id, | |
848 data, | |
849 mime_type)); | |
850 } | 840 } |
851 | 841 |
852 void DevToolsHttpHandler::Send404(int connection_id) { | 842 void DevToolsHttpHandler::Send404(int connection_id) { |
853 if (!thread_) | 843 if (!thread_) |
854 return; | 844 return; |
855 thread_->message_loop()->PostTask( | 845 thread_->task_runner()->PostTask( |
856 FROM_HERE, | 846 FROM_HERE, base::Bind(&ServerWrapper::Send404, |
857 base::Bind(&ServerWrapper::Send404, | 847 base::Unretained(server_wrapper_), connection_id)); |
858 base::Unretained(server_wrapper_), | |
859 connection_id)); | |
860 } | 848 } |
861 | 849 |
862 void DevToolsHttpHandler::Send500(int connection_id, | 850 void DevToolsHttpHandler::Send500(int connection_id, |
863 const std::string& message) { | 851 const std::string& message) { |
864 if (!thread_) | 852 if (!thread_) |
865 return; | 853 return; |
866 thread_->message_loop()->PostTask( | 854 thread_->task_runner()->PostTask( |
867 FROM_HERE, | 855 FROM_HERE, |
868 base::Bind(&ServerWrapper::Send500, | 856 base::Bind(&ServerWrapper::Send500, base::Unretained(server_wrapper_), |
869 base::Unretained(server_wrapper_), | 857 connection_id, message)); |
870 connection_id, | |
871 message)); | |
872 } | 858 } |
873 | 859 |
874 void DevToolsHttpHandler::AcceptWebSocket( | 860 void DevToolsHttpHandler::AcceptWebSocket( |
875 int connection_id, | 861 int connection_id, |
876 const net::HttpServerRequestInfo& request) { | 862 const net::HttpServerRequestInfo& request) { |
877 if (!thread_) | 863 if (!thread_) |
878 return; | 864 return; |
879 thread_->message_loop()->PostTask( | 865 thread_->task_runner()->PostTask( |
880 FROM_HERE, | 866 FROM_HERE, |
881 base::Bind(&ServerWrapper::AcceptWebSocket, | 867 base::Bind(&ServerWrapper::AcceptWebSocket, |
882 base::Unretained(server_wrapper_), | 868 base::Unretained(server_wrapper_), connection_id, request)); |
883 connection_id, | |
884 request)); | |
885 } | 869 } |
886 | 870 |
887 base::DictionaryValue* DevToolsHttpHandler::SerializeDescriptor( | 871 base::DictionaryValue* DevToolsHttpHandler::SerializeDescriptor( |
888 const DevToolsTargetDescriptor& descriptor, | 872 const DevToolsTargetDescriptor& descriptor, |
889 const std::string& host) { | 873 const std::string& host) { |
890 base::DictionaryValue* dictionary = new base::DictionaryValue; | 874 base::DictionaryValue* dictionary = new base::DictionaryValue; |
891 | 875 |
892 std::string id = descriptor.GetId(); | 876 std::string id = descriptor.GetId(); |
893 dictionary->SetString(kTargetIdField, id); | 877 dictionary->SetString(kTargetIdField, id); |
894 std::string parent_id = descriptor.GetParentId(); | 878 std::string parent_id = descriptor.GetParentId(); |
(...skipping 26 matching lines...) Expand all Loading... |
921 id.c_str(), | 905 id.c_str(), |
922 host); | 906 host); |
923 dictionary->SetString( | 907 dictionary->SetString( |
924 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 908 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
925 } | 909 } |
926 | 910 |
927 return dictionary; | 911 return dictionary; |
928 } | 912 } |
929 | 913 |
930 } // namespace devtools_http_handler | 914 } // namespace devtools_http_handler |
OLD | NEW |