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.
5 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.h"
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"
18 ChromeNotifierDelegate::ChromeNotifierDelegate(
19 const std::string& notification_id,
20 ChromeNotifierService* notifier)
21 : notification_id_(notification_id), chrome_notifier_(notifier) {}
23 ChromeNotifierDelegate::~ChromeNotifierDelegate() {}
25 void ChromeNotifierDelegate::Close(bool by_user) {
27 chrome_notifier_->MarkNotificationAsRead(notification_id_);
29 CollectAction(by_user ?
30 SYNCED_NOTIFICATION_ACTION_CLOSE_BY_USER :
31 SYNCED_NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
34 bool ChromeNotifierDelegate::HasClickedListener() {
35 return GetClickDestination().is_valid();
38 void ChromeNotifierDelegate::Click() {
39 GURL destination = GetClickDestination();
40 NavigateToUrl(destination);
41 chrome_notifier_->MarkNotificationAsRead(notification_id_);
43 // Record the action in UMA statistics.
44 CollectAction(SYNCED_NOTIFICATION_ACTION_CLICK);
47 void ChromeNotifierDelegate::ButtonClick(int button_index) {
48 SyncedNotification* notification =
49 chrome_notifier_->FindNotificationById(notification_id_);
51 GURL destination = notification->GetButtonUrl(button_index);
52 NavigateToUrl(destination);
53 chrome_notifier_->MarkNotificationAsRead(notification_id_);
56 // Now record the UMA statistics for this action.
57 CollectAction(SYNCED_NOTIFICATION_ACTION_BUTTON_CLICK);
60 std::string ChromeNotifierDelegate::id() const {
61 return notification_id_;
64 content::WebContents* ChromeNotifierDelegate::GetWebContents() const {
68 void ChromeNotifierDelegate::CollectAction(SyncedNotificationActionType type) {
69 DCHECK(!notification_id_.empty());
71 UMA_HISTOGRAM_ENUMERATION("SyncedNotifications.Actions",
73 SYNCED_NOTIFICATION_ACTION_COUNT);
76 void ChromeNotifierDelegate::NavigateToUrl(const GURL& destination) const {
77 if (!destination.is_valid())
80 // Navigate to the URL in a new tab.
81 content::OpenURLParams open_params(destination, content::Referrer(),
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();
90 const GURL ChromeNotifierDelegate::GetClickDestination() const {
91 SyncedNotification* notification =
92 chrome_notifier_->FindNotificationById(notification_id_);
93 if (notification == NULL)
96 return notification->GetDefaultDestinationUrl();
99 } // namespace notifier