This source file includes following definitions.
- action_taken_
- OnActionButtonClicked
- OnDismissButtonClicked
- OnLinkClicked
- Show
- OnWidgetDestroying
- ShowBubble
- Init
- ButtonPressed
- LinkClicked
- GetAccessibleState
- ViewHierarchyChanged
- anchor_view_
- MaybeShow
- MaybeShowSuspiciousExtensionsBubble
- MaybeShowStartupOverrideExtensionsBubble
- MaybeShowDevModeExtensionsBubble
- MaybeObserve
- MaybeStopObserving
- RecordProfileCheck
- IsInitialProfileCheck
- OnBrowserActionsContainerAnimationEnded
- OnBrowserActionsContainerDestroyed
- HighlightDevModeExtensions
- ShowDevModeBubble
- Finish
#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/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/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 "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "grit/locale_settings.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;
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;
const size_t kMaxExtensionsToShow = 7;
const int kBubbleAppearanceWaitTime = 5;
}
namespace extensions {
ExtensionMessageBubbleView::ExtensionMessageBubbleView(
views::View* anchor_view,
views::BubbleBorder::Arrow arrow_location,
scoped_ptr<extensions::ExtensionMessageBubbleController> controller)
: BubbleDelegateView(anchor_view, arrow_location),
weak_factory_(this),
controller_(controller.Pass()),
headline_(NULL),
learn_more_(NULL),
dismiss_button_(NULL),
link_clicked_(false),
action_taken_(false) {
DCHECK(anchor_view->GetWidget());
set_close_on_deactivate(controller_->CloseOnDeactivate());
set_move_with_anchor(true);
set_close_on_esc(true);
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() {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ExtensionMessageBubbleView::ShowBubble,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kBubbleAppearanceWaitTime));
}
void ExtensionMessageBubbleView::OnWidgetDestroying(views::Widget* widget) {
if (!link_clicked_ && !action_taken_)
dismiss_callback_.Run();
}
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());
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) {
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(
Profile* profile,
ToolbarView* toolbar_view)
: profile_(profile),
toolbar_view_(toolbar_view),
shown_suspicious_extensions_bubble_(false),
shown_startup_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)
if (!shown_suspicious_extensions_bubble_) {
if (MaybeShowSuspiciousExtensionsBubble(anchor_view))
return;
}
if (!shown_startup_override_extensions_bubble_ &&
IsInitialProfileCheck(profile_->GetOriginalProfile()) &&
MaybeShowStartupOverrideExtensionsBubble(anchor_view))
return;
if (!shown_dev_mode_extensions_bubble_)
MaybeShowDevModeExtensionsBubble(anchor_view);
RecordProfileCheck(profile_->GetOriginalProfile());
#endif
}
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.PassAs<ExtensionMessageBubbleController>());
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;
#endif
DCHECK(!shown_startup_override_extensions_bubble_);
const Extension* extension = OverridesStartupPages(profile_, NULL);
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;
SettingsApiBubbleController* weak_controller = settings_api_bubble.get();
ExtensionMessageBubbleView* bubble_delegate = new ExtensionMessageBubbleView(
anchor_view,
views::BubbleBorder::TOP_RIGHT,
settings_api_bubble.PassAs<ExtensionMessageBubbleController>());
views::BubbleDelegateView::CreateBubble(bubble_delegate);
weak_controller->Show(bubble_delegate);
return true;
}
bool ExtensionMessageBubbleFactory::MaybeShowDevModeExtensionsBubble(
views::View* anchor_view) {
DCHECK(!shown_dev_mode_extensions_bubble_);
scoped_ptr<DevModeBubbleController> dev_mode_extensions(
new DevModeBubbleController(profile_));
if (!dev_mode_extensions->ShouldShow())
return false;
shown_dev_mode_extensions_bubble_ = true;
DCHECK_EQ(stage_, STAGE_START);
controller_ = dev_mode_extensions.Pass();
anchor_view_ = anchor_view;
container_ = toolbar_view_->browser_actions();
if (container_->animating())
MaybeObserve();
else
HighlightDevModeExtensions();
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) {
HighlightDevModeExtensions();
} else if (stage_ == STAGE_HIGHLIGHTED) {
ShowDevModeBubble();
} else {
NOTREACHED();
Finish();
}
}
void ExtensionMessageBubbleFactory::OnBrowserActionsContainerDestroyed() {
Finish();
}
void ExtensionMessageBubbleFactory::HighlightDevModeExtensions() {
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
ShowDevModeBubble();
}
void ExtensionMessageBubbleFactory::ShowDevModeBubble() {
DCHECK_EQ(stage_, STAGE_HIGHLIGHTED);
stage_ = STAGE_COMPLETE;
views::View* reference_view = NULL;
if (container_->num_browser_actions() > 0)
reference_view = container_->GetBrowserActionViewAt(0);
if (reference_view && reference_view->visible())
anchor_view_ = reference_view;
DevModeBubbleController* 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;
}
}