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