OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/devtools/chrome_devtools_manager_delegate.h" | 5 #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
10 #include "chrome/browser/devtools/device/android_device_manager.h" | 12 #include "chrome/browser/devtools/device/android_device_manager.h" |
11 #include "chrome/browser/devtools/device/tcp_device_provider.h" | 13 #include "chrome/browser/devtools/device/tcp_device_provider.h" |
12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" | 14 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" |
13 #include "chrome/browser/devtools/devtools_protocol_constants.h" | 15 #include "chrome/browser/devtools/devtools_protocol_constants.h" |
14 #include "chrome/browser/devtools/devtools_window.h" | 16 #include "chrome/browser/devtools/devtools_window.h" |
15 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/ui/browser_navigator.h" | 20 #include "chrome/browser/ui/browser_navigator.h" |
19 #include "chrome/browser/ui/browser_navigator_params.h" | 21 #include "chrome/browser/ui/browser_navigator_params.h" |
| 22 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" |
20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 24 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/grit/browser_resources.h" | 26 #include "chrome/grit/browser_resources.h" |
22 #include "components/guest_view/browser/guest_view_base.h" | 27 #include "components/guest_view/browser/guest_view_base.h" |
23 #include "content/public/browser/devtools_agent_host.h" | 28 #include "content/public/browser/devtools_agent_host.h" |
24 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
26 #include "extensions/browser/extension_host.h" | 31 #include "extensions/browser/extension_host.h" |
27 #include "extensions/browser/extension_registry.h" | 32 #include "extensions/browser/extension_registry.h" |
28 #include "extensions/browser/process_manager.h" | 33 #include "extensions/browser/process_manager.h" |
29 #include "ui/base/resource/resource_bundle.h" | 34 #include "ui/base/resource/resource_bundle.h" |
30 | 35 |
31 using content::DevToolsAgentHost; | 36 using content::DevToolsAgentHost; |
32 | 37 |
33 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; | 38 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; |
34 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; | 39 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; |
35 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; | 40 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; |
36 | 41 |
37 namespace { | 42 namespace { |
38 | 43 |
39 char kLocationsParam[] = "locations"; | 44 char kLocationsParam[] = "locations"; |
40 char kHostParam[] = "host"; | 45 char kHostParam[] = "host"; |
41 char kPortParam[] = "port"; | 46 char kPortParam[] = "port"; |
42 | 47 |
| 48 BrowserWindow* GetBrowserWindow(int window_id) { |
| 49 for (auto* b : *BrowserList::GetInstance()) { |
| 50 if (b->session_id().id() == window_id) |
| 51 return b->window(); |
| 52 } |
| 53 return nullptr; |
| 54 } |
| 55 |
| 56 // Get the bounds and state of the browser window. The bounds is for the |
| 57 // restored window when the window is minimized. Otherwise, it is for the actual |
| 58 // window. |
| 59 std::unique_ptr<base::DictionaryValue> GetBounds(BrowserWindow* window) { |
| 60 gfx::Rect bounds; |
| 61 if (window->IsMinimized()) |
| 62 bounds = window->GetRestoredBounds(); |
| 63 else |
| 64 bounds = window->GetBounds(); |
| 65 |
| 66 auto bounds_object = base::MakeUnique<base::DictionaryValue>(); |
| 67 |
| 68 bounds_object->SetInteger("left", bounds.x()); |
| 69 bounds_object->SetInteger("top", bounds.y()); |
| 70 bounds_object->SetInteger("width", bounds.width()); |
| 71 bounds_object->SetInteger("height", bounds.height()); |
| 72 |
| 73 std::string window_state = "normal"; |
| 74 if (window->IsMinimized()) |
| 75 window_state = "minimized"; |
| 76 if (window->IsMaximized()) |
| 77 window_state = "maximized"; |
| 78 if (window->IsFullscreen()) |
| 79 window_state = "fullscreen"; |
| 80 bounds_object->SetString("windowState", window_state); |
| 81 |
| 82 return bounds_object; |
| 83 } |
| 84 |
43 bool GetExtensionInfo(content::RenderFrameHost* host, | 85 bool GetExtensionInfo(content::RenderFrameHost* host, |
44 std::string* name, | 86 std::string* name, |
45 std::string* type) { | 87 std::string* type) { |
46 content::WebContents* wc = content::WebContents::FromRenderFrameHost(host); | 88 content::WebContents* wc = content::WebContents::FromRenderFrameHost(host); |
47 if (!wc) | 89 if (!wc) |
48 return false; | 90 return false; |
49 Profile* profile = Profile::FromBrowserContext(wc->GetBrowserContext()); | 91 Profile* profile = Profile::FromBrowserContext(wc->GetBrowserContext()); |
50 if (!profile) | 92 if (!profile) |
51 return false; | 93 return false; |
52 const extensions::Extension* extension = | 94 const extensions::Extension* extension = |
(...skipping 13 matching lines...) Expand all Loading... |
66 extension->is_platform_app()) { | 108 extension->is_platform_app()) { |
67 *name = extension->name(); | 109 *name = extension->name(); |
68 *type = ChromeDevToolsManagerDelegate::kTypeApp; | 110 *type = ChromeDevToolsManagerDelegate::kTypeApp; |
69 return true; | 111 return true; |
70 } | 112 } |
71 return false; | 113 return false; |
72 } | 114 } |
73 | 115 |
74 } // namespace | 116 } // namespace |
75 | 117 |
| 118 // static |
| 119 std::unique_ptr<base::DictionaryValue> |
| 120 ChromeDevToolsManagerDelegate::GetWindowForTarget( |
| 121 int id, |
| 122 base::DictionaryValue* params) { |
| 123 std::string target_id; |
| 124 if (!params->GetString("targetId", &target_id)) |
| 125 return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); |
| 126 |
| 127 Browser* browser = nullptr; |
| 128 scoped_refptr<DevToolsAgentHost> host = |
| 129 DevToolsAgentHost::GetForId(target_id); |
| 130 if (!host) |
| 131 return DevToolsProtocol::CreateErrorResponse(id, "No target with given id"); |
| 132 content::WebContents* web_contents = host->GetWebContents(); |
| 133 if (!web_contents) { |
| 134 return DevToolsProtocol::CreateErrorResponse( |
| 135 id, "No web contents in the target"); |
| 136 } |
| 137 for (auto* b : *BrowserList::GetInstance()) { |
| 138 int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); |
| 139 if (tab_index != TabStripModel::kNoTab) |
| 140 browser = b; |
| 141 } |
| 142 if (!browser) { |
| 143 return DevToolsProtocol::CreateErrorResponse(id, |
| 144 "Browser window not found"); |
| 145 } |
| 146 |
| 147 auto result = base::MakeUnique<base::DictionaryValue>(); |
| 148 result->SetInteger("windowId", browser->session_id().id()); |
| 149 result->Set("bounds", GetBounds(browser->window())); |
| 150 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| 151 } |
| 152 |
| 153 // static |
| 154 std::unique_ptr<base::DictionaryValue> |
| 155 ChromeDevToolsManagerDelegate::GetWindowBounds(int id, |
| 156 base::DictionaryValue* params) { |
| 157 int window_id; |
| 158 if (!params->GetInteger("windowId", &window_id)) |
| 159 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); |
| 160 BrowserWindow* window = GetBrowserWindow(window_id); |
| 161 if (!window) { |
| 162 return DevToolsProtocol::CreateErrorResponse(id, |
| 163 "Browser window not found"); |
| 164 } |
| 165 |
| 166 auto result = base::MakeUnique<base::DictionaryValue>(); |
| 167 result->Set("bounds", GetBounds(window)); |
| 168 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| 169 } |
| 170 |
| 171 // static |
| 172 std::unique_ptr<base::DictionaryValue> |
| 173 ChromeDevToolsManagerDelegate::SetWindowBounds(int id, |
| 174 base::DictionaryValue* params) { |
| 175 int window_id; |
| 176 if (!params->GetInteger("windowId", &window_id)) |
| 177 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); |
| 178 BrowserWindow* window = GetBrowserWindow(window_id); |
| 179 if (!window) { |
| 180 return DevToolsProtocol::CreateErrorResponse(id, |
| 181 "Browser window not found"); |
| 182 } |
| 183 |
| 184 const base::Value* value = nullptr; |
| 185 const base::DictionaryValue* bounds_dict = nullptr; |
| 186 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) |
| 187 return DevToolsProtocol::CreateInvalidParamsResponse(id, "bounds"); |
| 188 |
| 189 std::string window_state; |
| 190 if (!bounds_dict->GetString("windowState", &window_state)) |
| 191 window_state = "normal"; |
| 192 else if (window_state != "normal" && window_state != "minimized" && |
| 193 window_state != "maximized" && window_state != "fullscreen") |
| 194 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowState"); |
| 195 |
| 196 // Compute updated bounds when window state is normal. |
| 197 bool set_bounds = false; |
| 198 gfx::Rect bounds = window->GetBounds(); |
| 199 int left, top, width, height; |
| 200 if (bounds_dict->GetInteger("left", &left)) { |
| 201 bounds.set_x(left); |
| 202 set_bounds = true; |
| 203 } |
| 204 if (bounds_dict->GetInteger("top", &top)) { |
| 205 bounds.set_y(top); |
| 206 set_bounds = true; |
| 207 } |
| 208 if (bounds_dict->GetInteger("width", &width)) { |
| 209 if (width < 0) |
| 210 return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); |
| 211 bounds.set_width(width); |
| 212 set_bounds = true; |
| 213 } |
| 214 if (bounds_dict->GetInteger("height", &height)) { |
| 215 if (height < 0) |
| 216 return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); |
| 217 bounds.set_height(height); |
| 218 set_bounds = true; |
| 219 } |
| 220 |
| 221 if (set_bounds && window_state != "normal") { |
| 222 return DevToolsProtocol::CreateErrorResponse( |
| 223 id, |
| 224 "The 'minimized', 'maximized' and 'fullscreen' states cannot be " |
| 225 "combined with 'left', 'top', 'width' or 'height'"); |
| 226 } |
| 227 |
| 228 if (set_bounds && (window->IsMinimized() || window->IsMaximized() || |
| 229 window->IsFullscreen())) { |
| 230 return DevToolsProtocol::CreateErrorResponse( |
| 231 id, |
| 232 "To resize minimized/maximized/fullscreen window, restore it to normal " |
| 233 "state first."); |
| 234 } |
| 235 |
| 236 if (window_state == "fullscreen") { |
| 237 if (window->IsMinimized()) { |
| 238 return DevToolsProtocol::CreateErrorResponse(id, |
| 239 "To make minimized window " |
| 240 "fullscreen, restore it to " |
| 241 "normal state first."); |
| 242 } |
| 243 window->GetExclusiveAccessContext()->EnterFullscreen( |
| 244 GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); |
| 245 } |
| 246 |
| 247 if (window_state == "maximized") { |
| 248 if (window->IsMinimized() || window->IsFullscreen()) { |
| 249 return DevToolsProtocol::CreateErrorResponse( |
| 250 id, |
| 251 "To maximize a minimized or fullscreen window, restore it to normal " |
| 252 "state first."); |
| 253 } |
| 254 window->Maximize(); |
| 255 } |
| 256 |
| 257 if (window_state == "minimized") { |
| 258 if (window->IsFullscreen()) { |
| 259 return DevToolsProtocol::CreateErrorResponse( |
| 260 id, |
| 261 "To minimize a fullscreen window, restore it to normal " |
| 262 "state first."); |
| 263 } |
| 264 window->Minimize(); |
| 265 } |
| 266 |
| 267 if (window_state == "normal") { |
| 268 if (window->IsFullscreen()) { |
| 269 window->GetExclusiveAccessContext()->ExitFullscreen(); |
| 270 } else if (window->IsMinimized()) { |
| 271 window->Show(); |
| 272 } else if (window->IsMaximized()) { |
| 273 window->Restore(); |
| 274 } else if (set_bounds) { |
| 275 window->SetBounds(bounds); |
| 276 } |
| 277 } |
| 278 |
| 279 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); |
| 280 } |
| 281 |
| 282 std::unique_ptr<base::DictionaryValue> |
| 283 ChromeDevToolsManagerDelegate::HandleBrowserCommand( |
| 284 int id, |
| 285 std::string method, |
| 286 base::DictionaryValue* params) { |
| 287 if (method == chrome::devtools::Browser::getWindowForTarget::kName) |
| 288 return GetWindowForTarget(id, params); |
| 289 if (method == chrome::devtools::Browser::getWindowBounds::kName) |
| 290 return GetWindowBounds(id, params); |
| 291 if (method == chrome::devtools::Browser::setWindowBounds::kName) |
| 292 return SetWindowBounds(id, params); |
| 293 return nullptr; |
| 294 } |
| 295 |
76 class ChromeDevToolsManagerDelegate::HostData { | 296 class ChromeDevToolsManagerDelegate::HostData { |
77 public: | 297 public: |
78 HostData() {} | 298 HostData() {} |
79 ~HostData() {} | 299 ~HostData() {} |
80 | 300 |
81 RemoteLocations& remote_locations() { return remote_locations_; } | 301 RemoteLocations& remote_locations() { return remote_locations_; } |
82 | 302 |
83 void set_remote_locations(RemoteLocations& locations) { | 303 void set_remote_locations(RemoteLocations& locations) { |
84 remote_locations_.swap(locations); | 304 remote_locations_.swap(locations); |
85 } | 305 } |
(...skipping 19 matching lines...) Expand all Loading... |
105 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 325 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
106 DevToolsAgentHost* agent_host, | 326 DevToolsAgentHost* agent_host, |
107 base::DictionaryValue* command_dict) { | 327 base::DictionaryValue* command_dict) { |
108 | 328 |
109 int id = 0; | 329 int id = 0; |
110 std::string method; | 330 std::string method; |
111 base::DictionaryValue* params = nullptr; | 331 base::DictionaryValue* params = nullptr; |
112 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) | 332 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
113 return nullptr; | 333 return nullptr; |
114 | 334 |
| 335 if (agent_host->GetType() == DevToolsAgentHost::kTypeBrowser && |
| 336 method.find("Browser.") == 0) |
| 337 return HandleBrowserCommand(id, method, params).release(); |
| 338 |
115 if (method == chrome::devtools::Target::setRemoteLocations::kName) | 339 if (method == chrome::devtools::Target::setRemoteLocations::kName) |
116 return SetRemoteLocations(agent_host, id, params).release(); | 340 return SetRemoteLocations(agent_host, id, params).release(); |
117 | 341 |
118 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 342 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
119 } | 343 } |
120 | 344 |
121 std::string ChromeDevToolsManagerDelegate::GetTargetType( | 345 std::string ChromeDevToolsManagerDelegate::GetTargetType( |
122 content::RenderFrameHost* host) { | 346 content::RenderFrameHost* host) { |
123 content::WebContents* web_contents = | 347 content::WebContents* web_contents = |
124 content::WebContents::FromRenderFrameHost(host); | 348 content::WebContents::FromRenderFrameHost(host); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { | 501 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { |
278 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, | 502 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, |
279 kLocationsParam); | 503 kLocationsParam); |
280 } | 504 } |
281 tcp_locations.insert(net::HostPortPair(host, port)); | 505 tcp_locations.insert(net::HostPortPair(host, port)); |
282 } | 506 } |
283 | 507 |
284 host_data_[agent_host]->set_remote_locations(tcp_locations); | 508 host_data_[agent_host]->set_remote_locations(tcp_locations); |
285 UpdateDeviceDiscovery(); | 509 UpdateDeviceDiscovery(); |
286 | 510 |
287 std::unique_ptr<base::DictionaryValue> result( | 511 return DevToolsProtocol::CreateSuccessResponse(command_id, nullptr); |
288 base::MakeUnique<base::DictionaryValue>()); | |
289 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); | |
290 } | 512 } |
OLD | NEW |