diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 02:40:53 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 02:40:53 +0000 |
commit | 22f22e700c7f47c077184e26667bc77d4767099a (patch) | |
tree | d2a68a49996104f3f3254ce4b6bf8f3a4ec8ec90 | |
parent | 372d832bd96a6422209e2d9891b89d2699502569 (diff) | |
download | chromium_src-22f22e700c7f47c077184e26667bc77d4767099a.zip chromium_src-22f22e700c7f47c077184e26667bc77d4767099a.tar.gz chromium_src-22f22e700c7f47c077184e26667bc77d4767099a.tar.bz2 |
Revert "Revert "implemented extensions api windows.update().""
This reverts commit 81242e3b9c6e6fbc42ccf5f973b27ed4cf4401d6.
This wasn't the cause of the breakage.
Review URL: http://codereview.chromium.org/115337
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16032 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 193 insertions, 10 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index bdc930a..b6943e9 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -61,6 +61,7 @@ FactoryRegistry::FactoryRegistry() { &NewExtensionFunction<GetLastFocusedWindowFunction>; factories_["GetAllWindows"] = &NewExtensionFunction<GetAllWindowsFunction>; factories_["CreateWindow"] = &NewExtensionFunction<CreateWindowFunction>; + factories_["UpdateWindow"] = &NewExtensionFunction<UpdateWindowFunction>; factories_["RemoveWindow"] = &NewExtensionFunction<RemoveWindowFunction>; // Tabs diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 6e61976..09415f4 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -253,6 +253,55 @@ bool CreateWindowFunction::RunImpl() { return true; } +bool UpdateWindowFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); + const ListValue* args = static_cast<const ListValue*>(args_); + int window_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &window_id)); + DictionaryValue* update_props; + EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props)); + + Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_); + if (!browser) + return false; + + gfx::Rect bounds = browser->window()->GetNormalBounds(); + // Any part of the bounds can optionally be set by the caller. + int bounds_val; + if (update_props->HasKey(kLeftKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kLeftKey, + &bounds_val)); + bounds.set_x(bounds_val); + } + + if (update_props->HasKey(kTopKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kTopKey, + &bounds_val)); + bounds.set_y(bounds_val); + } + + if (update_props->HasKey(kWidthKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kWidthKey, + &bounds_val)); + bounds.set_width(bounds_val); + } + + if (update_props->HasKey(kHeightKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kHeightKey, + &bounds_val)); + bounds.set_height(bounds_val); + } + + // TODO(rafaelw): This call to SetBounds() ends up resulting in the target + // window being activated (pushed to the front). On win32, this appears to be + // the result of HWND event handling. + browser->window()->SetBounds(bounds); + // TODO(rafaelw): Support |focused|. + result_.reset(CreateWindowValue(browser, false)); + + return true; +} + bool RemoveWindowFunction::RunImpl() { int window_id; EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); @@ -576,10 +625,10 @@ static bool GetTabById(int tab_id, Profile* profile, Browser** browser, if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, contents, tab_index)) return true; - + if (error_message) *error_message = ExtensionErrorUtils::FormatErrorMessage( kTabNotFoundError, IntToString(tab_id)); - + return false; } diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h index b8763fc..8a004185 100644 --- a/chrome/browser/extensions/extension_tabs_module.h +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -45,6 +45,9 @@ class GetAllWindowsFunction : public SyncExtensionFunction { class CreateWindowFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; +class UpdateWindowFunction : public SyncExtensionFunction { + virtual bool RunImpl(); +}; class RemoveWindowFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; diff --git a/chrome/renderer/extensions/extension_api_client_unittest.cc b/chrome/renderer/extensions/extension_api_client_unittest.cc index 6104bab..9689d95 100644 --- a/chrome/renderer/extensions/extension_api_client_unittest.cc +++ b/chrome/renderer/extensions/extension_api_client_unittest.cc @@ -172,6 +172,89 @@ TEST_F(ExtensionAPIClientTest, GetAllWindows) { "GetAllWindows", "null"); } +TEST_F(ExtensionAPIClientTest, CreateWindow) { + ExpectJsFail("chrome.windows.create({url: 1}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'url': Expected 'string' but got 'integer'."); + ExpectJsFail("chrome.windows.create({left: 'foo'}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'left': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.create({top: 'foo'}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'top': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.create({width: 'foo'}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'width': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.create({height: 'foo'}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'height': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.create({foo: 42}, function(){});", + "Uncaught Error: Invalid value for argument 0. Property " + "'foo': Unexpected property."); + + ExpectJsPass("chrome.windows.create({" + " url:'http://www.google.com/'," + " left:0," + " top: 10," + " width:100," + " height:200" + "})", + "CreateWindow", + "{\"url\":\"http://www.google.com/\"," + "\"left\":0," + "\"top\":10," + "\"width\":100," + "\"height\":200}"); +} + +TEST_F(ExtensionAPIClientTest, UpdateWindow) { + ExpectJsFail("chrome.windows.update(null);", + "Uncaught Error: Parameter 0 is required."); + ExpectJsFail("chrome.windows.update(42, {left: 'foo'});", + "Uncaught Error: Invalid value for argument 1. Property " + "'left': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.update(42, {top: 'foo'});", + "Uncaught Error: Invalid value for argument 1. Property " + "'top': Expected 'integer' but got 'string'."); + ExpectJsFail("chrome.windows.update(42, {height: false});", + "Uncaught Error: Invalid value for argument 1. Property " + "'height': Expected 'integer' but got 'boolean'."); + ExpectJsFail("chrome.windows.update(42, {width: false});", + "Uncaught Error: Invalid value for argument 1. Property " + "'width': Expected 'integer' but got 'boolean'."); + ExpectJsFail("chrome.windows.update(42, {foo: false});", + "Uncaught Error: Invalid value for argument 1. Property " + "'foo': Unexpected property."); + + ExpectJsPass("chrome.windows.update(42, {" + " width:100," + " height:200" + "})", + "UpdateWindow", + "[42," + "{\"width\":100," + "\"height\":200}]"); +} + +TEST_F(ExtensionAPIClientTest, RemoveWindow) { + ExpectJsFail("chrome.windows.remove(32, function(){}, 20);", + "Uncaught Error: Too many arguments."); + + ExpectJsFail("chrome.windows.remove('abc', function(){});", + "Uncaught Error: Invalid value for argument 0. " + "Expected 'integer' but got 'string'."); + + ExpectJsFail("chrome.windows.remove(1, 1);", + "Uncaught Error: Invalid value for argument 1. " + "Expected 'function' but got 'integer'."); + + ExpectJsPass("chrome.windows.remove(2, function(){})", + "RemoveWindow", "2"); + + ExpectJsPass("chrome.windows.remove(2)", + "RemoveWindow", "2"); +} + // Tab API tests TEST_F(ExtensionAPIClientTest, GetTab) { diff --git a/chrome/renderer/renderer_resources.grd b/chrome/renderer/renderer_resources.grd index c59e98c..cda253b 100644 --- a/chrome/renderer/renderer_resources.grd +++ b/chrome/renderer/renderer_resources.grd @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- This comment is only here because changes to resources are not picked up -without changes to the corresponding grd file. --> +without changes to the corresponding grd file. --> <grit latest_public_release="0" current_release="1"> <outputs> <output filename="grit/renderer_resources.h" type="rc_header"> diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index 8b17e63..3828ee2 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -14,6 +14,7 @@ var chrome; native function GetCurrentWindow(); native function GetLastFocusedWindow(); native function CreateWindow(); + native function UpdateWindow(); native function RemoveWindow(); native function GetAllWindows(); native function GetTab(); @@ -165,7 +166,25 @@ var chrome; }, chrome.types.optFun ]; - + + chrome.windows.update = function(windowId, updateData, callback) { + validate(arguments, arguments.callee.params); + sendRequest(UpdateWindow, [windowId, updateData], callback); + }; + chrome.windows.update.params = [ + chrome.types.pInt, + { + type: "object", + properties: { + left: chrome.types.optInt, + top: chrome.types.optInt, + width: chrome.types.optPInt, + height: chrome.types.optPInt + }, + }, + chrome.types.optFun + ]; + chrome.windows.remove = function(windowId, callback) { validate(arguments, arguments.callee.params); sendRequest(RemoveWindow, windowId, callback); diff --git a/chrome/test/data/extensions/samples/tabs/tabs_api.html b/chrome/test/data/extensions/samples/tabs/tabs_api.html index 7a60db7..a0b2dd23 100644 --- a/chrome/test/data/extensions/samples/tabs/tabs_api.html +++ b/chrome/test/data/extensions/samples/tabs/tabs_api.html @@ -19,6 +19,10 @@ function bootStrap() { }); } +function isInt(i) { + return (typeof i == "number") && !(i % 1) && !isNaN(i); +} + function loadWindowList() { chrome.windows.getAll(true, function(windowList) { tabs = {}; @@ -54,8 +58,7 @@ function updateTab(id){ } catch (e) { alert(e); } -} - +} function moveTabData(id) { return { @@ -182,10 +185,6 @@ chrome.tabs.onRemoved.addListener(function(tabId) { loadWindowList(); }); -function isInt(i) { - return (typeof i == "number") && !(i % 1) && !isNaN(i); -} - function createWindow() { var args = { 'left': parseInt(document.getElementById('new_window_left').value), @@ -226,6 +225,34 @@ function refreshWindow(windowId) { }); } +function updateWindowData(id) { + var retval = { + left: parseInt(document.getElementById('left_' + id).value), + top: parseInt(document.getElementById('top_' + id).value), + width: parseInt(document.getElementById('width_' + id).value), + height: parseInt(document.getElementById('height_' + id).value) + } + + if (!isInt(retval.left)) + delete retval.left; + if (!isInt(retval.top)) + delete retval.top; + if (!isInt(retval.width)) + delete retval.width; + if (!isInt(retval.height)) + delete retval.height; + + return retval; +} + +function updateWindow(id){ + try { + chrome.windows.update(id, updateWindowData(id)); + } catch (e) { + alert(e); + } +} + function removeWindow(windowId) { try { chrome.windows.remove(windowId, function() { @@ -300,6 +327,7 @@ function refreshSelectedTab(windowId) { </div> </div> </div> + <button onclick="updateWindow(this.jstdata);" jsvalues=".jstdata:id">Update Window</button> <button onclick="removeWindow(this.jstdata);" jsvalues=".jstdata:id">Close Window</button> <button onclick="refreshSelectedTab(this.jstdata);" jsvalues=".jstdata:id">Refresh Selected Tab</button> </div> |