• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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/notifications/sync_notifier/chrome_notifier_delegate.h"
6 
7 
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
10 #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
14 #include "content/public/browser/page_navigator.h"
15 #include "content/public/browser/user_metrics.h"
16 
17 namespace notifier {
ChromeNotifierDelegate(const std::string & notification_id,ChromeNotifierService * notifier)18 ChromeNotifierDelegate::ChromeNotifierDelegate(
19     const std::string& notification_id,
20     ChromeNotifierService* notifier)
21     : notification_id_(notification_id), chrome_notifier_(notifier) {}
22 
~ChromeNotifierDelegate()23 ChromeNotifierDelegate::~ChromeNotifierDelegate() {}
24 
Close(bool by_user)25 void ChromeNotifierDelegate::Close(bool by_user) {
26   if (by_user)
27     chrome_notifier_->MarkNotificationAsRead(notification_id_);
28 
29   CollectAction(by_user ?
30       SYNCED_NOTIFICATION_ACTION_CLOSE_BY_USER :
31       SYNCED_NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
32 }
33 
HasClickedListener()34 bool ChromeNotifierDelegate::HasClickedListener() {
35   return GetClickDestination().is_valid();
36 }
37 
Click()38 void ChromeNotifierDelegate::Click() {
39   GURL destination = GetClickDestination();
40   NavigateToUrl(destination);
41   chrome_notifier_->MarkNotificationAsRead(notification_id_);
42 
43   // Record the action in UMA statistics.
44   CollectAction(SYNCED_NOTIFICATION_ACTION_CLICK);
45 }
46 
ButtonClick(int button_index)47 void ChromeNotifierDelegate::ButtonClick(int button_index) {
48   SyncedNotification* notification =
49       chrome_notifier_->FindNotificationById(notification_id_);
50   if (notification) {
51     GURL destination = notification->GetButtonUrl(button_index);
52     NavigateToUrl(destination);
53     chrome_notifier_->MarkNotificationAsRead(notification_id_);
54   }
55 
56   // Now record the UMA statistics for this action.
57   CollectAction(SYNCED_NOTIFICATION_ACTION_BUTTON_CLICK);
58 }
59 
id() const60 std::string ChromeNotifierDelegate::id() const {
61   return notification_id_;
62 }
63 
GetWebContents() const64 content::WebContents* ChromeNotifierDelegate::GetWebContents() const {
65   return NULL;
66 }
67 
CollectAction(SyncedNotificationActionType type)68 void ChromeNotifierDelegate::CollectAction(SyncedNotificationActionType type) {
69   DCHECK(!notification_id_.empty());
70 
71   UMA_HISTOGRAM_ENUMERATION("SyncedNotifications.Actions",
72                             type,
73                             SYNCED_NOTIFICATION_ACTION_COUNT);
74 }
75 
NavigateToUrl(const GURL & destination) const76 void ChromeNotifierDelegate::NavigateToUrl(const GURL& destination) const {
77   if (!destination.is_valid())
78     return;
79 
80   // Navigate to the URL in a new tab.
81   content::OpenURLParams open_params(destination, content::Referrer(),
82                                     NEW_FOREGROUND_TAB,
83                                     content::PAGE_TRANSITION_LINK, false);
84   chrome::ScopedTabbedBrowserDisplayer displayer(
85       chrome_notifier_->profile(), chrome::GetActiveDesktop());
86   displayer.browser()->OpenURL(open_params);
87   displayer.browser()->window()->Activate();
88 }
89 
GetClickDestination() const90 const GURL ChromeNotifierDelegate::GetClickDestination() const {
91   SyncedNotification* notification =
92       chrome_notifier_->FindNotificationById(notification_id_);
93   if (notification == NULL)
94     return GURL();
95 
96   return notification->GetDefaultDestinationUrl();
97 }
98 
99 }  // namespace notifier
100