1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/frame/contents_web_view.h"
7 #include "chrome/browser/themes/theme_properties.h"
8 #include "chrome/browser/ui/views/status_bubble_views.h"
9 #include "content/public/browser/web_contents.h"
10 #include "ui/base/theme_provider.h"
11 #include "ui/compositor/layer_tree_owner.h"
12 #include "ui/views/background.h"
15 #include "ui/aura/window.h"
16 #include "ui/wm/core/window_util.h"
19 ContentsWebView::ContentsWebView(content::BrowserContext
* browser_context
)
20 : views::WebView(browser_context
),
21 status_bubble_(nullptr) {
24 ContentsWebView::~ContentsWebView() {
27 void ContentsWebView::SetStatusBubble(StatusBubbleViews
* status_bubble
) {
28 status_bubble_
= status_bubble
;
29 DCHECK(!status_bubble_
|| status_bubble_
->base_view() == this);
31 status_bubble_
->Reposition();
34 bool ContentsWebView::GetNeedsNotificationWhenVisibleBoundsChange() const {
38 void ContentsWebView::OnVisibleBoundsChanged() {
40 status_bubble_
->Reposition();
43 void ContentsWebView::ViewHierarchyChanged(
44 const ViewHierarchyChangedDetails
& details
) {
45 WebView::ViewHierarchyChanged(details
);
50 void ContentsWebView::OnThemeChanged() {
51 ui::ThemeProvider
* const theme
= GetThemeProvider();
55 // Set the background color to a dark tint of the new tab page's background
56 // color. This is the color filled within the WebView's bounds when its child
57 // view is sized specially for fullscreen tab capture. See WebView header
58 // file comments for more details.
59 const int kBackgroundBrightness
= 0x33; // 20%
60 const SkColor ntp_background
=
61 theme
->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND
);
62 set_background(views::Background::CreateSolidBackground(
63 SkColorGetR(ntp_background
) * kBackgroundBrightness
/ 0xFF,
64 SkColorGetG(ntp_background
) * kBackgroundBrightness
/ 0xFF,
65 SkColorGetB(ntp_background
) * kBackgroundBrightness
/ 0xFF,
66 SkColorGetA(ntp_background
)));
69 void ContentsWebView::OnLayerRecreated(ui::Layer
* old_layer
,
70 ui::Layer
* new_layer
) {
71 if (!cloned_layer_tree_
)
74 // Our layer has been recreated and we have a clone of the WebContents
75 // layer. Combined this means we're about to be destroyed and an animation is
76 // in effect. The animation cloned our layer, but it won't create another
77 // clone of the WebContents layer (|cloned_layer_tree_|). Another clone
78 // is not created as the clone has no owner (see CloneChildren()). Because we
79 // want the WebContents layer clone to be animated we move it to the
80 // old_layer, which is the layer the animation happens on. This animation ends
81 // up owning the layer (and all its descendants).
82 old_layer
->Add(cloned_layer_tree_
->release());
83 cloned_layer_tree_
.reset();
86 void ContentsWebView::CloneWebContentsLayer() {
90 // We don't need to clone the layers on non-Aura (Mac), because closing an
91 // NSWindow does not animate.
92 cloned_layer_tree_
= wm::RecreateLayers(web_contents()->GetNativeView());
94 if (!cloned_layer_tree_
|| !cloned_layer_tree_
->root()) {
95 cloned_layer_tree_
.reset();
99 SetPaintToLayer(true);
100 set_layer_owner_delegate(this);
102 // The cloned layer is in a different coordinate system them our layer (which
103 // is now the new parent of the cloned layer). Convert coordinates so that the
104 // cloned layer appears at the right location.
106 ui::Layer::ConvertPointToLayer(cloned_layer_tree_
->root(), layer(), &origin
);
107 cloned_layer_tree_
->root()->SetBounds(
108 gfx::Rect(origin
, cloned_layer_tree_
->root()->bounds().size()));
109 layer()->Add(cloned_layer_tree_
->root());
112 void ContentsWebView::DestroyClonedLayer() {
113 cloned_layer_tree_
.reset();
114 SetPaintToLayer(false);
115 set_layer_owner_delegate(nullptr);