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/network_profile_bubble_view.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/network_profile_bubble.h"
11 #include "chrome/browser/ui/views/frame/browser_view.h"
12 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "grit/components_strings.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/controls/button/label_button.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/controls/link.h"
21 #include "ui/views/layout/grid_layout.h"
22 #include "ui/views/layout/layout_constants.h"
26 // Bubble layout constants.
27 const int kAnchorVerticalInset
= 5;
29 const int kNotificationBubbleWidth
= 250;
34 void NetworkProfileBubble::ShowNotification(Browser
* browser
) {
35 views::View
* anchor
= NULL
;
36 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(browser
);
37 if (browser_view
&& browser_view
->GetToolbarView())
38 anchor
= browser_view
->GetToolbarView()->app_menu();
39 NetworkProfileBubbleView
* bubble
=
40 new NetworkProfileBubbleView(anchor
, browser
, browser
->profile());
41 views::BubbleDelegateView::CreateBubble(bubble
)->Show();
42 NetworkProfileBubble::SetNotificationShown(true);
44 // Mark the time of the last bubble and reduce the number of warnings left
45 // before the next silence period starts.
46 PrefService
* prefs
= browser
->profile()->GetPrefs();
47 prefs
->SetInt64(prefs::kNetworkProfileLastWarningTime
,
48 base::Time::Now().ToTimeT());
49 int left_warnings
= prefs
->GetInteger(prefs::kNetworkProfileWarningsLeft
);
50 if (left_warnings
> 0)
51 prefs
->SetInteger(prefs::kNetworkProfileWarningsLeft
, --left_warnings
);
54 ////////////////////////////////////////////////////////////////////////////////
55 // NetworkProfileBubbleView, public:
57 NetworkProfileBubbleView::NetworkProfileBubbleView(
59 content::PageNavigator
* navigator
,
61 : BubbleDelegateView(anchor
, views::BubbleBorder::TOP_RIGHT
),
62 navigator_(navigator
),
64 // Compensate for built-in vertical padding in the anchor view's image.
65 set_anchor_view_insets(
66 gfx::Insets(kAnchorVerticalInset
, 0, kAnchorVerticalInset
, 0));
69 ////////////////////////////////////////////////////////////////////////////////
70 // NetworkProfileBubbleView, private:
72 NetworkProfileBubbleView::~NetworkProfileBubbleView() {
75 void NetworkProfileBubbleView::Init() {
76 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
77 layout
->SetInsets(0, kInset
, kInset
, kInset
);
78 SetLayoutManager(layout
);
80 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
81 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::LEADING
, 0,
82 views::GridLayout::USE_PREF
, 0, 0);
84 layout
->StartRow(0, 0);
86 views::Label
* title
= new views::Label(
87 l10n_util::GetStringFUTF16(IDS_PROFILE_ON_NETWORK_WARNING
,
88 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
)));
89 title
->SetMultiLine(true);
90 title
->SizeToFit(kNotificationBubbleWidth
);
91 title
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
92 layout
->AddView(title
);
94 views::ColumnSet
* bottom_columns
= layout
->AddColumnSet(1);
95 bottom_columns
->AddColumn(views::GridLayout::CENTER
,
96 views::GridLayout::CENTER
, 0, views::GridLayout::USE_PREF
, 0, 0);
97 bottom_columns
->AddPaddingColumn(1, 0);
98 bottom_columns
->AddColumn(views::GridLayout::CENTER
,
99 views::GridLayout::CENTER
, 0, views::GridLayout::USE_PREF
, 0, 0);
100 layout
->StartRowWithPadding(0, 1, 0,
101 views::kRelatedControlSmallVerticalSpacing
);
103 views::Link
* learn_more
=
104 new views::Link(l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
105 learn_more
->set_listener(this);
106 layout
->AddView(learn_more
);
108 views::LabelButton
* ok_button
= new views::LabelButton(
109 this, l10n_util::GetStringUTF16(IDS_OK
));
110 ok_button
->SetStyle(views::Button::STYLE_BUTTON
);
111 ok_button
->SetIsDefault(true);
112 layout
->AddView(ok_button
);
115 void NetworkProfileBubbleView::ButtonPressed(views::Button
* sender
,
116 const ui::Event
& event
) {
117 NetworkProfileBubble::RecordUmaEvent(
118 NetworkProfileBubble::METRIC_ACKNOWLEDGED
);
120 GetWidget()->Close();
123 void NetworkProfileBubbleView::LinkClicked(views::Link
* source
,
125 NetworkProfileBubble::RecordUmaEvent(
126 NetworkProfileBubble::METRIC_LEARN_MORE_CLICKED
);
127 WindowOpenDisposition disposition
=
128 ui::DispositionFromEventFlags(event_flags
);
129 content::OpenURLParams
params(
130 GURL("https://sites.google.com/a/chromium.org/dev/administrators/"
131 "common-problems-and-solutions#network_profile"),
133 disposition
== CURRENT_TAB
? NEW_FOREGROUND_TAB
: disposition
,
134 ui::PAGE_TRANSITION_LINK
, false);
135 navigator_
->OpenURL(params
);
137 // If the user interacted with the bubble we don't reduce the number of
139 PrefService
* prefs
= profile_
->GetPrefs();
140 int left_warnings
= prefs
->GetInteger(prefs::kNetworkProfileWarningsLeft
);
141 prefs
->SetInteger(prefs::kNetworkProfileWarningsLeft
, ++left_warnings
);
142 GetWidget()->Close();