1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/scheduler/internal/proto_conversion.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
namespace notifications {
namespace {
// Helper method to convert base::TimeDelta to integer for serialization. Loses
// precision beyond miliseconds.
int64_t TimeDeltaToMilliseconds(const base::TimeDelta& delta) {
return delta.InMilliseconds();
}
// Helper method to convert serialized time delta as integer to base::TimeDelta
// for deserialization. Loses precision beyond miliseconds.
base::TimeDelta MillisecondsToTimeDelta(int64_t serialized_delat_ms) {
return base::Milliseconds(serialized_delat_ms);
}
// Helper method to convert base::Time to integer for serialization. Loses
// precision beyond miliseconds.
int64_t TimeToMilliseconds(const base::Time& time) {
return time.ToDeltaSinceWindowsEpoch().InMilliseconds();
}
// Helper method to convert serialized time as integer to base::Time for
// deserialization. Loses precision beyond miliseconds.
base::Time MillisecondsToTime(int64_t serialized_time_ms) {
return base::Time::FromDeltaSinceWindowsEpoch(
base::Milliseconds(serialized_time_ms));
}
// Converts SchedulerClientType to its associated enum in proto buffer.
proto::SchedulerClientType ToSchedulerClientType(SchedulerClientType type) {
switch (type) {
case SchedulerClientType::kTest1:
return proto::SchedulerClientType::TEST_1;
case SchedulerClientType::kTest2:
return proto::SchedulerClientType::TEST_2;
case SchedulerClientType::kTest3:
return proto::SchedulerClientType::TEST_3;
case SchedulerClientType::kUnknown:
case SchedulerClientType::kDeprecatedFeatureGuide:
return proto::SchedulerClientType::UNKNOWN;
case SchedulerClientType::kWebUI:
return proto::SchedulerClientType::WEBUI;
case SchedulerClientType::kChromeUpdate:
return proto::SchedulerClientType::CHROME_UPDATE;
case SchedulerClientType::kPrefetch:
return proto::SchedulerClientType::PREFETCH;
case SchedulerClientType::kReadingList:
return proto::SchedulerClientType::READING_LIST;
}
NOTREACHED();
}
// Converts SchedulerClientType from its associated enum in proto buffer.
SchedulerClientType FromSchedulerClientType(
proto::SchedulerClientType proto_type) {
switch (proto_type) {
case proto::SchedulerClientType::TEST_1:
return SchedulerClientType::kTest1;
case proto::SchedulerClientType::TEST_2:
return SchedulerClientType::kTest2;
case proto::SchedulerClientType::TEST_3:
return SchedulerClientType::kTest3;
case proto::SchedulerClientType::UNKNOWN:
return SchedulerClientType::kUnknown;
case proto::SchedulerClientType::WEBUI:
return SchedulerClientType::kWebUI;
case proto::SchedulerClientType::CHROME_UPDATE:
return SchedulerClientType::kChromeUpdate;
case proto::SchedulerClientType::PREFETCH:
return SchedulerClientType::kPrefetch;
case proto::SchedulerClientType::READING_LIST:
return SchedulerClientType::kReadingList;
}
NOTREACHED();
}
// Converts UserFeedback to its associated enum in proto buffer.
proto::Impression_UserFeedback ToUserFeedback(UserFeedback feedback) {
switch (feedback) {
case UserFeedback::kNoFeedback:
return proto::Impression_UserFeedback_NO_FEEDBACK;
case UserFeedback::kHelpful:
return proto::Impression_UserFeedback_HELPFUL;
case UserFeedback::kNotHelpful:
return proto::Impression_UserFeedback_NOT_HELPFUL;
case UserFeedback::kClick:
return proto::Impression_UserFeedback_CLICK;
case UserFeedback::kDismiss:
return proto::Impression_UserFeedback_DISMISS;
case UserFeedback::kIgnore:
return proto::Impression_UserFeedback_IGNORE;
}
NOTREACHED();
}
// Converts UserFeedback from its associated enum in proto buffer.
UserFeedback FromUserFeedback(proto::Impression_UserFeedback feedback) {
switch (feedback) {
case proto::Impression_UserFeedback_NO_FEEDBACK:
return UserFeedback::kNoFeedback;
case proto::Impression_UserFeedback_HELPFUL:
return UserFeedback::kHelpful;
case proto::Impression_UserFeedback_NOT_HELPFUL:
return UserFeedback::kNotHelpful;
case proto::Impression_UserFeedback_CLICK:
return UserFeedback::kClick;
case proto::Impression_UserFeedback_DISMISS:
return UserFeedback::kDismiss;
case proto::Impression_UserFeedback_IGNORE:
return UserFeedback::kIgnore;
}
NOTREACHED();
}
// Converts ImpressionResult to its associated enum in proto buffer.
proto::Impression_ImpressionResult ToImpressionResult(ImpressionResult result) {
switch (result) {
case ImpressionResult::kInvalid:
return proto::Impression_ImpressionResult_INVALID;
case ImpressionResult::kPositive:
return proto::Impression_ImpressionResult_POSITIVE;
case ImpressionResult::kNegative:
return proto::Impression_ImpressionResult_NEGATIVE;
case ImpressionResult::kNeutral:
return proto::Impression_ImpressionResult_NEUTRAL;
}
NOTREACHED();
}
// Converts ImpressionResult from its associated enum in proto buffer.
ImpressionResult FromImpressionResult(
proto::Impression_ImpressionResult result) {
switch (result) {
case proto::Impression_ImpressionResult_INVALID:
return ImpressionResult::kInvalid;
case proto::Impression_ImpressionResult_POSITIVE:
return ImpressionResult::kPositive;
case proto::Impression_ImpressionResult_NEGATIVE:
return ImpressionResult::kNegative;
case proto::Impression_ImpressionResult_NEUTRAL:
return ImpressionResult::kNeutral;
}
NOTREACHED();
}
proto::IconType ToIconType(IconType type) {
switch (type) {
case IconType::kUnknownType:
return proto::IconType::UNKNOWN_ICON_TYPE;
case IconType::kSmallIcon:
return proto::IconType::SMALL_ICON;
case IconType::kLargeIcon:
return proto::IconType::LARGE_ICON;
}
NOTREACHED();
}
IconType FromIconType(proto::IconType proto_type) {
switch (proto_type) {
case proto::IconType::UNKNOWN_ICON_TYPE:
return IconType::kUnknownType;
case proto::IconType::SMALL_ICON:
return IconType::kSmallIcon;
case proto::IconType::LARGE_ICON:
return IconType::kLargeIcon;
}
NOTREACHED();
}
proto::ActionButtonType ToActionButtonType(ActionButtonType type) {
switch (type) {
case ActionButtonType::kUnknownAction:
return proto::ActionButtonType::UNKNOWN_ACTION;
case ActionButtonType::kHelpful:
return proto::ActionButtonType::HELPFUL;
case ActionButtonType::kUnhelpful:
return proto::ActionButtonType::UNHELPFUL;
}
NOTREACHED();
}
ActionButtonType FromActionButtonType(proto::ActionButtonType proto_type) {
switch (proto_type) {
case proto::ActionButtonType::UNKNOWN_ACTION:
return ActionButtonType::kUnknownAction;
case proto::ActionButtonType::HELPFUL:
return ActionButtonType::kHelpful;
case proto::ActionButtonType::UNHELPFUL:
return ActionButtonType::kUnhelpful;
}
}
// Converts NotificationData to proto buffer type.
void NotificationDataToProto(NotificationData* notification_data,
proto::NotificationData* proto) {
proto->set_title(base::UTF16ToUTF8(notification_data->title));
proto->set_message(base::UTF16ToUTF8(notification_data->message));
for (const auto& pair : notification_data->custom_data) {
auto* data = proto->add_custom_data();
data->set_key(pair.first);
data->set_value(pair.second);
}
for (const auto& button : notification_data->buttons) {
auto* proto_button = proto->add_buttons();
proto_button->set_text(base::UTF16ToUTF8(button.text));
proto_button->set_button_type(ToActionButtonType(button.type));
proto_button->set_id(button.id);
}
}
// Converts NotificationData from proto buffer type.
void NotificationDataFromProto(proto::NotificationData* proto,
NotificationData* notification_data) {
notification_data->title = base::UTF8ToUTF16(proto->title());
notification_data->message = base::UTF8ToUTF16(proto->message());
for (int i = 0; i < proto->custom_data_size(); ++i) {
const auto& pair = proto->custom_data(i);
notification_data->custom_data.emplace(pair.key(), pair.value());
}
for (int i = 0; i < proto->buttons_size(); ++i) {
NotificationData::Button button;
const auto& proto_button = proto->buttons(i);
button.text = base::UTF8ToUTF16(proto_button.text());
button.type = FromActionButtonType(proto_button.button_type());
button.id = proto_button.id();
notification_data->buttons.emplace_back(button);
}
}
// Converts ScheduleParams::Priority to proto buffer type.
proto::ScheduleParams_Priority ScheduleParamsPriorityToProto(
ScheduleParams::Priority priority) {
using Priority = ScheduleParams::Priority;
switch (priority) {
case Priority::kLow:
return proto::ScheduleParams_Priority_LOW;
case Priority::kNoThrottle:
return proto::ScheduleParams_Priority_NO_THROTTLE;
}
}
// Converts ScheduleParams::Priority from proto buffer type.
ScheduleParams::Priority ScheduleParamsPriorityFromProto(
proto::ScheduleParams_Priority priority) {
using Priority = ScheduleParams::Priority;
switch (priority) {
case proto::ScheduleParams_Priority_LOW:
return Priority::kLow;
case proto::ScheduleParams_Priority_NO_THROTTLE:
return Priority::kNoThrottle;
}
}
// Converts ScheduleParams to proto buffer type.
void ScheduleParamsToProto(ScheduleParams* params,
proto::ScheduleParams* proto) {
proto->set_priority(ScheduleParamsPriorityToProto(params->priority));
for (const auto& mapping : params->impression_mapping) {
auto* proto_impression_mapping = proto->add_impression_mapping();
proto_impression_mapping->set_user_feedback(ToUserFeedback(mapping.first));
proto_impression_mapping->set_impression_result(
ToImpressionResult(mapping.second));
}
if (params->deliver_time_start.has_value()) {
proto->set_deliver_time_start(
TimeToMilliseconds(params->deliver_time_start.value()));
}
if (params->deliver_time_end.has_value()) {
proto->set_deliver_time_end(
TimeToMilliseconds(params->deliver_time_end.value()));
}
if (params->ignore_timeout_duration.has_value()) {
proto->set_ignore_timeout_duration(
TimeDeltaToMilliseconds(params->ignore_timeout_duration.value()));
}
}
// Converts ScheduleParams from proto buffer type.
void ScheduleParamsFromProto(proto::ScheduleParams* proto,
ScheduleParams* params) {
params->priority = ScheduleParamsPriorityFromProto(proto->priority());
for (int i = 0; i < proto->impression_mapping_size(); ++i) {
const auto& proto_impression_mapping = proto->impression_mapping(i);
auto user_feedback =
FromUserFeedback(proto_impression_mapping.user_feedback());
auto impression_result =
FromImpressionResult(proto_impression_mapping.impression_result());
params->impression_mapping[user_feedback] = impression_result;
}
if (proto->has_deliver_time_start()) {
params->deliver_time_start =
MillisecondsToTime(proto->deliver_time_start());
}
if (proto->has_deliver_time_end()) {
params->deliver_time_end = MillisecondsToTime(proto->deliver_time_end());
}
if (proto->has_ignore_timeout_duration()) {
params->ignore_timeout_duration =
MillisecondsToTimeDelta(proto->ignore_timeout_duration());
}
}
} // namespace
void IconEntryToProto(IconEntry* entry, notifications::proto::Icon* proto) {
proto->mutable_icon()->swap(entry->data);
}
void IconEntryFromProto(proto::Icon* proto, notifications::IconEntry* entry) {
DCHECK(proto->has_icon());
entry->data.swap(*proto->mutable_icon());
}
void ClientStateToProto(ClientState* client_state,
notifications::proto::ClientState* proto) {
proto->set_type(ToSchedulerClientType(client_state->type));
proto->set_current_max_daily_show(client_state->current_max_daily_show);
for (const auto& impression : client_state->impressions) {
auto* impression_ptr = proto->add_impressions();
impression_ptr->set_create_time(TimeToMilliseconds(impression.create_time));
impression_ptr->set_feedback(ToUserFeedback(impression.feedback));
impression_ptr->set_impression(ToImpressionResult(impression.impression));
impression_ptr->set_integrated(impression.integrated);
impression_ptr->set_guid(impression.guid);
for (const auto& mapping : impression.impression_mapping) {
auto* proto_impression_mapping = impression_ptr->add_impression_mapping();
proto_impression_mapping->set_user_feedback(
ToUserFeedback(mapping.first));
proto_impression_mapping->set_impression_result(
ToImpressionResult(mapping.second));
}
for (const auto& pair : impression.custom_data) {
auto* data = impression_ptr->add_custom_data();
data->set_key(pair.first);
data->set_value(pair.second);
}
if (impression.ignore_timeout_duration.has_value()) {
impression_ptr->set_ignore_timeout_duration(
TimeDeltaToMilliseconds(impression.ignore_timeout_duration.value()));
}
}
if (client_state->suppression_info.has_value()) {
const auto& suppression = *client_state->suppression_info;
auto* suppression_proto = proto->mutable_suppression_info();
suppression_proto->set_last_trigger_time(
TimeToMilliseconds(suppression.last_trigger_time));
suppression_proto->set_duration_ms(
TimeDeltaToMilliseconds(suppression.duration));
suppression_proto->set_recover_goal(suppression.recover_goal);
}
proto->set_negative_events_count(client_state->negative_events_count);
if (client_state->last_negative_event_ts.has_value()) {
proto->set_last_negative_event_ts(
TimeToMilliseconds(client_state->last_negative_event_ts.value()));
}
if (client_state->last_shown_ts.has_value()) {
proto->set_last_shown_ts(
TimeToMilliseconds(client_state->last_shown_ts.value()));
}
}
void ClientStateFromProto(proto::ClientState* proto,
notifications::ClientState* client_state) {
DCHECK(proto->has_type());
DCHECK(proto->has_current_max_daily_show());
client_state->type = FromSchedulerClientType(proto->type());
client_state->current_max_daily_show = proto->current_max_daily_show();
for (const auto& proto_impression : proto->impressions()) {
Impression impression;
DCHECK(proto_impression.has_create_time());
impression.create_time = MillisecondsToTime(proto_impression.create_time());
impression.feedback = FromUserFeedback(proto_impression.feedback());
impression.impression = FromImpressionResult(proto_impression.impression());
impression.integrated = proto_impression.integrated();
impression.guid = proto_impression.guid();
impression.type = client_state->type;
if (proto_impression.has_ignore_timeout_duration())
impression.ignore_timeout_duration =
MillisecondsToTimeDelta(proto_impression.ignore_timeout_duration());
for (int i = 0; i < proto_impression.impression_mapping_size(); ++i) {
const auto& proto_impression_mapping =
proto_impression.impression_mapping(i);
auto user_feedback =
FromUserFeedback(proto_impression_mapping.user_feedback());
auto impression_result =
FromImpressionResult(proto_impression_mapping.impression_result());
impression.impression_mapping[user_feedback] = impression_result;
}
for (int i = 0; i < proto_impression.custom_data_size(); ++i) {
const auto& pair = proto_impression.custom_data(i);
impression.custom_data.emplace(pair.key(), pair.value());
}
client_state->impressions.emplace_back(std::move(impression));
}
if (proto->has_suppression_info()) {
const auto& proto_suppression = proto->suppression_info();
DCHECK(proto_suppression.has_last_trigger_time());
DCHECK(proto_suppression.has_duration_ms());
DCHECK(proto_suppression.has_recover_goal());
SuppressionInfo suppression_info(
MillisecondsToTime(proto_suppression.last_trigger_time()),
MillisecondsToTimeDelta(proto_suppression.duration_ms()));
suppression_info.recover_goal = proto_suppression.recover_goal();
client_state->suppression_info = std::move(suppression_info);
}
client_state->negative_events_count = proto->negative_events_count();
if (proto->has_last_shown_ts()) {
client_state->last_shown_ts = MillisecondsToTime(proto->last_shown_ts());
}
if (proto->has_last_negative_event_ts()) {
client_state->last_negative_event_ts =
MillisecondsToTime(proto->last_negative_event_ts());
}
}
void NotificationEntryToProto(NotificationEntry* entry,
proto::NotificationEntry* proto) {
proto->set_type(ToSchedulerClientType(entry->type));
proto->set_guid(entry->guid);
proto->set_create_time(TimeToMilliseconds(entry->create_time));
auto* proto_notification_data = proto->mutable_notification_data();
for (const auto& icon_type_uuid_pair : entry->icons_uuid) {
auto* proto_icons = proto_notification_data->add_icons_uuid();
proto_icons->set_type(ToIconType(icon_type_uuid_pair.first));
proto_icons->set_uuid(icon_type_uuid_pair.second);
}
NotificationDataToProto(&entry->notification_data, proto_notification_data);
auto* proto_schedule_params = proto->mutable_schedule_params();
ScheduleParamsToProto(&entry->schedule_params, proto_schedule_params);
}
void NotificationEntryFromProto(proto::NotificationEntry* proto,
NotificationEntry* entry) {
entry->type = FromSchedulerClientType(proto->type());
entry->guid = proto->guid();
entry->create_time = MillisecondsToTime(proto->create_time());
NotificationDataFromProto(proto->mutable_notification_data(),
&entry->notification_data);
ScheduleParamsFromProto(proto->mutable_schedule_params(),
&entry->schedule_params);
for (int i = 0; i < proto->notification_data().icons_uuid_size(); i++) {
const auto& icon_uuid_pair = proto->notification_data().icons_uuid(i);
entry->icons_uuid.emplace(FromIconType(icon_uuid_pair.type()),
icon_uuid_pair.uuid());
}
}
} // namespace notifications
|