NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / ui / views / network_profile_bubble_view.cc
blobb6881dff02eaaf2766231c0f2d81077a63f4211d
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/browser/ui/views/toolbar/wrench_toolbar_button.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "grit/components_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/views/controls/button/label_button.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/controls/link.h"
22 #include "ui/views/layout/grid_layout.h"
23 #include "ui/views/layout/layout_constants.h"
25 namespace {
27 // Bubble layout constants.
28 const int kAnchorVerticalInset = 5;
29 const int kInset = 2;
30 const int kNotificationBubbleWidth = 250;
32 } // namespace
34 // static
35 void NetworkProfileBubble::ShowNotification(Browser* browser) {
36 views::View* anchor = NULL;
37 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
38 if (browser_view && browser_view->GetToolbarView())
39 anchor = browser_view->GetToolbarView()->app_menu();
40 NetworkProfileBubbleView* bubble =
41 new NetworkProfileBubbleView(anchor, browser, browser->profile());
42 views::BubbleDelegateView::CreateBubble(bubble)->Show();
43 NetworkProfileBubble::SetNotificationShown(true);
45 // Mark the time of the last bubble and reduce the number of warnings left
46 // before the next silence period starts.
47 PrefService* prefs = browser->profile()->GetPrefs();
48 prefs->SetInt64(prefs::kNetworkProfileLastWarningTime,
49 base::Time::Now().ToTimeT());
50 int left_warnings = prefs->GetInteger(prefs::kNetworkProfileWarningsLeft);
51 if (left_warnings > 0)
52 prefs->SetInteger(prefs::kNetworkProfileWarningsLeft, --left_warnings);
55 ////////////////////////////////////////////////////////////////////////////////
56 // NetworkProfileBubbleView, public:
58 NetworkProfileBubbleView::NetworkProfileBubbleView(
59 views::View* anchor,
60 content::PageNavigator* navigator,
61 Profile* profile)
62 : BubbleDelegateView(anchor, views::BubbleBorder::TOP_RIGHT),
63 navigator_(navigator),
64 profile_(profile) {
65 // Compensate for built-in vertical padding in the anchor view's image.
66 set_anchor_view_insets(
67 gfx::Insets(kAnchorVerticalInset, 0, kAnchorVerticalInset, 0));
70 ////////////////////////////////////////////////////////////////////////////////
71 // NetworkProfileBubbleView, private:
73 NetworkProfileBubbleView::~NetworkProfileBubbleView() {
76 void NetworkProfileBubbleView::Init() {
77 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
78 layout->SetInsets(0, kInset, kInset, kInset);
79 SetLayoutManager(layout);
81 views::ColumnSet* columns = layout->AddColumnSet(0);
82 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0,
83 views::GridLayout::USE_PREF, 0, 0);
85 layout->StartRow(0, 0);
87 views::Label* title = new views::Label(
88 l10n_util::GetStringFUTF16(IDS_PROFILE_ON_NETWORK_WARNING,
89 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
90 title->SetMultiLine(true);
91 title->SizeToFit(kNotificationBubbleWidth);
92 title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
93 layout->AddView(title);
95 views::ColumnSet* bottom_columns = layout->AddColumnSet(1);
96 bottom_columns->AddColumn(views::GridLayout::CENTER,
97 views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
98 bottom_columns->AddPaddingColumn(1, 0);
99 bottom_columns->AddColumn(views::GridLayout::CENTER,
100 views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
101 layout->StartRowWithPadding(0, 1, 0,
102 views::kRelatedControlSmallVerticalSpacing);
104 views::Link* learn_more =
105 new views::Link(l10n_util::GetStringUTF16(IDS_LEARN_MORE));
106 learn_more->set_listener(this);
107 layout->AddView(learn_more);
109 views::LabelButton* ok_button = new views::LabelButton(
110 this, l10n_util::GetStringUTF16(IDS_OK));
111 ok_button->SetStyle(views::Button::STYLE_BUTTON);
112 ok_button->SetIsDefault(true);
113 layout->AddView(ok_button);
116 void NetworkProfileBubbleView::ButtonPressed(views::Button* sender,
117 const ui::Event& event) {
118 NetworkProfileBubble::RecordUmaEvent(
119 NetworkProfileBubble::METRIC_ACKNOWLEDGED);
121 GetWidget()->Close();
124 void NetworkProfileBubbleView::LinkClicked(views::Link* source,
125 int event_flags) {
126 NetworkProfileBubble::RecordUmaEvent(
127 NetworkProfileBubble::METRIC_LEARN_MORE_CLICKED);
128 WindowOpenDisposition disposition =
129 ui::DispositionFromEventFlags(event_flags);
130 content::OpenURLParams params(
131 GURL("https://sites.google.com/a/chromium.org/dev/administrators/"
132 "common-problems-and-solutions#network_profile"),
133 content::Referrer(),
134 disposition == CURRENT_TAB ? NEW_FOREGROUND_TAB : disposition,
135 ui::PAGE_TRANSITION_LINK, false);
136 navigator_->OpenURL(params);
138 // If the user interacted with the bubble we don't reduce the number of
139 // warnings left.
140 PrefService* prefs = profile_->GetPrefs();
141 int left_warnings = prefs->GetInteger(prefs::kNetworkProfileWarningsLeft);
142 prefs->SetInteger(prefs::kNetworkProfileWarningsLeft, ++left_warnings);
143 GetWidget()->Close();