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 std::string ChromeNotifierDelegate::id() const {
26 return notification_id_;
29 content::RenderViewHost* ChromeNotifierDelegate::GetRenderViewHost() const {
33 void ChromeNotifierDelegate::CollectAction(SyncedNotificationActionType type) {
34 DCHECK(!notification_id_.empty());
36 UMA_HISTOGRAM_ENUMERATION("SyncedNotifications.Actions",
38 SYNCED_NOTIFICATION_ACTION_COUNT);
42 // TODO(petewil) Add the ability to do URL actions also.
43 void ChromeNotifierDelegate::Click() {
44 SyncedNotification* notification =
45 chrome_notifier_->FindNotificationById(notification_id_);
46 if (notification == NULL)
49 GURL destination = notification->GetDefaultDestinationUrl();
50 NavigateToUrl(destination);
51 // TODO(petewil): Once the service protobuf supports a viewed state, mark the
52 // notification as viewed here.
54 // Record the action in UMA statistics.
55 CollectAction(SYNCED_NOTIFICATION_ACTION_CLICK);
58 // TODO(petewil) Add the ability to do URL actions also.
59 void ChromeNotifierDelegate::ButtonClick(int button_index) {
60 SyncedNotification* notification =
61 chrome_notifier_->FindNotificationById(notification_id_);
63 GURL destination = notification->GetButtonUrl(button_index);
64 NavigateToUrl(destination);
65 // TODO(petewil): Once the service protobuf supports a viewed state, mark
66 // the notification as viewed here.
69 // Now record the UMA statistics for this action.
70 CollectAction(SYNCED_NOTIFICATION_ACTION_BUTTON_CLICK);
73 void ChromeNotifierDelegate::NavigateToUrl(const GURL& destination) const {
74 if (!destination.is_valid())
77 // Navigate to the URL in a new tab.
78 content::OpenURLParams open_params(destination, content::Referrer(),
80 content::PAGE_TRANSITION_LINK, false);
81 chrome::ScopedTabbedBrowserDisplayer displayer(
82 chrome_notifier_->profile(), chrome::GetActiveDesktop());
83 displayer.browser()->OpenURL(open_params);
84 displayer.browser()->window()->Activate();
87 void ChromeNotifierDelegate::Close(bool by_user) {
89 chrome_notifier_->MarkNotificationAsRead(notification_id_);
91 CollectAction(by_user ?
92 SYNCED_NOTIFICATION_ACTION_CLOSE_BY_USER :
93 SYNCED_NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
96 } // namespace notifier