summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/frame/contents_web_view.cc
blob: f77f34515dd190739e49d64309bea88f202821c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/frame/contents_web_view.h"

#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/views/status_bubble_views.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/theme_provider.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/views/background.h"

#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/wm/core/window_util.h"
#endif

ContentsWebView::ContentsWebView(content::BrowserContext* browser_context)
    : views::WebView(browser_context),
      status_bubble_(nullptr) {
}

ContentsWebView::~ContentsWebView() {
}

void ContentsWebView::SetStatusBubble(StatusBubbleViews* status_bubble) {
  status_bubble_ = status_bubble;
  DCHECK(!status_bubble_ || status_bubble_->base_view() == this);
  if (status_bubble_)
    status_bubble_->Reposition();
}

bool ContentsWebView::GetNeedsNotificationWhenVisibleBoundsChange() const {
  return true;
}

void ContentsWebView::OnVisibleBoundsChanged() {
  if (status_bubble_)
    status_bubble_->Reposition();
}

void ContentsWebView::ViewHierarchyChanged(
    const ViewHierarchyChangedDetails& details) {
  WebView::ViewHierarchyChanged(details);
  if (details.is_add)
    OnThemeChanged();
}

void ContentsWebView::OnThemeChanged() {
  const ui::ThemeProvider* const theme = GetThemeProvider();
  if (!theme)
    return;

  // Set the background color to a dark tint of the new tab page's background
  // color.  This is the color filled within the WebView's bounds when its child
  // view is sized specially for fullscreen tab capture.  See WebView header
  // file comments for more details.
  const int kBackgroundBrightness = 0x33;  // 20%
  const SkColor ntp_background =
      theme->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
  set_background(views::Background::CreateSolidBackground(
      SkColorGetR(ntp_background) * kBackgroundBrightness / 0xFF,
      SkColorGetG(ntp_background) * kBackgroundBrightness / 0xFF,
      SkColorGetB(ntp_background) * kBackgroundBrightness / 0xFF,
      SkColorGetA(ntp_background)));
}

void ContentsWebView::OnLayerRecreated(ui::Layer* old_layer,
                                       ui::Layer* new_layer) {
  if (!cloned_layer_tree_)
    return;

  // Our layer has been recreated and we have a clone of the WebContents
  // layer. Combined this means we're about to be destroyed and an animation is
  // in effect. The animation cloned our layer, but it won't create another
  // clone of the WebContents layer (|cloned_layer_tree_|). Another clone
  // is not created as the clone has no owner (see CloneChildren()). Because we
  // want the WebContents layer clone to be animated we move it to the
  // old_layer, which is the layer the animation happens on. This animation ends
  // up owning the layer (and all its descendants).
  old_layer->Add(cloned_layer_tree_->release());
  cloned_layer_tree_.reset();
}

void ContentsWebView::CloneWebContentsLayer() {
  if (!web_contents())
    return;
#if defined(USE_AURA)
  // We don't need to clone the layers on non-Aura (Mac), because closing an
  // NSWindow does not animate.
  cloned_layer_tree_ = wm::RecreateLayers(web_contents()->GetNativeView());
#endif
  if (!cloned_layer_tree_ || !cloned_layer_tree_->root()) {
    cloned_layer_tree_.reset();
    return;
  }

  SetPaintToLayer(true);
  set_layer_owner_delegate(this);

  // The cloned layer is in a different coordinate system them our layer (which
  // is now the new parent of the cloned layer). Convert coordinates so that the
  // cloned layer appears at the right location.
  gfx::Point origin;
  ui::Layer::ConvertPointToLayer(cloned_layer_tree_->root(), layer(), &origin);
  cloned_layer_tree_->root()->SetBounds(
      gfx::Rect(origin, cloned_layer_tree_->root()->bounds().size()));
  layer()->Add(cloned_layer_tree_->root());
}

void ContentsWebView::DestroyClonedLayer() {
  cloned_layer_tree_.reset();
  SetPaintToLayer(false);
  set_layer_owner_delegate(nullptr);
}