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..f4cee146e6b45e5272c1d3a880f2cbee27f2c885 100644 |
| --- a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| +++ b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
| @@ -17,7 +17,10 @@ |
| #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/exclusive_access/exclusive_access_context.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 "content/public/browser/devtools_agent_host.h" |
| @@ -38,6 +41,177 @@ char kLocationsParam[] = "locations"; |
| char kHostParam[] = "host"; |
| char kPortParam[] = "port"; |
| +BrowserWindow* GetBrowserWindow(int window_id) { |
|
dgozman
2017/03/22 21:28:34
Place all these functions into a single anonymous
jzfeng
2017/03/23 06:58:25
Done.
|
| + for (auto* b : *BrowserList::GetInstance()) { |
| + if (b->session_id().id() == window_id) |
| + return b->window(); |
| + } |
| + return nullptr; |
| +} |
| + |
| +base::DictionaryValue* GetWindowBounds(BrowserWindow* window) { |
| + base::DictionaryValue* bounds_object = new base::DictionaryValue(); |
| + gfx::Rect bounds; |
| + if (window->IsMinimized()) |
| + bounds = window->GetRestoredBounds(); |
| + else |
| + bounds = window->GetBounds(); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + bounds_object->SetInteger("left", bounds.x()); |
| + bounds_object->SetInteger("top", bounds.y()); |
| + bounds_object->SetInteger("width", bounds.width()); |
| + bounds_object->SetInteger("height", bounds.height()); |
| + |
| + std::string window_state = "normal"; |
| + if (window->IsMinimized()) |
| + window_state = "minimized"; |
| + if (window->IsMaximized()) |
| + window_state = "maximized"; |
| + if (window->IsFullscreen()) |
| + window_state = "fullscreen"; |
| + bounds_object->SetString("windowState", window_state); |
| + return bounds_object; |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> |
| +GetWindowForTarget(int id, std::string method, base::DictionaryValue* params) { |
| + std::string target_id; |
| + if (!params->GetString("targetId", &target_id)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); |
| + |
| + Browser* browser = nullptr; |
| + scoped_refptr<DevToolsAgentHost> host = |
| + DevToolsAgentHost::GetForId(target_id); |
| + if (!host) |
| + return DevToolsProtocol::CreateErrorResponse(id, "No target with given id"); |
| + content::WebContents* web_contents = host->GetWebContents(); |
| + if (!web_contents) |
| + return DevToolsProtocol::CreateErrorResponse( |
|
dgozman
2017/03/22 21:28:34
style: when |if| body takes more than one line, wr
jzfeng
2017/03/23 06:58:25
Thanks for the tip! Done.
|
| + id, "No web contents in the target"); |
| + 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( |
|
dgozman
2017/03/22 21:28:34
You don't have to use MakeUnique when you have a v
jzfeng
2017/03/23 06:58:25
The explanation is very helpful! Done.
|
| + base::MakeUnique<base::DictionaryValue>()); |
| + result->SetInteger("windowId", browser->session_id().id()); |
| + result->Set("bounds", GetWindowBounds(browser->window())); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> |
| +GetWindowBounds(int id, std::string method, base::DictionaryValue* params) { |
| + int window_id; |
| + if (!params->GetInteger("windowId", &window_id)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); |
| + BrowserWindow* window = GetBrowserWindow(window_id); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "Browser window not found"); |
| + std::unique_ptr<base::DictionaryValue> result( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + result->Set("bounds", GetWindowBounds(window)); |
| + return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> |
| +SetWindowBounds(int id, std::string method, base::DictionaryValue* params) { |
| + int window_id; |
| + if (!params->GetInteger("windowId", &window_id)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); |
| + BrowserWindow* window = GetBrowserWindow(window_id); |
| + if (!window) |
| + return DevToolsProtocol::CreateErrorResponse(id, |
| + "Browser window not found"); |
| + |
| + base::Value* value = nullptr; |
| + base::DictionaryValue* bounds_dict = nullptr; |
| + if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "bounds"); |
| + |
| + std::string window_state; |
| + if (!bounds_dict->GetString("windowState", &window_state)) |
| + window_state = "normal"; |
| + else if (window_state != "normal" && window_state != "minimized" && |
| + window_state != "maximized" && window_state != "fullscreen") |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowState"); |
| + |
| + if (window_state != "fullscreen" && window->IsFullscreen()) |
| + window->GetExclusiveAccessContext()->ExitFullscreen(); |
| + |
| + if (window_state == "minimized") { |
| + window->Minimize(); |
| + } else if (window_state == "maximized") { |
| + window->Maximize(); |
| + } else if (window_state == "fullscreen") { |
| + if (window->IsMinimized() || window->IsMaximized()) |
| + window->Restore(); |
| + window->GetExclusiveAccessContext()->EnterFullscreen( |
| + GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); |
| + } else { |
| + window->Restore(); |
|
dgozman
2017/03/22 21:28:34
Is it ok to call Restore when not maximized nor mi
jzfeng
2017/03/23 06:58:25
Changed to restore only when the window is maximiz
|
| + } |
| + |
| + bool set_bounds = false; |
| + gfx::Rect bounds; |
| + if (window->IsMinimized()) |
|
dgozman
2017/03/22 21:28:34
|| window->IsMaximized() ?
jzfeng
2017/03/23 06:58:25
Done.
|
| + bounds = window->GetRestoredBounds(); |
| + else |
| + bounds = window->GetBounds(); |
| + |
| + int left, top, width, height; |
| + if (bounds_dict->GetInteger("left", &left)) { |
| + bounds.set_x(left); |
| + set_bounds = true; |
| + } |
| + if (bounds_dict->GetInteger("top", &top)) { |
| + bounds.set_y(top); |
| + set_bounds = true; |
| + } |
| + if (bounds_dict->GetInteger("width", &width)) { |
| + if (width < 0) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); |
| + bounds.set_width(width); |
| + set_bounds = true; |
| + } |
| + if (bounds_dict->GetInteger("height", &height)) { |
| + if (height < 0) |
| + return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); |
| + bounds.set_height(height); |
| + set_bounds = true; |
| + } |
| + |
| + if (set_bounds && window_state != "normal") |
| + return DevToolsProtocol::CreateErrorResponse( |
| + id, |
| + "The 'minimized', 'maximized' and 'fullscreen' states cannot be " |
| + "combined with 'left', 'top', 'width' or 'height'"); |
| + |
| + if (set_bounds) |
| + window->SetBounds(bounds); |
| + |
| + return DevToolsProtocol::CreateSuccessResponse(id, nullptr); |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> HandleBrowserCommand( |
| + int id, |
| + std::string method, |
| + base::DictionaryValue* params) { |
| + if (method == chrome::devtools::Browser::getWindowForTarget::kName) |
| + return GetWindowForTarget(id, method, params); |
| + if (method == chrome::devtools::Browser::getWindowBounds::kName) |
| + return GetWindowBounds(id, method, params); |
| + if (method == chrome::devtools::Browser::setWindowBounds::kName) |
| + return SetWindowBounds(id, method, params); |
| + return nullptr; |
| +} |
| + |
| class ChromeDevToolsManagerDelegate::HostData { |
| public: |
| HostData() {} |
| @@ -77,6 +251,9 @@ base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
| return nullptr; |
| + if (method.find("Browser.") == 0) |
| + return HandleBrowserCommand(id, method, params).release(); |
|
dgozman
2017/03/22 21:28:34
Let's only do this if |agent_host->GetType() == De
jzfeng
2017/03/23 06:58:24
Done.
|
| + |
| if (method == chrome::devtools::Target::setRemoteLocations::kName) |
| return SetRemoteLocations(agent_host, id, params).release(); |