OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/protocol/browser_handler.h" | 5 #include "content/browser/devtools/protocol/browser_handler.h" |
6 | 6 |
| 7 #include "content/public/browser/render_widget_host_view.h" |
| 8 #include "content/public/browser/web_contents.h" |
| 9 #include "ui/gfx/geometry/size.h" |
| 10 |
7 namespace content { | 11 namespace content { |
8 namespace devtools { | 12 namespace devtools { |
9 namespace browser { | 13 namespace browser { |
10 | 14 |
11 namespace { | 15 namespace { |
12 const char kTargetTypeWebContents[] = "web_contents"; | 16 const char kTargetTypeWebContents[] = "web_contents"; |
13 const char kTargetTypeFrame[] = "frame"; | 17 const char kTargetTypeFrame[] = "frame"; |
14 const char kTargetTypeSharedWorker[] = "shared_worker"; | 18 const char kTargetTypeSharedWorker[] = "shared_worker"; |
15 const char kTargetTypeServiceWorker[] = "service_worker"; | 19 const char kTargetTypeServiceWorker[] = "service_worker"; |
16 const char kTargetTypeServiceOther[] = "other"; | 20 const char kTargetTypeServiceOther[] = "other"; |
17 } | 21 } |
18 | 22 |
19 using Response = DevToolsProtocolClient::Response; | 23 using Response = DevToolsProtocolClient::Response; |
20 | 24 |
21 BrowserHandler::BrowserHandler() { | 25 BrowserHandler::BrowserHandler() {} |
22 } | |
23 | 26 |
24 BrowserHandler::~BrowserHandler() { | 27 BrowserHandler::~BrowserHandler() {} |
25 } | |
26 | 28 |
27 void BrowserHandler::SetClient(std::unique_ptr<Client> client) { | 29 void BrowserHandler::SetClient(std::unique_ptr<Client> client) { |
28 client_.swap(client); | 30 client_.swap(client); |
29 } | 31 } |
30 | 32 |
31 static std::string GetTypeString(DevToolsAgentHost* agent_host) { | 33 static std::string GetTypeString(DevToolsAgentHost* agent_host) { |
32 switch (agent_host->GetType()) { | 34 switch (agent_host->GetType()) { |
33 case DevToolsAgentHost::TYPE_WEB_CONTENTS: | 35 case DevToolsAgentHost::TYPE_WEB_CONTENTS: |
34 return kTargetTypeWebContents; | 36 return kTargetTypeWebContents; |
35 case DevToolsAgentHost::TYPE_FRAME: | 37 case DevToolsAgentHost::TYPE_FRAME: |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 82 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
81 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 83 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
82 it != agents.end(); ++it) { | 84 it != agents.end(); ++it) { |
83 DevToolsAgentHost* agent_host = (*it).get(); | 85 DevToolsAgentHost* agent_host = (*it).get(); |
84 scoped_refptr<devtools::browser::TargetInfo> info = | 86 scoped_refptr<devtools::browser::TargetInfo> info = |
85 devtools::browser::TargetInfo::Create()-> | 87 devtools::browser::TargetInfo::Create()-> |
86 set_target_id(agent_host->GetId())-> | 88 set_target_id(agent_host->GetId())-> |
87 set_type(GetTypeString(agent_host))-> | 89 set_type(GetTypeString(agent_host))-> |
88 set_title(agent_host->GetTitle())-> | 90 set_title(agent_host->GetTitle())-> |
89 set_url(agent_host->GetURL().spec()); | 91 set_url(agent_host->GetURL().spec()); |
| 92 infos->push_back(info); |
90 } | 93 } |
91 return Response::OK(); | 94 return Response::OK(); |
92 } | 95 } |
93 | 96 |
| 97 Response BrowserHandler::GetFrameSize(const std::string& target_id, |
| 98 int* width, |
| 99 int* height) { |
| 100 scoped_refptr<DevToolsAgentHost> agent_host = |
| 101 DevToolsAgentHost::GetForId(target_id); |
| 102 if (!agent_host) |
| 103 return Response::ServerError("No target with given id found"); |
| 104 |
| 105 // Get size of frame from RWHV if available. |
| 106 WebContents* web_contents = agent_host->GetWebContents(); |
| 107 RenderWidgetHostView* view = |
| 108 web_contents ? web_contents->GetRenderWidgetHostView() : nullptr; |
| 109 if (!view) |
| 110 return Response::ServerError("Target does not support GetFrameSize"); |
| 111 |
| 112 gfx::Size size = view->GetViewBounds().size(); |
| 113 *width = size.width(); |
| 114 *height = size.height(); |
| 115 return Response::OK(); |
| 116 } |
| 117 |
| 118 Response BrowserHandler::SetFrameSize(const std::string& target_id, |
| 119 int width, |
| 120 int height) { |
| 121 scoped_refptr<DevToolsAgentHost> agent_host = |
| 122 DevToolsAgentHost::GetForId(target_id); |
| 123 if (!agent_host) |
| 124 return Response::ServerError("No target with given id found"); |
| 125 |
| 126 if (width < 0 || height < 0) |
| 127 return Response::InvalidParams("Width and height must be non-negative"); |
| 128 |
| 129 // Set size of frame by resizing RWHV if available. |
| 130 WebContents* web_contents = agent_host->GetWebContents(); |
| 131 RenderWidgetHostView* view = |
| 132 web_contents ? web_contents->GetRenderWidgetHostView() : nullptr; |
| 133 if (!view) |
| 134 return Response::ServerError("Target does not support SetFrameSize"); |
| 135 |
| 136 view->SetSize(gfx::Size(width, height)); |
| 137 return Response::OK(); |
| 138 } |
| 139 |
94 Response BrowserHandler::Attach(const std::string& targetId) { | 140 Response BrowserHandler::Attach(const std::string& targetId) { |
95 scoped_refptr<DevToolsAgentHost> agent_host = | 141 scoped_refptr<DevToolsAgentHost> agent_host = |
96 DevToolsAgentHost::GetForId(targetId); | 142 DevToolsAgentHost::GetForId(targetId); |
97 if (!agent_host) | 143 if (!agent_host) |
98 return Response::ServerError("No target with given id found"); | 144 return Response::ServerError("No target with given id found"); |
99 bool success = agent_host->AttachClient(this); | 145 bool success = agent_host->AttachClient(this); |
100 return success ? Response::OK() : | 146 return success ? Response::OK() |
101 Response::ServerError("Target is already being debugged"); | 147 : Response::ServerError("Target is already being debugged"); |
102 } | 148 } |
103 | 149 |
104 Response BrowserHandler::Detach(const std::string& targetId) { | 150 Response BrowserHandler::Detach(const std::string& targetId) { |
105 scoped_refptr<DevToolsAgentHost> agent_host = | 151 scoped_refptr<DevToolsAgentHost> agent_host = |
106 DevToolsAgentHost::GetForId(targetId); | 152 DevToolsAgentHost::GetForId(targetId); |
107 if (!agent_host) | 153 if (!agent_host) |
108 return Response::ServerError("No target with given id found"); | 154 return Response::ServerError("No target with given id found"); |
109 bool success = agent_host->DetachClient(this); | 155 bool success = agent_host->DetachClient(this); |
110 return success ? Response::OK() : | 156 return success ? Response::OK() |
111 Response::ServerError("Target is not being debugged"); | 157 : Response::ServerError("Target is not being debugged"); |
112 } | 158 } |
113 | 159 |
114 Response BrowserHandler::SendMessage(const std::string& targetId, | 160 Response BrowserHandler::SendMessage(const std::string& targetId, |
115 const std::string& message) { | 161 const std::string& message) { |
116 scoped_refptr<DevToolsAgentHost> agent_host = | 162 scoped_refptr<DevToolsAgentHost> agent_host = |
117 DevToolsAgentHost::GetForId(targetId); | 163 DevToolsAgentHost::GetForId(targetId); |
118 if (!agent_host) | 164 if (!agent_host) |
119 return Response::ServerError("No target with given id found"); | 165 return Response::ServerError("No target with given id found"); |
120 agent_host->DispatchProtocolMessage(this, message); | 166 agent_host->DispatchProtocolMessage(this, message); |
121 return Response::OK(); | 167 return Response::OK(); |
122 } | 168 } |
123 | 169 |
124 void BrowserHandler::DispatchProtocolMessage( | 170 void BrowserHandler::DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
125 DevToolsAgentHost* agent_host, const std::string& message) { | 171 const std::string& message) { |
126 client_->DispatchMessage(DispatchMessageParams::Create()-> | 172 client_->DispatchMessage(DispatchMessageParams::Create() |
127 set_target_id(agent_host->GetId())-> | 173 ->set_target_id(agent_host->GetId()) |
128 set_message(message)); | 174 ->set_message(message)); |
129 } | 175 } |
130 | 176 |
131 void BrowserHandler::AgentHostClosed(DevToolsAgentHost* agent_host, | 177 void BrowserHandler::AgentHostClosed(DevToolsAgentHost* agent_host, |
132 bool replaced_with_another_client) { | 178 bool replaced_with_another_client) {} |
133 } | |
134 | 179 |
135 } // namespace browser | 180 } // namespace browser |
136 } // namespace devtools | 181 } // namespace devtools |
137 } // namespace content | 182 } // namespace content |
OLD | NEW |