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 "content/browser/devtools/devtools_http_handler_impl.h" | 5 #include "content/browser/devtools/devtools_http_handler_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "content/browser/devtools/devtools_browser_target.h" | 20 #include "content/browser/devtools/devtools_browser_target.h" |
21 #include "content/browser/devtools/devtools_protocol.h" | 21 #include "content/browser/devtools/devtools_protocol.h" |
22 #include "content/browser/devtools/devtools_protocol_constants.h" | 22 #include "content/browser/devtools/devtools_protocol_constants.h" |
23 #include "content/browser/devtools/devtools_system_info_handler.h" | 23 #include "content/browser/devtools/devtools_system_info_handler.h" |
24 #include "content/browser/devtools/devtools_tracing_handler.h" | 24 #include "content/browser/devtools/devtools_tracing_handler.h" |
25 #include "content/browser/devtools/tethering_handler.h" | 25 #include "content/browser/devtools/tethering_handler.h" |
26 #include "content/common/devtools_messages.h" | 26 #include "content/common/devtools_messages.h" |
27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/devtools_agent_host.h" | 28 #include "content/public/browser/devtools_agent_host.h" |
| 29 #include "content/public/browser/devtools_client_host.h" |
29 #include "content/public/browser/devtools_http_handler_delegate.h" | 30 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 31 #include "content/public/browser/devtools_manager.h" |
30 #include "content/public/browser/devtools_target.h" | 32 #include "content/public/browser/devtools_target.h" |
31 #include "content/public/common/content_client.h" | 33 #include "content/public/common/content_client.h" |
32 #include "content/public/common/url_constants.h" | 34 #include "content/public/common/url_constants.h" |
33 #include "content/public/common/user_agent.h" | 35 #include "content/public/common/user_agent.h" |
34 #include "content/public/common/user_agent.h" | 36 #include "content/public/common/user_agent.h" |
35 #include "grit/devtools_resources_map.h" | 37 #include "grit/devtools_resources_map.h" |
36 #include "net/base/escape.h" | 38 #include "net/base/escape.h" |
37 #include "net/base/io_buffer.h" | 39 #include "net/base/io_buffer.h" |
38 #include "net/base/ip_endpoint.h" | 40 #include "net/base/ip_endpoint.h" |
39 #include "net/base/net_errors.h" | 41 #include "net/base/net_errors.h" |
(...skipping 20 matching lines...) Expand all Loading... |
60 const char kTargetParentIdField[] = "parentId"; | 62 const char kTargetParentIdField[] = "parentId"; |
61 const char kTargetTypeField[] = "type"; | 63 const char kTargetTypeField[] = "type"; |
62 const char kTargetTitleField[] = "title"; | 64 const char kTargetTitleField[] = "title"; |
63 const char kTargetDescriptionField[] = "description"; | 65 const char kTargetDescriptionField[] = "description"; |
64 const char kTargetUrlField[] = "url"; | 66 const char kTargetUrlField[] = "url"; |
65 const char kTargetThumbnailUrlField[] = "thumbnailUrl"; | 67 const char kTargetThumbnailUrlField[] = "thumbnailUrl"; |
66 const char kTargetFaviconUrlField[] = "faviconUrl"; | 68 const char kTargetFaviconUrlField[] = "faviconUrl"; |
67 const char kTargetWebSocketDebuggerUrlField[] = "webSocketDebuggerUrl"; | 69 const char kTargetWebSocketDebuggerUrlField[] = "webSocketDebuggerUrl"; |
68 const char kTargetDevtoolsFrontendUrlField[] = "devtoolsFrontendUrl"; | 70 const char kTargetDevtoolsFrontendUrlField[] = "devtoolsFrontendUrl"; |
69 | 71 |
70 // An internal implementation of DevToolsAgentHostClient that delegates | 72 // An internal implementation of DevToolsClientHost that delegates |
71 // messages sent to a DebuggerShell instance. | 73 // messages sent for DevToolsClient to a DebuggerShell instance. |
72 class DevToolsAgentHostClientImpl : public DevToolsAgentHostClient { | 74 class DevToolsClientHostImpl : public DevToolsClientHost { |
73 public: | 75 public: |
74 DevToolsAgentHostClientImpl(base::MessageLoop* message_loop, | 76 DevToolsClientHostImpl(base::MessageLoop* message_loop, |
75 net::HttpServer* server, | 77 net::HttpServer* server, |
76 int connection_id, | 78 int connection_id) |
77 DevToolsAgentHost* agent_host) | |
78 : message_loop_(message_loop), | 79 : message_loop_(message_loop), |
79 server_(server), | 80 server_(server), |
80 connection_id_(connection_id), | 81 connection_id_(connection_id), |
81 agent_host_(agent_host) { | 82 is_closed_(false), |
82 agent_host_->AttachClient(this); | 83 detach_reason_("target_closed") {} |
83 } | |
84 | 84 |
85 virtual ~DevToolsAgentHostClientImpl() { | 85 virtual ~DevToolsClientHostImpl() {} |
86 if (agent_host_) | |
87 agent_host_->DetachClient(); | |
88 } | |
89 | 86 |
90 virtual void AgentHostClosed( | 87 // DevToolsClientHost interface |
91 DevToolsAgentHost* agent_host, | 88 virtual void InspectedContentsClosing() OVERRIDE { |
92 bool replaced_with_another_client) OVERRIDE { | 89 if (is_closed_) |
93 DCHECK(agent_host == agent_host_.get()); | 90 return; |
94 agent_host_ = NULL; | 91 is_closed_ = true; |
95 | 92 |
96 base::DictionaryValue notification; | 93 base::DictionaryValue notification; |
97 notification.SetString( | 94 notification.SetString( |
98 devtools::Inspector::detached::kParamReason, | 95 devtools::Inspector::detached::kParamReason, detach_reason_); |
99 replaced_with_another_client ? | |
100 "replaced_with_devtools" : "target_closed"); | |
101 std::string response = DevToolsProtocol::CreateNotification( | 96 std::string response = DevToolsProtocol::CreateNotification( |
102 devtools::Inspector::detached::kName, | 97 devtools::Inspector::detached::kName, |
103 notification.DeepCopy())->Serialize(); | 98 notification.DeepCopy())->Serialize(); |
104 message_loop_->PostTask( | 99 message_loop_->PostTask( |
105 FROM_HERE, | 100 FROM_HERE, |
106 base::Bind(&net::HttpServer::SendOverWebSocket, | 101 base::Bind(&net::HttpServer::SendOverWebSocket, |
107 server_, | 102 server_, |
108 connection_id_, | 103 connection_id_, |
109 response)); | 104 response)); |
110 | 105 |
111 message_loop_->PostTask( | 106 message_loop_->PostTask( |
112 FROM_HERE, | 107 FROM_HERE, |
113 base::Bind(&net::HttpServer::Close, server_, connection_id_)); | 108 base::Bind(&net::HttpServer::Close, server_, connection_id_)); |
114 } | 109 } |
115 | 110 |
116 virtual void DispatchProtocolMessage( | 111 virtual void DispatchOnInspectorFrontend(const std::string& data) OVERRIDE { |
117 DevToolsAgentHost* agent_host, const std::string& message) OVERRIDE { | |
118 DCHECK(agent_host == agent_host_.get()); | |
119 message_loop_->PostTask( | 112 message_loop_->PostTask( |
120 FROM_HERE, | 113 FROM_HERE, |
121 base::Bind(&net::HttpServer::SendOverWebSocket, | 114 base::Bind(&net::HttpServer::SendOverWebSocket, |
122 server_, | 115 server_, |
123 connection_id_, | 116 connection_id_, |
124 message)); | 117 data)); |
125 } | 118 } |
126 | 119 |
127 void OnMessage(const std::string& message) { | 120 virtual void ReplacedWithAnotherClient() OVERRIDE { |
128 if (agent_host_) | 121 detach_reason_ = "replaced_with_devtools"; |
129 agent_host_->DispatchProtocolMessage(message); | |
130 } | 122 } |
131 | 123 |
132 private: | 124 private: |
133 base::MessageLoop* message_loop_; | 125 base::MessageLoop* message_loop_; |
134 net::HttpServer* server_; | 126 net::HttpServer* server_; |
135 int connection_id_; | 127 int connection_id_; |
136 scoped_refptr<DevToolsAgentHost> agent_host_; | 128 bool is_closed_; |
| 129 std::string detach_reason_; |
137 }; | 130 }; |
138 | 131 |
139 static bool TimeComparator(const DevToolsTarget* target1, | 132 static bool TimeComparator(const DevToolsTarget* target1, |
140 const DevToolsTarget* target2) { | 133 const DevToolsTarget* target2) { |
141 return target1->GetLastActivityTime() > target2->GetLastActivityTime(); | 134 return target1->GetLastActivityTime() > target2->GetLastActivityTime(); |
142 } | 135 } |
143 | 136 |
144 } // namespace | 137 } // namespace |
145 | 138 |
146 // static | 139 // static |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 Send500(connection_id, "No such target id: " + page_id); | 605 Send500(connection_id, "No such target id: " + page_id); |
613 return; | 606 return; |
614 } | 607 } |
615 | 608 |
616 if (agent->IsAttached()) { | 609 if (agent->IsAttached()) { |
617 Send500(connection_id, | 610 Send500(connection_id, |
618 "Target with given id is being inspected: " + page_id); | 611 "Target with given id is being inspected: " + page_id); |
619 return; | 612 return; |
620 } | 613 } |
621 | 614 |
622 DevToolsAgentHostClientImpl * client_host = new DevToolsAgentHostClientImpl( | 615 DevToolsClientHostImpl* client_host = new DevToolsClientHostImpl( |
623 thread_->message_loop(), server_.get(), connection_id, agent); | 616 thread_->message_loop(), server_.get(), connection_id); |
624 connection_to_client_ui_[connection_id] = client_host; | 617 connection_to_client_host_ui_[connection_id] = client_host; |
| 618 |
| 619 DevToolsManager::GetInstance()-> |
| 620 RegisterDevToolsClientHostFor(agent, client_host); |
625 | 621 |
626 AcceptWebSocket(connection_id, request); | 622 AcceptWebSocket(connection_id, request); |
627 } | 623 } |
628 | 624 |
629 void DevToolsHttpHandlerImpl::OnWebSocketMessageUI( | 625 void DevToolsHttpHandlerImpl::OnWebSocketMessageUI( |
630 int connection_id, | 626 int connection_id, |
631 const std::string& data) { | 627 const std::string& data) { |
632 ConnectionToClientMap::iterator it = | 628 ConnectionToClientHostMap::iterator it = |
633 connection_to_client_ui_.find(connection_id); | 629 connection_to_client_host_ui_.find(connection_id); |
634 if (it == connection_to_client_ui_.end()) | 630 if (it == connection_to_client_host_ui_.end()) |
635 return; | 631 return; |
636 | 632 |
637 DevToolsAgentHostClientImpl* client = | 633 DevToolsManager* manager = DevToolsManager::GetInstance(); |
638 static_cast<DevToolsAgentHostClientImpl*>(it->second); | 634 manager->DispatchOnInspectorBackend(it->second, data); |
639 client->OnMessage(data); | |
640 } | 635 } |
641 | 636 |
642 void DevToolsHttpHandlerImpl::OnCloseUI(int connection_id) { | 637 void DevToolsHttpHandlerImpl::OnCloseUI(int connection_id) { |
643 ConnectionToClientMap::iterator it = | 638 ConnectionToClientHostMap::iterator it = |
644 connection_to_client_ui_.find(connection_id); | 639 connection_to_client_host_ui_.find(connection_id); |
645 if (it != connection_to_client_ui_.end()) { | 640 if (it != connection_to_client_host_ui_.end()) { |
646 DevToolsAgentHostClientImpl* client = | 641 DevToolsClientHostImpl* client_host = |
647 static_cast<DevToolsAgentHostClientImpl*>(it->second); | 642 static_cast<DevToolsClientHostImpl*>(it->second); |
648 delete client; | 643 DevToolsManager::GetInstance()->ClientHostClosing(client_host); |
649 connection_to_client_ui_.erase(connection_id); | 644 delete client_host; |
| 645 connection_to_client_host_ui_.erase(connection_id); |
650 } | 646 } |
651 } | 647 } |
652 | 648 |
653 DevToolsHttpHandlerImpl::DevToolsHttpHandlerImpl( | 649 DevToolsHttpHandlerImpl::DevToolsHttpHandlerImpl( |
654 const net::StreamListenSocketFactory* socket_factory, | 650 const net::StreamListenSocketFactory* socket_factory, |
655 const std::string& frontend_url, | 651 const std::string& frontend_url, |
656 DevToolsHttpHandlerDelegate* delegate, | 652 DevToolsHttpHandlerDelegate* delegate, |
657 const base::FilePath& active_port_output_directory) | 653 const base::FilePath& active_port_output_directory) |
658 : frontend_url_(frontend_url), | 654 : frontend_url_(frontend_url), |
659 socket_factory_(socket_factory), | 655 socket_factory_(socket_factory), |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 id.c_str(), | 814 id.c_str(), |
819 host); | 815 host); |
820 dictionary->SetString( | 816 dictionary->SetString( |
821 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 817 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
822 } | 818 } |
823 | 819 |
824 return dictionary; | 820 return dictionary; |
825 } | 821 } |
826 | 822 |
827 } // namespace content | 823 } // namespace content |
OLD | NEW |