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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
// 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/views/extensions/extension_installed_bubble.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/message_loop.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/views/browser_actions_container.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/browser/views/location_bar_view.h"
#include "chrome/browser/views/toolbar_view.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "grit/generated_resources.h"
#include "views/controls/label.h"
#include "views/standard_layout.h"
#include "views/view.h"
namespace {
const int kIconSize = 43;
const int kRightColumnWidth = 270;
// The InfoBubble uses a BubbleBorder which adds about 6 pixels of whitespace
// around the content view. We compensate by reducing our outer borders by this
// amount.
const int kBubbleBorderInsert = 6;
const int kHorizOuterMargin = kPanelHorizMargin - kBubbleBorderInsert;
const int kVertOuterMargin = kPanelVertMargin - kBubbleBorderInsert;
// InstalledBubbleContent is the content view which is placed in the
// ExtensionInstalledBubble. It displays the install icon and explanatory
// text about the installed extension.
class InstalledBubbleContent : public views::View {
public:
InstalledBubbleContent(Extension* extension,
ExtensionInstalledBubble::BubbleType type,
SkBitmap* icon)
: type_(type),
info_(NULL) {
const gfx::Font& font =
ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
// Scale down to 43x43, but allow smaller icons (don't scale up).
gfx::Size size(icon->width(), icon->height());
if (size.width() > kIconSize || size.height() > kIconSize)
size = gfx::Size(kIconSize, kIconSize);
icon_ = new views::ImageView();
icon_->SetImageSize(size);
icon_->SetImage(*icon);
AddChildView(icon_);
heading_ = new views::Label(
l10n_util::GetStringF(IDS_EXTENSION_INSTALLED_HEADING,
UTF8ToWide(extension->name())));
heading_->SetFont(font.DeriveFont(3, gfx::Font::NORMAL));
heading_->SetMultiLine(true);
heading_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(heading_);
if (type_ == ExtensionInstalledBubble::PAGE_ACTION) {
info_ = new views::Label(l10n_util::GetString(
IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO));
info_->SetFont(font);
info_->SetMultiLine(true);
info_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(info_);
}
manage_ = new views::Label(l10n_util::GetString(
IDS_EXTENSION_INSTALLED_MANAGE_INFO));
manage_->SetFont(font);
manage_->SetMultiLine(true);
manage_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(manage_);
}
private:
virtual gfx::Size GetPreferredSize() {
int width = kRightColumnWidth + kHorizOuterMargin + kHorizOuterMargin;
width += kIconSize;
width += kPanelHorizMargin;
int height = kVertOuterMargin * 2;
height += heading_->GetHeightForWidth(kRightColumnWidth);
height += kPanelVertMargin;
if (type_ == ExtensionInstalledBubble::PAGE_ACTION) {
height += info_->GetHeightForWidth(kRightColumnWidth);
height += kPanelVertMargin;
}
height += manage_->GetHeightForWidth(kRightColumnWidth);
return gfx::Size(width, std::max(height, kIconSize + kVertOuterMargin * 2));
}
virtual void Layout() {
int x = kHorizOuterMargin;
int y = kVertOuterMargin;
icon_->SetBounds(x, y, kIconSize, kIconSize);
x += kIconSize;
x += kPanelHorizMargin;
heading_->SizeToFit(kRightColumnWidth);
heading_->SetX(x);
heading_->SetY(y);
y += heading_->height();
y += kPanelVertMargin;
if (type_ == ExtensionInstalledBubble::PAGE_ACTION) {
info_->SizeToFit(kRightColumnWidth);
info_->SetX(x);
info_->SetY(y);
y += info_->height();
y += kPanelVertMargin;
}
manage_->SizeToFit(kRightColumnWidth);
manage_->SetX(x);
manage_->SetY(y);
}
ExtensionInstalledBubble::BubbleType type_;
views::ImageView* icon_;
views::Label* heading_;
views::Label* info_;
views::Label* manage_;
DISALLOW_COPY_AND_ASSIGN(InstalledBubbleContent);
};
} // namespace
void ExtensionInstalledBubble::Show(Extension *extension, Browser *browser,
SkBitmap icon) {
new ExtensionInstalledBubble(extension, browser, icon);
}
ExtensionInstalledBubble::ExtensionInstalledBubble(Extension *extension,
Browser *browser,
SkBitmap icon)
: extension_(extension),
browser_(browser),
icon_(icon) {
AddRef(); // Balanced in InfoBubbleClosing.
if (extension_->browser_action()) {
type_ = BROWSER_ACTION;
} else if (extension->page_action() &&
!extension->page_action()->default_icon_path().empty()) {
type_ = PAGE_ACTION;
} else {
type_ = GENERIC;
}
// |extension| has been initialized but not loaded at this point. We need
// to wait on showing the Bubble until not only the EXTENSION_LOADED gets
// fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
// be sure that a BrowserAction or PageAction has had views created which we
// can inspect for the purpose of previewing of pointing to them.
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
NotificationService::AllSources());
}
void ExtensionInstalledBubble::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::EXTENSION_LOADED) {
Extension* extension = Details<Extension>(details).ptr();
if (extension == extension_) {
// PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
MessageLoopForUI::current()->PostTask(FROM_HERE, NewRunnableMethod(this,
&ExtensionInstalledBubble::ShowInternal));
}
} else {
NOTREACHED() << L"Received unexpected notification";
}
}
void ExtensionInstalledBubble::ShowInternal() {
BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(
browser_->window()->GetNativeHandle());
views::View* reference_view = NULL;
if (type_ == BROWSER_ACTION) {
reference_view = browser_view->GetToolbarView()->browser_actions()
->GetBrowserActionView(extension_);
DCHECK(reference_view);
} else if (type_ == PAGE_ACTION) {
LocationBarView* location_bar_view = browser_view->GetLocationBarView();
location_bar_view->SetPreviewEnabledPageAction(extension_->page_action(),
true); // preview_enabled
reference_view = location_bar_view->GetPageActionView(
extension_->page_action());
DCHECK(reference_view);
}
// Default case.
if (reference_view == NULL)
reference_view = browser_view->GetToolbarView()->app_menu();
gfx::Point origin;
views::View::ConvertPointToScreen(reference_view, &origin);
gfx::Rect bounds = reference_view->bounds();
bounds.set_x(origin.x());
bounds.set_y(origin.y());
views::View* bubble_content = new InstalledBubbleContent(extension_, type_,
&icon_);
InfoBubble::Show(browser_view->GetWindow(), bounds, bubble_content, this);
}
// InfoBubbleDelegate
void ExtensionInstalledBubble::InfoBubbleClosing(InfoBubble* info_bubble,
bool closed_by_escape) {
if (extension_->page_action()) {
BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(
browser_->window()->GetNativeHandle());
browser_view->GetLocationBarView()->SetPreviewEnabledPageAction(
extension_->page_action(),
false); // preview_enabled
}
Release(); // Balanced in ctor.
}
|