fix to ensure plugins are not shown when they are marked hidden by the DOM.

Change-Id: Ic32359b699d71dcd1603ac9295c95100014fc484
diff --git a/WebCore/plugins/android/PluginViewAndroid.cpp b/WebCore/plugins/android/PluginViewAndroid.cpp
index 541fd21..c91b480 100644
--- a/WebCore/plugins/android/PluginViewAndroid.cpp
+++ b/WebCore/plugins/android/PluginViewAndroid.cpp
@@ -603,12 +603,31 @@
 {
     setSelfVisible(true);
     Widget::show();
+
+    if (platformPluginWidget())
+        platformPluginWidget()->updateSurfaceIfNeeded();
+
 }
 
 void PluginView::hide()
 {
     setSelfVisible(false);
     Widget::hide();
+
+   if (platformPluginWidget())
+        platformPluginWidget()->updateSurfaceIfNeeded();
+}
+
+void PluginView::setParentVisible(bool visible) {
+
+    if (isParentVisible() == visible)
+        return;
+
+    Widget::setParentVisible(visible);
+
+    if (platformPluginWidget())
+        platformPluginWidget()->updateSurfaceIfNeeded();
+
 }
 
 void PluginView::paint(GraphicsContext* context, const IntRect& rect)
@@ -645,11 +664,6 @@
     }
 }
 
-// new as of SVN 38068, Nov 5 2008
-void PluginView::setParentVisible(bool) {
-    notImplemented();
-}
-
 void PluginView::halt() {
     notImplemented();
 }
diff --git a/WebKit/android/plugins/PluginDebugAndroid.h b/WebKit/android/plugins/PluginDebugAndroid.h
index 92092d5..3f328f9 100644
--- a/WebKit/android/plugins/PluginDebugAndroid.h
+++ b/WebKit/android/plugins/PluginDebugAndroid.h
@@ -35,7 +35,7 @@
 // Android plug-in code in this directory.
 #define PLUGIN_DEBUG_GLOBAL     0
 
-#if PLUGIN_DEBUG_GLOBAL || defined(PLUGIN_DEBUG_LOCAL)
+#if PLUGIN_DEBUG_GLOBAL || (defined(PLUGIN_DEBUG_LOCAL) && PLUGIN_DEBUG_LOCAL)
 # define PLUGIN_LOG(FORMAT, ARGS...) do { anp_logPlugin(FORMAT, ## ARGS); } while(0)
 # define PLUGIN_LOG_EVENT(NPP, EVT, RET, TIME) do { anp_logPluginEvent(NPP, EVT, RET, TIME); } while(0)
 
diff --git a/WebKit/android/plugins/PluginWidgetAndroid.cpp b/WebKit/android/plugins/PluginWidgetAndroid.cpp
index cec254e..7642f13 100644
--- a/WebKit/android/plugins/PluginWidgetAndroid.cpp
+++ b/WebKit/android/plugins/PluginWidgetAndroid.cpp
@@ -68,6 +68,7 @@
     m_visible = true;
     m_zoomLevel = 0;
     m_embeddedView = NULL;
+    m_embeddedViewAttached = false;
     m_acceptEvents = false;
 }
 
@@ -119,36 +120,13 @@
                        m_pluginWindow->x + m_pluginWindow->width,
                        m_pluginWindow->y + m_pluginWindow->height);
 
-    if (m_drawingModel == kSurface_ANPDrawingModel) {
+    PLUGIN_LOG("%p PluginBounds (%d,%d,%d,%d)", m_pluginView->instance(),
+               m_pluginBounds.fLeft, m_pluginBounds.fTop,
+               m_pluginBounds.fRight, m_pluginBounds.fBottom);
 
-        // if the surface does not exist then create a new surface
-        if (!m_embeddedView) {
+    updateSurfaceIfNeeded(m_pluginBounds != oldPluginBounds);
 
-            WebCore::PluginPackage* pkg = m_pluginView->plugin();
-            NPP instance = m_pluginView->instance();
-
-            jobject pluginSurface;
-            pkg->pluginFuncs()->getvalue(instance, kJavaSurface_ANPGetValue,
-                                         static_cast<void*>(&pluginSurface));
-
-            jobject tempObj = m_core->addSurface(pluginSurface,
-                                                 window->x, window->y,
-                                                 window->width, window->height);
-            if (tempObj) {
-                JNIEnv* env = JSC::Bindings::getJNIEnv();
-                m_embeddedView = env->NewGlobalRef(tempObj);
-            }
-        } else if (m_pluginBounds != oldPluginBounds) {
-            // if the surface exists check for changes and update accordingly
-            if (m_isFullScreen) {
-                m_core->updateFullScreenPlugin(window->x, window->y,
-                        window->width, window->height);
-            } else {
-                m_core->updateSurface(m_embeddedView, window->x, window->y,
-                                      window->width, window->height);
-            }
-        }
-    } else {
+    if (m_drawingModel != kSurface_ANPDrawingModel) {
         m_flipPixelRef->safeUnref();
         m_flipPixelRef = new SkFlipPixelRef(computeConfig(isTransparent),
                                             window->width, window->height);
@@ -231,6 +209,50 @@
     }
 }
 
+void PluginWidgetAndroid::updateSurfaceIfNeeded(bool pluginBoundsChanged) {
+
+    if (m_drawingModel != kSurface_ANPDrawingModel)
+        return;
+
+    PLUGIN_LOG("%p Plugin Visible self=[%d] parent=[%d]", m_pluginView->instance(),
+            m_pluginView->isSelfVisible(), m_pluginView->isParentVisible());
+
+    // if the surface does not exist then create a new surface
+    if (!m_embeddedView && m_pluginView->isVisible()) {
+
+        WebCore::PluginPackage* pkg = m_pluginView->plugin();
+        NPP instance = m_pluginView->instance();
+
+        jobject pluginSurface;
+        pkg->pluginFuncs()->getvalue(instance, kJavaSurface_ANPGetValue,
+                                     static_cast<void*>(&pluginSurface));
+
+        jobject tempObj = m_core->addSurface(pluginSurface,
+                m_pluginWindow->x, m_pluginWindow->y,
+                m_pluginWindow->width, m_pluginWindow->height);
+
+        if (tempObj) {
+            JNIEnv* env = JSC::Bindings::getJNIEnv();
+            m_embeddedView = env->NewGlobalRef(tempObj);
+            m_embeddedViewAttached = true;
+        }
+    // if the view is unattached but visible then attach it
+    } else if (m_embeddedView && !m_embeddedViewAttached && m_pluginView->isVisible() && !m_isFullScreen) {
+        m_core->updateSurface(m_embeddedView, m_pluginWindow->x, m_pluginWindow->y,
+                              m_pluginWindow->width, m_pluginWindow->height);
+        m_embeddedViewAttached = true;
+    // if the view is attached but invisible then remove it
+    } else if (m_embeddedView && m_embeddedViewAttached && !m_pluginView->isVisible()) {
+        m_core->destroySurface(m_embeddedView);
+        m_embeddedViewAttached = false;
+    // if the plugin's bounds have changed and it's visible then update it
+    } else if (pluginBoundsChanged && m_pluginView->isVisible() && !m_isFullScreen) {
+        m_core->updateSurface(m_embeddedView, m_pluginWindow->x, m_pluginWindow->y,
+                              m_pluginWindow->width, m_pluginWindow->height);
+
+    }
+}
+
 int16 PluginWidgetAndroid::sendEvent(const ANPEvent& evt) {
     if (!m_acceptEvents)
         return 0;
@@ -316,16 +338,24 @@
     int newScreenW = m_visibleDocRect.width();
     int newScreenH = m_visibleDocRect.height();
 
-    PLUGIN_LOG("%s VisibleDoc Dimensions old=[%d,%d] new=[%d,%d] ",
-               __FUNCTION__, oldScreenW, oldScreenH, newScreenW, newScreenH);
-
     // if the screen dimensions have changed by more than 5 pixels in either
     // direction then recompute the plugin's visible rectangle
-    if (abs(oldScreenW - newScreenW) > 5 || abs(oldScreenH - newScreenH) > 5)
+    if (abs(oldScreenW - newScreenW) > 5 || abs(oldScreenH - newScreenH) > 5) {
+        PLUGIN_LOG("%s VisibleDoc old=[%d,%d] new=[%d,%d] ", __FUNCTION__,
+                   oldScreenW, oldScreenH, newScreenW, newScreenH);
         computeVisiblePluginRect();
+    }
 
     bool visible = SkIRect::Intersects(m_visibleDocRect, m_pluginBounds);
     if(m_visible != visible) {
+
+#if DEBUG_VISIBLE_RECTS
+        PLUGIN_LOG("%p changeVisiblity[%d] pluginBounds(%d,%d,%d,%d)",
+                   m_pluginView->instance(), visible,
+                   m_pluginBounds.fLeft, m_pluginBounds.fTop,
+                   m_pluginBounds.fRight, m_pluginBounds.fBottom);
+#endif
+
         // change the visibility
         m_visible = visible;
         // send the event
diff --git a/WebKit/android/plugins/PluginWidgetAndroid.h b/WebKit/android/plugins/PluginWidgetAndroid.h
index b47a4b3..7223a16 100644
--- a/WebKit/android/plugins/PluginWidgetAndroid.h
+++ b/WebKit/android/plugins/PluginWidgetAndroid.h
@@ -141,6 +141,13 @@
      */
     bool hasFocus() const { return m_hasFocus; }
 
+    /** Called to ensure the surface is being correctly displayed within the
+        view hierarchy. For instance, if the visibility of the plugin has
+        changed then we need to ensure the surface is added or removed from the
+        view system.
+     */
+    void updateSurfaceIfNeeded(bool pluginBoundsChanged = false);
+
 private:
     void computeVisiblePluginRect();
     void scrollToVisiblePluginRect();
@@ -159,6 +166,7 @@
     bool                    m_visible;
     float                   m_zoomLevel;
     jobject                 m_embeddedView;
+    bool                    m_embeddedViewAttached;
     bool                    m_acceptEvents;
 
     /* We limit the number of rectangles to minimize storage and ensure adequate