Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / ui / kiosk_external_update_notification.cc
blobecb327263c1d3c7e23222091011923375fd3be4a
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/chromeos/ui/kiosk_external_update_notification.h"
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/window.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/compositor/scoped_layer_animation_settings.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/fill_layout.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/widget/widget_delegate.h"
20 namespace chromeos {
22 namespace {
24 const SkColor kTextColor = SK_ColorBLACK;
25 const SkColor kWindowBackgroundColor = SK_ColorWHITE;
26 const int kWindowCornerRadius = 4;
27 const int kPreferredWidth = 600;
28 const int kPreferredHeight = 250;
30 } // namespace
32 class KioskExternalUpdateNotificationView : public views::WidgetDelegateView {
33 public:
34 explicit KioskExternalUpdateNotificationView(
35 KioskExternalUpdateNotification* owner)
36 : owner_(owner), widget_closed_(false) {
37 AddLabel();
38 SetLayoutManager(new views::FillLayout);
41 ~KioskExternalUpdateNotificationView() override {
42 widget_closed_ = true;
43 InformOwnerForDismiss();
46 // Closes the widget immediately from |owner_|.
47 void CloseByOwner() {
48 owner_ = NULL;
49 if (!widget_closed_) {
50 widget_closed_ = true;
51 GetWidget()->Close();
55 void SetMessage(const base::string16& message) { label_->SetText(message); }
57 // views::WidgetDelegateView overrides:
58 void OnPaint(gfx::Canvas* canvas) override {
59 SkPaint paint;
60 paint.setStyle(SkPaint::kFill_Style);
61 paint.setColor(kWindowBackgroundColor);
62 canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, paint);
63 views::WidgetDelegateView::OnPaint(canvas);
66 gfx::Size GetPreferredSize() const override {
67 return gfx::Size(kPreferredWidth, kPreferredHeight);
70 private:
71 void AddLabel() {
72 label_ = new views::Label;
73 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
74 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
75 label_->SetFontList(rb->GetFontList(ui::ResourceBundle::BoldFont));
76 label_->SetEnabledColor(kTextColor);
77 label_->SetDisabledColor(kTextColor);
78 label_->SetAutoColorReadabilityEnabled(false);
79 label_->SetMultiLine(true);
80 AddChildView(label_);
83 void InformOwnerForDismiss() {
84 // Inform the |owner_| that we are going away.
85 if (owner_) {
86 KioskExternalUpdateNotification* owner = owner_;
87 owner_ = NULL;
88 owner->Dismiss();
92 // The owner of this message which needs to get notified when the message
93 // closes.
94 KioskExternalUpdateNotification* owner_;
95 views::Label* label_; // owned by views hierarchy.
97 // True if the widget got already closed.
98 bool widget_closed_;
100 DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdateNotificationView);
103 KioskExternalUpdateNotification::KioskExternalUpdateNotification(
104 const base::string16& message) {
105 CreateAndShowNotificationView(message);
108 KioskExternalUpdateNotification::~KioskExternalUpdateNotification() {
109 Dismiss();
112 void KioskExternalUpdateNotification::ShowMessage(
113 const base::string16& message) {
114 if (view_)
115 view_->SetMessage(message);
118 void KioskExternalUpdateNotification::CreateAndShowNotificationView(
119 const base::string16& message) {
120 view_ = new KioskExternalUpdateNotificationView(this);
121 view_->SetMessage(message);
123 aura::Window* root_window = ash::Shell::GetTargetRootWindow();
124 gfx::Size rs = root_window->bounds().size();
125 gfx::Size ps = view_->GetPreferredSize();
126 gfx::Rect bounds((rs.width() - ps.width()) / 2,
127 (rs.height() - ps.height()) / 10,
128 ps.width(),
129 ps.height());
130 views::Widget::InitParams params;
131 params.type = views::Widget::InitParams::TYPE_POPUP;
132 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
133 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET;
134 params.accept_events = false;
135 params.keep_on_top = true;
136 params.remove_standard_frame = true;
137 params.delegate = view_;
138 params.bounds = bounds;
139 params.parent = ash::Shell::GetContainer(
140 root_window, ash::kShellWindowId_SettingBubbleContainer);
141 views::Widget* widget = new views::Widget;
142 widget->Init(params);
143 widget->SetContentsView(view_);
144 gfx::NativeView native_view = widget->GetNativeView();
145 native_view->SetName("KioskExternalUpdateNotification");
146 widget->Show();
149 void KioskExternalUpdateNotification::Dismiss() {
150 if (view_) {
151 KioskExternalUpdateNotificationView* view = view_;
152 view_ = NULL;
153 view->CloseByOwner();
157 } // namespace chromeos