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

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

Issue 2093953002: Introduce a new API to handle native notification clicks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: initial review Created 4 years, 5 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 "base/memory/ptr_util.h"
7 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/notifications/nonpersistent_notification_handler.h"
8 #include "chrome/browser/notifications/notification.h" 10 #include "chrome/browser/notifications/notification.h"
11 #include "chrome/browser/notifications/notification_delegate.h"
12 #include "chrome/browser/notifications/notification_handler.h"
9 #include "chrome/browser/notifications/notification_platform_bridge.h" 13 #include "chrome/browser/notifications/notification_platform_bridge.h"
14 #include "chrome/browser/notifications/persistent_notification_handler.h"
10 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
11 16
12 namespace { 17 namespace {
13 18
14 std::string GetProfileId(Profile* profile) { 19 std::string GetProfileId(Profile* profile) {
15 #if defined(OS_WIN) 20 #if defined(OS_WIN)
16 std::string profile_id = 21 std::string profile_id =
17 base::WideToUTF8(profile->GetPath().BaseName().value()); 22 base::WideToUTF8(profile->GetPath().BaseName().value());
18 #elif defined(OS_POSIX) 23 #elif defined(OS_POSIX)
19 std::string profile_id = profile->GetPath().BaseName().value(); 24 std::string profile_id = profile->GetPath().BaseName().value();
20 #endif 25 #endif
21 return profile_id; 26 return profile_id;
22 } 27 }
23 28
24 } // namespace 29 } // namespace
25 30
26 NativeNotificationDisplayService::NativeNotificationDisplayService( 31 NativeNotificationDisplayService::NativeNotificationDisplayService(
27 Profile* profile, 32 Profile* profile,
28 NotificationPlatformBridge* notification_bridge) 33 NotificationPlatformBridge* notification_bridge)
29 : profile_(profile), notification_bridge_(notification_bridge) { 34 : profile_(profile), notification_bridge_(notification_bridge) {
30 DCHECK(profile_); 35 DCHECK(profile_);
31 DCHECK(notification_bridge_); 36 DCHECK(notification_bridge_);
37
38 std::unique_ptr<NonPersistentNotificationHandler> non_persistent(
39 new NonPersistentNotificationHandler());
40 std::unique_ptr<PersistentNotificationHandler> persistent(
41 new PersistentNotificationHandler());
42
43 AddNotificationHandler(non_persistent->NotificationType(),
44 std::move(non_persistent));
45 AddNotificationHandler(persistent->NotificationType(), std::move(persistent));
Peter Beverloo 2016/07/05 14:25:33 Brainfart: What if we add a GetNotificationHandle
Peter Beverloo 2016/07/05 14:25:34 These are the only two calls to the NotificationTy
Peter Beverloo 2016/07/05 14:25:34 Do you think it'd make sense to change PlatformNot
Miguel Garcia 2016/07/05 17:12:51 Well extensions will need something similar too..
Miguel Garcia 2016/07/05 17:12:52 Maybe eventually? I would like to continue removin
Miguel Garcia 2016/07/05 17:12:52 I was struggling with this, I thought the knowledg
32 } 46 }
33 47
34 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} 48 NativeNotificationDisplayService::~NativeNotificationDisplayService() {
49 notification_handlers_.clear();
Peter Beverloo 2016/07/05 14:25:33 Why is this necessary? The map's destructor will d
Miguel Garcia 2016/07/05 17:12:52 indeed
50 }
35 51
36 void NativeNotificationDisplayService::Display( 52 void NativeNotificationDisplayService::Display(
53 NotificationCommon::Type notification_type,
37 const std::string& notification_id, 54 const std::string& notification_id,
38 const Notification& notification) { 55 const Notification& notification) {
39 notification_bridge_->Display(notification_id, GetProfileId(profile_), 56 notification_bridge_->Display(notification_type, notification_id,
57 GetProfileId(profile_),
40 profile_->IsOffTheRecord(), notification); 58 profile_->IsOffTheRecord(), notification);
41 notification.delegate()->Display(); 59 notification.delegate()->Display();
60 NotificationHandler* handler = GetNotificationHandler(notification_type);
61 handler->RegisterNotification(notification_id, notification.delegate());
42 } 62 }
43 63
44 void NativeNotificationDisplayService::Close( 64 void NativeNotificationDisplayService::Close(
65 NotificationCommon::Type notification_type,
45 const std::string& notification_id) { 66 const std::string& notification_id) {
67 NotificationHandler* handler = GetNotificationHandler(notification_type);
46 notification_bridge_->Close(GetProfileId(profile_), notification_id); 68 notification_bridge_->Close(GetProfileId(profile_), notification_id);
69
70 // TODO(miguelg): Figure out something better here, passing an empty
71 // origin works because only non persistent notifications care about
72 // this method for JS generated close calls and they don't require
73 // the origin.
Peter Beverloo 2016/07/05 14:25:34 It seems to me like the GetNotificationHandler() i
74 handler->Close(profile_, "", notification_id, false /* by user */);
47 } 75 }
48 76
49 bool NativeNotificationDisplayService::GetDisplayed( 77 bool NativeNotificationDisplayService::GetDisplayed(
50 std::set<std::string>* notifications) const { 78 std::set<std::string>* notifications) const {
51 return notification_bridge_->GetDisplayed( 79 return notification_bridge_->GetDisplayed(
52 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications); 80 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications);
53 } 81 }
54 82
83 void NativeNotificationDisplayService::ProcessNotificationOperation(
84 NotificationCommon::Operation operation,
85 NotificationCommon::Type notification_type,
86 const std::string& origin,
87 const std::string& notification_id,
88 int action_index) {
89 NotificationHandler* handler = GetNotificationHandler(notification_type);
Peter Beverloo 2016/07/05 14:25:34 Handle the |!handler| case— CHECK()?
Miguel Garcia 2016/07/05 17:12:52 Added a check, the GetNotificationHandler itself h
90 switch (operation) {
91 case NotificationCommon::CLICK:
92 handler->Click(profile_, origin, notification_id, action_index);
93 break;
94 case NotificationCommon::CLOSE:
95 handler->Close(profile_, origin, notification_id, true /*by user*/);
Peter Beverloo 2016/07/05 14:25:34 nit: /* by_user */
Miguel Garcia 2016/07/05 17:12:52 Done.
96 break;
97 case NotificationCommon::SETTINGS:
98 handler->Settings(profile_);
99 break;
100 }
101 }
102
103 void NativeNotificationDisplayService::AddNotificationHandler(
104 NotificationCommon::Type notification_type,
105 std::unique_ptr<NotificationHandler> handler) {
106 DCHECK(handler);
107 DCHECK_EQ(notification_handlers_.count(notification_type), 0u);
108 notification_handlers_[notification_type] = std::move(handler);
109 }
110
111 void NativeNotificationDisplayService::RemoveNotificationHandler(
112 NotificationCommon::Type notification_type) {
113 notification_handlers_.erase(notification_type);
114 }
115
116 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler(
117 NotificationCommon::Type notification_type) {
118 DCHECK(notification_handlers_.find(notification_type) !=
119 notification_handlers_.end())
120 << notification_type << " NOT REGISTERED";
Peter Beverloo 2016/07/05 14:25:34 NOT REGISTERED -> is not registered.
Peter Beverloo 2016/07/05 14:25:34 Maybe it should just be OK to return nullptrs here
Miguel Garcia 2016/07/05 17:12:51 In what situation do you think returning a null po
Miguel Garcia 2016/07/05 17:12:52 Done.
121 return notification_handlers_[notification_type].get();
122 }
123
55 bool NativeNotificationDisplayService::SupportsNotificationCenter() const { 124 bool NativeNotificationDisplayService::SupportsNotificationCenter() const {
56 return notification_bridge_->SupportsNotificationCenter(); 125 return notification_bridge_->SupportsNotificationCenter();
57 } 126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698