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

Unified Diff: chrome/browser/extensions/api/notifications/extension_notification_handler.cc

Issue 2875673002: Minimize the delegate dependencies for native extension notifications. (Closed)
Patch Set: review 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/notifications/extension_notification_handler.cc
diff --git a/chrome/browser/extensions/api/notifications/extension_notification_handler.cc b/chrome/browser/extensions/api/notifications/extension_notification_handler.cc
index f5578ffd8a53e293fea92ce68b168741645c0372..38bd2c70d956274a14e8f31af6ef3747c813fa89 100644
--- a/chrome/browser/extensions/api/notifications/extension_notification_handler.cc
+++ b/chrome/browser/extensions/api/notifications/extension_notification_handler.cc
@@ -5,12 +5,119 @@
#include "chrome/browser/extensions/api/notifications/extension_notification_handler.h"
#include "base/logging.h"
+#include "base/strings/nullable_string16.h"
+#include "base/strings/string_piece.h"
+#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper.h"
+#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/extensions/api/notifications.h"
+#include "extensions/common/constants.h"
+#include "url/gurl.h"
+
+namespace extensions {
+
+namespace {
+
+std::string GetExtensionId(const std::string& extension_url) {
+ GURL url(extension_url);
+ if (!url.is_valid() || !url.SchemeIs(extensions::kExtensionScheme))
+ return "";
+ return url.GetOrigin().host_piece().as_string();
+}
+
+std::unique_ptr<base::ListValue> CreateBaseEventArgs(
+ const std::string& extension_id,
+ const std::string& scoped_notification_id) {
+ // Unscope the notification id before returning it.
+ size_t index_of_separator = extension_id.length() + 1;
+ DCHECK_LT(index_of_separator, scoped_notification_id.length());
+ std::string unscoped_notification_id =
+ scoped_notification_id.substr(index_of_separator);
+
+ std::unique_ptr<base::ListValue> args(new base::ListValue());
+ args->AppendString(unscoped_notification_id);
+ return args;
+}
+
+} // namespace
ExtensionNotificationHandler::ExtensionNotificationHandler() = default;
ExtensionNotificationHandler::~ExtensionNotificationHandler() = default;
+void ExtensionNotificationHandler::OnClose(Profile* profile,
+ const std::string& origin,
+ const std::string& notification_id,
+ bool by_user) {
+ EventRouter::UserGestureState gesture =
+ by_user ? EventRouter::USER_GESTURE_ENABLED
+ : EventRouter::USER_GESTURE_NOT_ENABLED;
+ std::string extension_id(GetExtensionId(origin));
+ DCHECK(!extension_id.empty());
+
+ std::unique_ptr<base::ListValue> args(
+ CreateBaseEventArgs(extension_id, notification_id));
+ args->AppendBoolean(by_user);
+ SendEvent(profile, extension_id, events::NOTIFICATIONS_ON_CLOSED,
+ api::notifications::OnClosed::kEventName, gesture, std::move(args));
+
+ ExtensionNotificationDisplayHelper* display_helper =
+ ExtensionNotificationDisplayHelperFactory::GetForProfile(profile);
+ if (display_helper)
+ display_helper->EraseDataForNotificationId(notification_id);
+}
+
+void ExtensionNotificationHandler::OnClick(
+ Profile* profile,
+ const std::string& origin,
+ const std::string& notification_id,
+ int action_index,
+ const base::NullableString16& reply) {
+ DCHECK(reply.is_null());
+
+ std::string extension_id(GetExtensionId(origin));
+ std::unique_ptr<base::ListValue> args(
+ CreateBaseEventArgs(extension_id, notification_id));
+ if (action_index > -1)
+ args->AppendInteger(action_index);
+ events::HistogramValue histogram_value =
+ action_index > -1 ? events::NOTIFICATIONS_ON_BUTTON_CLICKED
+ : events::NOTIFICATIONS_ON_CLICKED;
+ const std::string& event_name =
+ action_index > -1 ? api::notifications::OnButtonClicked::kEventName
+ : api::notifications::OnClicked::kEventName;
+
+ SendEvent(profile, extension_id, histogram_value, event_name,
+ EventRouter::USER_GESTURE_ENABLED, std::move(args));
+}
+
void ExtensionNotificationHandler::OpenSettings(Profile* profile) {
// Extension notifications don't display a settings button.
NOTREACHED();
}
+
+void ExtensionNotificationHandler::RegisterNotification(
+ const std::string& notification_id,
+ NotificationDelegate* delegate) {}
+
+void ExtensionNotificationHandler::SendEvent(
+ Profile* profile,
+ const std::string& extension_id,
+ events::HistogramValue histogram_value,
+ const std::string& event_name,
+ EventRouter::UserGestureState user_gesture,
+ std::unique_ptr<base::ListValue> args) {
+ if (extension_id.empty())
+ return;
+
+ EventRouter* event_router = EventRouter::Get(profile);
+ if (!event_router)
+ return;
+
+ std::unique_ptr<Event> event(
+ new Event(histogram_value, event_name, std::move(args)));
+ event->user_gesture = user_gesture;
+ event_router->DispatchEventToExtension(extension_id, std::move(event));
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698