Skip to content

Commit

Permalink
[Android] Add supportMultipleWindows setting
Browse files Browse the repository at this point in the history
Add supportMultipleWindows settings for reusing the same view
when opening popups. This is required for emulating the behavior
of Android WebView.

Depends on the WebKit patch: https://bugs.webkit.org/show_bug.cgi?id=99716

R=joth@chromium.org
BUG=156600


Review URL: https://chromiumcodereview.appspot.com/11192057

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164909 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mnaganov@chromium.org committed Oct 30, 2012
1 parent a35aa36 commit b2142e9
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,72 @@ private String getData() {
}
}

class AwSettingsJavaScriptPopupsTestHelper extends AwSettingsTestHelper<Boolean> {
static private final String POPUP_ENABLED = "Popup enabled";
static private final String POPUP_BLOCKED = "Popup blocked";

AwSettingsJavaScriptPopupsTestHelper(
AwContents awContents,
TestAwContentsClient contentViewClient,
int index) throws Throwable {
super(awContents, contentViewClient, true);
}

@Override
protected Boolean getAlteredValue() {
return ENABLED;
}

@Override
protected Boolean getInitialValue() {
return DISABLED;
}

@Override
protected Boolean getCurrentValue() {
return mContentSettings.getJavaScriptCanOpenWindowsAutomatically();
}

@Override
protected void setCurrentValue(Boolean value) {
mContentSettings.setJavaScriptCanOpenWindowsAutomatically(value);
}

@Override
protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
loadDataSync(getData());
final boolean expectPopupEnabled = value;
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
String title = getTitleOnUiThread();
return expectPopupEnabled ? POPUP_ENABLED.equals(title) :
POPUP_BLOCKED.equals(title);
} catch (Throwable t) {
t.printStackTrace();
fail("Failed to getTitleOnUiThread: " + t.toString());
return false;
}
}
}, TEST_TIMEOUT, CHECK_INTERVAL));
assertEquals(value ? POPUP_ENABLED : POPUP_BLOCKED, getTitleOnUiThread());
}

private String getData() {
return "<html><head>" +
"<script>" +
" function tryOpenWindow() {" +
" var newWindow = window.open(" +
" 'data:text/html;charset=utf-8," +
" <html><head><title>" + POPUP_ENABLED + "</title></head></html>');" +
" if (!newWindow) document.title = '" + POPUP_BLOCKED + "';" +
" }" +
"</script></head>" +
"<body onload='tryOpenWindow()'></body></html>";
}
}

// The test verifies that JavaScript is disabled upon WebView
// creation without accessing ContentSettings. If the test passes,
// it means that WebView-specific web preferences configuration
Expand Down Expand Up @@ -1825,6 +1891,27 @@ public void testTextZoomBoth() throws Throwable {
new AwSettingsTextZoomTestHelper(views.getContents1(), views.getClient1()));
}

public void testJavaScriptPopupsNormal() throws Throwable {
ViewPair views = createViews(NORMAL_VIEW, NORMAL_VIEW);
runPerViewSettingsTest(
new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views.getClient0(), 0),
new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views.getClient1(), 1));
}

public void testJavaScriptPopupsIncognito() throws Throwable {
ViewPair views = createViews(INCOGNITO_VIEW, INCOGNITO_VIEW);
runPerViewSettingsTest(
new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views.getClient0(), 0),
new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views.getClient1(), 1));
}

public void testJavaScriptPopupsBoth() throws Throwable {
ViewPair views = createViews(NORMAL_VIEW, INCOGNITO_VIEW);
runPerViewSettingsTest(
new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views.getClient0(), 0),
new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views.getClient1(), 1));
}

class ViewPair {
private final AwContents contents0;
private final TestAwContentsClient client0;
Expand Down
12 changes: 12 additions & 0 deletions content/browser/android/content_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ struct ContentSettings::FieldIds {
GetFieldID(env, clazz, "mAllowFileAccessFromFileURLs", "Z");
java_script_can_open_windows_automatically =
GetFieldID(env, clazz, "mJavaScriptCanOpenWindowsAutomatically", "Z");
support_multiple_windows =
GetFieldID(env, clazz, "mSupportMultipleWindows", "Z");
dom_storage_enabled =
GetFieldID(env, clazz, "mDomStorageEnabled", "Z");
}
Expand All @@ -100,6 +102,7 @@ struct ContentSettings::FieldIds {
jfieldID allow_universal_access_from_file_urls;
jfieldID allow_file_access_from_file_urls;
jfieldID java_script_can_open_windows_automatically;
jfieldID support_multiple_windows;
jfieldID dom_storage_enabled;
};

Expand Down Expand Up @@ -236,6 +239,12 @@ void ContentSettings::SyncFromNativeImpl() {
prefs.javascript_can_open_windows_automatically);
CheckException(env);

env->SetBooleanField(
obj,
field_ids_->support_multiple_windows,
prefs.supports_multiple_windows);
CheckException(env);

Java_ContentSettings_setPluginsDisabled(env, obj, !prefs.plugins_enabled);
CheckException(env);

Expand Down Expand Up @@ -337,6 +346,9 @@ void ContentSettings::SyncToNativeImpl() {
prefs.javascript_can_open_windows_automatically = env->GetBooleanField(
obj, field_ids_->java_script_can_open_windows_automatically);

prefs.supports_multiple_windows = env->GetBooleanField(
obj, field_ids_->support_multiple_windows);

prefs.plugins_enabled = !Java_ContentSettings_getPluginsDisabled(env, obj);

prefs.local_storage_enabled = env->GetBooleanField(
Expand Down
1 change: 1 addition & 0 deletions content/common/view_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ IPC_STRUCT_TRAITS_BEGIN(webkit_glue::WebPreferences)
IPC_STRUCT_TRAITS_MEMBER(text_autosizing_enabled)
IPC_STRUCT_TRAITS_MEMBER(font_scale_factor)
IPC_STRUCT_TRAITS_MEMBER(force_enable_zoom)
IPC_STRUCT_TRAITS_MEMBER(supports_multiple_windows)
#endif
IPC_STRUCT_TRAITS_END()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ContentSettings {
private boolean mAllowUniversalAccessFromFileURLs = false;
private boolean mAllowFileAccessFromFileURLs = false;
private boolean mJavaScriptCanOpenWindowsAutomatically = false;
private boolean mSupportMultipleWindows = false;
private PluginState mPluginState = PluginState.OFF;
private boolean mDomStorageEnabled = false;

Expand Down Expand Up @@ -886,6 +887,34 @@ public boolean getJavaScriptCanOpenWindowsAutomatically() {
}
}

/**
* Tells the WebView whether it supports multiple windows. True means
* that {@link WebChromeClient#onCreateWindow(WebView, boolean,
* boolean, Message)} is implemented by the host application.
*/
public void setSupportMultipleWindows(boolean support) {
assert mCanModifySettings;
synchronized (mContentSettingsLock) {
if (mSupportMultipleWindows != support) {
mSupportMultipleWindows = support;
mEventHandler.syncSettingsLocked();
}
}
}

/**
* Gets whether the WebView is supporting multiple windows.
*
* @return true if the WebView is supporting multiple windows. This means
* that {@link WebChromeClient#onCreateWindow(WebView, boolean,
* boolean, Message)} is implemented by the host application.
*/
public boolean supportMultipleWindows() {
synchronized (mContentSettingsLock) {
return mSupportMultipleWindows;
}
}

/**
* Sets whether the DOM storage API is enabled. The default value is false.
*
Expand Down
15 changes: 11 additions & 4 deletions content/renderer/render_view_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2343,13 +2343,20 @@ void RenderViewImpl::didBlur() {
// created RenderView (i.e., as a blocked popup or as a new tab).
//
void RenderViewImpl::show(WebNavigationPolicy policy) {
DCHECK(!did_show_) << "received extraneous Show call";
DCHECK(opener_id_ != MSG_ROUTING_NONE);

if (did_show_)
if (did_show_) {
#if defined(OS_ANDROID)
// When supports_multiple_windows is disabled, popups are reusing
// the same view. In some scenarios, this makes WebKit to call show() twice.
if (!webkit_preferences_.supports_multiple_windows)
return;
#endif
NOTREACHED() << "received extraneous Show call";
return;
}
did_show_ = true;

DCHECK(opener_id_ != MSG_ROUTING_NONE);

if (GetContentClient()->renderer()->AllowPopup(creator_url_))
opened_by_user_gesture_ = true;

Expand Down
37 changes: 21 additions & 16 deletions webkit/glue/webpreferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ WebPreferences::WebPreferences()
fullscreen_enabled(false),
allow_displaying_insecure_content(true),
allow_running_insecure_content(false),
#if defined(OS_ANDROID)
text_autosizing_enabled(true),
font_scale_factor(1.0f),
force_enable_zoom(false),
user_gesture_required_for_media_playback(true),
#endif
password_echo_enabled(false),
should_print_backgrounds(false),
enable_scroll_animator(false),
Expand All @@ -124,7 +118,16 @@ WebPreferences::WebPreferences()
deferred_image_decoding_enabled(false),
number_of_cpu_cores(1),
cookie_enabled(true),
apply_page_scale_factor_in_compositor(false) {
apply_page_scale_factor_in_compositor(false)
#if defined(OS_ANDROID)
,
text_autosizing_enabled(true),
font_scale_factor(1.0f),
force_enable_zoom(false),
user_gesture_required_for_media_playback(true),
supports_multiple_windows(true)
#endif
{
standard_font_family_map[kCommonScript] =
ASCIIToUTF16("Times New Roman");
fixed_font_family_map[kCommonScript] =
Expand Down Expand Up @@ -407,15 +410,6 @@ void WebPreferences::Apply(WebView* web_view) const {
settings->setFullScreenEnabled(fullscreen_enabled);
settings->setAllowDisplayOfInsecureContent(allow_displaying_insecure_content);
settings->setAllowRunningOfInsecureContent(allow_running_insecure_content);
#if defined(OS_ANDROID)
settings->setTextAutosizingEnabled(text_autosizing_enabled);
settings->setTextAutosizingFontScaleFactor(font_scale_factor);
web_view->setIgnoreViewportTagMaximumScale(force_enable_zoom);
settings->setAutoZoomFocusedNodeToLegibleScale(true);
settings->setDoubleTapToZoomEnabled(true);
settings->setMediaPlaybackRequiresUserGesture(
user_gesture_required_for_media_playback);
#endif
settings->setPasswordEchoEnabled(password_echo_enabled);
settings->setShouldPrintBackgrounds(should_print_backgrounds);
settings->setEnableScrollAnimator(enable_scroll_animator);
Expand All @@ -442,6 +436,17 @@ void WebPreferences::Apply(WebView* web_view) const {

settings->setDeferredImageDecodingEnabled(deferred_image_decoding_enabled);

#if defined(OS_ANDROID)
settings->setTextAutosizingEnabled(text_autosizing_enabled);
settings->setTextAutosizingFontScaleFactor(font_scale_factor);
web_view->setIgnoreViewportTagMaximumScale(force_enable_zoom);
settings->setAutoZoomFocusedNodeToLegibleScale(true);
settings->setDoubleTapToZoomEnabled(true);
settings->setMediaPlaybackRequiresUserGesture(
user_gesture_required_for_media_playback);
settings->setSupportsMultipleWindows(supports_multiple_windows);
#endif

WebNetworkStateNotifier::setOnLine(is_online);
}

Expand Down
14 changes: 8 additions & 6 deletions webkit/glue/webpreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ struct WEBKIT_GLUE_EXPORT WebPreferences {
bool fullscreen_enabled;
bool allow_displaying_insecure_content;
bool allow_running_insecure_content;
#if defined(OS_ANDROID)
bool text_autosizing_enabled;
float font_scale_factor;
bool force_enable_zoom;
bool user_gesture_required_for_media_playback;
#endif
bool password_echo_enabled;
bool should_print_backgrounds;
bool enable_scroll_animator;
Expand Down Expand Up @@ -151,6 +145,14 @@ struct WEBKIT_GLUE_EXPORT WebPreferences {

bool apply_page_scale_factor_in_compositor;

#if defined(OS_ANDROID)
bool text_autosizing_enabled;
float font_scale_factor;
bool force_enable_zoom;
bool user_gesture_required_for_media_playback;
bool supports_multiple_windows;
#endif

// We try to keep the default values the same as the default values in
// chrome, except for the cases where it would require lots of extra work for
// the embedder to use the same default value.
Expand Down

0 comments on commit b2142e9

Please sign in to comment.