Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 5 | #include "content/browser/notifications/notification_database_conversions.h" |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 9 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 10 | #include <optional> |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 11 | |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 12 | #include "base/check.h" |
Avi Drissman | 02553b8 | 2024-10-16 16:14:16 | [diff] [blame] | 13 | #include "base/containers/span.h" |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 14 | #include "base/notreached.h" |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 16 | #include "base/threading/thread_restrictions.h" |
peter | b8afbbe | 2016-02-01 14:01:11 | [diff] [blame] | 17 | #include "base/time/time.h" |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 18 | #include "content/browser/notifications/notification_database_data.pb.h" |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 19 | #include "content/browser/notifications/notification_database_resources.pb.h" |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 20 | #include "content/public/browser/notification_database_data.h" |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 21 | #include "third_party/blink/public/common/notifications/notification_resources.h" |
Gyuyoung Kim | 74e4058a | 2019-09-19 08:06:31 | [diff] [blame] | 22 | #include "third_party/blink/public/mojom/notifications/notification.mojom.h" |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 23 | #include "third_party/skia/include/core/SkBitmap.h" |
| 24 | #include "ui/gfx/codec/png_codec.h" |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 25 | |
| 26 | namespace content { |
| 27 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // static |
| 31 | SkBitmap DeserializeImage(const std::string& image_data) { |
| 32 | base::AssertLongCPUWorkAllowed(); |
Avi Drissman | 02553b8 | 2024-10-16 16:14:16 | [diff] [blame] | 33 | return gfx::PNGCodec::Decode(base::as_byte_span(image_data)); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // static |
| 37 | std::vector<unsigned char> SerializeImage(const SkBitmap& image) { |
| 38 | base::AssertLongCPUWorkAllowed(); |
Avi Drissman | 02553b8 | 2024-10-16 16:14:16 | [diff] [blame] | 39 | std::optional<std::vector<uint8_t>> image_data = |
| 40 | gfx::PNGCodec::EncodeBGRASkBitmap(image, /*discard_transparency=*/false); |
| 41 | return image_data.value_or(std::vector<uint8_t>()); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 46 | bool DeserializeNotificationDatabaseData(const std::string& input, |
| 47 | NotificationDatabaseData* output) { |
| 48 | DCHECK(output); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 49 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 50 | NotificationDatabaseDataProto message; |
| 51 | if (!message.ParseFromString(input)) |
| 52 | return false; |
| 53 | |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 54 | output->notification_id = message.notification_id(); |
| 55 | output->origin = GURL(message.origin()); |
| 56 | output->service_worker_registration_id = |
| 57 | message.service_worker_registration_id(); |
Sharon Yang | d817469 | 2018-05-18 16:49:05 | [diff] [blame] | 58 | output->replaced_existing_notification = |
| 59 | message.replaced_existing_notification(); |
| 60 | output->num_clicks = message.num_clicks(); |
| 61 | output->num_action_button_clicks = message.num_action_button_clicks(); |
| 62 | output->creation_time_millis = base::Time::FromDeltaSinceWindowsEpoch( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 63 | base::Microseconds(message.creation_time_millis())); |
Sharon Yang | d0ad806 | 2018-08-01 10:31:49 | [diff] [blame] | 64 | |
| 65 | if (message.has_time_until_close_millis()) { |
| 66 | output->time_until_close_millis = |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 67 | base::Milliseconds(message.time_until_close_millis()); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 68 | } else { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 69 | output->time_until_close_millis = std::nullopt; |
Sharon Yang | d0ad806 | 2018-08-01 10:31:49 | [diff] [blame] | 70 | } |
| 71 | if (message.has_time_until_first_click_millis()) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 72 | output->time_until_first_click_millis = |
| 73 | base::Milliseconds(message.time_until_first_click_millis()); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 74 | } else { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 75 | output->time_until_first_click_millis = std::nullopt; |
Sharon Yang | d0ad806 | 2018-08-01 10:31:49 | [diff] [blame] | 76 | } |
| 77 | if (message.has_time_until_last_click_millis()) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 78 | output->time_until_last_click_millis = |
| 79 | base::Milliseconds(message.time_until_last_click_millis()); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 80 | } else { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 81 | output->time_until_last_click_millis = std::nullopt; |
Sharon Yang | d0ad806 | 2018-08-01 10:31:49 | [diff] [blame] | 82 | } |
Sharon Yang | d817469 | 2018-05-18 16:49:05 | [diff] [blame] | 83 | |
| 84 | switch (message.closed_reason()) { |
| 85 | case NotificationDatabaseDataProto::USER: |
| 86 | output->closed_reason = NotificationDatabaseData::ClosedReason::USER; |
| 87 | break; |
| 88 | case NotificationDatabaseDataProto::DEVELOPER: |
| 89 | output->closed_reason = NotificationDatabaseData::ClosedReason::DEVELOPER; |
| 90 | break; |
| 91 | case NotificationDatabaseDataProto::UNKNOWN: |
| 92 | output->closed_reason = NotificationDatabaseData::ClosedReason::UNKNOWN; |
| 93 | break; |
| 94 | } |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 95 | |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 96 | blink::PlatformNotificationData* notification_data = |
| 97 | &output->notification_data; |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 98 | const NotificationDatabaseDataProto::NotificationData& payload = |
| 99 | message.notification_data(); |
| 100 | |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 101 | notification_data->title = base::UTF8ToUTF16(payload.title()); |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 102 | |
| 103 | switch (payload.direction()) { |
| 104 | case NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT: |
| 105 | notification_data->direction = |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 106 | blink::mojom::NotificationDirection::LEFT_TO_RIGHT; |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 107 | break; |
| 108 | case NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT: |
| 109 | notification_data->direction = |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 110 | blink::mojom::NotificationDirection::RIGHT_TO_LEFT; |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 111 | break; |
| 112 | case NotificationDatabaseDataProto::NotificationData::AUTO: |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 113 | notification_data->direction = blink::mojom::NotificationDirection::AUTO; |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 117 | notification_data->lang = payload.lang(); |
| 118 | notification_data->body = base::UTF8ToUTF16(payload.body()); |
| 119 | notification_data->tag = payload.tag(); |
johnme | 578c5cc8 | 2016-08-24 22:38:54 | [diff] [blame] | 120 | notification_data->image = GURL(payload.image()); |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 121 | notification_data->icon = GURL(payload.icon()); |
mvanouwerkerk | 2dc0200 | 2016-03-18 10:21:45 | [diff] [blame] | 122 | notification_data->badge = GURL(payload.badge()); |
sh919.park | 1593bf8 | 2015-05-10 21:30:08 | [diff] [blame] | 123 | |
| 124 | if (payload.vibration_pattern().size() > 0) { |
| 125 | notification_data->vibration_pattern.assign( |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 126 | payload.vibration_pattern().begin(), payload.vibration_pattern().end()); |
Peter Beverloo | 9ea5307 | 2018-06-22 14:20:59 | [diff] [blame] | 127 | } else { |
| 128 | notification_data->vibration_pattern.clear(); |
sh919.park | 1593bf8 | 2015-05-10 21:30:08 | [diff] [blame] | 129 | } |
| 130 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 131 | notification_data->timestamp = base::Time::FromDeltaSinceWindowsEpoch( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 132 | base::Microseconds(payload.timestamp())); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 133 | |
harkness | 39c49fe | 2016-02-10 20:15:27 | [diff] [blame] | 134 | notification_data->renotify = payload.renotify(); |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 135 | notification_data->silent = payload.silent(); |
johnme | 046341a | 2015-09-01 14:40:02 | [diff] [blame] | 136 | notification_data->require_interaction = payload.require_interaction(); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 137 | |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 138 | if (payload.data().length()) { |
| 139 | notification_data->data.assign(payload.data().begin(), |
| 140 | payload.data().end()); |
Peter Beverloo | 9ea5307 | 2018-06-22 14:20:59 | [diff] [blame] | 141 | } else { |
| 142 | notification_data->data.clear(); |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 143 | } |
| 144 | |
Peter Beverloo | 9ea5307 | 2018-06-22 14:20:59 | [diff] [blame] | 145 | notification_data->actions.clear(); |
| 146 | |
John Mellor | 5f170b9 | 2015-08-05 10:33:50 | [diff] [blame] | 147 | for (const auto& payload_action : payload.actions()) { |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 148 | auto action = blink::mojom::NotificationAction::New(); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 149 | |
| 150 | switch (payload_action.type()) { |
| 151 | case NotificationDatabaseDataProto::NotificationAction::BUTTON: |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 152 | action->type = blink::mojom::NotificationActionType::BUTTON; |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 153 | break; |
| 154 | case NotificationDatabaseDataProto::NotificationAction::TEXT: |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 155 | action->type = blink::mojom::NotificationActionType::TEXT; |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 156 | break; |
| 157 | default: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 158 | NOTREACHED(); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 159 | } |
| 160 | |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 161 | action->action = payload_action.action(); |
| 162 | action->title = base::UTF8ToUTF16(payload_action.title()); |
| 163 | action->icon = GURL(payload_action.icon()); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 164 | if (payload_action.has_placeholder()) { |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 165 | action->placeholder = base::UTF8ToUTF16(payload_action.placeholder()); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 166 | } |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 167 | notification_data->actions.push_back(std::move(action)); |
John Mellor | 5f170b9 | 2015-08-05 10:33:50 | [diff] [blame] | 168 | } |
| 169 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 170 | if (payload.has_show_trigger_timestamp()) { |
| 171 | notification_data->show_trigger_timestamp = |
| 172 | base::Time::FromDeltaSinceWindowsEpoch( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 173 | base::Microseconds(payload.show_trigger_timestamp())); |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 174 | } else { |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 175 | notification_data->show_trigger_timestamp = std::nullopt; |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | output->has_triggered = message.has_triggered(); |
| 179 | |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 180 | output->is_shown_by_browser = message.is_shown_by_browser(); |
| 181 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 182 | output->notification_resources = std::nullopt; |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 183 | |
Sarah Krakowiak Criel | e7e3aac | 2025-04-10 15:38:27 | [diff] [blame] | 184 | output->serialized_metadata = |
| 185 | std::map<std::string, std::string>(message.serialized_metadata().begin(), |
| 186 | message.serialized_metadata().end()); |
| 187 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 191 | bool SerializeNotificationDatabaseData(const NotificationDatabaseData& input, |
| 192 | std::string* output) { |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 193 | DCHECK(output); |
| 194 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 195 | auto payload = |
| 196 | std::make_unique<NotificationDatabaseDataProto::NotificationData>(); |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 197 | |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 198 | const blink::PlatformNotificationData& notification_data = |
| 199 | input.notification_data; |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 200 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 201 | payload->set_title(base::UTF16ToUTF8(notification_data.title)); |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 202 | |
| 203 | switch (notification_data.direction) { |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 204 | case blink::mojom::NotificationDirection::LEFT_TO_RIGHT: |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 205 | payload->set_direction( |
| 206 | NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT); |
| 207 | break; |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 208 | case blink::mojom::NotificationDirection::RIGHT_TO_LEFT: |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 209 | payload->set_direction( |
| 210 | NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT); |
| 211 | break; |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 212 | case blink::mojom::NotificationDirection::AUTO: |
peter | aaa9ca53 | 2015-08-04 14:53:24 | [diff] [blame] | 213 | payload->set_direction( |
| 214 | NotificationDatabaseDataProto::NotificationData::AUTO); |
| 215 | break; |
| 216 | } |
| 217 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 218 | payload->set_lang(notification_data.lang); |
| 219 | payload->set_body(base::UTF16ToUTF8(notification_data.body)); |
| 220 | payload->set_tag(notification_data.tag); |
johnme | 578c5cc8 | 2016-08-24 22:38:54 | [diff] [blame] | 221 | payload->set_image(notification_data.image.spec()); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 222 | payload->set_icon(notification_data.icon.spec()); |
mvanouwerkerk | 2dc0200 | 2016-03-18 10:21:45 | [diff] [blame] | 223 | payload->set_badge(notification_data.badge.spec()); |
sh919.park | 1593bf8 | 2015-05-10 21:30:08 | [diff] [blame] | 224 | |
| 225 | for (size_t i = 0; i < notification_data.vibration_pattern.size(); ++i) |
| 226 | payload->add_vibration_pattern(notification_data.vibration_pattern[i]); |
| 227 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 228 | payload->set_timestamp( |
| 229 | notification_data.timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds()); |
harkness | 39c49fe | 2016-02-10 20:15:27 | [diff] [blame] | 230 | payload->set_renotify(notification_data.renotify); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 231 | payload->set_silent(notification_data.silent); |
johnme | 046341a | 2015-09-01 14:40:02 | [diff] [blame] | 232 | payload->set_require_interaction(notification_data.require_interaction); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 233 | |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 234 | if (notification_data.data.size()) { |
| 235 | payload->set_data(¬ification_data.data.front(), |
| 236 | notification_data.data.size()); |
| 237 | } |
| 238 | |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 239 | for (const auto& action : notification_data.actions) { |
John Mellor | 5f170b9 | 2015-08-05 10:33:50 | [diff] [blame] | 240 | NotificationDatabaseDataProto::NotificationAction* payload_action = |
| 241 | payload->add_actions(); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 242 | |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 243 | switch (action->type) { |
Gyuyoung Kim | 3fd1c30 | 2020-10-27 07:57:15 | [diff] [blame] | 244 | case blink::mojom::NotificationActionType::BUTTON: |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 245 | payload_action->set_type( |
| 246 | NotificationDatabaseDataProto::NotificationAction::BUTTON); |
| 247 | break; |
Gyuyoung Kim | 3fd1c30 | 2020-10-27 07:57:15 | [diff] [blame] | 248 | case blink::mojom::NotificationActionType::TEXT: |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 249 | payload_action->set_type( |
| 250 | NotificationDatabaseDataProto::NotificationAction::TEXT); |
| 251 | break; |
| 252 | default: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 253 | NOTREACHED() << "Unknown action type: " << action->type; |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 254 | } |
| 255 | |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 256 | payload_action->set_action(action->action); |
| 257 | payload_action->set_title(base::UTF16ToUTF8(action->title)); |
| 258 | payload_action->set_icon(action->icon.spec()); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 259 | |
Gyuyoung Kim | 109ae2d3 | 2021-02-26 08:40:56 | [diff] [blame] | 260 | if (action->placeholder) { |
| 261 | payload_action->set_placeholder(base::UTF16ToUTF8(*action->placeholder)); |
nsatragno | d1c3e840 | 2016-04-07 12:32:20 | [diff] [blame] | 262 | } |
John Mellor | 5f170b9 | 2015-08-05 10:33:50 | [diff] [blame] | 263 | } |
| 264 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 265 | if (notification_data.show_trigger_timestamp.has_value()) { |
| 266 | payload->set_show_trigger_timestamp( |
| 267 | notification_data.show_trigger_timestamp.value() |
| 268 | .ToDeltaSinceWindowsEpoch() |
| 269 | .InMicroseconds()); |
| 270 | } |
| 271 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 272 | NotificationDatabaseDataProto message; |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 273 | message.set_notification_id(input.notification_id); |
| 274 | message.set_origin(input.origin.spec()); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 275 | message.set_service_worker_registration_id( |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 276 | input.service_worker_registration_id); |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 277 | message.set_allocated_notification_data(payload.release()); |
Sharon Yang | d817469 | 2018-05-18 16:49:05 | [diff] [blame] | 278 | message.set_replaced_existing_notification( |
| 279 | input.replaced_existing_notification); |
| 280 | message.set_num_clicks(input.num_clicks); |
| 281 | message.set_num_action_button_clicks(input.num_action_button_clicks); |
| 282 | message.set_creation_time_millis( |
| 283 | input.creation_time_millis.ToDeltaSinceWindowsEpoch().InMicroseconds()); |
Sharon Yang | 71bfb2be | 2018-06-04 16:04:40 | [diff] [blame] | 284 | if (input.time_until_first_click_millis.has_value()) { |
| 285 | message.set_time_until_first_click_millis( |
| 286 | input.time_until_first_click_millis.value().InMilliseconds()); |
| 287 | } |
| 288 | if (input.time_until_last_click_millis.has_value()) { |
| 289 | message.set_time_until_last_click_millis( |
| 290 | input.time_until_last_click_millis.value().InMilliseconds()); |
| 291 | } |
| 292 | if (input.time_until_close_millis.has_value()) { |
| 293 | message.set_time_until_close_millis( |
| 294 | input.time_until_close_millis.value().InMilliseconds()); |
| 295 | } |
Sharon Yang | d817469 | 2018-05-18 16:49:05 | [diff] [blame] | 296 | |
| 297 | switch (input.closed_reason) { |
| 298 | case NotificationDatabaseData::ClosedReason::USER: |
| 299 | message.set_closed_reason(NotificationDatabaseDataProto::USER); |
| 300 | break; |
| 301 | case NotificationDatabaseData::ClosedReason::DEVELOPER: |
| 302 | message.set_closed_reason(NotificationDatabaseDataProto::DEVELOPER); |
| 303 | break; |
| 304 | case NotificationDatabaseData::ClosedReason::UNKNOWN: |
| 305 | message.set_closed_reason(NotificationDatabaseDataProto::UNKNOWN); |
| 306 | break; |
| 307 | } |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 308 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 309 | message.set_has_triggered(input.has_triggered); |
| 310 | |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 311 | message.set_is_shown_by_browser(input.is_shown_by_browser); |
| 312 | |
Sarah Krakowiak Criel | e7e3aac | 2025-04-10 15:38:27 | [diff] [blame] | 313 | message.mutable_serialized_metadata()->insert( |
| 314 | input.serialized_metadata.begin(), input.serialized_metadata.end()); |
| 315 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 316 | return message.SerializeToString(output); |
| 317 | } |
| 318 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 319 | bool DeserializeNotificationDatabaseResources( |
| 320 | const std::string& serialized_resources, |
| 321 | blink::NotificationResources* output) { |
| 322 | DCHECK(output); |
| 323 | |
| 324 | NotificationDatabaseResourcesProto message; |
| 325 | if (!message.ParseFromString(serialized_resources)) |
| 326 | return false; |
| 327 | |
| 328 | if (message.has_image()) |
| 329 | output->image = DeserializeImage(message.image()); |
| 330 | else |
| 331 | output->image = SkBitmap(); |
| 332 | |
| 333 | if (message.has_notification_icon()) |
| 334 | output->notification_icon = DeserializeImage(message.notification_icon()); |
| 335 | else |
| 336 | output->notification_icon = SkBitmap(); |
| 337 | |
| 338 | if (message.has_badge()) |
| 339 | output->badge = DeserializeImage(message.badge()); |
| 340 | else |
| 341 | output->badge = SkBitmap(); |
| 342 | |
| 343 | output->action_icons.clear(); |
| 344 | for (int i = 0; i < message.action_icons_size(); ++i) |
| 345 | output->action_icons.push_back(DeserializeImage(message.action_icons(i))); |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool SerializeNotificationDatabaseResources( |
| 351 | const blink::NotificationResources& input, |
| 352 | std::string* serialized_resources) { |
| 353 | DCHECK(serialized_resources); |
| 354 | |
| 355 | NotificationDatabaseResourcesProto message; |
| 356 | |
| 357 | if (!input.image.isNull()) { |
| 358 | auto image_data = SerializeImage(input.image); |
| 359 | message.set_image(image_data.data(), image_data.size()); |
| 360 | } |
| 361 | if (!input.notification_icon.isNull()) { |
| 362 | auto image_data = SerializeImage(input.notification_icon); |
| 363 | message.set_notification_icon(image_data.data(), image_data.size()); |
| 364 | } |
| 365 | if (!input.badge.isNull()) { |
| 366 | auto image_data = SerializeImage(input.badge); |
| 367 | message.set_badge(image_data.data(), image_data.size()); |
| 368 | } |
| 369 | for (const auto& image : input.action_icons) { |
| 370 | if (!image.isNull()) { |
| 371 | auto image_data = SerializeImage(image); |
| 372 | message.add_action_icons(image_data.data(), image_data.size()); |
| 373 | } else { |
| 374 | message.add_action_icons(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return message.SerializeToString(serialized_resources); |
| 379 | } |
| 380 | |
peter | 05cb4db | 2015-03-13 14:33:38 | [diff] [blame] | 381 | } // namespace content |