Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / welcome_delegate.cc
1 // Copyright 2014 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 "base/message_loop/message_loop.h"
6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/notifications/desktop_notification_service.h"
8 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
9 #include "chrome/browser/notifications/message_center_notification_manager.h"
10 #include "chrome/browser/notifications/notification_ui_manager.h"
11 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
12 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
13 #include "chrome/browser/notifications/sync_notifier/welcome_delegate.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser_navigator.h"
16 #include "ui/message_center/message_center.h"
17 #include "url/gurl.h"
18
19 namespace notifier {
20 namespace {
21 void UpdateMessageCenter() {
22   NotificationUIManager* notification_ui_manager =
23       g_browser_process->notification_ui_manager();
24   MessageCenterNotificationManager* message_center_notification_manager =
25       static_cast<MessageCenterNotificationManager*>(notification_ui_manager);
26
27   message_center_notification_manager->EnsureMessageCenterClosed();
28 }
29 }  // namespace
30
31 WelcomeDelegate::WelcomeDelegate(const std::string& notification_id,
32                                  Profile* profile,
33                                  const message_center::NotifierId notifier_id,
34                                  const GURL& on_click_link)
35     : notification_id_(notification_id),
36       profile_(profile),
37       notifier_id_(notifier_id),
38       on_click_link_(on_click_link) {
39   DCHECK_EQ(message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE,
40             notifier_id.type);
41 }
42
43 WelcomeDelegate::~WelcomeDelegate() {}
44
45 void WelcomeDelegate::Display() {}
46
47 void WelcomeDelegate::Error() {}
48
49 void WelcomeDelegate::Close(bool by_user) {}
50
51 bool WelcomeDelegate::HasClickedListener() {
52   return on_click_link_.is_valid();
53 }
54
55 void WelcomeDelegate::Click() {
56   g_browser_process->notification_ui_manager()->CancelById(notification_id_);
57
58   if (!on_click_link_.is_valid()) {
59     // TODO(dewittj): Notifications that remove themselves are currently poorly
60     // supported.  We need to make it possible to completely remove a
61     // notification
62     // while the center is open, and then this section can be removed.
63     UpdateMessageCenter();
64     return;
65   }
66
67   chrome::NavigateParams params(
68       profile_, on_click_link_, content::PAGE_TRANSITION_AUTO_BOOKMARK);
69
70   params.disposition = SINGLETON_TAB;
71   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
72   params.user_gesture = true;
73
74   chrome::Navigate(&params);
75 }
76
77 void WelcomeDelegate::ButtonClick(int button_index) {
78   DCHECK_EQ(0, button_index);
79
80   // We take a reference to ourselves to prevent destruction until the end of
81   // this method.
82   scoped_refptr<WelcomeDelegate> this_ptr(this);
83
84   // WARNING: This line causes the |this| to be released.
85   g_browser_process->notification_ui_manager()->CancelById(notification_id_);
86
87   DesktopNotificationService* notification_service =
88       DesktopNotificationServiceFactory::GetForProfile(profile_);
89   if (notification_service)
90     notification_service->SetNotifierEnabled(notifier_id_, false);
91
92   ChromeNotifierService* notifier_service =
93       ChromeNotifierServiceFactory::GetForProfile(profile_,
94                                                   Profile::EXPLICIT_ACCESS);
95   if (notifier_service) {
96     notifier_service->OnSyncedNotificationServiceEnabled(notifier_id_.id,
97                                                          false);
98   }
99
100   // TODO(dewittj): Notifications that remove themselves are currently poorly
101   // supported.  We need to make it possible to completely remove a notification
102   // while the center is open, and then this section can be removed.
103   UpdateMessageCenter();
104 }
105
106 std::string WelcomeDelegate::id() const { return notification_id_; }
107
108 content::WebContents* WelcomeDelegate::GetWebContents() const {
109   return NULL;
110 }
111
112 }  // namespace notifier