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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2009 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/gtk/infobar_container_gtk.h"
#include <gtk/gtk.h>
#include "base/command_line.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/gtk/browser_window_gtk.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "chrome/browser/gtk/infobar_gtk.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/notification_service.h"
#include "third_party/skia/include/core/SkPaint.h"
namespace {
// If |infobar_widget| matches |info_bar_delegate|, then close the infobar.
void AnimateClosingForDelegate(GtkWidget* infobar_widget,
gpointer info_bar_delegate) {
InfoBarDelegate* delegate =
static_cast<InfoBarDelegate*>(info_bar_delegate);
InfoBar* infobar = reinterpret_cast<InfoBar*>(
g_object_get_data(G_OBJECT(infobar_widget), "info-bar"));
if (!infobar) {
NOTREACHED();
return;
}
if (delegate == infobar->delegate())
infobar->AnimateClose();
}
// If |infobar_widget| matches |info_bar_delegate|, then close the infobar w/o
// an animation.
void ClosingForDelegate(GtkWidget* infobar_widget, gpointer info_bar_delegate) {
InfoBarDelegate* delegate =
static_cast<InfoBarDelegate*>(info_bar_delegate);
InfoBar* infobar = reinterpret_cast<InfoBar*>(
g_object_get_data(G_OBJECT(infobar_widget), "info-bar"));
if (!infobar) {
NOTREACHED();
return;
}
if (delegate == infobar->delegate())
infobar->Close();
}
// Get the height of the widget and add it to |userdata|, but only if it is in
// the process of animating.
void SumAnimatingBarHeight(GtkWidget* widget, gpointer userdata) {
int* height_sum = static_cast<int*>(userdata);
InfoBar* infobar = reinterpret_cast<InfoBar*>(
g_object_get_data(G_OBJECT(widget), "info-bar"));
if (infobar->IsAnimating())
*height_sum += widget->allocation.height;
}
} // namespace
// InfoBarContainerGtk, public: ------------------------------------------------
InfoBarContainerGtk::InfoBarContainerGtk(Profile* profile)
: profile_(profile),
tab_contents_(NULL),
container_(gtk_vbox_new(FALSE, 0)) {
gtk_widget_show(widget());
}
InfoBarContainerGtk::~InfoBarContainerGtk() {
ChangeTabContents(NULL);
container_.Destroy();
}
void InfoBarContainerGtk::ChangeTabContents(TabContents* contents) {
if (tab_contents_)
registrar_.RemoveAll();
gtk_util::RemoveAllChildren(widget());
UpdateToolbarInfoBarState(NULL, false);
tab_contents_ = contents;
if (tab_contents_) {
UpdateInfoBars();
Source<TabContents> source(tab_contents_);
registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED, source);
registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
source);
registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
source);
}
}
void InfoBarContainerGtk::RemoveDelegate(InfoBarDelegate* delegate) {
tab_contents_->RemoveInfoBar(delegate);
}
int InfoBarContainerGtk::TotalHeightOfAnimatingBars() const {
int sum = 0;
gtk_container_foreach(GTK_CONTAINER(widget()), SumAnimatingBarHeight, &sum);
return sum;
}
// InfoBarContainerGtk, NotificationObserver implementation: -------------------
void InfoBarContainerGtk::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::TAB_CONTENTS_INFOBAR_ADDED) {
AddInfoBar(Details<InfoBarDelegate>(details).ptr(), true);
} else if (type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED) {
RemoveInfoBar(Details<InfoBarDelegate>(details).ptr(), true);
} else if (type == NotificationType::TAB_CONTENTS_INFOBAR_REPLACED) {
std::pair<InfoBarDelegate*, InfoBarDelegate*>* delegates =
Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details).ptr();
// By not animating the removal/addition, this appears to be a replace.
RemoveInfoBar(delegates->first, false);
AddInfoBar(delegates->second, false);
} else {
NOTREACHED();
}
}
// InfoBarContainerGtk, private: -----------------------------------------------
void InfoBarContainerGtk::UpdateInfoBars() {
for (int i = 0; i < tab_contents_->infobar_delegate_count(); ++i) {
InfoBarDelegate* delegate = tab_contents_->GetInfoBarDelegateAt(i);
AddInfoBar(delegate, false);
}
}
void InfoBarContainerGtk::AddInfoBar(InfoBarDelegate* delegate, bool animate) {
InfoBar* infobar = delegate->CreateInfoBar();
infobar->set_container(this);
infobar->SetThemeProvider(GtkThemeProvider::GetFrom(profile_));
gtk_box_pack_start(GTK_BOX(widget()), infobar->widget(),
FALSE, FALSE, 0);
if (animate)
infobar->AnimateOpen();
else
infobar->Open();
if (tab_contents_->GetInfoBarDelegateAt(0) == delegate)
UpdateToolbarInfoBarState(infobar, animate);
}
void InfoBarContainerGtk::RemoveInfoBar(InfoBarDelegate* delegate,
bool animate) {
if (animate) {
gtk_container_foreach(GTK_CONTAINER(widget()),
AnimateClosingForDelegate, delegate);
} else {
gtk_container_foreach(GTK_CONTAINER(widget()), ClosingForDelegate,
delegate);
}
if (tab_contents_->GetInfoBarDelegateAt(0) == delegate) {
InfoBar* bar = NULL;
// Get the next infobar, if it exists, so we can change the color of the
// arrow to it.
GList* children = gtk_container_get_children(GTK_CONTAINER(widget()));
if (children->next) {
bar = reinterpret_cast<InfoBar*>(
g_object_get_data(G_OBJECT(children->next->data), "info-bar"));
}
g_list_free(children);
UpdateToolbarInfoBarState(bar, animate);
}
}
void InfoBarContainerGtk::UpdateToolbarInfoBarState(
InfoBar* infobar, bool animate) {
if (!CommandLine::ForCurrentProcess()->
HasSwitch(switches::kEnableSecureInfoBars)) {
return;
}
scoped_ptr<std::pair<SkColor, SkColor> > colors;
if (infobar) {
double r, g, b;
infobar->GetTopColor(infobar->delegate()->GetInfoBarType(), &r, &g, &b);
SkColor top = SkColorSetRGB(r * 0xff, g * 0xff, b * 0xff);
infobar->GetBottomColor(infobar->delegate()->GetInfoBarType(), &r, &g, &b);
SkColor bottom = SkColorSetRGB(r * 0xff, g * 0xff, b * 0xff);
colors.reset(new std::pair<SkColor, SkColor>(top, bottom));
}
GtkWindow* parent = platform_util::GetTopLevel(widget());
BrowserWindowGtk* browser_window =
BrowserWindowGtk::GetBrowserWindowForNativeWindow(parent);
if (browser_window)
browser_window->SetInfoBarShowing(colors.get(), animate);
}
|