Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / network_profile_bubble_view.cc
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 "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"
23
24 namespace {
25
26 // Bubble layout constants.
27 const int kAnchorVerticalInset = 5;
28 const int kInset = 2;
29 const int kNotificationBubbleWidth = 250;
30
31 }  // namespace
32
33 // static
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);
43
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);
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55 // NetworkProfileBubbleView, public:
56
57 NetworkProfileBubbleView::NetworkProfileBubbleView(
58     views::View* anchor,
59     content::PageNavigator* navigator,
60     Profile* profile)
61     : BubbleDelegateView(anchor, views::BubbleBorder::TOP_RIGHT),
62       navigator_(navigator),
63       profile_(profile) {
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));
67 }
68
69 ////////////////////////////////////////////////////////////////////////////////
70 // NetworkProfileBubbleView, private:
71
72 NetworkProfileBubbleView::~NetworkProfileBubbleView() {
73 }
74
75 void NetworkProfileBubbleView::Init() {
76   views::GridLayout* layout = views::GridLayout::CreatePanel(this);
77   layout->SetInsets(0, kInset, kInset, kInset);
78   SetLayoutManager(layout);
79
80   views::ColumnSet* columns = layout->AddColumnSet(0);
81   columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0,
82                      views::GridLayout::USE_PREF, 0, 0);
83
84   layout->StartRow(0, 0);
85
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);
93
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);
102
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);
107
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);
113 }
114
115 void NetworkProfileBubbleView::ButtonPressed(views::Button* sender,
116                                              const ui::Event& event) {
117   NetworkProfileBubble::RecordUmaEvent(
118       NetworkProfileBubble::METRIC_ACKNOWLEDGED);
119
120   GetWidget()->Close();
121 }
122
123 void NetworkProfileBubbleView::LinkClicked(views::Link* source,
124                                            int event_flags) {
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"),
132       content::Referrer(),
133       disposition == CURRENT_TAB ? NEW_FOREGROUND_TAB : disposition,
134       ui::PAGE_TRANSITION_LINK, false);
135   navigator_->OpenURL(params);
136
137   // If the user interacted with the bubble we don't reduce the number of
138   // warnings left.
139   PrefService* prefs = profile_->GetPrefs();
140   int left_warnings = prefs->GetInteger(prefs::kNetworkProfileWarningsLeft);
141   prefs->SetInteger(prefs::kNetworkProfileWarningsLeft, ++left_warnings);
142   GetWidget()->Close();
143 }