Chromium Code Reviews| 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 char kLocationsParam[] = "locations"; | 42 char kLocationsParam[] = "locations"; |
| 38 char kHostParam[] = "host"; | 43 char kHostParam[] = "host"; |
| 39 char kPortParam[] = "port"; | 44 char kPortParam[] = "port"; |
| 40 | 45 |
| 46 namespace { | |
| 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 | |
| 85 } // namespace | |
| 86 | |
| 87 // static | |
| 88 std::unique_ptr<base::DictionaryValue> | |
| 89 ChromeDevToolsManagerDelegate::GetWindowForTarget( | |
| 90 int id, | |
| 91 base::DictionaryValue* params) { | |
| 92 std::string target_id; | |
| 93 if (!params->GetString("targetId", &target_id)) | |
| 94 return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); | |
| 95 | |
| 96 Browser* browser = nullptr; | |
| 97 scoped_refptr<DevToolsAgentHost> host = | |
| 98 DevToolsAgentHost::GetForId(target_id); | |
| 99 if (!host) | |
| 100 return DevToolsProtocol::CreateErrorResponse(id, "No target with given id"); | |
| 101 content::WebContents* web_contents = host->GetWebContents(); | |
| 102 if (!web_contents) { | |
| 103 return DevToolsProtocol::CreateErrorResponse( | |
| 104 id, "No web contents in the target"); | |
| 105 } | |
| 106 for (auto* b : *BrowserList::GetInstance()) { | |
| 107 int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); | |
| 108 if (tab_index != TabStripModel::kNoTab) | |
| 109 browser = b; | |
| 110 } | |
| 111 if (!browser) { | |
| 112 return DevToolsProtocol::CreateErrorResponse(id, | |
| 113 "Browser window not found"); | |
| 114 } | |
| 115 | |
| 116 auto result = base::MakeUnique<base::DictionaryValue>(); | |
| 117 result->SetInteger("windowId", browser->session_id().id()); | |
| 118 result->Set("bounds", GetBounds(browser->window())); | |
| 119 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 120 } | |
| 121 | |
| 122 // static | |
| 123 std::unique_ptr<base::DictionaryValue> | |
| 124 ChromeDevToolsManagerDelegate::GetWindowBounds(int id, | |
| 125 base::DictionaryValue* params) { | |
| 126 int window_id; | |
| 127 if (!params->GetInteger("windowId", &window_id)) | |
| 128 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 129 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 130 if (!window) { | |
| 131 return DevToolsProtocol::CreateErrorResponse(id, | |
| 132 "Browser window not found"); | |
| 133 } | |
| 134 | |
| 135 auto result = base::MakeUnique<base::DictionaryValue>(); | |
| 136 result->Set("bounds", GetBounds(window)); | |
| 137 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 138 } | |
| 139 | |
| 140 // static | |
| 141 std::unique_ptr<base::DictionaryValue> | |
| 142 ChromeDevToolsManagerDelegate::SetWindowBounds(int id, | |
| 143 base::DictionaryValue* params) { | |
| 144 int window_id; | |
| 145 if (!params->GetInteger("windowId", &window_id)) | |
| 146 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 147 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 148 if (!window) { | |
| 149 return DevToolsProtocol::CreateErrorResponse(id, | |
| 150 "Browser window not found"); | |
| 151 } | |
| 152 | |
| 153 const base::Value* value = nullptr; | |
| 154 const base::DictionaryValue* bounds_dict = nullptr; | |
| 155 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) | |
| 156 return DevToolsProtocol::CreateInvalidParamsResponse(id, "bounds"); | |
| 157 | |
| 158 std::string window_state; | |
| 159 if (!bounds_dict->GetString("windowState", &window_state)) | |
| 160 window_state = "normal"; | |
| 161 else if (window_state != "normal" && window_state != "minimized" && | |
| 162 window_state != "maximized" && window_state != "fullscreen") | |
| 163 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowState"); | |
| 164 | |
| 165 // Compute updated bounds when window state is normal. | |
| 166 bool set_bounds = false; | |
| 167 gfx::Rect bounds; | |
| 168 if (window->IsMinimized()) | |
| 169 bounds = window->GetRestoredBounds(); | |
| 170 else | |
| 171 bounds = window->GetBounds(); | |
| 172 | |
| 173 int left, top, width, height; | |
| 174 if (bounds_dict->GetInteger("left", &left)) { | |
| 175 bounds.set_x(left); | |
| 176 set_bounds = true; | |
| 177 } | |
| 178 if (bounds_dict->GetInteger("top", &top)) { | |
| 179 bounds.set_y(top); | |
| 180 set_bounds = true; | |
| 181 } | |
| 182 if (bounds_dict->GetInteger("width", &width)) { | |
| 183 if (width < 0) | |
| 184 return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); | |
| 185 bounds.set_width(width); | |
| 186 set_bounds = true; | |
| 187 } | |
| 188 if (bounds_dict->GetInteger("height", &height)) { | |
| 189 if (height < 0) | |
| 190 return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); | |
| 191 bounds.set_height(height); | |
| 192 set_bounds = true; | |
| 193 } | |
| 194 | |
| 195 if (set_bounds && window_state != "normal") { | |
| 196 return DevToolsProtocol::CreateErrorResponse( | |
| 197 id, | |
| 198 "The 'minimized', 'maximized' and 'fullscreen' states cannot be " | |
| 199 "combined with 'left', 'top', 'width' or 'height'"); | |
| 200 } | |
| 201 | |
| 202 if (window->IsFullscreen()) { | |
| 203 if (window_state == "minimized" || window_state == "maximized" || | |
| 204 set_bounds) { | |
| 205 return DevToolsProtocol::CreateErrorResponse( | |
| 206 id, | |
| 207 "Browser window is in fullscreen mode, can not be minimized, " | |
|
dgozman
2017/04/06 21:56:32
To resize fullscreen window, restore it to normal
jzfeng
2017/04/07 04:18:57
Done.
| |
| 208 "maximized or resized."); | |
| 209 } | |
| 210 if (window_state == "normal") | |
| 211 window->GetExclusiveAccessContext()->ExitFullscreen(); | |
| 212 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); | |
| 213 } | |
| 214 | |
| 215 if (window->IsMinimized()) { | |
| 216 if (window_state == "fullscreen" || window_state == "maximized" || | |
| 217 set_bounds) { | |
| 218 return DevToolsProtocol::CreateErrorResponse( | |
| 219 id, | |
| 220 "Browser window is minimized, can not enter fullscreen, be maximized " | |
| 221 "or resized."); | |
|
dgozman
2017/04/06 21:56:32
To resize minimized window, restore it to normal s
jzfeng
2017/04/07 04:18:56
Done.
| |
| 222 } | |
| 223 if (window_state == "normal") | |
| 224 window->Show(); | |
| 225 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); | |
| 226 } | |
| 227 | |
| 228 // current window is normal or maximized. | |
|
dgozman
2017/04/06 21:56:32
I find this code hard to read now. Let's structure
jzfeng
2017/04/07 04:18:56
Done.
| |
| 229 if (window_state == "minimized") { | |
| 230 window->Minimize(); | |
| 231 } else if (window_state == "maximized") { | |
| 232 window->Maximize(); | |
| 233 } else if (window_state == "fullscreen") { | |
| 234 window->GetExclusiveAccessContext()->EnterFullscreen( | |
| 235 GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); | |
| 236 } else { | |
| 237 if (window->IsMaximized() && set_bounds) { | |
| 238 return DevToolsProtocol::CreateErrorResponse( | |
| 239 id, "Browser window is maximized, can not be resized."); | |
| 240 } | |
| 241 if (window->IsMaximized()) | |
| 242 window->Restore(); | |
| 243 if (set_bounds) | |
| 244 window->SetBounds(bounds); | |
| 245 } | |
| 246 | |
| 247 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); | |
| 248 } | |
| 249 | |
| 250 std::unique_ptr<base::DictionaryValue> | |
| 251 ChromeDevToolsManagerDelegate::HandleBrowserCommand( | |
| 252 int id, | |
| 253 std::string method, | |
| 254 base::DictionaryValue* params) { | |
| 255 if (method == chrome::devtools::Browser::getWindowForTarget::kName) | |
| 256 return GetWindowForTarget(id, params); | |
| 257 if (method == chrome::devtools::Browser::getWindowBounds::kName) | |
| 258 return GetWindowBounds(id, params); | |
| 259 if (method == chrome::devtools::Browser::setWindowBounds::kName) | |
| 260 return SetWindowBounds(id, params); | |
| 261 return nullptr; | |
| 262 } | |
| 263 | |
| 41 class ChromeDevToolsManagerDelegate::HostData { | 264 class ChromeDevToolsManagerDelegate::HostData { |
| 42 public: | 265 public: |
| 43 HostData() {} | 266 HostData() {} |
| 44 ~HostData() {} | 267 ~HostData() {} |
| 45 | 268 |
| 46 RemoteLocations& remote_locations() { return remote_locations_; } | 269 RemoteLocations& remote_locations() { return remote_locations_; } |
| 47 | 270 |
| 48 void set_remote_locations(RemoteLocations& locations) { | 271 void set_remote_locations(RemoteLocations& locations) { |
| 49 remote_locations_.swap(locations); | 272 remote_locations_.swap(locations); |
| 50 } | 273 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 70 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 293 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| 71 DevToolsAgentHost* agent_host, | 294 DevToolsAgentHost* agent_host, |
| 72 base::DictionaryValue* command_dict) { | 295 base::DictionaryValue* command_dict) { |
| 73 | 296 |
| 74 int id = 0; | 297 int id = 0; |
| 75 std::string method; | 298 std::string method; |
| 76 base::DictionaryValue* params = nullptr; | 299 base::DictionaryValue* params = nullptr; |
| 77 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) | 300 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
| 78 return nullptr; | 301 return nullptr; |
| 79 | 302 |
| 303 if (agent_host->GetType() == DevToolsAgentHost::kTypeBrowser && | |
| 304 method.find("Browser.") == 0) | |
| 305 return HandleBrowserCommand(id, method, params).release(); | |
| 306 | |
| 80 if (method == chrome::devtools::Target::setRemoteLocations::kName) | 307 if (method == chrome::devtools::Target::setRemoteLocations::kName) |
| 81 return SetRemoteLocations(agent_host, id, params).release(); | 308 return SetRemoteLocations(agent_host, id, params).release(); |
| 82 | 309 |
| 83 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 310 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 84 } | 311 } |
| 85 | 312 |
| 86 std::string ChromeDevToolsManagerDelegate::GetTargetType( | 313 std::string ChromeDevToolsManagerDelegate::GetTargetType( |
| 87 content::RenderFrameHost* host) { | 314 content::RenderFrameHost* host) { |
| 88 content::WebContents* web_contents = | 315 content::WebContents* web_contents = |
| 89 content::WebContents::FromRenderFrameHost(host); | 316 content::WebContents::FromRenderFrameHost(host); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { | 496 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { |
| 270 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, | 497 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, |
| 271 kLocationsParam); | 498 kLocationsParam); |
| 272 } | 499 } |
| 273 tcp_locations.insert(net::HostPortPair(host, port)); | 500 tcp_locations.insert(net::HostPortPair(host, port)); |
| 274 } | 501 } |
| 275 | 502 |
| 276 host_data_[agent_host]->set_remote_locations(tcp_locations); | 503 host_data_[agent_host]->set_remote_locations(tcp_locations); |
| 277 UpdateDeviceDiscovery(); | 504 UpdateDeviceDiscovery(); |
| 278 | 505 |
| 279 std::unique_ptr<base::DictionaryValue> result( | 506 return DevToolsProtocol::CreateSuccessResponse(command_id, nullptr); |
| 280 base::MakeUnique<base::DictionaryValue>()); | |
| 281 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); | |
| 282 } | 507 } |
| OLD | NEW |