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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
// Copyright (c) 2013 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/ui/views/extensions/extension_message_bubble_view.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/dev_mode_bubble_controller.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_message_bubble_controller.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/extensions/proxy_overridden_bubble_controller.h"
#include "chrome/browser/extensions/settings_api_bubble_controller.h"
#include "chrome/browser/extensions/settings_api_helpers.h"
#include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
#include "chrome/browser/ui/views/toolbar/browser_actions_container_observer.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/grit/locale_settings.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace {
base::LazyInstance<std::set<Profile*> > g_profiles_evaluated =
LAZY_INSTANCE_INITIALIZER;
// Layout constants.
const int kExtensionListPadding = 10;
const int kInsetBottomRight = 13;
const int kInsetLeft = 14;
const int kInsetTop = 9;
const int kHeadlineMessagePadding = 4;
const int kHeadlineRowPadding = 10;
const int kMessageBubblePadding = 11;
// How many extensions to show in the bubble (max).
const size_t kMaxExtensionsToShow = 7;
// How long to wait until showing the bubble (in seconds).
const int kBubbleAppearanceWaitTime = 5;
} // namespace
namespace extensions {
ExtensionMessageBubbleView::ExtensionMessageBubbleView(
views::View* anchor_view,
views::BubbleBorder::Arrow arrow_location,
scoped_ptr<extensions::ExtensionMessageBubbleController> controller)
: BubbleDelegateView(anchor_view, arrow_location),
controller_(controller.Pass()),
anchor_view_(anchor_view),
headline_(NULL),
learn_more_(NULL),
dismiss_button_(NULL),
link_clicked_(false),
action_taken_(false),
weak_factory_(this) {
DCHECK(anchor_view->GetWidget());
set_close_on_deactivate(controller_->CloseOnDeactivate());
set_close_on_esc(true);
// Compensate for built-in vertical padding in the anchor view's image.
set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
}
void ExtensionMessageBubbleView::OnActionButtonClicked(
const base::Closure& callback) {
action_callback_ = callback;
}
void ExtensionMessageBubbleView::OnDismissButtonClicked(
const base::Closure& callback) {
dismiss_callback_ = callback;
}
void ExtensionMessageBubbleView::OnLinkClicked(
const base::Closure& callback) {
link_callback_ = callback;
}
void ExtensionMessageBubbleView::Show() {
// Not showing the bubble right away (during startup) has a few benefits:
// We don't have to worry about focus being lost due to the Omnibox (or to
// other things that want focus at startup). This allows Esc to work to close
// the bubble and also solves the keyboard accessibility problem that comes
// with focus being lost (we don't have a good generic mechanism of injecting
// bubbles into the focus cycle). Another benefit of delaying the show is
// that fade-in works (the fade-in isn't apparent if the the bubble appears at
// startup).
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ExtensionMessageBubbleView::ShowBubble,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kBubbleAppearanceWaitTime));
}
void ExtensionMessageBubbleView::OnWidgetDestroying(views::Widget* widget) {
// To catch Esc, we monitor destroy message. Unless the link has been clicked,
// we assume Dismiss was the action taken.
if (!link_clicked_ && !action_taken_)
dismiss_callback_.Run();
}
////////////////////////////////////////////////////////////////////////////////
// ExtensionMessageBubbleView - private.
ExtensionMessageBubbleView::~ExtensionMessageBubbleView() {}
void ExtensionMessageBubbleView::ShowBubble() {
GetWidget()->Show();
}
void ExtensionMessageBubbleView::Init() {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
views::GridLayout* layout = views::GridLayout::CreatePanel(this);
layout->SetInsets(kInsetTop, kInsetLeft,
kInsetBottomRight, kInsetBottomRight);
SetLayoutManager(layout);
ExtensionMessageBubbleController::Delegate* delegate =
controller_->delegate();
const int headline_column_set_id = 0;
views::ColumnSet* top_columns = layout->AddColumnSet(headline_column_set_id);
top_columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
top_columns->AddPaddingColumn(1, 0);
layout->StartRow(0, headline_column_set_id);
headline_ = new views::Label(delegate->GetTitle(),
rb.GetFontList(ui::ResourceBundle::MediumFont));
layout->AddView(headline_);
layout->AddPaddingRow(0, kHeadlineRowPadding);
const int text_column_set_id = 1;
views::ColumnSet* upper_columns = layout->AddColumnSet(text_column_set_id);
upper_columns->AddColumn(
views::GridLayout::LEADING, views::GridLayout::LEADING,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, text_column_set_id);
views::Label* message = new views::Label();
message->SetMultiLine(true);
message->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message->SetText(delegate->GetMessageBody(
anchor_view_->id() == VIEW_ID_BROWSER_ACTION));
message->SizeToFit(views::Widget::GetLocalizedContentsWidth(
IDS_EXTENSION_WIPEOUT_BUBBLE_WIDTH_CHARS));
layout->AddView(message);
if (delegate->ShouldShowExtensionList()) {
const int extension_list_column_set_id = 2;
views::ColumnSet* middle_columns =
layout->AddColumnSet(extension_list_column_set_id);
middle_columns->AddPaddingColumn(0, kExtensionListPadding);
middle_columns->AddColumn(
views::GridLayout::LEADING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRowWithPadding(0, extension_list_column_set_id,
0, kHeadlineMessagePadding);
views::Label* extensions = new views::Label();
extensions->SetMultiLine(true);
extensions->SetHorizontalAlignment(gfx::ALIGN_LEFT);
std::vector<base::string16> extension_list;
base::char16 bullet_point = 0x2022;
std::vector<base::string16> suspicious = controller_->GetExtensionList();
size_t i = 0;
for (; i < suspicious.size() && i < kMaxExtensionsToShow; ++i) {
// Add each extension with bullet point.
extension_list.push_back(
bullet_point + base::ASCIIToUTF16(" ") + suspicious[i]);
}
if (i > kMaxExtensionsToShow) {
base::string16 difference = base::IntToString16(i - kMaxExtensionsToShow);
extension_list.push_back(bullet_point + base::ASCIIToUTF16(" ") +
delegate->GetOverflowText(difference));
}
extensions->SetText(JoinString(extension_list, base::ASCIIToUTF16("\n")));
extensions->SizeToFit(views::Widget::GetLocalizedContentsWidth(
IDS_EXTENSION_WIPEOUT_BUBBLE_WIDTH_CHARS));
layout->AddView(extensions);
}
base::string16 action_button = delegate->GetActionButtonLabel();
const int action_row_column_set_id = 3;
views::ColumnSet* bottom_columns =
layout->AddColumnSet(action_row_column_set_id);
bottom_columns->AddColumn(views::GridLayout::LEADING,
views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
bottom_columns->AddPaddingColumn(1, 0);
bottom_columns->AddColumn(views::GridLayout::TRAILING,
views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
if (!action_button.empty()) {
bottom_columns->AddColumn(views::GridLayout::TRAILING,
views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
}
layout->StartRowWithPadding(0, action_row_column_set_id,
0, kMessageBubblePadding);
learn_more_ = new views::Link(delegate->GetLearnMoreLabel());
learn_more_->set_listener(this);
layout->AddView(learn_more_);
if (!action_button.empty()) {
action_button_ = new views::LabelButton(this, action_button.c_str());
action_button_->SetStyle(views::Button::STYLE_BUTTON);
layout->AddView(action_button_);
}
dismiss_button_ = new views::LabelButton(this,
delegate->GetDismissButtonLabel());
dismiss_button_->SetStyle(views::Button::STYLE_BUTTON);
layout->AddView(dismiss_button_);
}
void ExtensionMessageBubbleView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
if (sender == action_button_) {
action_taken_ = true;
action_callback_.Run();
} else {
DCHECK_EQ(dismiss_button_, sender);
}
GetWidget()->Close();
}
void ExtensionMessageBubbleView::LinkClicked(views::Link* source,
int event_flags) {
DCHECK_EQ(learn_more_, source);
link_clicked_ = true;
link_callback_.Run();
GetWidget()->Close();
}
void ExtensionMessageBubbleView::GetAccessibleState(
ui::AXViewState* state) {
state->role = ui::AX_ROLE_ALERT;
}
void ExtensionMessageBubbleView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (details.is_add && details.child == this)
NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
}
////////////////////////////////////////////////////////////////////////////////
// ExtensionMessageBubbleFactory
ExtensionMessageBubbleFactory::ExtensionMessageBubbleFactory(
Profile* profile,
ToolbarView* toolbar_view)
: profile_(profile),
toolbar_view_(toolbar_view),
shown_suspicious_extensions_bubble_(false),
shown_startup_override_extensions_bubble_(false),
shown_proxy_override_extensions_bubble_(false),
shown_dev_mode_extensions_bubble_(false),
is_observing_(false),
stage_(STAGE_START),
container_(NULL),
anchor_view_(NULL) {}
ExtensionMessageBubbleFactory::~ExtensionMessageBubbleFactory() {
MaybeStopObserving();
}
void ExtensionMessageBubbleFactory::MaybeShow(views::View* anchor_view) {
#if defined(OS_WIN)
bool is_initial_check = IsInitialProfileCheck(profile_->GetOriginalProfile());
RecordProfileCheck(profile_->GetOriginalProfile());
// The list of suspicious extensions takes priority over the dev mode bubble
// and the settings API bubble, since that needs to be shown as soon as we
// disable something. The settings API bubble is shown on first startup after
// an extension has changed the startup pages and it is acceptable if that
// waits until the next startup because of the suspicious extension bubble.
// The dev mode bubble is not time sensitive like the other two so we'll catch
// the dev mode extensions on the next startup/next window that opens. That
// way, we're not too spammy with the bubbles.
if (!shown_suspicious_extensions_bubble_ &&
MaybeShowSuspiciousExtensionsBubble(anchor_view))
return;
if (!shown_startup_override_extensions_bubble_ &&
is_initial_check &&
MaybeShowStartupOverrideExtensionsBubble(anchor_view))
return;
if (!shown_proxy_override_extensions_bubble_ &&
MaybeShowProxyOverrideExtensionsBubble(anchor_view))
return;
if (!shown_dev_mode_extensions_bubble_)
MaybeShowDevModeExtensionsBubble(anchor_view);
#endif // OS_WIN
}
bool ExtensionMessageBubbleFactory::MaybeShowSuspiciousExtensionsBubble(
views::View* anchor_view) {
DCHECK(!shown_suspicious_extensions_bubble_);
scoped_ptr<SuspiciousExtensionBubbleController> suspicious_extensions(
new SuspiciousExtensionBubbleController(profile_));
if (!suspicious_extensions->ShouldShow())
return false;
shown_suspicious_extensions_bubble_ = true;
SuspiciousExtensionBubbleController* weak_controller =
suspicious_extensions.get();
ExtensionMessageBubbleView* bubble_delegate =
new ExtensionMessageBubbleView(anchor_view,
views::BubbleBorder::TOP_RIGHT,
suspicious_extensions.Pass());
views::BubbleDelegateView::CreateBubble(bubble_delegate);
weak_controller->Show(bubble_delegate);
return true;
}
bool ExtensionMessageBubbleFactory::MaybeShowStartupOverrideExtensionsBubble(
views::View* anchor_view) {
#if !defined(OS_WIN)
return false;
#else
DCHECK(!shown_startup_override_extensions_bubble_);
const Extension* extension = GetExtensionOverridingStartupPages(profile_);
if (!extension)
return false;
scoped_ptr<SettingsApiBubbleController> settings_api_bubble(
new SettingsApiBubbleController(profile_,
BUBBLE_TYPE_STARTUP_PAGES));
if (!settings_api_bubble->ShouldShow(extension->id()))
return false;
shown_startup_override_extensions_bubble_ = true;
PrepareToHighlightExtensions(settings_api_bubble.Pass(), anchor_view);
return true;
#endif
}
bool ExtensionMessageBubbleFactory::MaybeShowProxyOverrideExtensionsBubble(
views::View* anchor_view) {
#if !defined(OS_WIN)
return false;
#else
DCHECK(!shown_proxy_override_extensions_bubble_);
const Extension* extension = GetExtensionOverridingProxy(profile_);
if (!extension)
return false;
scoped_ptr<ProxyOverriddenBubbleController> proxy_bubble(
new ProxyOverriddenBubbleController(profile_));
if (!proxy_bubble->ShouldShow(extension->id()))
return false;
shown_proxy_override_extensions_bubble_ = true;
PrepareToHighlightExtensions(proxy_bubble.Pass(), anchor_view);
return true;
#endif
}
bool ExtensionMessageBubbleFactory::MaybeShowDevModeExtensionsBubble(
views::View* anchor_view) {
DCHECK(!shown_dev_mode_extensions_bubble_);
// Check the Developer Mode extensions.
scoped_ptr<DevModeBubbleController> dev_mode_extensions(
new DevModeBubbleController(profile_));
// Return early if we have none to show.
if (!dev_mode_extensions->ShouldShow())
return false;
shown_dev_mode_extensions_bubble_ = true;
PrepareToHighlightExtensions(dev_mode_extensions.Pass(), anchor_view);
return true;
}
void ExtensionMessageBubbleFactory::MaybeObserve() {
if (!is_observing_) {
is_observing_ = true;
container_->AddObserver(this);
}
}
void ExtensionMessageBubbleFactory::MaybeStopObserving() {
if (is_observing_) {
is_observing_ = false;
container_->RemoveObserver(this);
}
}
void ExtensionMessageBubbleFactory::RecordProfileCheck(Profile* profile) {
g_profiles_evaluated.Get().insert(profile);
}
bool ExtensionMessageBubbleFactory::IsInitialProfileCheck(Profile* profile) {
return g_profiles_evaluated.Get().count(profile) == 0;
}
void ExtensionMessageBubbleFactory::OnBrowserActionsContainerAnimationEnded() {
MaybeStopObserving();
if (stage_ == STAGE_START) {
HighlightExtensions();
} else if (stage_ == STAGE_HIGHLIGHTED) {
ShowHighlightingBubble();
} else { // We shouldn't be observing if we've completed the process.
NOTREACHED();
Finish();
}
}
void ExtensionMessageBubbleFactory::OnBrowserActionsContainerDestroyed() {
// If the container associated with the bubble is destroyed, abandon the
// process.
Finish();
}
void ExtensionMessageBubbleFactory::PrepareToHighlightExtensions(
scoped_ptr<ExtensionMessageBubbleController> controller,
views::View* anchor_view) {
// We should be in the start stage (i.e., should not have a pending attempt to
// show a bubble).
DCHECK_EQ(stage_, STAGE_START);
// Prepare to display and highlight the extensions before showing the bubble.
// Since this is an asynchronous process, set member variables for later use.
controller_ = controller.Pass();
anchor_view_ = anchor_view;
container_ = toolbar_view_->browser_actions();
if (container_->animating())
MaybeObserve();
else
HighlightExtensions();
}
void ExtensionMessageBubbleFactory::HighlightExtensions() {
DCHECK_EQ(STAGE_START, stage_);
stage_ = STAGE_HIGHLIGHTED;
const ExtensionIdList extension_list = controller_->GetExtensionIdList();
DCHECK(!extension_list.empty());
ExtensionToolbarModel::Get(profile_)->HighlightExtensions(extension_list);
if (container_->animating())
MaybeObserve();
else
ShowHighlightingBubble();
}
void ExtensionMessageBubbleFactory::ShowHighlightingBubble() {
DCHECK_EQ(stage_, STAGE_HIGHLIGHTED);
stage_ = STAGE_COMPLETE;
views::View* reference_view = NULL;
if (container_->num_toolbar_actions() > 0u)
reference_view = container_->GetToolbarActionViewAt(0);
if (reference_view && reference_view->visible())
anchor_view_ = reference_view;
ExtensionMessageBubbleController* weak_controller = controller_.get();
ExtensionMessageBubbleView* bubble_delegate =
new ExtensionMessageBubbleView(
anchor_view_,
views::BubbleBorder::TOP_RIGHT,
scoped_ptr<ExtensionMessageBubbleController>(
controller_.release()));
views::BubbleDelegateView::CreateBubble(bubble_delegate);
weak_controller->Show(bubble_delegate);
Finish();
}
void ExtensionMessageBubbleFactory::Finish() {
MaybeStopObserving();
controller_.reset();
anchor_view_ = NULL;
container_ = NULL;
}
} // namespace extensions
|