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

Unified Diff: chrome/browser/extensions/api/tabs/tabs_api.cc

Issue 225093019: Zoom Extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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/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 39607d2b0f7f2532c3d2d37871f069edea697e07..50ec90df8b06971a51e3388146f0ebaf44f16697 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"
@@ -60,6 +61,7 @@
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
@@ -215,6 +217,35 @@ Browser* CreateBrowserWindow(const Browser::CreateParams& params,
} // namespace
+void ZoomModeToZoomSettings(content::ZoomMode zoom_mode,
+ base::DictionaryValue* zoom_settings) {
+ std::string zoom_settings_mode;
+ std::string zoom_settings_scope;
+ switch (zoom_mode) {
+ case content::kZoomModeDefault:
+ zoom_settings_mode = "automatic";
+ zoom_settings_scope = "per-origin";
+ break;
+ case content::kZoomModeIsolated:
+ zoom_settings_mode = "automatic";
+ zoom_settings_scope = "per-tab";
+ break;
+ case content::kZoomModeManual:
+ zoom_settings_mode = "manual";
+ zoom_settings_scope = "per-tab";
+ break;
+ case content::kZoomModeDisabled:
+ zoom_settings_mode = "disabled";
+ zoom_settings_scope = "per-tab";
+ break;
+ default:
+ NOTREACHED();
+ }
+
+ zoom_settings->SetString(keys::kZoomSettingsMode, zoom_settings_mode);
+ zoom_settings->SetString(keys::kZoomSettingsScope, zoom_settings_scope);
+}
+
// Windows ---------------------------------------------------------------------
bool WindowsGetFunction::RunImpl() {
@@ -1597,7 +1628,7 @@ bool TabsReloadFunction::RunImpl() {
&web_contents,
NULL,
&error_))
- return false;
+ return false;
Fady Samuel 2014/04/07 20:01:14 Spacing looks wrong here.
paulmeyer 2014/04/07 20:56:10 Done.
}
if (web_contents->ShowingInterstitialPage()) {
@@ -1917,4 +1948,181 @@ bool TabsInsertCSSFunction::ShouldInsertCSS() const {
return true;
}
+bool ZoomAPIFunction::GetWebContents(int* tab_id,
+ 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;
+ return false;
+ }
+ if (!ExtensionTabUtil::GetDefaultTab(browser, web_contents, NULL)) {
+ error_ = keys::kNoCurrentTabError;
+ return false;
+ }
+ }
+ return true;
+}
+
+TabsSetZoomFunction::TabsSetZoomFunction() : zoom_id_(0) {}
+
+bool TabsSetZoomFunction::RunImpl() {
+ 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)) {
+ SendResponse(false);
+ return false;
+ } else if (web_contents->GetURL().SchemeIs("chrome")) {
Fady Samuel 2014/04/07 20:01:14 Are we sure this API is also isolated from <webvie
paulmeyer 2014/04/07 20:56:10 Perhaps not for now due to the existing <webview>
+ error_ = keys::kCannotZoomChromePagesError;
+ SendResponse(false);
+ return false;
+ }
+
+ ZoomController* zoom_controller =
+ ZoomController::FromWebContents(web_contents);
+ double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor);
+ zoom_id_ = zoom_controller->SetZoomLevelByExtension(zoom_level,
+ GetExtension());
+
+ if (!zoom_id_) {
+ // Tried to zoom a tab in disabled mode.
+ error_ = keys::kCannotZoomDisabledTabError;
+ SendResponse(false);
+ return false;
+ }
+
+ // Keep this function alive so that it can call its callback once the zoom
+ // change has completed.
+ AddRef();
+ registrar_.Add(this, chrome::NOTIFICATION_TAB_ZOOM_CHANGE_COMPLETE,
Fady Samuel 2014/04/07 20:04:53 So you don't need this notification if you have a
paulmeyer 2014/04/07 20:56:10 I believe that is true.
+ content::NotificationService::AllSources());
+
+ return true;
+}
+
+void TabsSetZoomFunction::Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ DCHECK(type == chrome::NOTIFICATION_TAB_ZOOM_CHANGE_COMPLETE);
Fady Samuel 2014/04/07 20:01:14 If we switch to a callback approach, would this no
paulmeyer 2014/04/07 20:56:10 I believe it can be taken out if callbacks are use
+
+ // Make sure it is this zoom change that has completed.
+ int zoom_id = *content::Details<int>(details).ptr();
+ if (zoom_id != zoom_id_) {
+ return;
+ }
+
+ // Release the function once it has called its callback.
+ SendResponse(true);
+ registrar_.RemoveAll();
+ Release();
+}
+
+bool TabsGetZoomFunction::RunImpl() {
+ 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)) {
+ SendResponse(false);
+ return false;
+ }
+
+ double zoom_level = web_contents->GetZoomLevel();
+ double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
+ SetResult(new base::FundamentalValue(zoom_factor));
+ SendResponse(true);
+ return true;
+}
+
+bool TabsSetZoomSettingsFunction::RunImpl() {
+ 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)) {
+ SendResponse(false);
+ return false;
+ } else if (web_contents->GetURL().SchemeIs("chrome")) {
Fady Samuel 2014/04/07 20:01:14 What about <webview>?
paulmeyer 2014/04/07 20:56:10 Can be fixed in a separate patch.
+ error_ = keys::kCannotChangeChromePageZoomSettingsError;
+ SendResponse(false);
+ return false;
+ }
+
+ if (params->zoom_settings.scope ==
Fady Samuel 2014/04/07 20:01:14 This is very confusing. I think it would be nice t
paulmeyer 2014/04/07 20:56:10 I made this clearer by setting the default mode an
+ api::tabs::ZoomSettings::SCOPE_PER_ORIGIN &&
+ params->zoom_settings.mode != api::tabs::ZoomSettings::MODE_NONE &&
+ params->zoom_settings.mode != api::tabs::ZoomSettings::MODE_AUTOMATIC) {
+ error_ = keys::kPerOriginOnlyInAutomaticError;
+ SendResponse(false);
+ return false;
+ }
+
+ content::ZoomMode zoom_mode = content::kZoomModeDefault;
+ switch (params->zoom_settings.mode) {
+ case api::tabs::ZoomSettings::MODE_NONE:
+ case api::tabs::ZoomSettings::MODE_AUTOMATIC:
+ switch (params->zoom_settings.scope) {
+ case api::tabs::ZoomSettings::SCOPE_NONE:
+ case api::tabs::ZoomSettings::SCOPE_PER_ORIGIN:
+ zoom_mode = content::kZoomModeDefault;
+ break;
+ case api::tabs::ZoomSettings::SCOPE_PER_TAB:
+ zoom_mode = content::kZoomModeIsolated;
+ break;
+ default:
+ NOTREACHED();
+ }
+ break;
+ case api::tabs::ZoomSettings::MODE_MANUAL:
+ zoom_mode = content::kZoomModeManual;
+ break;
+ case api::tabs::ZoomSettings::MODE_DISABLED:
+ zoom_mode = content::kZoomModeDisabled;
+ break;
+ default:
+ NOTREACHED();
+ }
+
+ web_contents->SetZoomMode(zoom_mode);
+
+ SendResponse(true);
+ return true;
+}
+
+bool TabsGetZoomSettingsFunction::RunImpl() {
+ 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)) {
+ SendResponse(false);
+ return false;
+ }
+
+ content::ZoomMode zoom_mode = web_contents->GetZoomMode();
+ base::DictionaryValue* zoom_settings = new base::DictionaryValue();
+ ZoomModeToZoomSettings(zoom_mode, zoom_settings);
+
+ SetResult(zoom_settings->DeepCopy());
Fady Samuel 2014/04/07 20:01:14 You're leaking memory here. Just SetResult(zoom_se
paulmeyer 2014/04/07 20:56:10 Done.
+ SendResponse(true);
+ return true;
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698