Chromium Code Reviews| Index: chrome/browser/extensions/api/tabs/tabs_api.cc |
| diff --git a/chrome/browser/extensions/api/tabs/tabs_api.cc b/chrome/browser/extensions/api/tabs/tabs_api.cc |
| index 71c441c6fdd825c7c6aa5b4fa285bcaff778c62b..142e2fea0a87bb6a89587e1e51e297e1aa06486a 100644 |
| --- a/chrome/browser/extensions/api/tabs/tabs_api.cc |
| +++ b/chrome/browser/extensions/api/tabs/tabs_api.cc |
| @@ -44,6 +44,7 @@ |
| #include "chrome/browser/ui/panels/panel_manager.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/browser/ui/window_sizer/window_sizer.h" |
| +#include "chrome/browser/ui/zoom/zoom_controller.h" |
| #include "chrome/browser/web_applications/web_app.h" |
| #include "chrome/common/chrome_switches.h" |
| #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| @@ -163,6 +164,29 @@ void AssignOptionalValue(const scoped_ptr<T>& source, |
| } // namespace |
| +void ZoomModeToZoomSettings(ZoomMode zoom_mode, |
| + api::tabs::ZoomSettings* zoom_settings) { |
| + DCHECK(zoom_settings); |
| + switch (zoom_mode) { |
| + case kZoomModeDefault: |
| + zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC; |
| + zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_ORIGIN; |
| + break; |
| + case kZoomModeIsolated: |
| + zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC; |
| + zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| + break; |
| + case kZoomModeManual: |
| + zoom_settings->mode = api::tabs::ZoomSettings::MODE_MANUAL; |
| + zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| + break; |
| + case kZoomModeDisabled: |
| + zoom_settings->mode = api::tabs::ZoomSettings::MODE_DISABLED; |
| + zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| + break; |
| + } |
| +} |
| + |
| // Windows --------------------------------------------------------------------- |
| bool WindowsGetFunction::RunSync() { |
| @@ -1420,8 +1444,9 @@ bool TabsReloadFunction::RunSync() { |
| NULL, |
| &web_contents, |
| NULL, |
| - &error_)) |
| - return false; |
| + &error_)) { |
| + return false; |
| + } |
| } |
| if (web_contents->ShowingInterstitialPage()) { |
| @@ -1749,4 +1774,151 @@ bool TabsInsertCSSFunction::ShouldInsertCSS() const { |
| return true; |
| } |
| +bool ZoomAPIFunction::GetWebContents(int* tab_id, |
|
Devlin
2014/06/16 19:04:47
Why not content::WebContents* GetWebContents(int*
wjmaclean
2014/06/18 19:03:52
I inherited this code from Paul Meyer, and I suspe
|
| + content::WebContents** web_contents) { |
| + if (tab_id) { |
| + if (!GetTabById(*tab_id, |
| + GetProfile(), |
| + include_incognito(), |
| + NULL, |
| + NULL, |
| + web_contents, |
| + NULL, |
| + &error_)) { |
| + return false; |
| + } |
| + } else { |
| + Browser* browser = GetCurrentBrowser(); |
| + if (!browser) { |
| + error_ = keys::kNoCurrentWindowError; |
|
Devlin
2014/06/16 19:04:47
It's odd that a function called GetWebContents als
wjmaclean
2014/06/18 19:03:52
I've mentioned in the .h file that this function m
|
| + return false; |
| + } |
| + if (!ExtensionTabUtil::GetDefaultTab(browser, web_contents, NULL)) { |
| + error_ = keys::kNoSelectedTabError; |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +bool TabsSetZoomFunction::RunAsync() { |
| + scoped_ptr<tabs::SetZoom::Params> params( |
| + tabs::SetZoom::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + WebContents* web_contents = NULL; |
| + if (!GetWebContents(params->tab_id.get(), &web_contents)) |
| + return false; |
| + if (web_contents->GetURL().SchemeIs(content::kChromeUIScheme)) { |
|
Devlin
2014/06/16 19:04:47
WebContents::GetURL() is deprecated. See web_cont
wjmaclean
2014/06/18 19:03:52
Done.
|
| + error_ = keys::kCannotZoomChromePagesError; |
|
Devlin
2014/06/16 19:04:47
This looks like it should probably be using Permis
Devlin
2014/06/17 19:10:20
Going back to this - I thought developers would ne
wjmaclean
2014/06/18 19:03:52
Possibly. It seems to mean that any extension want
|
| + return false; |
| + } |
| + |
| + ZoomController* zoom_controller = |
| + ZoomController::FromWebContents(web_contents); |
| + double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor); |
| + |
| + if (!zoom_controller->SetZoomLevelByExtension(zoom_level, GetExtension())) { |
| + // Tried to zoom a tab in disabled mode. |
| + error_ = keys::kCannotZoomDisabledTabError; |
| + return false; |
| + } |
| + |
| + SendResponse(true); |
| + return true; |
| +} |
| + |
| +bool TabsGetZoomFunction::RunAsync() { |
| + scoped_ptr<tabs::GetZoom::Params> params( |
| + tabs::GetZoom::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + WebContents* web_contents = NULL; |
| + if (!GetWebContents(params->tab_id.get(), &web_contents)) { |
| + return false; |
|
Devlin
2014/06/16 19:04:47
nit: no braces around single-line conditionals.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + } |
| + |
| + double zoom_level = |
| + ZoomController::FromWebContents(web_contents)->GetZoomLevel(); |
| + double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level); |
| + results_ = tabs::GetZoom::Results::Create(zoom_factor); |
| + SendResponse(true); |
| + return true; |
| +} |
| + |
| +bool TabsSetZoomSettingsFunction::RunAsync() { |
| + using api::tabs::ZoomSettings; |
| + |
| + scoped_ptr<tabs::SetZoomSettings::Params> params( |
| + tabs::SetZoomSettings::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + WebContents* web_contents = NULL; |
| + if (!GetWebContents(params->tab_id.get(), &web_contents)) |
| + return false; |
| + if (web_contents->GetURL().SchemeIs(content::kChromeUIScheme)) { |
|
Devlin
2014/06/16 19:04:47
Same comments as above.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + error_ = keys::kCannotChangeChromePageZoomSettingsError; |
| + return false; |
| + } |
| + ZoomController* zoom_controller = |
|
Devlin
2014/06/16 19:04:47
nit: Move this down where it's used.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + ZoomController::FromWebContents(web_contents); |
| + |
| + // "per-origin" scope is only available in "automatic" mode. |
| + if (params->zoom_settings.scope == ZoomSettings::SCOPE_PER_ORIGIN && |
| + params->zoom_settings.mode != ZoomSettings::MODE_AUTOMATIC && |
| + params->zoom_settings.mode != ZoomSettings::MODE_NONE) { |
| + error_ = keys::kPerOriginOnlyInAutomaticError; |
| + return false; |
| + } |
| + |
| + // Determine the correct internal zoom mode to set |web_contents| to from the |
| + // user-specified |zoom_settings|. |
| + ZoomMode zoom_mode = kZoomModeDefault; |
| + switch (params->zoom_settings.mode) { |
| + case ZoomSettings::MODE_NONE: |
| + case ZoomSettings::MODE_AUTOMATIC: |
| + switch (params->zoom_settings.scope) { |
| + case ZoomSettings::SCOPE_NONE: |
| + case ZoomSettings::SCOPE_PER_ORIGIN: |
| + zoom_mode = kZoomModeDefault; |
| + break; |
| + case ZoomSettings::SCOPE_PER_TAB: |
| + zoom_mode = kZoomModeIsolated; |
| + break; |
|
Devlin
2014/06/16 19:04:47
nit: Remove trailing break.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + } |
| + break; |
| + case ZoomSettings::MODE_MANUAL: |
| + zoom_mode = kZoomModeManual; |
| + break; |
| + case ZoomSettings::MODE_DISABLED: |
| + zoom_mode = kZoomModeDisabled; |
| + break; |
|
Devlin
2014/06/16 19:04:47
nit: Remove trailing break.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + } |
| + |
| + zoom_controller->SetZoomMode(zoom_mode); |
| + |
| + SendResponse(true); |
| + return true; |
| +} |
| + |
| +bool TabsGetZoomSettingsFunction::RunAsync() { |
| + scoped_ptr<tabs::GetZoomSettings::Params> params( |
| + tabs::GetZoomSettings::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + WebContents* web_contents = NULL; |
| + if (!GetWebContents(params->tab_id.get(), &web_contents)) |
| + return false; |
| + ZoomController* zoom_controller = |
| + ZoomController::FromWebContents(web_contents); |
| + |
| + ZoomMode zoom_mode = zoom_controller->zoom_mode(); |
| + api::tabs::ZoomSettings zoom_settings; |
| + ZoomModeToZoomSettings(zoom_mode, &zoom_settings); |
| + |
| + results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); |
| + SendResponse(true); |
| + return true; |
| +} |
| + |
| } // namespace extensions |