summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.cc
blob: f5e77e6de874f4ef427e60f11edacfe2187ca62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.h"


#include "base/metrics/histogram.h"
#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
#include "chrome/browser/notifications/sync_notifier/synced_notification.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/user_metrics.h"

namespace notifier {
ChromeNotifierDelegate::ChromeNotifierDelegate(
    const std::string& notification_id,
    ChromeNotifierService* notifier)
    : notification_id_(notification_id), chrome_notifier_(notifier) {}

ChromeNotifierDelegate::~ChromeNotifierDelegate() {}

std::string ChromeNotifierDelegate::id() const {
   return notification_id_;
}

content::RenderViewHost* ChromeNotifierDelegate::GetRenderViewHost() const {
    return NULL;
}

void ChromeNotifierDelegate::CollectAction(SyncedNotificationActionType type) {
  DCHECK(!notification_id_.empty());

  UMA_HISTOGRAM_ENUMERATION("SyncedNotifications.Actions",
                            type,
                            SYNCED_NOTIFICATION_ACTION_COUNT);
}


// TODO(petewil) Add the ability to do URL actions also.
void ChromeNotifierDelegate::Click() {
  SyncedNotification* notification =
      chrome_notifier_->FindNotificationById(notification_id_);
  if (notification == NULL)
    return;

  GURL destination = notification->GetDefaultDestinationUrl();
  NavigateToUrl(destination);
  // TODO(petewil): Once the service protobuf supports a viewed state, mark the
  // notification as viewed here.

  // Record the action in UMA statistics.
  CollectAction(SYNCED_NOTIFICATION_ACTION_CLICK);
}

// TODO(petewil) Add the ability to do URL actions also.
void ChromeNotifierDelegate::ButtonClick(int button_index) {
  SyncedNotification* notification =
      chrome_notifier_->FindNotificationById(notification_id_);
  if (notification) {
    GURL destination = notification->GetButtonUrl(button_index);
    NavigateToUrl(destination);
    // TODO(petewil): Once the service protobuf supports a viewed state, mark
    // the notification as viewed here.
  }

  // Now record the UMA statistics for this action.
  CollectAction(SYNCED_NOTIFICATION_ACTION_BUTTON_CLICK);
}

void ChromeNotifierDelegate::NavigateToUrl(const GURL& destination) const {
  if (!destination.is_valid())
    return;

  // Navigate to the URL in a new tab.
  content::OpenURLParams open_params(destination, content::Referrer(),
                                    NEW_FOREGROUND_TAB,
                                    content::PAGE_TRANSITION_LINK, false);
  chrome::ScopedTabbedBrowserDisplayer displayer(
      chrome_notifier_->profile(), chrome::GetActiveDesktop());
  displayer.browser()->OpenURL(open_params);
  displayer.browser()->window()->Activate();
}

void ChromeNotifierDelegate::Close(bool by_user) {
  if (by_user)
    chrome_notifier_->MarkNotificationAsRead(notification_id_);

  CollectAction(by_user ?
      SYNCED_NOTIFICATION_ACTION_CLOSE_BY_USER :
      SYNCED_NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
}

}  // namespace notifier