Cherry-pick: [Android WebView] API to enable fullscreen.

NOTE: this cherry-pick will only be landed after these changes
https://googleplex-android-review.git.corp.google.com/#/c/523937/
https://googleplex-android-review.git.corp.google.com/#/c/523939/

Added a new method, ie. AwContents.setFullscreenSupported, that
will be called by the glue layer when the WebView app supports
fullscreen.

This is follow-up change to:
https://codereview.chromium.org/428633004/

BUG=389496

Review URL: https://codereview.chromium.org/425343002

Cr-Commit-Position: refs/heads/master@{#288723}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288723 0039d316-1c4b-4281-b951-d872f2087c98

Conflicts:
	android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenVideoTest.java
	android_webview/native/aw_settings.cc
	content/public/common/common_param_traits_macros.h
	content/public/common/web_preferences.cc
	content/public/common/web_preferences.h
	content/renderer/render_view_impl.cc

Change-Id: Ie0fb7da4a7ccb5557a22ba5c2ab1d9df2e373876
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 587ca48..c93dcdd 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwSettings.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwSettings.java
@@ -111,6 +111,7 @@
     private boolean mShouldFocusFirstNode = true;
     private boolean mGeolocationEnabled = true;
     private boolean mAutoCompleteEnabled = true;
+    private boolean mFullscreenSupported = false;
     private boolean mSupportZoom = true;
     private boolean mBuiltInZoomControls = false;
     private boolean mDisplayZoomControls = true;
@@ -461,6 +462,21 @@
         return mEnableSupportedHardwareAcceleratedFeatures;
     }
 
+    public void setFullscreenSupported(boolean supported) {
+        synchronized (mAwSettingsLock) {
+            if (mFullscreenSupported != supported) {
+                mFullscreenSupported = supported;
+                mEventHandler.updateWebkitPreferencesLocked();
+            }
+        }
+    }
+
+    @CalledByNative
+    private boolean getFullscreenSupportedLocked() {
+        assert Thread.holdsLock(mAwSettingsLock);
+        return mFullscreenSupported;
+    }
+
     /**
      * See {@link android.webkit.WebSettings#setNeedInitialFocus}.
      */
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenVideoTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenVideoTest.java
index fb29ea5..ece626c 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenVideoTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenVideoTest.java
@@ -10,6 +10,7 @@
 
 import junit.framework.Assert;
 
+import org.chromium.android_webview.test.util.JavascriptEventObserver;
 import org.chromium.android_webview.test.util.VideoTestWebServer;
 import org.chromium.base.CommandLine;
 import org.chromium.base.test.util.Feature;
@@ -35,6 +36,7 @@
                 createAwTestContainerViewOnMainSync(mContentsClient);
         mContentViewCore = testContainerView.getContentViewCore();
         enableJavaScriptOnUiThread(testContainerView.getAwContents());
+        mTestContainerView.getAwContents().getSettings().setFullscreenSupported(true);
         webServer = new VideoTestWebServer(
                 getInstrumentation().getTargetContext());
     }
@@ -109,6 +111,25 @@
                 mContentViewCore, VideoTestWebServer.VIDEO_ID));
     }
 
+    @MediumTest
+    @Feature({"AndroidWebView"})
+    public void testFullscreenNotSupported() throws Throwable {
+        mTestContainerView.getAwContents().getSettings().setFullscreenSupported(false);
+
+        final JavascriptEventObserver fullScreenErrorObserver = new JavascriptEventObserver();
+        getInstrumentation().runOnMainSync(new Runnable() {
+            @Override
+            public void run() {
+                fullScreenErrorObserver.register(mContentViewCore, "javaFullScreenErrorObserver");
+            }
+        });
+
+        loadTestPageAndClickFullscreen();
+
+        Assert.assertTrue(fullScreenErrorObserver.waitForEvent(500));
+        Assert.assertFalse(mContentsClient.wasCustomViewShownCalled());
+    }
+
     private static boolean areHtmlControlsEnabled() {
         return !CommandLine.getInstance().hasSwitch(
                 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE);
@@ -122,13 +143,17 @@
     }
 
     private void doOnShowCustomViewTest() throws Exception {
-        loadUrlSync(testContainerView.getAwContents(),
+        loadTestPageAndClickFullscreen();
+        mContentsClient.waitForCustomViewShown();
+    }
+
+    private void loadTestPageAndClickFullscreen() throws Exception {
+        loadUrlSync(mTestContainerView.getAwContents(),
                 mContentsClient.getOnPageFinishedHelper(),
                 webServer.getFullScreenVideoTestURL());
 
         // Click the button in full_screen_video_test.html to enter fullscreen.
         TouchCommon touchCommon = new TouchCommon(this);
         touchCommon.singleClickView(testContainerView);
-        mContentsClient.waitForCustomViewShown();
     }
 }
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientGetVideoLoadingProgressViewTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientGetVideoLoadingProgressViewTest.java
index 0d424f5..25b1b03 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientGetVideoLoadingProgressViewTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientGetVideoLoadingProgressViewTest.java
@@ -62,6 +62,7 @@
         final AwTestContainerView testContainerView =
                 createAwTestContainerViewOnMainSync(contentsClient);
         final AwContents awContents = testContainerView.getAwContents();
+        awContents.getSettings().setFullscreenSupported(true);
         enableJavaScriptOnUiThread(awContents);
         VideoTestWebServer webServer = new VideoTestWebServer(
                 getInstrumentation().getTargetContext());
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/FullScreenVideoTestAwContentsClient.java b/android_webview/javatests/src/org/chromium/android_webview/test/FullScreenVideoTestAwContentsClient.java
index 37edab5..c1a82b8 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/FullScreenVideoTestAwContentsClient.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/FullScreenVideoTestAwContentsClient.java
@@ -65,6 +65,10 @@
         return mCustomView;
     }
 
+    public boolean wasCustomViewShownCalled() {
+        return mOnShowCustomViewCallbackHelper.getCallCount() > 0;
+    }
+
     public void waitForCustomViewShown() throws TimeoutException, InterruptedException {
         mOnShowCustomViewCallbackHelper.waitForCallback(0, 1, WAITING_SECONDS, TimeUnit.SECONDS);
     }
diff --git a/android_webview/native/aw_settings.cc b/android_webview/native/aw_settings.cc
index 0ed0945..338aaa1 100644
--- a/android_webview/native/aw_settings.cc
+++ b/android_webview/native/aw_settings.cc
@@ -433,6 +433,8 @@
       Java_AwSettings_getAllowRunningInsecureContentLocked(env, obj);
 
   web_prefs->disallow_fullscreen_for_non_media_elements = true;
+  web_prefs->fullscreen_supported =
+      Java_AwSettings_getFullscreenSupportedLocked(env, obj);
 }
 
 static jlong Init(JNIEnv* env,
diff --git a/android_webview/test/shell/assets/full_screen_video_test.html b/android_webview/test/shell/assets/full_screen_video_test.html
index 618b342..d36c546 100644
--- a/android_webview/test/shell/assets/full_screen_video_test.html
+++ b/android_webview/test/shell/assets/full_screen_video_test.html
@@ -8,10 +8,13 @@
   }
 }
 
-addEventListener('Loaded', function() {
+addEventListener('DOMContentLoaded', function() {
     document.getElementById('video').addEventListener('play', function() {
         console.log('Video Played');
     }, false);
+    document.addEventListener('webkitfullscreenerror', function() {
+        javaFullScreenErrorObserver.notifyJava();
+    }, false);
 }, false);
 </script>
 </head>
diff --git a/content/public/common/common_param_traits_macros.h b/content/public/common/common_param_traits_macros.h
index bb24995..32443d0 100644
--- a/content/public/common/common_param_traits_macros.h
+++ b/content/public/common/common_param_traits_macros.h
@@ -185,6 +185,7 @@
   IPC_STRUCT_TRAITS_MEMBER(device_scale_adjustment)
   IPC_STRUCT_TRAITS_MEMBER(force_enable_zoom)
   IPC_STRUCT_TRAITS_MEMBER(disallow_fullscreen_for_non_media_elements)
+  IPC_STRUCT_TRAITS_MEMBER(fullscreen_supported)
   IPC_STRUCT_TRAITS_MEMBER(double_tap_to_zoom_enabled)
   IPC_STRUCT_TRAITS_MEMBER(user_gesture_required_for_media_playback)
   IPC_STRUCT_TRAITS_MEMBER(default_video_poster_url)
diff --git a/content/renderer/web_preferences.cc b/content/renderer/web_preferences.cc
index 0eb198d..f4136c3 100644
--- a/content/renderer/web_preferences.cc
+++ b/content/renderer/web_preferences.cc
@@ -296,6 +296,7 @@
   settings->setAutoZoomFocusedNodeToLegibleScale(true);
   settings->setDisallowFullscreenForNonMediaElements(
       prefs.disallow_fullscreen_for_non_media_elements);
+  settings->setFullscreenSupported(prefs.fullscreen_supported);
   settings->setDoubleTapToZoomEnabled(prefs.double_tap_to_zoom_enabled);
   settings->setMediaControlsOverlayPlayButtonEnabled(true);
   settings->setMediaPlaybackRequiresUserGesture(
diff --git a/webkit/common/webpreferences.cc b/webkit/common/webpreferences.cc
index 78834ac..faa89da 100644
--- a/webkit/common/webpreferences.cc
+++ b/webkit/common/webpreferences.cc
@@ -121,6 +121,7 @@
       device_scale_adjustment(1.0f),
       force_enable_zoom(false),
       disallow_fullscreen_for_non_media_elements(false),
+      fullscreen_supported(true),
       double_tap_to_zoom_enabled(true),
       user_gesture_required_for_media_playback(true),
       support_deprecated_target_density_dpi(false),
diff --git a/webkit/common/webpreferences.h b/webkit/common/webpreferences.h
index cd675f9..cd36fd2 100644
--- a/webkit/common/webpreferences.h
+++ b/webkit/common/webpreferences.h
@@ -166,6 +166,7 @@
   float device_scale_adjustment;
   bool force_enable_zoom;
   bool disallow_fullscreen_for_non_media_elements;
+  bool fullscreen_supported;
   bool double_tap_to_zoom_enabled;
   bool user_gesture_required_for_media_playback;
   GURL default_video_poster_url;