summaryrefslogtreecommitdiff
path: root/chromium/ui/views/controls/webview/web_contents_set_background_color.cc
blob: 0cf575a78767aad47e0d1cc958158c0faab3f4c7 (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
// Copyright 2017 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 "ui/views/controls/webview/web_contents_set_background_color.h"

#include "base/memory/ptr_util.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"

namespace views {

// static
void WebContentsSetBackgroundColor::CreateForWebContentsWithColor(
    content::WebContents* web_contents,
    SkColor color) {
  if (FromWebContents(web_contents))
    return;

  // SupportsUserData::Data takes ownership over the
  // WebContentsSetBackgroundColor instance and will destroy it when the
  // WebContents instance is destroyed.
  web_contents->SetUserData(
      UserDataKey(),
      base::WrapUnique(new WebContentsSetBackgroundColor(web_contents, color)));
}

WebContentsSetBackgroundColor::WebContentsSetBackgroundColor(
    content::WebContents* web_contents,
    SkColor color)
    : content::WebContentsObserver(web_contents), color_(color) {}

WebContentsSetBackgroundColor::~WebContentsSetBackgroundColor() = default;

void WebContentsSetBackgroundColor::RenderFrameCreated(
    content::RenderFrameHost* render_frame_host) {
  // We set the background color just on the main frame's widget. Other frames
  // that are local roots would have a widget of their own, but their background
  // colors are part of, and controlled by, the webpage.
  if (!render_frame_host->GetParent())
    render_frame_host->GetView()->SetBackgroundColor(color_);
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsSetBackgroundColor)

}  // namespace views