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