Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: chrome/browser/notifications/native_notification_display_service.cc

Issue 2906913002: Move handler processing to NotificationDisplayService. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/native_notification_display_service.h" 5 #include "chrome/browser/notifications/native_notification_display_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/nullable_string16.h"
13 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/notifications/message_center_display_service.h" 14 #include "chrome/browser/notifications/message_center_display_service.h"
16 #include "chrome/browser/notifications/non_persistent_notification_handler.h"
17 #include "chrome/browser/notifications/notification.h" 15 #include "chrome/browser/notifications/notification.h"
18 #include "chrome/browser/notifications/notification_delegate.h" 16 #include "chrome/browser/notifications/notification_delegate.h"
19 #include "chrome/browser/notifications/notification_handler.h" 17 #include "chrome/browser/notifications/notification_handler.h"
20 #include "chrome/browser/notifications/notification_platform_bridge.h" 18 #include "chrome/browser/notifications/notification_platform_bridge.h"
21 #include "chrome/browser/notifications/persistent_notification_handler.h"
22 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
23 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_event_dispatcher.h" 21 #include "content/public/browser/notification_event_dispatcher.h"
25 #include "extensions/features/features.h" 22 // #include "extensions/features/features.h"
Peter Beverloo 2017/06/01 11:32:45 Please delete if this is unused.
Miguel Garcia 2017/06/02 15:02:55 Done.
26
27 #if BUILDFLAG(ENABLE_EXTENSIONS)
28 #include "chrome/browser/extensions/api/notifications/extension_notification_han dler.h"
29 #endif
30 23
31 namespace { 24 namespace {
32 25
33 std::string GetProfileId(Profile* profile) { 26 std::string GetProfileId(Profile* profile) {
34 #if defined(OS_WIN) 27 #if defined(OS_WIN)
35 std::string profile_id = 28 std::string profile_id =
36 base::WideToUTF8(profile->GetPath().BaseName().value()); 29 base::WideToUTF8(profile->GetPath().BaseName().value());
37 #elif defined(OS_POSIX) 30 #elif defined(OS_POSIX)
38 std::string profile_id = profile->GetPath().BaseName().value(); 31 std::string profile_id = profile->GetPath().BaseName().value();
39 #endif 32 #endif
40 return profile_id; 33 return profile_id;
41 } 34 }
42 35
43 } // namespace 36 } // namespace
44 37
45 NativeNotificationDisplayService::NativeNotificationDisplayService( 38 NativeNotificationDisplayService::NativeNotificationDisplayService(
46 Profile* profile, 39 Profile* profile,
47 NotificationPlatformBridge* notification_bridge) 40 NotificationPlatformBridge* notification_bridge)
48 : profile_(profile), 41 : NotificationDisplayService(profile),
42 profile_(profile),
49 notification_bridge_(notification_bridge), 43 notification_bridge_(notification_bridge),
50 notification_bridge_ready_(false), 44 notification_bridge_ready_(false),
51 weak_factory_(this) { 45 weak_factory_(this) {
52 DCHECK(profile_); 46 DCHECK(profile_);
53 DCHECK(notification_bridge_); 47 DCHECK(notification_bridge_);
54 48
55 notification_bridge->SetReadyCallback(base::BindOnce( 49 notification_bridge->SetReadyCallback(base::BindOnce(
56 &NativeNotificationDisplayService::OnNotificationPlatformBridgeReady, 50 &NativeNotificationDisplayService::OnNotificationPlatformBridgeReady,
57 weak_factory_.GetWeakPtr())); 51 weak_factory_.GetWeakPtr()));
58
59 AddNotificationHandler(NotificationCommon::NON_PERSISTENT,
60 base::MakeUnique<NonPersistentNotificationHandler>());
61 AddNotificationHandler(NotificationCommon::PERSISTENT,
62 base::MakeUnique<PersistentNotificationHandler>());
63 #if BUILDFLAG(ENABLE_EXTENSIONS)
64 AddNotificationHandler(
65 NotificationCommon::EXTENSION,
66 base::MakeUnique<extensions::ExtensionNotificationHandler>());
67 #endif
68 } 52 }
69 53
70 NativeNotificationDisplayService::~NativeNotificationDisplayService() = default; 54 NativeNotificationDisplayService::~NativeNotificationDisplayService() = default;
71 55
72 void NativeNotificationDisplayService::OnNotificationPlatformBridgeReady( 56 void NativeNotificationDisplayService::OnNotificationPlatformBridgeReady(
73 bool success) { 57 bool success) {
74 UMA_HISTOGRAM_BOOLEAN("Notifications.UsingNativeNotificationCenter", success); 58 UMA_HISTOGRAM_BOOLEAN("Notifications.UsingNativeNotificationCenter", success);
75 if (success) { 59 if (success) {
76 notification_bridge_ready_ = true; 60 notification_bridge_ready_ = true;
77 } else { 61 } else {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 return notification_bridge_->GetDisplayed( 121 return notification_bridge_->GetDisplayed(
138 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); 122 GetProfileId(profile_), profile_->IsOffTheRecord(), callback);
139 } else if (message_center_display_service_) { 123 } else if (message_center_display_service_) {
140 message_center_display_service_->GetDisplayed(callback); 124 message_center_display_service_->GetDisplayed(callback);
141 } else { 125 } else {
142 actions_.push( 126 actions_.push(
143 base::BindOnce(&NativeNotificationDisplayService::GetDisplayed, 127 base::BindOnce(&NativeNotificationDisplayService::GetDisplayed,
144 weak_factory_.GetWeakPtr(), callback)); 128 weak_factory_.GetWeakPtr(), callback));
145 } 129 }
146 } 130 }
147
148 void NativeNotificationDisplayService::ProcessNotificationOperation(
149 NotificationCommon::Operation operation,
150 NotificationCommon::Type notification_type,
151 const std::string& origin,
152 const std::string& notification_id,
153 int action_index,
154 const base::NullableString16& reply) {
155 NotificationHandler* handler = GetNotificationHandler(notification_type);
156 CHECK(handler);
157 switch (operation) {
158 case NotificationCommon::CLICK:
159 handler->OnClick(profile_, origin, notification_id, action_index, reply);
160 break;
161 case NotificationCommon::CLOSE:
162 handler->OnClose(profile_, origin, notification_id, true /* by_user */);
163 break;
164 case NotificationCommon::SETTINGS:
165 handler->OpenSettings(profile_);
166 break;
167 }
168 }
169
170 void NativeNotificationDisplayService::AddNotificationHandler(
171 NotificationCommon::Type notification_type,
172 std::unique_ptr<NotificationHandler> handler) {
173 DCHECK(handler);
174 DCHECK_EQ(notification_handlers_.count(notification_type), 0u);
175 notification_handlers_[notification_type] = std::move(handler);
176 }
177
178 void NativeNotificationDisplayService::RemoveNotificationHandler(
179 NotificationCommon::Type notification_type) {
180 notification_handlers_.erase(notification_type);
181 }
182
183 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler(
184 NotificationCommon::Type notification_type) {
185 DCHECK(notification_handlers_.find(notification_type) !=
186 notification_handlers_.end())
187 << notification_type << " is not registered.";
188 return notification_handlers_[notification_type].get();
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698