OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/extensions/api/notifications/extension_notification_han
dler.h" | 5 #include "chrome/browser/extensions/api/notifications/extension_notification_han
dler.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/nullable_string16.h" |
| 9 #include "base/strings/string_piece.h" |
| 10 #include "chrome/browser/extensions/api/notifications/extension_notification_dis
play_helper.h" |
| 11 #include "chrome/browser/extensions/api/notifications/extension_notification_dis
play_helper_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/extensions/api/notifications.h" |
| 14 #include "extensions/common/constants.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 namespace { |
| 20 |
| 21 std::string GetExtensionId(const std::string& extension_url) { |
| 22 GURL url(extension_url); |
| 23 if (!url.is_valid() || !url.SchemeIs(extensions::kExtensionScheme)) |
| 24 return ""; |
| 25 return url.GetOrigin().host_piece().as_string(); |
| 26 } |
| 27 |
| 28 std::unique_ptr<base::ListValue> CreateBaseEventArgs( |
| 29 const std::string& extension_id, |
| 30 const std::string& scoped_notification_id) { |
| 31 // Unscope the notification id before returning it. |
| 32 size_t index_of_separator = extension_id.length() + 1; |
| 33 DCHECK_LT(index_of_separator, scoped_notification_id.length()); |
| 34 std::string unscoped_notification_id = |
| 35 scoped_notification_id.substr(index_of_separator); |
| 36 |
| 37 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 38 args->AppendString(unscoped_notification_id); |
| 39 return args; |
| 40 } |
| 41 |
| 42 } // namespace |
8 | 43 |
9 ExtensionNotificationHandler::ExtensionNotificationHandler() = default; | 44 ExtensionNotificationHandler::ExtensionNotificationHandler() = default; |
10 | 45 |
11 ExtensionNotificationHandler::~ExtensionNotificationHandler() = default; | 46 ExtensionNotificationHandler::~ExtensionNotificationHandler() = default; |
12 | 47 |
| 48 void ExtensionNotificationHandler::OnClose(Profile* profile, |
| 49 const std::string& origin, |
| 50 const std::string& notification_id, |
| 51 bool by_user) { |
| 52 EventRouter::UserGestureState gesture = |
| 53 by_user ? EventRouter::USER_GESTURE_ENABLED |
| 54 : EventRouter::USER_GESTURE_NOT_ENABLED; |
| 55 std::string extension_id(GetExtensionId(origin)); |
| 56 DCHECK(!extension_id.empty()); |
| 57 |
| 58 std::unique_ptr<base::ListValue> args( |
| 59 CreateBaseEventArgs(extension_id, notification_id)); |
| 60 args->AppendBoolean(by_user); |
| 61 SendEvent(profile, extension_id, events::NOTIFICATIONS_ON_CLOSED, |
| 62 api::notifications::OnClosed::kEventName, gesture, std::move(args)); |
| 63 |
| 64 ExtensionNotificationDisplayHelper* display_helper = |
| 65 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile); |
| 66 if (display_helper) |
| 67 display_helper->EraseDataForNotificationId(notification_id); |
| 68 } |
| 69 |
| 70 void ExtensionNotificationHandler::OnClick( |
| 71 Profile* profile, |
| 72 const std::string& origin, |
| 73 const std::string& notification_id, |
| 74 int action_index, |
| 75 const base::NullableString16& reply) { |
| 76 DCHECK(reply.is_null()); |
| 77 |
| 78 std::string extension_id(GetExtensionId(origin)); |
| 79 std::unique_ptr<base::ListValue> args( |
| 80 CreateBaseEventArgs(extension_id, notification_id)); |
| 81 if (action_index > -1) |
| 82 args->AppendInteger(action_index); |
| 83 events::HistogramValue histogram_value = |
| 84 action_index > -1 ? events::NOTIFICATIONS_ON_BUTTON_CLICKED |
| 85 : events::NOTIFICATIONS_ON_CLICKED; |
| 86 const std::string& event_name = |
| 87 action_index > -1 ? api::notifications::OnButtonClicked::kEventName |
| 88 : api::notifications::OnClicked::kEventName; |
| 89 |
| 90 SendEvent(profile, extension_id, histogram_value, event_name, |
| 91 EventRouter::USER_GESTURE_ENABLED, std::move(args)); |
| 92 } |
| 93 |
13 void ExtensionNotificationHandler::OpenSettings(Profile* profile) { | 94 void ExtensionNotificationHandler::OpenSettings(Profile* profile) { |
14 // Extension notifications don't display a settings button. | 95 // Extension notifications don't display a settings button. |
15 NOTREACHED(); | 96 NOTREACHED(); |
16 } | 97 } |
| 98 |
| 99 void ExtensionNotificationHandler::RegisterNotification( |
| 100 const std::string& notification_id, |
| 101 NotificationDelegate* delegate) {} |
| 102 |
| 103 void ExtensionNotificationHandler::SendEvent( |
| 104 Profile* profile, |
| 105 const std::string& extension_id, |
| 106 events::HistogramValue histogram_value, |
| 107 const std::string& event_name, |
| 108 EventRouter::UserGestureState user_gesture, |
| 109 std::unique_ptr<base::ListValue> args) { |
| 110 if (extension_id.empty()) |
| 111 return; |
| 112 |
| 113 EventRouter* event_router = EventRouter::Get(profile); |
| 114 if (!event_router) |
| 115 return; |
| 116 |
| 117 std::unique_ptr<Event> event( |
| 118 new Event(histogram_value, event_name, std::move(args))); |
| 119 event->user_gesture = user_gesture; |
| 120 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 121 } |
| 122 |
| 123 } // namespace extensions |
OLD | NEW |