diff options
author | mnaganov@chromium.org <mnaganov@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-28 13:47:54 +0000 |
---|---|---|
committer | mnaganov@chromium.org <mnaganov@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-28 13:47:54 +0000 |
commit | 1d7c8e031c589b3f4b9707460976c26a7e687019 (patch) | |
tree | f2378e6564d76637e08b648c177ad667c1481920 /android_webview | |
parent | da116732d15bbe8ffc7db3c1d7de6ace7b8effaa (diff) | |
download | chromium_src-1d7c8e031c589b3f4b9707460976c26a7e687019.zip chromium_src-1d7c8e031c589b3f4b9707460976c26a7e687019.tar.gz chromium_src-1d7c8e031c589b3f4b9707460976c26a7e687019.tar.bz2 |
[Android WebView] Implement WebView.setInitialScale
WebView.setInitialScale allows to set up the initial page scale
which overrides viewport meta tag settings.
Review URL: https://chromiumcodereview.appspot.com/12315104
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@185236 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview')
9 files changed, 103 insertions, 0 deletions
diff --git a/android_webview/browser/renderer_host/aw_render_view_host_ext.cc b/android_webview/browser/renderer_host/aw_render_view_host_ext.cc index 1874a41..ee1d65e 100644 --- a/android_webview/browser/renderer_host/aw_render_view_host_ext.cc +++ b/android_webview/browser/renderer_host/aw_render_view_host_ext.cc @@ -79,6 +79,12 @@ void AwRenderViewHostExt::ResetScrollAndScaleState() { Send(new AwViewMsg_ResetScrollAndScaleState(web_contents()->GetRoutingID())); } +void AwRenderViewHostExt::SetInitialPageScale(double page_scale_factor) { + DCHECK(CalledOnValidThread()); + Send(new AwViewMsg_SetInitialPageScale(web_contents()->GetRoutingID(), + page_scale_factor)); +} + void AwRenderViewHostExt::RenderViewGone(base::TerminationStatus status) { DCHECK(CalledOnValidThread()); for (std::map<int, DocumentHasImagesResult>::iterator pending_req = diff --git a/android_webview/browser/renderer_host/aw_render_view_host_ext.h b/android_webview/browser/renderer_host/aw_render_view_host_ext.h index ac99d1c..e8f56f2 100644 --- a/android_webview/browser/renderer_host/aw_render_view_host_ext.h +++ b/android_webview/browser/renderer_host/aw_render_view_host_ext.h @@ -62,6 +62,10 @@ class AwRenderViewHostExt : public content::WebContentsObserver, void ResetScrollAndScaleState(); + // Sets the initial page scale. This overrides initial scale set by + // the meta viewport tag. + void SetInitialPageScale(double page_scale_factor); + private: // content::WebContentsObserver implementation. virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; diff --git a/android_webview/common/render_view_messages.h b/android_webview/common/render_view_messages.h index dfc48bc..c84e094 100644 --- a/android_webview/common/render_view_messages.h +++ b/android_webview/common/render_view_messages.h @@ -75,6 +75,11 @@ IPC_MESSAGE_ROUTED0(AwViewMsg_ResetScrollAndScaleState) IPC_MESSAGE_ROUTED1(AwViewMsg_SetEnableFixedLayoutMode, bool /* enabled */) +// Sets the initial page scale. This overrides initial scale set by +// the meta viewport tag. +IPC_MESSAGE_ROUTED1(AwViewMsg_SetInitialPageScale, + double /* page_scale_factor */) + //----------------------------------------------------------------------------- // RenderView messages // These are messages sent from the renderer to the browser process. diff --git a/android_webview/java/src/org/chromium/android_webview/AwSettings.java b/android_webview/java/src/org/chromium/android_webview/AwSettings.java index 3ff16e2..7af9eaf 100644 --- a/android_webview/java/src/org/chromium/android_webview/AwSettings.java +++ b/android_webview/java/src/org/chromium/android_webview/AwSettings.java @@ -160,6 +160,22 @@ public class AwSettings { } /** + * Sets the initial scale for this WebView. The default value + * is -1. A non-default value overrides initial scale set by + * the meta viewport tag. + */ + public void setInitialPageScale(final float scaleInPercent) { + // There is no need to lock, because the native code doesn't + // read anything from the Java side. + ThreadUtils.runOnUiThread(new Runnable() { + @Override + public void run() { + nativeSetInitialPageScale(mNativeAwSettings, scaleInPercent); + } + }); + } + + /** * Sets the text zoom of the page in percent. This kind of zooming is * only applicable when Text Autosizing is turned off. Passing -1 will * reset the zoom to the default value. @@ -216,5 +232,7 @@ public class AwSettings { private native void nativeSetEnableFixedLayoutMode(int nativeAwSettings, boolean enable); + private native void nativeSetInitialPageScale(int nativeAwSettings, float scaleInPercent); + private native void nativeSetTextZoom(int nativeAwSettings, int textZoom); } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java index d35509e..685b0f2 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java @@ -2413,6 +2413,43 @@ public class AwSettingsTest extends AndroidWebViewTestBase { assertEquals((float)pageScale, getScaleOnUiThread(awContents)); } + public void testSetInitialScale() throws Throwable { + final TestAwContentsClient contentClient = new TestAwContentsClient(); + final AwTestContainerView testContainerView = + createAwTestContainerViewOnMainSync(contentClient); + final AwContents awContents = testContainerView.getAwContents(); + final ContentViewCore contentViewCore = awContents.getContentViewCore(); + final AwSettings awSettings = getAwSettingsOnUiThread(awContents); + CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper(); + + final String page = "<html><body>" + + "<p style='height:1000px;width:1000px'>testSetInitialScale</p>" + + "</body></html>"; + final float defaultScale = 1.0f; + + assertEquals(defaultScale, getScaleOnUiThread(awContents)); + loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false); + assertEquals(defaultScale, getScaleOnUiThread(awContents)); + + int onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount(); + awSettings.setInitialPageScale(50); + loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false); + contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount); + assertEquals(0.5f, getScaleOnUiThread(awContents)); + + onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount(); + awSettings.setInitialPageScale(200); + loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false); + contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount); + assertEquals(2.0f, getScaleOnUiThread(awContents)); + + onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount(); + awSettings.setInitialPageScale(-1); + loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false); + contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount); + assertEquals(defaultScale, getScaleOnUiThread(awContents)); + } + static class ViewPair { private final AwContents contents0; private final TestAwContentsClient client0; diff --git a/android_webview/native/aw_settings.cc b/android_webview/native/aw_settings.cc index 5e3caeb..941eb0b 100644 --- a/android_webview/native/aw_settings.cc +++ b/android_webview/native/aw_settings.cc @@ -14,6 +14,7 @@ namespace android_webview { AwSettings::AwSettings(JNIEnv* env, jobject obj) : java_ref_(env, obj), enable_fixed_layout_(false), + initial_page_scale_percent_(-1), text_zoom_percent_(100) { } @@ -38,6 +39,13 @@ void AwSettings::SetEnableFixedLayoutMode( UpdateEnableFixedLayoutMode(); } +void AwSettings::SetInitialPageScale( + JNIEnv* env, jobject obj, jfloat page_scale_percent) { + if (initial_page_scale_percent_ == page_scale_percent) return; + initial_page_scale_percent_ = page_scale_percent; + UpdateInitialPageScale(); +} + void AwSettings::SetTextZoom(JNIEnv* env, jobject obj, jint text_zoom_percent) { if (text_zoom_percent_ == text_zoom_percent) return; text_zoom_percent_ = text_zoom_percent; @@ -48,12 +56,23 @@ void AwSettings::SetWebContents(JNIEnv* env, jobject obj, jint web_contents) { Observe(reinterpret_cast<content::WebContents*>(web_contents)); } + void AwSettings::UpdateEnableFixedLayoutMode() { AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); if (!rvhe) return; rvhe->SetEnableFixedLayoutMode(enable_fixed_layout_); } +void AwSettings::UpdateInitialPageScale() { + AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); + if (!rvhe) return; + if (initial_page_scale_percent_ == -1) { + rvhe->SetInitialPageScale(-1); + } else { + rvhe->SetInitialPageScale(initial_page_scale_percent_ / 100.0f); + } +} + void AwSettings::UpdateTextZoom() { AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); if (!rvhe) return; @@ -68,6 +87,7 @@ void AwSettings::UpdateTextZoom() { void AwSettings::RenderViewCreated(content::RenderViewHost* render_view_host) { UpdateEnableFixedLayoutMode(); + UpdateInitialPageScale(); UpdateTextZoom(); } diff --git a/android_webview/native/aw_settings.h b/android_webview/native/aw_settings.h index 4f7ec97..3088b1f 100644 --- a/android_webview/native/aw_settings.h +++ b/android_webview/native/aw_settings.h @@ -23,12 +23,14 @@ class AwSettings : public content::WebContentsObserver { // Called from Java. void Destroy(JNIEnv* env, jobject obj); void SetEnableFixedLayoutMode(JNIEnv* env, jobject obj, jboolean enabled); + void SetInitialPageScale(JNIEnv* env, jobject obj, jfloat page_scale_percent); void SetTextZoom(JNIEnv* env, jobject obj, jint text_zoom_percent); void SetWebContents(JNIEnv* env, jobject obj, jint web_contents); private: AwRenderViewHostExt* GetAwRenderViewHostExt(); void UpdateEnableFixedLayoutMode(); + void UpdateInitialPageScale(); void UpdateTextZoom(); // WebContentsObserver overrides: @@ -37,6 +39,7 @@ class AwSettings : public content::WebContentsObserver { JavaObjectWeakGlobalRef java_ref_; bool enable_fixed_layout_; + float initial_page_scale_percent_; int text_zoom_percent_; }; diff --git a/android_webview/renderer/aw_render_view_ext.cc b/android_webview/renderer/aw_render_view_ext.cc index b9b82dc..fdcfbee 100644 --- a/android_webview/renderer/aw_render_view_ext.cc +++ b/android_webview/renderer/aw_render_view_ext.cc @@ -149,6 +149,7 @@ bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomLevel, OnSetTextZoomLevel) IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState, OnResetScrollAndScaleState) + IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; @@ -263,4 +264,11 @@ void AwRenderViewExt::OnResetScrollAndScaleState() { render_view()->GetWebView()->resetScrollAndScaleState(); } +void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) { + if (!render_view() || !render_view()->GetWebView()) + return; + render_view()->GetWebView()->setInitialPageScaleOverride( + page_scale_factor); +} + } // namespace android_webview diff --git a/android_webview/renderer/aw_render_view_ext.h b/android_webview/renderer/aw_render_view_ext.h index 3583ac5..26daff8 100644 --- a/android_webview/renderer/aw_render_view_ext.h +++ b/android_webview/renderer/aw_render_view_ext.h @@ -47,6 +47,8 @@ class AwRenderViewExt : public content::RenderViewObserver, void OnResetScrollAndScaleState(); + void OnSetInitialPageScale(double page_scale_factor); + // WebKit::WebPermissionClient implementation. virtual bool allowImage(WebKit::WebFrame* frame, bool enabledPerSettings, |