Fork: Add setting to blend background with existing content

Cherry-pick of uncommitted https://codereview.chromium.org/23483051/ PS8

BUG: 10750978

If both base background color (WebView::setBaseBackgroundColor) and
background color (set by document) has alpha, then the background color
should be blended into existing content.

This behavior is important in Android WebView, which is a view inside
the  Android view tree and needs to blend with views below it.

Change-Id: Ica4cdeaa35b971b75ef1e664cd54f3fc7f95507a
diff --git a/Source/core/page/Settings.in b/Source/core/page/Settings.in
index e4c6f77..c00b5e5 100644
--- a/Source/core/page/Settings.in
+++ b/Source/core/page/Settings.in
@@ -38,6 +38,7 @@
 supportsMultipleWindows initial=true
 javaScriptCanAccessClipboard initial=false
 shouldPrintBackgrounds initial=false
+shouldClearDocumentBackground initial=true
 
 textAreasAreResizable initial=false, setNeedsStyleRecalcInAllFrames=1
 authorAndUserStylesEnabled initial=true, setNeedsStyleRecalcInAllFrames=1
diff --git a/Source/core/rendering/RenderBoxModelObject.cpp b/Source/core/rendering/RenderBoxModelObject.cpp
index d2be443..289bfed 100644
--- a/Source/core/rendering/RenderBoxModelObject.cpp
+++ b/Source/core/rendering/RenderBoxModelObject.cpp
@@ -706,29 +706,28 @@
             if (!boxShadowShouldBeAppliedToBackground)
                 backgroundRect.intersect(paintInfo.rect);
 
-            // If we have an alpha and we are painting the root element, go ahead and blend with the base background color.
-            Color baseColor;
-            bool shouldClearBackground = false;
-            if (isOpaqueRoot) {
-                baseColor = view()->frameView()->baseBackgroundColor();
-                if (!baseColor.alpha())
-                    shouldClearBackground = true;
-            }
-
             GraphicsContextStateSaver shadowStateSaver(*context, boxShadowShouldBeAppliedToBackground);
             if (boxShadowShouldBeAppliedToBackground)
                 applyBoxShadowForBackground(context, this);
 
-            if (baseColor.alpha()) {
-                if (bgColor.alpha())
-                    baseColor = baseColor.blend(bgColor);
+            if (isOpaqueRoot) {
+                // If we have an alpha and we are painting the root element, go ahead and blend with the base background color.
+                Color baseColor = view()->frameView()->baseBackgroundColor();
+                bool shouldClearDocumentBackground = document()->settings() && document()->settings()->shouldClearDocumentBackground();
+                CompositeOperator operation = shouldClearDocumentBackground ? CompositeCopy : context->compositeOperation();
 
-                context->fillRect(backgroundRect, baseColor, CompositeCopy);
+                if (baseColor.alpha()) {
+                    if (bgColor.alpha())
+                        baseColor = baseColor.blend(bgColor);
+                    context->fillRect(backgroundRect, baseColor, operation);
+                } else if (bgColor.alpha()) {
+                    context->fillRect(backgroundRect, bgColor, operation);
+                } else if (shouldClearDocumentBackground) {
+                    context->clearRect(backgroundRect);
+                }
             } else if (bgColor.alpha()) {
-                CompositeOperator operation = shouldClearBackground ? CompositeCopy : context->compositeOperation();
-                context->fillRect(backgroundRect, bgColor, operation);
-            } else if (shouldClearBackground)
-                context->clearRect(backgroundRect);
+                context->fillRect(backgroundRect, bgColor, context->compositeOperation());
+            }
         }
     }
 
diff --git a/Source/web/WebSettingsImpl.cpp b/Source/web/WebSettingsImpl.cpp
index b69c54d..cdaafc4 100644
--- a/Source/web/WebSettingsImpl.cpp
+++ b/Source/web/WebSettingsImpl.cpp
@@ -597,6 +597,11 @@
     m_settings->setShouldPrintBackgrounds(enabled);
 }
 
+void WebSettingsImpl::setShouldClearDocumentBackground(bool enabled)
+{
+    m_settings->setShouldClearDocumentBackground(enabled);
+}
+
 void WebSettingsImpl::setEnableScrollAnimator(bool enabled)
 {
     m_settings->setScrollAnimatorEnabled(enabled);
diff --git a/Source/web/WebSettingsImpl.h b/Source/web/WebSettingsImpl.h
index 9908d74..e25d45a 100644
--- a/Source/web/WebSettingsImpl.h
+++ b/Source/web/WebSettingsImpl.h
@@ -140,6 +140,7 @@
     virtual void setShouldDisplaySubtitles(bool);
     virtual void setShouldDisplayTextDescriptions(bool);
     virtual void setShouldPrintBackgrounds(bool);
+    virtual void setShouldClearDocumentBackground(bool);
     virtual void setShouldRespectImageOrientation(bool);
     virtual void setShowFPSCounter(bool);
     virtual void setShowPaintRects(bool);
diff --git a/public/web/WebSettings.h b/public/web/WebSettings.h
index 56668da..69a68f9 100644
--- a/public/web/WebSettings.h
+++ b/public/web/WebSettings.h
@@ -144,6 +144,7 @@
     virtual void setSelectionIncludesAltImageText(bool) = 0;
     virtual void setSerifFontFamily(const WebString&, UScriptCode = USCRIPT_COMMON) = 0;
     virtual void setShouldPrintBackgrounds(bool) = 0;
+    virtual void setShouldClearDocumentBackground(bool) = 0;
     virtual void setShouldRespectImageOrientation(bool) = 0;
     virtual void setShowFPSCounter(bool) = 0;
     virtual void setShowPaintRects(bool) = 0;