Chromium Code Reviews| Index: chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| diff --git a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| index 101d544a6be728d7695c7639e35c8e0173ba8192..fde6686e6be3d2fc8e74481e970ec32654e08aaa 100644 |
| --- a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| +++ b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| @@ -17,9 +17,12 @@ |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/browser/ui/browser_navigator.h" |
| #include "chrome/browser/ui/browser_navigator_params.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/grit/browser_resources.h" |
| #include "components/guest_view/browser/guest_view_base.h" |
| +#include "components/ui_devtools/string_util.h" |
| #include "content/public/browser/devtools_agent_host.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/web_contents.h" |
| @@ -38,6 +41,161 @@ char kLocationsParam[] = "locations"; |
| char kHostParam[] = "host"; |
| char kPortParam[] = "port"; |
| +BrowserWindow* GetBrowserWindow(base::DictionaryValue* params) { |
| + if (!params) |
| + return nullptr; |
| + int window_id; |
| + if (!params->GetInteger("windowId", &window_id)) |
| + return nullptr; |
| + for (auto* b : *BrowserList::GetInstance()) { |
| + if (b->session_id().id() == window_id) |
| + return b->window(); |
| + } |
| + return nullptr; |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> HandleUICommand( |
| + content::DevToolsAgentHost* agent_host, |
| + int id, |
| + std::string method, |
| + base::DictionaryValue* params) { |
| + if (method == chrome::devtools::UI::getWindowFromTarget::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + std::string target_id; |
| + if (!params->GetString( |
| + chrome::devtools::UI::getWindowFromTarget::kParamTargetId, |
| + &target_id)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); |
| + |
| + Browser* browser = nullptr; |
| + scoped_refptr<DevToolsAgentHost> host = |
| + DevToolsAgentHost::GetForId(target_id); |
|
dgozman
2017/03/20 22:00:48
if (!host)
CreateErrorResponse(id, "No target wi
jzfeng
2017/03/21 09:13:32
Done.
|
| + content::WebContents* web_contents = |
| + host ? host->GetWebContents() : nullptr; |
| + for (auto* b : *BrowserList::GetInstance()) { |
| + int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); |
| + if (tab_index != TabStripModel::kNoTab) |
| + browser = b; |
| + } |
| + if (!browser) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + result->SetInteger("windowId", browser->session_id().id()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + |
| + if (method == chrome::devtools::UI::maximizeWindow::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + BrowserWindow* window = GetBrowserWindow(params); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + window->Maximize(); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + if (method == chrome::devtools::UI::minimizeWindow::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + BrowserWindow* window = GetBrowserWindow(params); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + window->Minimize(); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + if (method == chrome::devtools::UI::getWindowBounds::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + BrowserWindow* window = GetBrowserWindow(params); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + gfx::Rect bounds; |
| + if (window->IsMinimized()) |
| + bounds = window->GetRestoredBounds(); |
| + else |
| + bounds = window->GetBounds(); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + result->SetInteger("left", bounds.x()); |
| + result->SetInteger("top", bounds.y()); |
| + result->SetInteger("width", bounds.width()); |
| + result->SetInteger("height", bounds.height()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + if (method == chrome::devtools::UI::setWindowBounds::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + BrowserWindow* window = GetBrowserWindow(params); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + gfx::Rect bounds; |
| + if (window->IsMinimized()) |
| + bounds = window->GetRestoredBounds(); |
| + else |
| + bounds = window->GetBounds(); |
| + |
| + namespace names = chrome::devtools::UI::setWindowBounds; |
| + int left, top, width, height; |
| + bool set_bounds = false; |
| + // Any part of the bounds can optionally be set by the caller. |
| + if (params->GetInteger(names::kParamLeft, &left)) { |
| + bounds.set_x(left); |
| + set_bounds = true; |
| + } |
| + if (params->GetInteger(names::kParamTop, &top)) { |
| + bounds.set_y(top); |
| + set_bounds = true; |
| + } |
| + if (params->GetInteger(names::kParamWidth, &width)) { |
| + if (width < 0) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); |
| + bounds.set_width(width); |
| + set_bounds = true; |
| + } |
| + if (params->GetInteger(names::kParamHeight, &height)) { |
| + if (height < 0) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); |
| + bounds.set_height(height); |
| + set_bounds = true; |
| + } |
| + |
| + if (set_bounds) |
| + window->SetBounds(bounds); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + if (method == chrome::devtools::UI::setWindowFullscreen::kName) { |
| + if (!params) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "params"); |
| + BrowserWindow* window = GetBrowserWindow(params); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "browser window not found"); |
| + bool fullscreen = false; |
| + if (!params->GetBoolean( |
| + chrome::devtools::UI::setWindowFullscreen::kParamFullscreen, |
| + &fullscreen)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "fullscreen"); |
| + window->SetFullscreen(fullscreen); |
|
dgozman
2017/03/20 22:00:48
Why not just call window->GetExclusiveAccessContex
jzfeng
2017/03/21 09:13:32
Good point! Done.
|
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| + } |
| + return nullptr; |
| +} |
| + |
| class ChromeDevToolsManagerDelegate::HostData { |
| public: |
| HostData() {} |
| @@ -77,6 +235,14 @@ base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
| return nullptr; |
| + using stringUtil = ::ui::devtools::protocol::StringUtil; |
| + size_t dotIndex = stringUtil::find(method, "."); |
| + if (dotIndex == stringUtil::kNotFound) |
| + return nullptr; |
| + std::string domain = stringUtil::substring(method, 0, dotIndex); |
| + if (domain == chrome::devtools::UI::kName) |
|
dgozman
2017/03/20 22:00:48
if (method.find("UI.") == 0)
jzfeng
2017/03/21 09:13:32
Done.
|
| + return HandleUICommand(agent_host, id, method, params).release(); |
| + |
| if (method == chrome::devtools::Target::setRemoteLocations::kName) |
| return SetRemoteLocations(agent_host, id, params).release(); |