Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: chrome/browser/devtools/chrome_devtools_manager_delegate.cc

Issue 2734123004: add a new set of commands to resize and position windows (Closed)
Patch Set: add tests Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8ea669c32beb2e97c8cc5a9b728539e4dfebd022 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,191 @@ 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;
+}
+
+base::DictionaryValue* GetBounds(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(new 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> HandleBrowserCommand(
+ int id,
+ std::string method,
+ base::DictionaryValue* params) {
+ if (method == chrome::devtools::Browser::getWindowForTarget::kName)
+ return ChromeDevToolsManagerDelegate::GetWindowForTarget(id, params);
+ if (method == chrome::devtools::Browser::getWindowBounds::kName)
+ return ChromeDevToolsManagerDelegate::GetWindowBounds(id, params);
+ if (method == chrome::devtools::Browser::setWindowBounds::kName)
+ return ChromeDevToolsManagerDelegate::SetWindowBounds(id, params);
+ return nullptr;
+}
+
+} // 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");
+ }
+
+ std::unique_ptr<base::DictionaryValue> result(new 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");
+ }
+
+ std::unique_ptr<base::DictionaryValue> result(new 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");
+ }
+ 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->IsMinimized())
+ window->Show();
+
+ 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()) {
dgozman 2017/03/23 22:56:01 || window->IsMinimized()
jzfeng 2017/03/24 02:24:40 Done.
+ window->Restore();
+ }
+
+ // compute updated bounds when window state is normal.
+ bool set_bounds = false;
+ gfx::Rect bounds;
+ if (window->IsMinimized() || window->IsMaximized())
+ 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
+ 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);
+}
+
class ChromeDevToolsManagerDelegate::HostData {
public:
HostData() {}
@@ -77,6 +265,10 @@ base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand(
if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, &params))
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 +468,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);
}

Powered by Google App Engine
This is Rietveld 408576698