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