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.
4
5#include "chrome/browser/ui/views/frame/contents_web_view.h"
6
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/aura/window.h"
11#include "ui/base/theme_provider.h"
12#include "ui/compositor/layer_tree_owner.h"
13#include "ui/views/background.h"
14#include "ui/wm/core/window_util.h"
15
16ContentsWebView::ContentsWebView(content::BrowserContext* browser_context)
17    : views::WebView(browser_context),
18      status_bubble_(NULL) {
19}
20
21ContentsWebView::~ContentsWebView() {
22}
23
24void ContentsWebView::SetStatusBubble(StatusBubbleViews* status_bubble) {
25  status_bubble_ = status_bubble;
26  DCHECK(!status_bubble_ || status_bubble_->base_view() == this);
27  if (status_bubble_)
28    status_bubble_->Reposition();
29}
30
31bool ContentsWebView::GetNeedsNotificationWhenVisibleBoundsChange() const {
32  return true;
33}
34
35void ContentsWebView::OnVisibleBoundsChanged() {
36  if (status_bubble_)
37    status_bubble_->Reposition();
38}
39
40void ContentsWebView::ViewHierarchyChanged(
41    const ViewHierarchyChangedDetails& details) {
42  WebView::ViewHierarchyChanged(details);
43  if (details.is_add)
44    OnThemeChanged();
45}
46
47void ContentsWebView::OnThemeChanged() {
48  ui::ThemeProvider* const theme = GetThemeProvider();
49  if (!theme)
50    return;
51
52  // Set the background color to a dark tint of the new tab page's background
53  // color.  This is the color filled within the WebView's bounds when its child
54  // view is sized specially for fullscreen tab capture.  See WebView header
55  // file comments for more details.
56  const int kBackgroundBrightness = 0x33;  // 20%
57  const SkColor ntp_background =
58      theme->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
59  set_background(views::Background::CreateSolidBackground(
60      SkColorGetR(ntp_background) * kBackgroundBrightness / 0xFF,
61      SkColorGetG(ntp_background) * kBackgroundBrightness / 0xFF,
62      SkColorGetB(ntp_background) * kBackgroundBrightness / 0xFF,
63      SkColorGetA(ntp_background)));
64}
65
66void ContentsWebView::OnLayerRecreated(ui::Layer* old_layer,
67                                       ui::Layer* new_layer) {
68  if (!cloned_layer_tree_)
69    return;
70
71  // Our layer has been recreated and we have a clone of the WebContents
72  // layer. Combined this means we're about to be destroyed and an animation is
73  // in effect. The animation cloned our layer, but it won't create another
74  // clone of the WebContents layer (|cloned_layer_tree_|). Another clone
75  // is not created as the clone has no owner (see CloneChildren()). Because we
76  // want the WebContents layer clone to be animated we move it to the
77  // old_layer, which is the layer the animation happens on. This animation ends
78  // up owning the layer (and all its descendants).
79  old_layer->Add(cloned_layer_tree_->release());
80  cloned_layer_tree_.reset();
81}
82
83void ContentsWebView::CloneWebContentsLayer() {
84  if (!web_contents())
85    return;
86  cloned_layer_tree_ = wm::RecreateLayers(web_contents()->GetNativeView());
87  if (!cloned_layer_tree_ || !cloned_layer_tree_->root()) {
88    cloned_layer_tree_.reset();
89    return;
90  }
91
92  SetPaintToLayer(true);
93  set_layer_owner_delegate(this);
94
95  // The cloned layer is in a different coordinate system them our layer (which
96  // is now the new parent of the cloned layer). Convert coordinates so that the
97  // cloned layer appears at the right location.
98  gfx::Point origin;
99  ui::Layer::ConvertPointToLayer(cloned_layer_tree_->root(), layer(), &origin);
100  cloned_layer_tree_->root()->SetBounds(
101      gfx::Rect(origin, cloned_layer_tree_->root()->bounds().size()));
102  layer()->Add(cloned_layer_tree_->root());
103}
104
105void ContentsWebView::DestroyClonedLayer() {
106  cloned_layer_tree_.reset();
107  SetPaintToLayer(false);
108  set_layer_owner_delegate(NULL);
109}
110