From 5448610da81a6438bdf2f1b0a1827c0287073f6f Mon Sep 17 00:00:00 2001 From: wjmaclean Date: Thu, 2 Apr 2015 09:21:24 -0700 Subject: [PATCH] Add default zoom functionality to chrome.tabs Zoom API. This CL adds default zoom factor functionality to the tabs zoom api. Tabs can now be set to the current default zoom via setZoom(tabId, 0) and the current default zoom level can be retrieved as a parameter default_zoom_factor in the ZoomSettings structure returned by getZoomSettings. BUG=470550 Review URL: https://codereview.chromium.org/1051013002 Cr-Commit-Position: refs/heads/master@{#323505} --- chrome/browser/extensions/api/tabs/tabs_api.cc | 6 +- chrome/browser/extensions/api/tabs/tabs_test.cc | 95 +++++++++++++++++++++- chrome/common/extensions/api/tabs.json | 6 ++ .../docs/examples/api/tabs/zoom/popup.html | 3 +- .../docs/examples/api/tabs/zoom/popup.js | 7 +- 5 files changed, 112 insertions(+), 5 deletions(-) diff --git a/chrome/browser/extensions/api/tabs/tabs_api.cc b/chrome/browser/extensions/api/tabs/tabs_api.cc index 229d89aaed24..e6130ec1153c 100644 --- a/chrome/browser/extensions/api/tabs/tabs_api.cc +++ b/chrome/browser/extensions/api/tabs/tabs_api.cc @@ -1854,7 +1854,9 @@ bool TabsSetZoomFunction::RunAsync() { ZoomController* zoom_controller = ZoomController::FromWebContents(web_contents); - double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor); + double zoom_level = params->zoom_factor > 0 + ? content::ZoomFactorToZoomLevel(params->zoom_factor) + : zoom_controller->GetDefaultZoomLevel(); scoped_refptr client( new ExtensionZoomRequestClient(extension())); @@ -1953,6 +1955,8 @@ bool TabsGetZoomSettingsFunction::RunAsync() { ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode(); api::tabs::ZoomSettings zoom_settings; ZoomModeToZoomSettings(zoom_mode, &zoom_settings); + zoom_settings.default_zoom_factor.reset(new double( + content::ZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()))); results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); SendResponse(true); diff --git a/chrome/browser/extensions/api/tabs/tabs_test.cc b/chrome/browser/extensions/api/tabs/tabs_test.cc index 38309a6a1340..f2f3aa3ca189 100644 --- a/chrome/browser/extensions/api/tabs/tabs_test.cc +++ b/chrome/browser/extensions/api/tabs/tabs_test.cc @@ -22,8 +22,11 @@ #include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/storage_partition.h" #include "content/public/common/page_zoom.h" #include "content/public/common/url_constants.h" #include "extensions/browser/api_test_utils.h" @@ -666,6 +669,10 @@ class ExtensionTabsZoomTest : public ExtensionTabsTest { std::string* mode, std::string* scope); + // Runs chrome.tabs.getZoomSettings() and returns default zoom. + testing::AssertionResult RunGetDefaultZoom(int tab_id, + double* default_zoom_factor); + // Runs chrome.tabs.setZoom(), expecting an error. std::string RunSetZoomExpectError(int tab_id, double zoom_factor); @@ -694,8 +701,7 @@ bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) { return utils::RunFunction( set_zoom_function.get(), - base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), - browser(), + base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), browser(), extension_function_test_utils::NONE); } @@ -768,6 +774,33 @@ testing::AssertionResult ExtensionTabsZoomTest::RunGetZoomSettings( return testing::AssertionSuccess(); } +testing::AssertionResult ExtensionTabsZoomTest::RunGetDefaultZoom( + int tab_id, + double* default_zoom_factor) { + DCHECK(default_zoom_factor); + scoped_refptr get_zoom_settings_function( + new TabsGetZoomSettingsFunction()); + get_zoom_settings_function->set_extension(extension_.get()); + get_zoom_settings_function->set_has_callback(true); + + scoped_ptr get_zoom_settings_result( + utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( + get_zoom_settings_function.get(), + base::StringPrintf("[%u]", tab_id), + browser()))); + + if (!get_zoom_settings_result) + return testing::AssertionFailure() << "no result"; + + if (!get_zoom_settings_result->GetDouble("default_zoom_factor", + default_zoom_factor)) { + return testing::AssertionFailure() + << "default zoom factor not found in result"; + } + + return testing::AssertionSuccess(); +} + std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, double zoom_factor) { scoped_refptr set_zoom_function( @@ -847,6 +880,64 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { EXPECT_EQ(kZoomLevel, zoom_factor); } +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetDefaultZoom) { + content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); + content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); + int tab_id = ExtensionTabUtil::GetTabId(web_contents); + + ui_zoom::ZoomController* zoom_controller = + ui_zoom::ZoomController::FromWebContents(web_contents); + double default_zoom_factor = -1.0; + EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); + EXPECT_TRUE(content::ZoomValuesEqual( + zoom_controller->GetDefaultZoomLevel(), + content::ZoomFactorToZoomLevel(default_zoom_factor))); + + // Change the default zoom level and verify GetDefaultZoom returns the + // correct value. + content::StoragePartition* partition = + content::BrowserContext::GetStoragePartition( + web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); + chrome::ChromeZoomLevelPrefs* zoom_prefs = + static_cast( + partition->GetZoomLevelDelegate()); + + double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); + zoom_prefs->SetDefaultZoomLevelPref(default_zoom_level + 0.5); + default_zoom_factor = -1.0; + EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); + EXPECT_TRUE(content::ZoomValuesEqual( + default_zoom_level + 0.5, + content::ZoomFactorToZoomLevel(default_zoom_factor))); +} + +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetToDefaultZoom) { + content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); + content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); + int tab_id = ExtensionTabUtil::GetTabId(web_contents); + + ui_zoom::ZoomController* zoom_controller = + ui_zoom::ZoomController::FromWebContents(web_contents); + double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); + double new_default_zoom_level = default_zoom_level + 0.42; + + content::StoragePartition* partition = + content::BrowserContext::GetStoragePartition( + web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); + chrome::ChromeZoomLevelPrefs* zoom_prefs = + static_cast( + partition->GetZoomLevelDelegate()); + + zoom_prefs->SetDefaultZoomLevelPref(new_default_zoom_level); + + double observed_zoom_factor = -1.0; + EXPECT_TRUE(RunSetZoom(tab_id, 0.0)); + EXPECT_TRUE(RunGetZoom(tab_id, &observed_zoom_factor)); + EXPECT_TRUE(content::ZoomValuesEqual( + new_default_zoom_level, + content::ZoomFactorToZoomLevel(observed_zoom_factor))); +} + IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { // In this test we need two URLs that (1) represent real pages (i.e. they // load without causing an error page load), (2) have different domains, and diff --git a/chrome/common/extensions/api/tabs.json b/chrome/common/extensions/api/tabs.json index 1fa588766095..f187633f2727 100644 --- a/chrome/common/extensions/api/tabs.json +++ b/chrome/common/extensions/api/tabs.json @@ -68,6 +68,12 @@ "description": "Zoom changes only take effect in this tab, and zoom changes in other tabs will not affect the zooming of this tab. Also, per-tab zoom changes are reset on navigation; navigating a tab will always load pages with their per-origin zoom factors." } ] + }, + "default_zoom_factor": { + "type": "number", + "name": "defaultZoomFactor", + "optional": true, + "description": "Used to return the default zoom level for the current tab in calls to tabs.getZoomSettings." } } } diff --git a/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.html b/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.html index b224234607c9..8b7f276c1183 100644 --- a/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.html +++ b/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.html @@ -30,7 +30,8 @@ - + +

diff --git a/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.js b/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.js index 1b0fcd8db3ed..d96bc74eff11 100644 --- a/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.js +++ b/chrome/common/extensions/docs/examples/api/tabs/zoom/popup.js @@ -49,6 +49,11 @@ document.addEventListener('DOMContentLoaded', function() { if (scopeRadios[i].value == zoomSettings.scope) scopeRadios[i].checked = true; } + + var percentDefaultZoom = + parseFloat(zoomSettings.default_zoom_factor) * 100; + document.getElementById('defaultLabel').textContent = + 'Default: ' + percentDefaultZoom.toFixed(1) + '%'; }); chrome.tabs.getZoom(tabId, displayZoomLevel); @@ -92,7 +97,7 @@ function doZoomDefault() { if (tabId == -1) return; - chrome.tabs.setZoom(tabId, 1.0, function() { + chrome.tabs.setZoom(tabId, 0, function() { if (chrome.runtime.lastError) console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message); }); -- 2.11.4.GIT