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 "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/devtools/device/android_device_manager.h" | 10 #include "chrome/browser/devtools/device/android_device_manager.h" |
| 11 #include "chrome/browser/devtools/device/tcp_device_provider.h" | 11 #include "chrome/browser/devtools/device/tcp_device_provider.h" |
| 12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" | 12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" |
| 13 #include "chrome/browser/devtools/devtools_protocol_constants.h" | 13 #include "chrome/browser/devtools/devtools_protocol_constants.h" |
| 14 #include "chrome/browser/devtools/devtools_window.h" | 14 #include "chrome/browser/devtools/devtools_window.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "chrome/browser/ui/browser_navigator.h" | 18 #include "chrome/browser/ui/browser_navigator.h" |
| 19 #include "chrome/browser/ui/browser_navigator_params.h" | 19 #include "chrome/browser/ui/browser_navigator_params.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | |
| 21 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" | |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 21 #include "chrome/grit/browser_resources.h" | 24 #include "chrome/grit/browser_resources.h" |
| 22 #include "components/guest_view/browser/guest_view_base.h" | 25 #include "components/guest_view/browser/guest_view_base.h" |
| 23 #include "content/public/browser/devtools_agent_host.h" | 26 #include "content/public/browser/devtools_agent_host.h" |
| 24 #include "content/public/browser/render_frame_host.h" | 27 #include "content/public/browser/render_frame_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 26 #include "extensions/browser/extension_host.h" | 29 #include "extensions/browser/extension_host.h" |
| 27 #include "extensions/browser/extension_registry.h" | 30 #include "extensions/browser/extension_registry.h" |
| 28 #include "extensions/browser/process_manager.h" | 31 #include "extensions/browser/process_manager.h" |
| 29 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| 30 | 33 |
| 31 using content::DevToolsAgentHost; | 34 using content::DevToolsAgentHost; |
| 32 | 35 |
| 33 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; | 36 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; |
| 34 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; | 37 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; |
| 35 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; | 38 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; |
| 36 | 39 |
| 37 char kLocationsParam[] = "locations"; | 40 char kLocationsParam[] = "locations"; |
| 38 char kHostParam[] = "host"; | 41 char kHostParam[] = "host"; |
| 39 char kPortParam[] = "port"; | 42 char kPortParam[] = "port"; |
| 40 | 43 |
| 44 namespace { | |
| 45 | |
| 46 BrowserWindow* GetBrowserWindow(int window_id) { | |
| 47 for (auto* b : *BrowserList::GetInstance()) { | |
| 48 if (b->session_id().id() == window_id) | |
| 49 return b->window(); | |
| 50 } | |
| 51 return nullptr; | |
| 52 } | |
| 53 | |
| 54 base::DictionaryValue* GetBounds(BrowserWindow* window) { | |
| 55 base::DictionaryValue* bounds_object = new base::DictionaryValue(); | |
| 56 gfx::Rect bounds; | |
| 57 if (window->IsMinimized()) | |
| 58 bounds = window->GetRestoredBounds(); | |
| 59 else | |
| 60 bounds = window->GetBounds(); | |
| 61 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 62 bounds_object->SetInteger("left", bounds.x()); | |
| 63 bounds_object->SetInteger("top", bounds.y()); | |
| 64 bounds_object->SetInteger("width", bounds.width()); | |
| 65 bounds_object->SetInteger("height", bounds.height()); | |
| 66 | |
| 67 std::string window_state = "normal"; | |
| 68 if (window->IsMinimized()) | |
| 69 window_state = "minimized"; | |
| 70 if (window->IsMaximized()) | |
| 71 window_state = "maximized"; | |
| 72 if (window->IsFullscreen()) | |
| 73 window_state = "fullscreen"; | |
| 74 bounds_object->SetString("windowState", window_state); | |
| 75 return bounds_object; | |
| 76 } | |
| 77 | |
| 78 std::unique_ptr<base::DictionaryValue> HandleBrowserCommand( | |
| 79 int id, | |
| 80 std::string method, | |
| 81 base::DictionaryValue* params) { | |
| 82 if (method == chrome::devtools::Browser::getWindowForTarget::kName) | |
| 83 return ChromeDevToolsManagerDelegate::GetWindowForTarget(id, params); | |
| 84 if (method == chrome::devtools::Browser::getWindowBounds::kName) | |
| 85 return ChromeDevToolsManagerDelegate::GetWindowBounds(id, params); | |
| 86 if (method == chrome::devtools::Browser::setWindowBounds::kName) | |
| 87 return ChromeDevToolsManagerDelegate::SetWindowBounds(id, params); | |
| 88 return nullptr; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 // static | |
| 94 std::unique_ptr<base::DictionaryValue> | |
| 95 ChromeDevToolsManagerDelegate::GetWindowForTarget( | |
| 96 int id, | |
| 97 base::DictionaryValue* params) { | |
| 98 std::string target_id; | |
| 99 if (!params->GetString("targetId", &target_id)) | |
| 100 return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); | |
| 101 | |
| 102 Browser* browser = nullptr; | |
| 103 scoped_refptr<DevToolsAgentHost> host = | |
| 104 DevToolsAgentHost::GetForId(target_id); | |
| 105 if (!host) | |
| 106 return DevToolsProtocol::CreateErrorResponse(id, "No target with given id"); | |
| 107 content::WebContents* web_contents = host->GetWebContents(); | |
| 108 if (!web_contents) { | |
| 109 return DevToolsProtocol::CreateErrorResponse( | |
| 110 id, "No web contents in the target"); | |
| 111 } | |
| 112 for (auto* b : *BrowserList::GetInstance()) { | |
| 113 int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); | |
| 114 if (tab_index != TabStripModel::kNoTab) | |
| 115 browser = b; | |
| 116 } | |
| 117 if (!browser) { | |
| 118 return DevToolsProtocol::CreateErrorResponse(id, | |
| 119 "Browser window not found"); | |
| 120 } | |
| 121 | |
| 122 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 123 result->SetInteger("windowId", browser->session_id().id()); | |
| 124 result->Set("bounds", GetBounds(browser->window())); | |
| 125 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 126 } | |
| 127 | |
| 128 // static | |
| 129 std::unique_ptr<base::DictionaryValue> | |
| 130 ChromeDevToolsManagerDelegate::GetWindowBounds(int id, | |
| 131 base::DictionaryValue* params) { | |
| 132 int window_id; | |
| 133 if (!params->GetInteger("windowId", &window_id)) | |
| 134 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 135 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 136 if (!window) { | |
| 137 return DevToolsProtocol::CreateErrorResponse(id, | |
| 138 "Browser window not found"); | |
| 139 } | |
| 140 | |
| 141 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 142 result->Set("bounds", GetBounds(window)); | |
| 143 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 144 } | |
| 145 | |
| 146 // static | |
| 147 std::unique_ptr<base::DictionaryValue> | |
| 148 ChromeDevToolsManagerDelegate::SetWindowBounds(int id, | |
| 149 base::DictionaryValue* params) { | |
| 150 int window_id; | |
| 151 if (!params->GetInteger("windowId", &window_id)) | |
| 152 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 153 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 154 if (!window) { | |
| 155 return DevToolsProtocol::CreateErrorResponse(id, | |
| 156 "Browser window not found"); | |
| 157 } | |
| 158 base::Value* value = nullptr; | |
| 159 base::DictionaryValue* bounds_dict = nullptr; | |
| 160 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) | |
| 161 return DevToolsProtocol::CreateInvalidParamsResponse(id, "bounds"); | |
| 162 | |
| 163 std::string window_state; | |
| 164 if (!bounds_dict->GetString("windowState", &window_state)) | |
| 165 window_state = "normal"; | |
| 166 else if (window_state != "normal" && window_state != "minimized" && | |
| 167 window_state != "maximized" && window_state != "fullscreen") | |
| 168 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowState"); | |
| 169 | |
| 170 if (window_state != "fullscreen" && window->IsFullscreen()) | |
| 171 window->GetExclusiveAccessContext()->ExitFullscreen(); | |
| 172 if (window_state != "minimized" && window->IsMinimized()) | |
| 173 window->Show(); | |
| 174 | |
| 175 if (window_state == "minimized") { | |
| 176 window->Minimize(); | |
| 177 } else if (window_state == "maximized") { | |
| 178 window->Maximize(); | |
| 179 } else if (window_state == "fullscreen") { | |
| 180 window->GetExclusiveAccessContext()->EnterFullscreen( | |
| 181 GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); | |
| 182 } else if (window->IsMaximized()) { | |
|
dgozman
2017/03/23 22:56:01
|| window->IsMinimized()
jzfeng
2017/03/24 02:24:40
Done.
| |
| 183 window->Restore(); | |
| 184 } | |
| 185 | |
| 186 // compute updated bounds when window state is normal. | |
| 187 bool set_bounds = false; | |
| 188 gfx::Rect bounds; | |
| 189 if (window->IsMinimized() || window->IsMaximized()) | |
| 190 bounds = window->GetRestoredBounds(); | |
|
dgozman
2017/03/23 22:56:01
If minimize/maximize are synchronous, this should
jzfeng
2017/03/24 02:24:40
Done. Looks like it is async on linux. So I moved
| |
| 191 else | |
| 192 bounds = window->GetBounds(); | |
| 193 | |
| 194 int left, top, width, height; | |
| 195 if (bounds_dict->GetInteger("left", &left)) { | |
| 196 bounds.set_x(left); | |
| 197 set_bounds = true; | |
| 198 } | |
| 199 if (bounds_dict->GetInteger("top", &top)) { | |
| 200 bounds.set_y(top); | |
| 201 set_bounds = true; | |
| 202 } | |
| 203 if (bounds_dict->GetInteger("width", &width)) { | |
| 204 if (width < 0) | |
| 205 return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); | |
| 206 bounds.set_width(width); | |
| 207 set_bounds = true; | |
| 208 } | |
| 209 if (bounds_dict->GetInteger("height", &height)) { | |
| 210 if (height < 0) | |
| 211 return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); | |
| 212 bounds.set_height(height); | |
| 213 set_bounds = true; | |
| 214 } | |
| 215 | |
| 216 if (set_bounds && window_state != "normal") { | |
| 217 return DevToolsProtocol::CreateErrorResponse( | |
| 218 id, | |
| 219 "The 'minimized', 'maximized' and 'fullscreen' states cannot be " | |
| 220 "combined with 'left', 'top', 'width' or 'height'"); | |
| 221 } | |
| 222 | |
| 223 if (set_bounds) | |
| 224 window->SetBounds(bounds); | |
| 225 | |
| 226 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); | |
| 227 } | |
| 228 | |
| 41 class ChromeDevToolsManagerDelegate::HostData { | 229 class ChromeDevToolsManagerDelegate::HostData { |
| 42 public: | 230 public: |
| 43 HostData() {} | 231 HostData() {} |
| 44 ~HostData() {} | 232 ~HostData() {} |
| 45 | 233 |
| 46 RemoteLocations& remote_locations() { return remote_locations_; } | 234 RemoteLocations& remote_locations() { return remote_locations_; } |
| 47 | 235 |
| 48 void set_remote_locations(RemoteLocations& locations) { | 236 void set_remote_locations(RemoteLocations& locations) { |
| 49 remote_locations_.swap(locations); | 237 remote_locations_.swap(locations); |
| 50 } | 238 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 70 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 258 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| 71 DevToolsAgentHost* agent_host, | 259 DevToolsAgentHost* agent_host, |
| 72 base::DictionaryValue* command_dict) { | 260 base::DictionaryValue* command_dict) { |
| 73 | 261 |
| 74 int id = 0; | 262 int id = 0; |
| 75 std::string method; | 263 std::string method; |
| 76 base::DictionaryValue* params = nullptr; | 264 base::DictionaryValue* params = nullptr; |
| 77 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) | 265 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
| 78 return nullptr; | 266 return nullptr; |
| 79 | 267 |
| 268 if (agent_host->GetType() == DevToolsAgentHost::kTypeBrowser && | |
| 269 method.find("Browser.") == 0) | |
| 270 return HandleBrowserCommand(id, method, params).release(); | |
| 271 | |
| 80 if (method == chrome::devtools::Target::setRemoteLocations::kName) | 272 if (method == chrome::devtools::Target::setRemoteLocations::kName) |
| 81 return SetRemoteLocations(agent_host, id, params).release(); | 273 return SetRemoteLocations(agent_host, id, params).release(); |
| 82 | 274 |
| 83 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 275 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 84 } | 276 } |
| 85 | 277 |
| 86 std::string ChromeDevToolsManagerDelegate::GetTargetType( | 278 std::string ChromeDevToolsManagerDelegate::GetTargetType( |
| 87 content::RenderFrameHost* host) { | 279 content::RenderFrameHost* host) { |
| 88 content::WebContents* web_contents = | 280 content::WebContents* web_contents = |
| 89 content::WebContents::FromRenderFrameHost(host); | 281 content::WebContents::FromRenderFrameHost(host); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { | 461 if (!dictionary->GetIntegerWithoutPathExpansion(kPortParam, &port)) { |
| 270 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, | 462 return DevToolsProtocol::CreateInvalidParamsResponse(command_id, |
| 271 kLocationsParam); | 463 kLocationsParam); |
| 272 } | 464 } |
| 273 tcp_locations.insert(net::HostPortPair(host, port)); | 465 tcp_locations.insert(net::HostPortPair(host, port)); |
| 274 } | 466 } |
| 275 | 467 |
| 276 host_data_[agent_host]->set_remote_locations(tcp_locations); | 468 host_data_[agent_host]->set_remote_locations(tcp_locations); |
| 277 UpdateDeviceDiscovery(); | 469 UpdateDeviceDiscovery(); |
| 278 | 470 |
| 279 std::unique_ptr<base::DictionaryValue> result( | 471 return DevToolsProtocol::CreateSuccessResponse(command_id, nullptr); |
| 280 base::MakeUnique<base::DictionaryValue>()); | |
| 281 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); | |
| 282 } | 472 } |
| OLD | NEW |