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..e66abc1b14517c6e007682e109f9aab6b56ae0f6 100644 |
--- a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
+++ b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc |
@@ -4,6 +4,8 @@ |
#include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
+#include <utility> |
+ |
#include "base/memory/ptr_util.h" |
#include "base/strings/utf_string_conversions.h" |
#include "build/build_config.h" |
@@ -17,7 +19,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 +43,224 @@ char kLocationsParam[] = "locations"; |
char kHostParam[] = "host"; |
char kPortParam[] = "port"; |
+namespace { |
+ |
+BrowserWindow* GetBrowserWindow(int window_id) { |
+ for (auto* b : *BrowserList::GetInstance()) { |
+ if (b->session_id().id() == window_id) |
+ return b->window(); |
+ } |
+ return nullptr; |
+} |
+ |
+// Get the bounds and state of the browser window. The bounds is for the |
+// restored window when the window is minimized. Otherwise, it is for the actual |
+// window. |
+std::unique_ptr<base::DictionaryValue> GetBounds(BrowserWindow* window) { |
+ gfx::Rect bounds; |
+ if (window->IsMinimized()) |
+ bounds = window->GetRestoredBounds(); |
+ else |
+ bounds = window->GetBounds(); |
+ |
+ auto bounds_object = 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; |
+} |
+ |
+} // namespace |
+ |
+// static |
+std::unique_ptr<base::DictionaryValue> |
+ChromeDevToolsManagerDelegate::GetWindowForTarget( |
+ int id, |
+ 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( |
+ 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"); |
+ } |
+ |
+ auto result = base::MakeUnique<base::DictionaryValue>(); |
+ result->SetInteger("windowId", browser->session_id().id()); |
+ result->Set("bounds", GetBounds(browser->window())); |
+ return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
+} |
+ |
+// static |
+std::unique_ptr<base::DictionaryValue> |
+ChromeDevToolsManagerDelegate::GetWindowBounds(int id, |
+ 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"); |
+ } |
+ |
+ auto result = base::MakeUnique<base::DictionaryValue>(); |
+ result->Set("bounds", GetBounds(window)); |
+ return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); |
+} |
+ |
+// static |
+std::unique_ptr<base::DictionaryValue> |
+ChromeDevToolsManagerDelegate::SetWindowBounds(int id, |
+ 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"); |
+ } |
+ |
+ const base::Value* value = nullptr; |
+ const 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"); |
+ |
+ // Compute updated bounds when window state is normal. |
+ bool set_bounds = false; |
+ gfx::Rect bounds; |
+ if (window->IsMinimized()) |
+ 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 (window->IsFullscreen()) { |
+ if (window_state == "minimized" || window_state == "maximized" || |
+ set_bounds) { |
+ return DevToolsProtocol::CreateErrorResponse( |
+ id, |
+ "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.
|
+ "maximized or resized."); |
+ } |
+ if (window_state == "normal") |
+ window->GetExclusiveAccessContext()->ExitFullscreen(); |
+ return DevToolsProtocol::CreateSuccessResponse(id, nullptr); |
+ } |
+ |
+ if (window->IsMinimized()) { |
+ if (window_state == "fullscreen" || window_state == "maximized" || |
+ set_bounds) { |
+ return DevToolsProtocol::CreateErrorResponse( |
+ id, |
+ "Browser window is minimized, can not enter fullscreen, be maximized " |
+ "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.
|
+ } |
+ if (window_state == "normal") |
+ window->Show(); |
+ return DevToolsProtocol::CreateSuccessResponse(id, nullptr); |
+ } |
+ |
+ // 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.
|
+ if (window_state == "minimized") { |
+ window->Minimize(); |
+ } else if (window_state == "maximized") { |
+ window->Maximize(); |
+ } else if (window_state == "fullscreen") { |
+ window->GetExclusiveAccessContext()->EnterFullscreen( |
+ GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); |
+ } else { |
+ if (window->IsMaximized() && set_bounds) { |
+ return DevToolsProtocol::CreateErrorResponse( |
+ id, "Browser window is maximized, can not be resized."); |
+ } |
+ if (window->IsMaximized()) |
+ window->Restore(); |
+ if (set_bounds) |
+ window->SetBounds(bounds); |
+ } |
+ |
+ return DevToolsProtocol::CreateSuccessResponse(id, nullptr); |
+} |
+ |
+std::unique_ptr<base::DictionaryValue> |
+ChromeDevToolsManagerDelegate::HandleBrowserCommand( |
+ int id, |
+ std::string method, |
+ base::DictionaryValue* params) { |
+ if (method == chrome::devtools::Browser::getWindowForTarget::kName) |
+ return GetWindowForTarget(id, params); |
+ if (method == chrome::devtools::Browser::getWindowBounds::kName) |
+ return GetWindowBounds(id, params); |
+ if (method == chrome::devtools::Browser::setWindowBounds::kName) |
+ return SetWindowBounds(id, params); |
+ return nullptr; |
+} |
+ |
class ChromeDevToolsManagerDelegate::HostData { |
public: |
HostData() {} |
@@ -77,6 +300,10 @@ base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
return nullptr; |
+ if (agent_host->GetType() == DevToolsAgentHost::kTypeBrowser && |
+ method.find("Browser.") == 0) |
+ return HandleBrowserCommand(id, method, params).release(); |
+ |
if (method == chrome::devtools::Target::setRemoteLocations::kName) |
return SetRemoteLocations(agent_host, id, params).release(); |
@@ -276,7 +503,5 @@ ChromeDevToolsManagerDelegate::SetRemoteLocations( |
host_data_[agent_host]->set_remote_locations(tcp_locations); |
UpdateDeviceDiscovery(); |
- std::unique_ptr<base::DictionaryValue> result( |
- base::MakeUnique<base::DictionaryValue>()); |
- return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); |
+ return DevToolsProtocol::CreateSuccessResponse(command_id, nullptr); |
} |