- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / chrome_notifier_delegate.cc
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 {
18 ChromeNotifierDelegate::ChromeNotifierDelegate(
19     const std::string& notification_id,
20     ChromeNotifierService* notifier)
21     : notification_id_(notification_id), chrome_notifier_(notifier) {}
22
23 ChromeNotifierDelegate::~ChromeNotifierDelegate() {}
24
25 std::string ChromeNotifierDelegate::id() const {
26    return notification_id_;
27 }
28
29 content::RenderViewHost* ChromeNotifierDelegate::GetRenderViewHost() const {
30     return NULL;
31 }
32
33 void ChromeNotifierDelegate::CollectAction(SyncedNotificationActionType type) {
34   DCHECK(!notification_id_.empty());
35
36   UMA_HISTOGRAM_ENUMERATION("SyncedNotifications.Actions",
37                             type,
38                             SYNCED_NOTIFICATION_ACTION_COUNT);
39 }
40
41
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)
47     return;
48
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.
53
54   // Record the action in UMA statistics.
55   CollectAction(SYNCED_NOTIFICATION_ACTION_CLICK);
56 }
57
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_);
62   if (notification) {
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.
67   }
68
69   // Now record the UMA statistics for this action.
70   CollectAction(SYNCED_NOTIFICATION_ACTION_BUTTON_CLICK);
71 }
72
73 void ChromeNotifierDelegate::NavigateToUrl(const GURL& destination) const {
74   if (!destination.is_valid())
75     return;
76
77   // Navigate to the URL in a new tab.
78   content::OpenURLParams open_params(destination, content::Referrer(),
79                                     NEW_FOREGROUND_TAB,
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();
85 }
86
87 void ChromeNotifierDelegate::Close(bool by_user) {
88   if (by_user)
89     chrome_notifier_->MarkNotificationAsRead(notification_id_);
90
91   CollectAction(by_user ?
92       SYNCED_NOTIFICATION_ACTION_CLOSE_BY_USER :
93       SYNCED_NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
94 }
95
96 }  // namespace notifier