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 BrowserWindow* GetBrowserWindow(int window_id) { | |
| 45 for (auto* b : *BrowserList::GetInstance()) { | |
| 46 if (b->session_id().id() == window_id) | |
| 47 return b->window(); | |
| 48 } | |
| 49 return nullptr; | |
| 50 } | |
| 51 | |
| 52 std::unique_ptr<base::DictionaryValue> HandleUICommand( | |
|
pfeldman
2017/03/21 17:36:02
dgozman: is this time to instantiate inspector_pro
jzfeng
2017/03/22 06:56:09
Done.
| |
| 53 content::DevToolsAgentHost* agent_host, | |
| 54 int id, | |
| 55 std::string method, | |
| 56 base::DictionaryValue* params) { | |
| 57 if (method == chrome::devtools::UI::getWindowFromTarget::kName) { | |
| 58 std::string target_id; | |
| 59 if (!params->GetString("targetId", &target_id)) | |
| 60 return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); | |
| 61 | |
| 62 Browser* browser = nullptr; | |
| 63 scoped_refptr<DevToolsAgentHost> host = | |
| 64 DevToolsAgentHost::GetForId(target_id); | |
| 65 if (!host) | |
| 66 return DevToolsProtocol::CreateErrorResponse(id, | |
| 67 "No target with given id"); | |
| 68 content::WebContents* web_contents = host->GetWebContents(); | |
|
pfeldman
2017/03/21 17:36:02
This is nullable, no need to run getIndexOfWebCont
jzfeng
2017/03/22 06:56:09
Done.
| |
| 69 for (auto* b : *BrowserList::GetInstance()) { | |
| 70 int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); | |
| 71 if (tab_index != TabStripModel::kNoTab) | |
| 72 browser = b; | |
| 73 } | |
| 74 if (!browser) | |
| 75 return DevToolsProtocol::CreateErrorResponse(id, | |
| 76 "Browser window not found"); | |
| 77 | |
| 78 std::unique_ptr<base::DictionaryValue> result( | |
| 79 base::MakeUnique<base::DictionaryValue>()); | |
| 80 result->SetInteger("windowId", browser->session_id().id()); | |
| 81 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 82 } | |
| 83 | |
| 84 if (method == chrome::devtools::UI::maximizeWindow::kName) { | |
| 85 int window_id; | |
| 86 if (!params->GetInteger("windowId", &window_id)) | |
| 87 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 88 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 89 if (!window) | |
| 90 return DevToolsProtocol::CreateErrorResponse(id, | |
| 91 "browser window not found"); | |
| 92 window->Maximize(); | |
| 93 std::unique_ptr<base::DictionaryValue> result( | |
| 94 base::MakeUnique<base::DictionaryValue>()); | |
| 95 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 96 } | |
| 97 if (method == chrome::devtools::UI::minimizeWindow::kName) { | |
| 98 int window_id; | |
| 99 if (!params->GetInteger("windowId", &window_id)) | |
| 100 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 101 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 102 if (!window) | |
| 103 return DevToolsProtocol::CreateErrorResponse(id, | |
| 104 "browser window not found"); | |
| 105 window->Minimize(); | |
| 106 std::unique_ptr<base::DictionaryValue> result( | |
| 107 base::MakeUnique<base::DictionaryValue>()); | |
| 108 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 109 } | |
| 110 if (method == chrome::devtools::UI::getWindowBounds::kName) { | |
| 111 int window_id; | |
| 112 if (!params->GetInteger("windowId", &window_id)) | |
| 113 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 114 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 115 if (!window) | |
| 116 return DevToolsProtocol::CreateErrorResponse(id, | |
| 117 "browser window not found"); | |
| 118 gfx::Rect bounds; | |
| 119 if (window->IsMinimized()) | |
| 120 bounds = window->GetRestoredBounds(); | |
| 121 else | |
| 122 bounds = window->GetBounds(); | |
| 123 std::unique_ptr<base::DictionaryValue> result( | |
| 124 base::MakeUnique<base::DictionaryValue>()); | |
| 125 result->SetInteger("left", bounds.x()); | |
| 126 result->SetInteger("top", bounds.y()); | |
| 127 result->SetInteger("width", bounds.width()); | |
| 128 result->SetInteger("height", bounds.height()); | |
| 129 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 130 } | |
| 131 if (method == chrome::devtools::UI::setWindowBounds::kName) { | |
| 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 gfx::Rect bounds; | |
| 140 if (window->IsMinimized()) | |
| 141 bounds = window->GetRestoredBounds(); | |
| 142 else | |
| 143 bounds = window->GetBounds(); | |
| 144 | |
| 145 int left, top, width, height; | |
| 146 bool set_bounds = false; | |
| 147 // Any part of the bounds can optionally be set by the caller. | |
| 148 if (params->GetInteger("left", &left)) { | |
| 149 bounds.set_x(left); | |
| 150 set_bounds = true; | |
| 151 } | |
| 152 if (params->GetInteger("top", &top)) { | |
| 153 bounds.set_y(top); | |
| 154 set_bounds = true; | |
| 155 } | |
| 156 if (params->GetInteger("width", &width)) { | |
| 157 if (width < 0) | |
| 158 return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); | |
| 159 bounds.set_width(width); | |
| 160 set_bounds = true; | |
| 161 } | |
| 162 if (params->GetInteger("height", &height)) { | |
| 163 if (height < 0) | |
| 164 return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); | |
| 165 bounds.set_height(height); | |
| 166 set_bounds = true; | |
| 167 } | |
| 168 | |
| 169 if (set_bounds) | |
| 170 window->SetBounds(bounds); | |
| 171 std::unique_ptr<base::DictionaryValue> result( | |
| 172 base::MakeUnique<base::DictionaryValue>()); | |
| 173 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 174 } | |
| 175 if (method == chrome::devtools::UI::setWindowFullscreen::kName) { | |
| 176 int window_id; | |
| 177 if (!params->GetInteger("windowId", &window_id)) | |
| 178 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
| 179 BrowserWindow* window = GetBrowserWindow(window_id); | |
| 180 if (!window) | |
| 181 return DevToolsProtocol::CreateErrorResponse(id, | |
| 182 "browser window not found"); | |
| 183 bool fullscreen = false; | |
| 184 if (!params->GetBoolean("fullscreen", &fullscreen)) | |
| 185 return DevToolsProtocol::CreateInvalidParamsResponse(id, "fullscreen"); | |
| 186 auto* context = window->GetExclusiveAccessContext(); | |
| 187 if (fullscreen) | |
| 188 context->EnterFullscreen(GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); | |
| 189 else | |
| 190 context->ExitFullscreen(); | |
| 191 std::unique_ptr<base::DictionaryValue> result( | |
| 192 base::MakeUnique<base::DictionaryValue>()); | |
| 193 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
| 194 } | |
| 195 return nullptr; | |
| 196 } | |
| 197 | |
| 41 class ChromeDevToolsManagerDelegate::HostData { | 198 class ChromeDevToolsManagerDelegate::HostData { |
| 42 public: | 199 public: |
| 43 HostData() {} | 200 HostData() {} |
| 44 ~HostData() {} | 201 ~HostData() {} |
| 45 | 202 |
| 46 RemoteLocations& remote_locations() { return remote_locations_; } | 203 RemoteLocations& remote_locations() { return remote_locations_; } |
| 47 | 204 |
| 48 void set_remote_locations(RemoteLocations& locations) { | 205 void set_remote_locations(RemoteLocations& locations) { |
| 49 remote_locations_.swap(locations); | 206 remote_locations_.swap(locations); |
| 50 } | 207 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 70 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 227 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| 71 DevToolsAgentHost* agent_host, | 228 DevToolsAgentHost* agent_host, |
| 72 base::DictionaryValue* command_dict) { | 229 base::DictionaryValue* command_dict) { |
| 73 | 230 |
| 74 int id = 0; | 231 int id = 0; |
| 75 std::string method; | 232 std::string method; |
| 76 base::DictionaryValue* params = nullptr; | 233 base::DictionaryValue* params = nullptr; |
| 77 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) | 234 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
| 78 return nullptr; | 235 return nullptr; |
| 79 | 236 |
| 237 if (method.find("UI.") == 0) | |
| 238 return HandleUICommand(agent_host, id, method, params).release(); | |
| 239 | |
| 80 if (method == chrome::devtools::Target::setRemoteLocations::kName) | 240 if (method == chrome::devtools::Target::setRemoteLocations::kName) |
| 81 return SetRemoteLocations(agent_host, id, params).release(); | 241 return SetRemoteLocations(agent_host, id, params).release(); |
| 82 | 242 |
| 83 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 243 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 84 } | 244 } |
| 85 | 245 |
| 86 std::string ChromeDevToolsManagerDelegate::GetTargetType( | 246 std::string ChromeDevToolsManagerDelegate::GetTargetType( |
| 87 content::RenderFrameHost* host) { | 247 content::RenderFrameHost* host) { |
| 88 content::WebContents* web_contents = | 248 content::WebContents* web_contents = |
| 89 content::WebContents::FromRenderFrameHost(host); | 249 content::WebContents::FromRenderFrameHost(host); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 tcp_locations.insert(net::HostPortPair(host, port)); | 433 tcp_locations.insert(net::HostPortPair(host, port)); |
| 274 } | 434 } |
| 275 | 435 |
| 276 host_data_[agent_host]->set_remote_locations(tcp_locations); | 436 host_data_[agent_host]->set_remote_locations(tcp_locations); |
| 277 UpdateDeviceDiscovery(); | 437 UpdateDeviceDiscovery(); |
| 278 | 438 |
| 279 std::unique_ptr<base::DictionaryValue> result( | 439 std::unique_ptr<base::DictionaryValue> result( |
| 280 base::MakeUnique<base::DictionaryValue>()); | 440 base::MakeUnique<base::DictionaryValue>()); |
| 281 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); | 441 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); |
| 282 } | 442 } |
| OLD | NEW |