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, | |
dewittj
2017/05/16 22:07:17
Is it possible to unit test this?
Miguel Garcia
2017/05/20 07:04:10
Yes! Done now
| |
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)); | |
dewittj
2017/05/16 22:07:17
need to check for empty string?
Miguel Garcia
2017/05/20 07:04:10
I have added a DCHECK since it should not be possi
| |
56 | |
57 std::unique_ptr<base::ListValue> args( | |
58 CreateBaseEventArgs(extension_id, notification_id)); | |
59 args->AppendBoolean(by_user); | |
60 SendEvent(profile, extension_id, events::NOTIFICATIONS_ON_CLOSED, | |
61 api::notifications::OnClosed::kEventName, gesture, std::move(args)); | |
62 | |
63 ExtensionNotificationDisplayHelper* display_helper = | |
64 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile); | |
65 if (display_helper) | |
66 display_helper->EraseDataForNotificationId(notification_id); | |
67 } | |
68 | |
69 void ExtensionNotificationHandler::OnClick( | |
70 Profile* profile, | |
71 const std::string& origin, | |
72 const std::string& notification_id, | |
73 int action_index, | |
74 const base::NullableString16& reply) { | |
75 DCHECK(reply.is_null()); | |
76 | |
77 std::string extension_id(GetExtensionId(origin)); | |
78 std::unique_ptr<base::ListValue> args( | |
79 CreateBaseEventArgs(extension_id, notification_id)); | |
80 if (action_index > -1) | |
dewittj
2017/05/16 22:07:17
(Not a required change) It seems like if you alrea
Miguel Garcia
2017/05/20 07:04:10
Acknowledged.
| |
81 args->AppendInteger(action_index); | |
82 events::HistogramValue histogram_value = | |
83 action_index > -1 ? events::NOTIFICATIONS_ON_BUTTON_CLICKED | |
84 : events::NOTIFICATIONS_ON_CLICKED; | |
85 const std::string& event = | |
dewittj
2017/05/16 22:07:17
nit: event_name
Miguel Garcia
2017/05/20 07:04:10
Done.
| |
86 action_index > -1 ? api::notifications::OnButtonClicked::kEventName | |
87 : api::notifications::OnClicked::kEventName; | |
88 | |
89 SendEvent(profile, extension_id, histogram_value, event, | |
90 EventRouter::USER_GESTURE_ENABLED, std::move(args)); | |
91 } | |
92 | |
13 void ExtensionNotificationHandler::OpenSettings(Profile* profile) { | 93 void ExtensionNotificationHandler::OpenSettings(Profile* profile) { |
14 // Extension notifications don't display a settings button. | 94 // Extension notifications don't display a settings button. |
15 NOTREACHED(); | 95 NOTREACHED(); |
16 } | 96 } |
97 | |
98 void ExtensionNotificationHandler::RegisterNotification( | |
99 const std::string& notification_id, | |
100 NotificationDelegate* delegate) {} | |
101 | |
102 void ExtensionNotificationHandler::SendEvent( | |
103 Profile* profile, | |
104 const std::string& extension_id, | |
105 events::HistogramValue histogram_value, | |
106 const std::string& name, | |
dewittj
2017/05/16 22:07:17
nit: event_name
Miguel Garcia
2017/05/20 07:04:10
Done.
| |
107 EventRouter::UserGestureState user_gesture, | |
108 std::unique_ptr<base::ListValue> args) { | |
109 if (extension_id.empty()) | |
110 return; | |
111 | |
112 EventRouter* event_router = EventRouter::Get(profile); | |
113 if (!event_router) | |
114 return; | |
115 | |
116 std::unique_ptr<Event> event( | |
117 new Event(histogram_value, name, std::move(args))); | |
118 event->user_gesture = user_gesture; | |
119 event_router->DispatchEventToExtension(extension_id, std::move(event)); | |
120 } | |
121 | |
122 } // namespace extensions | |
OLD | NEW |