[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / ui / views / infobars / confirm_infobar.cc
blobb27eb3a66f40afe226a92c8af7793d80d7635287
1 // Copyright (c) 2012 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/ui/views/infobars/confirm_infobar.h"
7 #include "base/logging.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/ui/views/elevation_icon_setter.h"
10 #include "components/infobars/core/confirm_infobar_delegate.h"
11 #include "ui/base/window_open_disposition.h"
12 #include "ui/views/controls/button/label_button.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/controls/link.h"
16 // InfoBarService -------------------------------------------------------------
18 scoped_ptr<infobars::InfoBar> InfoBarService::CreateConfirmInfoBar(
19 scoped_ptr<ConfirmInfoBarDelegate> delegate) {
20 return make_scoped_ptr(new ConfirmInfoBar(delegate.Pass()));
24 // ConfirmInfoBar -------------------------------------------------------------
26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate> delegate)
27 : InfoBarView(delegate.Pass()),
28 label_(NULL),
29 ok_button_(NULL),
30 cancel_button_(NULL),
31 link_(NULL) {
34 ConfirmInfoBar::~ConfirmInfoBar() {
35 // Ensure |elevation_icon_setter_| is destroyed before |ok_button_|.
36 elevation_icon_setter_.reset();
39 void ConfirmInfoBar::Layout() {
40 InfoBarView::Layout();
42 int x = StartX();
43 Labels labels;
44 labels.push_back(label_);
45 labels.push_back(link_);
46 AssignWidths(&labels, std::max(0, EndX() - x - NonLabelWidth()));
48 label_->SetPosition(gfx::Point(x, OffsetY(label_)));
49 if (!label_->text().empty())
50 x = label_->bounds().right() + kEndOfLabelSpacing;
52 if (ok_button_) {
53 ok_button_->SetPosition(gfx::Point(x, OffsetY(ok_button_)));
54 x = ok_button_->bounds().right() + kButtonButtonSpacing;
57 if (cancel_button_)
58 cancel_button_->SetPosition(gfx::Point(x, OffsetY(cancel_button_)));
60 link_->SetPosition(gfx::Point(EndX() - link_->width(), OffsetY(link_)));
63 void ConfirmInfoBar::ViewHierarchyChanged(
64 const ViewHierarchyChangedDetails& details) {
65 if (details.is_add && details.child == this && (label_ == NULL)) {
66 ConfirmInfoBarDelegate* delegate = GetDelegate();
67 label_ = CreateLabel(delegate->GetMessageText());
68 AddChildView(label_);
70 if (delegate->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK) {
71 ok_button_ = CreateLabelButton(
72 this, delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK));
73 if (delegate->OKButtonTriggersUACPrompt())
74 elevation_icon_setter_.reset(new ElevationIconSetter(
75 ok_button_,
76 base::Bind(&ConfirmInfoBar::Layout, base::Unretained(this))));
77 AddChildView(ok_button_);
80 if (delegate->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
81 cancel_button_ = CreateLabelButton(
82 this,
83 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL));
84 AddChildView(cancel_button_);
87 base::string16 link_text(delegate->GetLinkText());
88 link_ = CreateLink(link_text, this);
89 AddChildView(link_);
92 // This must happen after adding all other children so InfoBarView can ensure
93 // the close button is the last child.
94 InfoBarView::ViewHierarchyChanged(details);
97 void ConfirmInfoBar::ButtonPressed(views::Button* sender,
98 const ui::Event& event) {
99 if (!owner())
100 return; // We're closing; don't call anything, it might access the owner.
101 ConfirmInfoBarDelegate* delegate = GetDelegate();
102 if ((ok_button_ != NULL) && sender == ok_button_) {
103 if (delegate->Accept())
104 RemoveSelf();
105 } else if ((cancel_button_ != NULL) && (sender == cancel_button_)) {
106 if (delegate->Cancel())
107 RemoveSelf();
108 } else {
109 InfoBarView::ButtonPressed(sender, event);
113 int ConfirmInfoBar::ContentMinimumWidth() const {
114 return label_->GetMinimumSize().width() + link_->GetMinimumSize().width() +
115 NonLabelWidth();
118 void ConfirmInfoBar::LinkClicked(views::Link* source, int event_flags) {
119 if (!owner())
120 return; // We're closing; don't call anything, it might access the owner.
121 DCHECK_EQ(link_, source);
122 if (GetDelegate()->LinkClicked(ui::DispositionFromEventFlags(event_flags)))
123 RemoveSelf();
126 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() {
127 return delegate()->AsConfirmInfoBarDelegate();
130 int ConfirmInfoBar::NonLabelWidth() const {
131 int width = (label_->text().empty() || (!ok_button_ && !cancel_button_)) ?
132 0 : kEndOfLabelSpacing;
133 if (ok_button_)
134 width += ok_button_->width() + (cancel_button_ ? kButtonButtonSpacing : 0);
135 width += (cancel_button_ ? cancel_button_->width() : 0);
136 return width + ((link_->text().empty() || !width) ? 0 : kEndOfLabelSpacing);