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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/phonehub/notification_processor.h"
#include "ash/constants/ash_features.h"
#include "base/barrier_closure.h"
#include "base/functional/callback.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/phonehub/notification.h"
#include "chromeos/ash/components/phonehub/notification_manager.h"
#include "services/data_decoder/public/cpp/decode_image.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia.h"
namespace ash::phonehub {
namespace {
// Constants to override the Messages app monochrome icon color.
const char kMessagesPackageName[] = "com.google.android.apps.messaging";
const SkColor kMessagesOverrideColor = gfx::kGoogleBlue600;
std::optional<SkColor> getMonochromeIconColor(const proto::Notification& proto,
const gfx::Image& icon) {
if (icon.IsEmpty() || !proto.origin_app().has_monochrome_icon_color()) {
return std::nullopt;
}
if (proto.origin_app().package_name() == kMessagesPackageName) {
// The notification color supplied by the Messages app (Bugle) is based
// on light/dark mode of the phone, not the Chromebook, with no way to
// query for both at runtime. These constants are used to override with
// a fixed color. See conversation at b/207089786 for more details.
return kMessagesOverrideColor;
}
return SkColorSetRGB(proto.origin_app().monochrome_icon_color().red(),
proto.origin_app().monochrome_icon_color().green(),
proto.origin_app().monochrome_icon_color().blue());
}
Notification::Importance GetNotificationImportanceFromProto(
proto::NotificationImportance importance) {
switch (importance) {
case proto::NotificationImportance::UNSPECIFIED:
return Notification::Importance::kUnspecified;
case proto::NotificationImportance::NONE:
return Notification::Importance::kNone;
case proto::NotificationImportance::MIN:
return Notification::Importance::kMin;
case proto::NotificationImportance::LOW:
return Notification::Importance::kLow;
case proto::NotificationImportance::DEFAULT:
return Notification::Importance::kDefault;
case proto::NotificationImportance::HIGH:
return Notification::Importance::kHigh;
default:
return Notification::Importance::kUnspecified;
}
}
bool HasSupportedActionIdInProto(const proto::Notification& proto) {
for (const auto& action : proto.actions()) {
if (action.type() == proto::Action_InputType::Action_InputType_TEXT ||
action.call_action() ==
proto::Action_CallAction::Action_CallAction_ANSWER ||
action.call_action() ==
proto::Action_CallAction::Action_CallAction_DECLINE ||
action.call_action() ==
proto::Action_CallAction::Action_CallAction_HANGUP)
return true;
}
return false;
}
bool IsMonochromeIconEnabled(const proto::Notification& notification_proto) {
return features::IsPhoneHubMonochromeNotificationIconsEnabled() &&
notification_proto.origin_app().has_monochrome_icon_mask();
}
Notification CreateInternalNotification(const proto::Notification& proto,
const gfx::Image& color_icon,
const gfx::Image& monochrome_icon,
const gfx::Image& shared_image,
const gfx::Image& contact_image) {
base::flat_map<Notification::ActionType, int64_t> action_id_map;
Notification::InteractionBehavior behavior =
Notification::InteractionBehavior::kNone;
for (const auto& action : proto.actions()) {
if (action.type() == proto::Action_InputType::Action_InputType_TEXT) {
action_id_map[Notification::ActionType::kInlineReply] = action.id();
} else if (action.type() ==
proto::Action_InputType::Action_InputType_OPEN) {
behavior = Notification::InteractionBehavior::kOpenable;
} else if (action.call_action() ==
proto::Action_CallAction::Action_CallAction_ANSWER) {
action_id_map[Notification::ActionType::kAnswer] = action.id();
} else if (action.call_action() ==
proto::Action_CallAction::Action_CallAction_DECLINE) {
action_id_map[Notification::ActionType::kDecline] = action.id();
} else if (action.call_action() ==
proto::Action_CallAction::Action_CallAction_HANGUP) {
action_id_map[Notification::ActionType::kHangup] = action.id();
}
}
Notification::Category category = Notification::Category::kNone;
switch (proto.category()) {
case proto::Notification::Category::Notification_Category_UNSPECIFIED:
category = Notification::Category::kNone;
break;
case proto::Notification::Category::Notification_Category_CONVERSATION:
category = Notification::Category::kConversation;
break;
case proto::Notification::Category::Notification_Category_INCOMING_CALL:
category = Notification::Category::kIncomingCall;
break;
case proto::Notification::Category::Notification_Category_ONGOING_CALL:
category = Notification::Category::kOngoingCall;
break;
case proto::Notification::Category::Notification_Category_SCREEN_CALL:
category = Notification::Category::kScreenCall;
break;
case proto::
Notification_Category_Notification_Category_INT_MIN_SENTINEL_DO_NOT_USE_: // NOLINT
case proto::
Notification_Category_Notification_Category_INT_MAX_SENTINEL_DO_NOT_USE_: // NOLINT
PA_LOG(WARNING) << "Notification category is unknown or unspecified.";
break;
}
std::optional<std::u16string> title = std::nullopt;
if (!proto.title().empty())
title = base::UTF8ToUTF16(proto.title());
std::optional<std::u16string> text_content = std::nullopt;
if (!proto.text_content().empty())
text_content = base::UTF8ToUTF16(proto.text_content());
std::optional<gfx::Image> opt_shared_image = std::nullopt;
if (!shared_image.IsEmpty())
opt_shared_image = shared_image;
std::optional<gfx::Image> opt_contact_image = std::nullopt;
if (!contact_image.IsEmpty())
opt_contact_image = contact_image;
bool icon_is_monochrome = IsMonochromeIconEnabled(proto);
std::optional<SkColor> icon_color =
icon_is_monochrome ? getMonochromeIconColor(proto, monochrome_icon)
: std::nullopt;
return Notification(
proto.id(),
Notification::AppMetadata(
base::UTF8ToUTF16(proto.origin_app().visible_name()),
proto.origin_app().package_name(), color_icon, monochrome_icon,
icon_color, icon_is_monochrome, proto.origin_app().user_id(),
proto.origin_app().app_streamability_status()),
base::Time::FromMillisecondsSinceUnixEpoch(proto.epoch_time_millis()),
GetNotificationImportanceFromProto(proto.importance()), category,
action_id_map, behavior, title, text_content, opt_shared_image,
opt_contact_image);
}
} // namespace
NotificationProcessor::NotificationImages::NotificationImages() = default;
NotificationProcessor::NotificationImages::~NotificationImages() = default;
NotificationProcessor::NotificationImages::NotificationImages(
const NotificationImages& other) = default;
NotificationProcessor::NotificationImages&
NotificationProcessor::NotificationImages::operator=(
const NotificationImages& other) = default;
NotificationProcessor::DecodeImageRequestMetadata::DecodeImageRequestMetadata(
int64_t notification_id,
NotificationImageField image_field,
const std::string& data)
: notification_id(notification_id), image_field(image_field), data(data) {}
NotificationProcessor::NotificationProcessor(
NotificationManager* notification_manager)
: NotificationProcessor(notification_manager,
std::make_unique<ImageDecoderDelegate>()) {}
NotificationProcessor::NotificationProcessor(
NotificationManager* notification_manager,
std::unique_ptr<ImageDecoderDelegate> delegate)
: notification_manager_(notification_manager),
delegate_(std::move(delegate)) {}
NotificationProcessor::~NotificationProcessor() {}
void NotificationProcessor::ClearNotificationsAndPendingUpdates() {
notification_manager_->ClearNotificationsInternal();
// Clear pending updates that may occur.
weak_ptr_factory_.InvalidateWeakPtrs();
pending_notification_requests_ = base::queue<base::OnceClosure>();
id_to_images_map_.clear();
}
void NotificationProcessor::AddNotifications(
const std::vector<proto::Notification>& notification_protos) {
if (notification_protos.empty())
return;
std::vector<proto::Notification> processed_notification_protos;
std::vector<DecodeImageRequestMetadata> decode_image_requests;
for (const auto& proto : notification_protos) {
// Only process notifications that are messaging apps with inline-replies
// or dialer apps with call-style actions.
if (!HasSupportedActionIdInProto(proto))
continue;
processed_notification_protos.emplace_back(proto);
decode_image_requests.emplace_back(proto.id(),
NotificationImageField::kColorIcon,
proto.origin_app().icon());
if (IsMonochromeIconEnabled(proto)) {
decode_image_requests.emplace_back(
proto.id(), NotificationImageField::kMonochromeIcon,
proto.origin_app().monochrome_icon_mask());
} else {
decode_image_requests.emplace_back(
proto.id(), NotificationImageField::kMonochromeIcon,
proto.origin_app().icon());
}
if (!proto.shared_image().empty()) {
decode_image_requests.emplace_back(proto.id(),
NotificationImageField::kSharedImage,
proto.shared_image());
}
if (!proto.contact_image().empty()) {
decode_image_requests.emplace_back(proto.id(),
NotificationImageField::kContactImage,
proto.contact_image());
}
}
if (decode_image_requests.empty()) {
PA_LOG(INFO) << "Cannot find any image to decode for the notifications";
return;
}
base::RepeatingClosure barrier = base::BarrierClosure(
decode_image_requests.size(),
base::BindOnce(&NotificationProcessor::OnAllImagesDecoded,
weak_ptr_factory_.GetWeakPtr(),
std::move(processed_notification_protos)));
base::OnceClosure add_request =
base::BindOnce(&NotificationProcessor::StartDecodingImages,
weak_ptr_factory_.GetWeakPtr(),
std::move(decode_image_requests), barrier);
pending_notification_requests_.emplace(std::move(add_request));
ProcessRequestQueue();
}
void NotificationProcessor::RemoveNotifications(
const base::flat_set<int64_t>& notification_ids) {
if (notification_ids.empty())
return;
base::flat_set<int64_t> removed_notification_ids;
for (const int64_t& id : notification_ids) {
removed_notification_ids.emplace(id);
}
base::OnceClosure remove_request = base::BindOnce(
&NotificationProcessor::RemoveNotificationsAndProcessNextRequest,
weak_ptr_factory_.GetWeakPtr(), std::move(removed_notification_ids));
pending_notification_requests_.emplace(std::move(remove_request));
ProcessRequestQueue();
}
void NotificationProcessor::StartDecodingImages(
const std::vector<DecodeImageRequestMetadata>& decode_image_requests,
base::RepeatingClosure done_closure) {
DCHECK(!decode_image_requests.empty());
DCHECK(!done_closure.is_null());
id_to_images_map_.clear();
for (const auto& request : decode_image_requests) {
delegate_->PerformImageDecode(
request.data,
base::BindOnce(&NotificationProcessor::OnDecodedBitmapReady,
weak_ptr_factory_.GetWeakPtr(), request, done_closure));
}
}
void NotificationProcessor::ImageDecoderDelegate::PerformImageDecode(
const std::string& data,
DecodeImageCallback single_image_decoded_closure) {
data_decoder::DecodeImage(&data_decoder_, base::as_byte_span(data),
data_decoder::mojom::ImageCodec::kDefault,
/*shrink_to_fit=*/true,
data_decoder::kDefaultMaxSizeInBytes,
/*desired_image_frame_size=*/gfx::Size(),
std::move(single_image_decoded_closure));
}
void NotificationProcessor::OnDecodedBitmapReady(
const DecodeImageRequestMetadata& request,
base::OnceClosure done_closure,
const SkBitmap& decoded_bitmap) {
gfx::ImageSkia image_skia =
gfx::ImageSkia::CreateFrom1xBitmap(decoded_bitmap);
// If |image_skia| is null, indicating that the data decoder failed to decode
// the image, the image will be empty, and cannot be made thread safe.
if (!image_skia.isNull())
image_skia.MakeThreadSafe();
auto it = id_to_images_map_.find(request.notification_id);
if (it == id_to_images_map_.end()) {
it = id_to_images_map_.insert(
it, std::pair<int64_t, NotificationImages>(request.notification_id,
NotificationImages()));
}
switch (request.image_field) {
case NotificationImageField::kColorIcon:
it->second.color_icon = gfx::Image(image_skia);
break;
case NotificationImageField::kMonochromeIcon:
it->second.monochrome_icon_mask = gfx::Image(image_skia);
break;
case NotificationImageField::kSharedImage:
it->second.shared_image = gfx::Image(image_skia);
break;
case NotificationImageField::kContactImage:
it->second.contact_image = gfx::Image(image_skia);
break;
}
std::move(done_closure).Run();
}
void NotificationProcessor::OnAllImagesDecoded(
std::vector<proto::Notification> notification_protos) {
base::flat_set<Notification> notifications;
for (const auto& proto : notification_protos) {
auto it = id_to_images_map_.find(proto.id());
if (it == id_to_images_map_.end())
continue;
NotificationImages notification_images = it->second;
notifications.emplace(CreateInternalNotification(
proto, notification_images.color_icon,
notification_images.monochrome_icon_mask,
notification_images.shared_image, notification_images.contact_image));
}
AddNotificationsAndProcessNextRequest(notifications);
}
void NotificationProcessor::ProcessRequestQueue() {
if (pending_notification_requests_.empty())
return;
// Processing the latest request has not been completed.
if (pending_notification_requests_.front().is_null())
return;
std::move(pending_notification_requests_.front()).Run();
}
void NotificationProcessor::CompleteRequest() {
DCHECK(!pending_notification_requests_.empty());
DCHECK(pending_notification_requests_.front().is_null());
pending_notification_requests_.pop();
ProcessRequestQueue();
}
void NotificationProcessor::AddNotificationsAndProcessNextRequest(
const base::flat_set<Notification>& notifications) {
notification_manager_->SetNotificationsInternal(notifications);
CompleteRequest();
}
void NotificationProcessor::RemoveNotificationsAndProcessNextRequest(
base::flat_set<int64_t> removed_notification_ids) {
notification_manager_->RemoveNotificationsInternal(removed_notification_ids);
CompleteRequest();
}
} // namespace ash::phonehub
|