From 801ead5ec66705d05416262bfa542ae1635df6f2 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Fri, 23 Dec 2016 14:57:36 -0500 Subject: [PATCH] Support zoom limiting on webFrame Auditors: @bsclifton Electron side of the fix for https://github.com/brave/browser-laptop/issues/6315 --- .../api/resources/web_frame_bindings.js | 12 +++++ brave/common/extensions/api/web_frame.json | 41 +++++++++++++++ .../renderer/extensions/web_frame_bindings.cc | 51 ++++++++++++++----- .../renderer/extensions/web_frame_bindings.h | 5 ++ 4 files changed, 97 insertions(+), 12 deletions(-) diff --git a/atom/common/api/resources/web_frame_bindings.js b/atom/common/api/resources/web_frame_bindings.js index f6f47788a9..704f86ecbb 100644 --- a/atom/common/api/resources/web_frame_bindings.js +++ b/atom/common/api/resources/web_frame_bindings.js @@ -17,6 +17,18 @@ binding.registerCustomHook(function(bindingsAPI, extensionId) { apiFunctions.setHandleRequest('setSpellCheckProvider', function(lang, autoCorrectEnabled, spellCheckProvider) { return webFrameNatives.setSpellCheckProvider(lang, autoCorrectEnabled, spellCheckProvider) }) + + apiFunctions.setHandleRequest('setZoomLevel', function(level) { + return webFrameNatives.setZoomLevel(level) + }) + + apiFunctions.setHandleRequest('setZoomLevelLimits', function(minimumLevel, maximumLevel) { + return webFrameNatives.setZoomLevelLimits(minimumLevel, maximumLevel) + }) + + apiFunctions.setHandleRequest('setPageScaleLimits', function(minScale, maxScale) { + return webFrameNatives.setPageScaleLimits(minScale, maxScale) + }) }) exports.$set('binding', binding.generate()) diff --git a/brave/common/extensions/api/web_frame.json b/brave/common/extensions/api/web_frame.json index f1905573a2..97c9f57894 100644 --- a/brave/common/extensions/api/web_frame.json +++ b/brave/common/extensions/api/web_frame.json @@ -45,6 +45,47 @@ } ] }, + { + "name": "setZoomLevel", + "nocompile": true, + "type": "function", + "parameters": [ + { + "type": "number", + "name": "level" + } + ] + }, + { + "name": "setZoomLevelLimits", + "nocompile": true, + "type": "function", + "parameters": [ + { + "type": "number", + "name": "minimumLevel" + }, + { + "type": "number", + "name": "maximumLevel" + } + ] + }, + { + "name": "setPageScaleLimits", + "nocompile": true, + "type": "function", + "parameters": [ + { + "type": "number", + "name": "minScale" + }, + { + "type": "number", + "name": "maxScale" + } + ] + }, { "name": "executeJavaScript", "nocompile": true, diff --git a/brave/renderer/extensions/web_frame_bindings.cc b/brave/renderer/extensions/web_frame_bindings.cc index 836b17b557..5f794c5220 100644 --- a/brave/renderer/extensions/web_frame_bindings.cc +++ b/brave/renderer/extensions/web_frame_bindings.cc @@ -50,6 +50,18 @@ WebFrameBindings::WebFrameBindings(extensions::ScriptContext* context) "setSpellCheckProvider", base::Bind(&WebFrameBindings::SetSpellCheckProvider, base::Unretained(this))); + RouteFunction( + "setZoomLevel", + base::Bind(&WebFrameBindings::SetZoomLevel, + base::Unretained(this))); + RouteFunction( + "setZoomLevelLimits", + base::Bind(&WebFrameBindings::SetZoomLevelLimits, + base::Unretained(this))); + RouteFunction( + "setPageScaleLimits", + base::Bind(&WebFrameBindings::SetPageScaleLimits, + base::Unretained(this))); RouteFunction( "setGlobal", base::Bind(&WebFrameBindings::SetGlobal, base::Unretained(this))); @@ -148,7 +160,6 @@ void WebFrameBindings::ExecuteJavaScript( callback.release()); } - void WebFrameBindings::SetGlobal( const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = GetIsolate(); @@ -183,10 +194,13 @@ void WebFrameBindings::SetGlobal( } } -// double WebFrame::SetZoomLevel(double level) { -// double ret = web_frame_->view()->setZoomLevel(level); -// return ret; -// } +void WebFrameBindings::SetZoomLevel( + const v8::FunctionCallbackInfo& args) { + CHECK_EQ(args.Length(), 1); + CHECK(args[0]->IsInt32()); + double level = args[0].As()->Value(); + context()->web_frame()->view()->setZoomLevel(level); +} // double WebFrame::GetZoomLevel() const { // return web_frame_->view()->zoomLevel(); @@ -201,9 +215,15 @@ void WebFrameBindings::SetGlobal( // return blink::WebView::zoomLevelToZoomFactor(GetZoomLevel()); // } -// void WebFrame::SetZoomLevelLimits(double min_level, double max_level) { -// web_frame_->view()->zoomLimitsChanged(min_level, max_level); -// } +void WebFrameBindings::SetZoomLevelLimits( + const v8::FunctionCallbackInfo& args) { + CHECK_EQ(args.Length(), 2); + CHECK(args[0]->IsInt32()); + CHECK(args[1]->IsInt32()); + float min_level = args[0].As()->Value(); + float max_level = args[1].As()->Value(); + context()->web_frame()->view()->zoomLimitsChanged(min_level, max_level); +} // float WebFrame::GetPageScaleFactor() { // return web_frame_->view()->pageScaleFactor(); @@ -213,10 +233,17 @@ void WebFrameBindings::SetGlobal( // web_frame_->view()->setPageScaleFactor(factor); // } -// void WebFrame::SetPageScaleLimits(float min_scale, float max_scale) { -// web_frame_->view()->setDefaultPageScaleLimits(min_scale, max_scale); -// web_frame_->view()->setIgnoreViewportTagScaleLimits(true); -// } +void WebFrameBindings::SetPageScaleLimits( + const v8::FunctionCallbackInfo& args) { + CHECK_EQ(args.Length(), 2); + CHECK(args[0]->IsInt32()); + CHECK(args[1]->IsInt32()); + auto web_frame = context()->web_frame(); + float min_scale = args[0].As()->Value(); + float max_scale = args[1].As()->Value(); + web_frame->view()->setDefaultPageScaleLimits(min_scale, max_scale); + web_frame->view()->setIgnoreViewportTagScaleLimits(true); +} // float WebFrame::GetTextZoomFactor() { // return web_frame_->view()->textZoomFactor(); diff --git a/brave/renderer/extensions/web_frame_bindings.h b/brave/renderer/extensions/web_frame_bindings.h index 039e819c0d..87c1936fc6 100644 --- a/brave/renderer/extensions/web_frame_bindings.h +++ b/brave/renderer/extensions/web_frame_bindings.h @@ -30,6 +30,11 @@ class WebFrameBindings : public extensions::ObjectBackedNativeHandler { void RegisterEmbedderCustomElement( const v8::FunctionCallbackInfo& args); void SetSpellCheckProvider(const v8::FunctionCallbackInfo& args); + + void SetZoomLevel(const v8::FunctionCallbackInfo& args); + void SetZoomLevelLimits(const v8::FunctionCallbackInfo& args); + void SetPageScaleLimits(const v8::FunctionCallbackInfo& args); + void SetGlobal(const v8::FunctionCallbackInfo& args); void ExecuteJavaScript(const v8::FunctionCallbackInfo& args); void Invalidate() override;