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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
// Copyright 2012 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/extensions/api/notifications/notifications_api.h"
#include <stddef.h>
#include <string.h>
#include <utility>
#include "base/functional/callback.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper_factory.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_handler.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "chrome/browser/notifications/notifier_state_tracker.h"
#include "chrome/browser/notifications/notifier_state_tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/notifications/notification_style.h"
#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
#include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system_provider.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notifier_id.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_PLATFORM_APPS)
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/app_window/native_app_window.h"
#endif // BUILDFLAG(ENABLE_PLATFORM_APPS)
using message_center::NotifierId;
namespace extensions {
namespace notifications = api::notifications;
namespace {
// The maximum length of a notification ID, in number of characters. Some
// platforms have limitattions on the length of the ID.
constexpr int kNotificationIdLengthLimit = 500;
const char kMissingRequiredPropertiesForCreateNotification[] =
"Some of the required properties are missing: type, iconUrl, title and "
"message.";
const char kUnableToDecodeIconError[] =
"Unable to successfully use the provided image.";
const char kUnexpectedProgressValueForNonProgressType[] =
"The progress value should not be specified for non-progress notification";
const char kInvalidProgressValue[] =
"The progress value should range from 0 to 100";
const char kExtraListItemsProvided[] =
"List items provided for notification type != list";
const char kExtraImageProvided[] =
"Image resource provided for notification type != image";
const char kNotificationIdTooLong[] =
"The notification's ID should be %d characters or less";
#if !BUILDFLAG(IS_CHROMEOS)
const char kLowPriorityDeprecatedOnPlatform[] =
"Low-priority notifications are deprecated on this platform.";
#endif
// Given an extension id and another id, returns an id that is unique
// relative to other extensions.
std::string CreateScopedIdentifier(const ExtensionId& extension_id,
const std::string& id) {
return extension_id + "-" + id;
}
// Removes the unique internal identifier to send the ID as the
// extension expects it.
std::string StripScopeFromIdentifier(const ExtensionId& extension_id,
const std::string& scoped_id) {
size_t index_of_separator = extension_id.length() + 1;
DCHECK_LT(index_of_separator, scoped_id.length());
return scoped_id.substr(index_of_separator);
}
// Converts the |notification_bitmap| (in RGBA format) to the |*return_image|
// (which is in ARGB format).
bool NotificationBitmapToGfxImage(
float max_scale,
const gfx::Size& target_size_dips,
const notifications::NotificationBitmap& notification_bitmap,
gfx::Image* return_image) {
const int max_device_pixel_width = target_size_dips.width() * max_scale;
const int max_device_pixel_height = target_size_dips.height() * max_scale;
const int kBytesPerPixel = 4;
const int width = notification_bitmap.width;
const int height = notification_bitmap.height;
if (width < 0 || height < 0 || width > max_device_pixel_width ||
height > max_device_pixel_height)
return false;
// Ensure we have rgba data.
if (!notification_bitmap.data) {
return false;
}
const std::vector<uint8_t>& rgba_data = *notification_bitmap.data;
const size_t rgba_data_length = rgba_data.size();
const size_t rgba_area = width * height;
if (rgba_data_length != rgba_area * kBytesPerPixel)
return false;
SkBitmap bitmap;
// Allocate the actual backing store with the sanitized dimensions.
std::vector<uint32_t> pixels(rgba_area);
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
bitmap.setInfo(info, width * kBytesPerPixel);
bitmap.setPixels(&pixels[0]);
// Ensure that our bitmap and our data now refer to the same number of pixels.
if (rgba_data_length != bitmap.computeByteSize())
return false;
for (size_t t = 0; t < rgba_area; ++t) {
// `rgba_data` is RGBA, pixels is ARGB.
size_t rgba_index = t * kBytesPerPixel;
pixels[t] = SkPreMultiplyColor(((rgba_data[rgba_index + 3] & 0xFF) << 24) |
((rgba_data[rgba_index + 0] & 0xFF) << 16) |
((rgba_data[rgba_index + 1] & 0xFF) << 8) |
((rgba_data[rgba_index + 2] & 0xFF) << 0));
}
// Make a copy, since the current bitmap is using a local std::vector for
// storage.
SkBitmap copy;
if (!copy.tryAllocPixels(bitmap.info())) {
return false;
}
// Copy the bitmap.
bitmap.readPixels(copy.pixmap());
// TODO(dewittj): Handle HiDPI images with more than one scale factor
// representation.
gfx::ImageSkia skia = gfx::ImageSkia::CreateFromBitmap(copy, 1.0f);
*return_image = gfx::Image(skia);
return true;
}
// Returns true if a notification with the given origin should show over the
// currently fullscreen app window. If there is no fullscreen app window,
// returns false.
bool ShouldShowOverCurrentFullscreenWindow(Profile* profile,
const GURL& origin) {
#if BUILDFLAG(ENABLE_PLATFORM_APPS)
DCHECK(profile);
ExtensionId extension_id =
ExtensionNotificationHandler::GetExtensionId(origin);
DCHECK(!extension_id.empty());
AppWindowRegistry::AppWindowList windows =
AppWindowRegistry::Get(profile)->GetAppWindowsForApp(extension_id);
for (AppWindow* window : windows) {
if (window->IsFullscreen() && window->GetBaseWindow()->IsActive())
return true;
}
#endif // BUILDFLAG(ENABLE_PLATFORM_APPS)
return false;
}
} // namespace
bool NotificationsApiFunction::IsNotificationsApiAvailable() {
// We need to check this explicitly rather than letting
// _permission_features.json enforce it, because we're sharing the
// chrome.notifications permissions namespace with WebKit notifications.
return extension()->is_platform_app() || extension()->is_extension();
}
NotificationsApiFunction::NotificationsApiFunction() = default;
NotificationsApiFunction::~NotificationsApiFunction() = default;
bool NotificationsApiFunction::CreateNotification(
const std::string& id,
api::notifications::NotificationOptions* options,
std::string* error) {
// First, make sure the required fields exist: type, title, message, icon.
// These fields are defined as optional in IDL such that they can be used as
// optional for notification updates. But for notification creations, they
// should be present.
if (options->type == api::notifications::TemplateType::kNone ||
!options->icon_url || !options->title || !options->message) {
*error = kMissingRequiredPropertiesForCreateNotification;
return false;
}
#if !BUILDFLAG(IS_CHROMEOS)
if (options->priority &&
*options->priority < message_center::DEFAULT_PRIORITY) {
*error = kLowPriorityDeprecatedOnPlatform;
return false;
}
#endif
NotificationBitmapSizes bitmap_sizes = GetNotificationBitmapSizes();
const float image_scale = ui::GetScaleForMaxSupportedResourceScaleFactor();
// Extract required fields: type, title, message, and icon.
message_center::NotificationType type =
MapApiTemplateTypeToType(options->type);
const std::u16string title(base::UTF8ToUTF16(*options->title));
const std::u16string message(base::UTF8ToUTF16(*options->message));
gfx::Image icon;
if (!options->icon_bitmap ||
!NotificationBitmapToGfxImage(image_scale, bitmap_sizes.icon_size,
*options->icon_bitmap, &icon)) {
*error = kUnableToDecodeIconError;
return false;
}
// Then, handle any optional data that's been provided.
message_center::RichNotificationData optional_fields;
if (options->app_icon_mask_url) {
gfx::Image small_icon_mask;
if (!NotificationBitmapToGfxImage(
image_scale, bitmap_sizes.app_icon_mask_size,
*options->app_icon_mask_bitmap, &small_icon_mask)) {
*error = kUnableToDecodeIconError;
return false;
}
optional_fields.small_image = small_icon_mask;
optional_fields.small_image_needs_additional_masking = true;
}
if (options->priority)
optional_fields.priority = *options->priority;
if (options->event_time)
optional_fields.timestamp =
base::Time::FromMillisecondsSinceUnixEpoch(*options->event_time);
if (options->silent)
optional_fields.silent = *options->silent;
if (options->buttons) {
// Currently we allow up to 2 buttons.
size_t number_of_buttons = options->buttons->size();
number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons;
for (size_t i = 0; i < number_of_buttons; i++) {
message_center::ButtonInfo info(
base::UTF8ToUTF16((*options->buttons)[i].title));
const auto& icon_bitmap = (*options->buttons)[i].icon_bitmap;
if (icon_bitmap) {
NotificationBitmapToGfxImage(image_scale, bitmap_sizes.button_icon_size,
*icon_bitmap, &info.icon);
}
optional_fields.buttons.push_back(info);
}
}
if (options->context_message) {
optional_fields.context_message =
base::UTF8ToUTF16(*options->context_message);
}
bool has_image = options->image_bitmap &&
NotificationBitmapToGfxImage(
image_scale, bitmap_sizes.image_size,
*options->image_bitmap, &optional_fields.image);
// We should have an image if and only if the type is an image type.
if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) {
*error = kExtraImageProvided;
return false;
}
// We should have list items if and only if the type is a multiple type.
bool has_list_items = options->items && !options->items->empty();
if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) {
*error = kExtraListItemsProvided;
return false;
}
if (options->progress) {
// We should have progress if and only if the type is a progress type.
if (type != message_center::NOTIFICATION_TYPE_PROGRESS) {
*error = kUnexpectedProgressValueForNonProgressType;
return false;
}
optional_fields.progress = *options->progress;
// Progress value should range from 0 to 100.
if (optional_fields.progress < 0 || optional_fields.progress > 100) {
*error = kInvalidProgressValue;
return false;
}
}
if (has_list_items) {
using api::notifications::NotificationItem;
for (const NotificationItem& api_item : *options->items) {
optional_fields.items.push_back({base::UTF8ToUTF16(api_item.title),
base::UTF8ToUTF16(api_item.message)});
}
}
optional_fields.settings_button_handler =
message_center::SettingsButtonHandler::INLINE;
// TODO(crbug.com/41348342): Remove the manual limitation in favor of an IDL
// annotation once supported.
if (id.size() > kNotificationIdLengthLimit) {
*error =
base::StringPrintf(kNotificationIdTooLong, kNotificationIdLengthLimit);
return false;
}
std::string notification_id = CreateScopedIdentifier(extension_->id(), id);
message_center::Notification notification(
type, notification_id, title, message, ui::ImageModel::FromImage(icon),
base::UTF8ToUTF16(extension_->name()), extension_->url(),
message_center::NotifierId(message_center::NotifierType::APPLICATION,
extension_->id()),
optional_fields, nullptr /* delegate */);
// Apply the "requireInteraction" flag. The value defaults to false.
notification.set_never_timeout(options->require_interaction &&
*options->require_interaction);
// For a progress notification the message parameter won't be displayed in the
// notification. Therefore, its value is passed to progress_status which will
// be displayed.
if (type == message_center::NOTIFICATION_TYPE_PROGRESS) {
notification.set_progress_status(message);
}
if (ShouldShowOverCurrentFullscreenWindow(GetProfile(),
notification.origin_url())) {
notification.set_fullscreen_visibility(
message_center::FullscreenVisibility::OVER_USER);
}
GetDisplayHelper()->Display(notification);
return true;
}
bool NotificationsApiFunction::UpdateNotification(
const std::string& id,
api::notifications::NotificationOptions* options,
message_center::Notification* notification,
std::string* error) {
#if !BUILDFLAG(IS_CHROMEOS)
if (options->priority &&
*options->priority < message_center::DEFAULT_PRIORITY) {
*error = kLowPriorityDeprecatedOnPlatform;
return false;
}
#endif
NotificationBitmapSizes bitmap_sizes = GetNotificationBitmapSizes();
const float image_scale = ui::GetScaleForMaxSupportedResourceScaleFactor();
// Update optional fields if provided.
if (options->type != api::notifications::TemplateType::kNone) {
notification->set_type(MapApiTemplateTypeToType(options->type));
}
if (options->title)
notification->set_title(base::UTF8ToUTF16(*options->title));
if (options->message)
notification->set_message(base::UTF8ToUTF16(*options->message));
if (options->icon_bitmap) {
gfx::Image icon;
if (!NotificationBitmapToGfxImage(
image_scale, bitmap_sizes.icon_size, *options->icon_bitmap,
&icon)) {
*error = kUnableToDecodeIconError;
return false;
}
notification->set_icon(ui::ImageModel::FromImage(icon));
}
if (options->app_icon_mask_bitmap) {
gfx::Image app_icon_mask;
if (!NotificationBitmapToGfxImage(
image_scale, bitmap_sizes.app_icon_mask_size,
*options->app_icon_mask_bitmap, &app_icon_mask)) {
*error = kUnableToDecodeIconError;
return false;
}
notification->SetSmallImage(app_icon_mask);
notification->set_small_image_needs_additional_masking(true);
}
if (options->priority)
notification->set_priority(*options->priority);
if (options->event_time)
notification->set_timestamp(
base::Time::FromMillisecondsSinceUnixEpoch(*options->event_time));
if (options->silent)
notification->set_silent(*options->silent);
if (options->buttons) {
// Currently we allow up to 2 buttons.
size_t number_of_buttons = options->buttons->size();
number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons;
std::vector<message_center::ButtonInfo> buttons;
for (size_t i = 0; i < number_of_buttons; i++) {
message_center::ButtonInfo button(
base::UTF8ToUTF16((*options->buttons)[i].title));
const auto& icon_bitmap = (*options->buttons)[i].icon_bitmap;
if (icon_bitmap) {
NotificationBitmapToGfxImage(image_scale, bitmap_sizes.button_icon_size,
*icon_bitmap, &button.icon);
}
buttons.push_back(button);
}
notification->set_buttons(buttons);
}
if (options->context_message) {
notification->set_context_message(
base::UTF8ToUTF16(*options->context_message));
}
gfx::Image image;
bool has_image =
options->image_bitmap &&
NotificationBitmapToGfxImage(image_scale, bitmap_sizes.image_size,
*options->image_bitmap, &image);
if (has_image) {
// We should have an image if and only if the type is an image type.
if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) {
*error = kExtraImageProvided;
return false;
}
notification->SetImage(image);
}
if (options->progress) {
// We should have progress if and only if the type is a progress type.
if (notification->type() != message_center::NOTIFICATION_TYPE_PROGRESS) {
*error = kUnexpectedProgressValueForNonProgressType;
return false;
}
int progress = *options->progress;
// Progress value should range from 0 to 100.
if (progress < 0 || progress > 100) {
*error = kInvalidProgressValue;
return false;
}
notification->set_progress(progress);
}
// For a progress notification the message parameter won't be displayed in the
// notification. Therefore, its value is passed to progress_status which will
// be displayed.
if (options->message &&
notification->type() == message_center::NOTIFICATION_TYPE_PROGRESS) {
notification->set_progress_status(base::UTF8ToUTF16(*options->message));
}
if (options->items && !options->items->empty()) {
// We should have list items if and only if the type is a multiple type.
if (notification->type() != message_center::NOTIFICATION_TYPE_MULTIPLE) {
*error = kExtraListItemsProvided;
return false;
}
std::vector<message_center::NotificationItem> items;
using api::notifications::NotificationItem;
for (const NotificationItem& api_item : *options->items) {
items.push_back({base::UTF8ToUTF16(api_item.title),
base::UTF8ToUTF16(api_item.message)});
}
notification->set_items(items);
}
// It's safe to follow the regular path for adding a new notification as it's
// already been verified that there is a notification that can be updated.
GetDisplayHelper()->Display(*notification);
return true;
}
bool NotificationsApiFunction::AreExtensionNotificationsAllowed() const {
NotifierStateTracker* notifier_state_tracker =
NotifierStateTrackerFactory::GetForProfile(GetProfile());
return notifier_state_tracker->IsNotifierEnabled(message_center::NotifierId(
message_center::NotifierType::APPLICATION, extension_->id()));
}
bool NotificationsApiFunction::IsNotificationsApiEnabled() const {
return CanRunWhileDisabled() || AreExtensionNotificationsAllowed();
}
bool NotificationsApiFunction::CanRunWhileDisabled() const {
return false;
}
ExtensionNotificationDisplayHelper* NotificationsApiFunction::GetDisplayHelper()
const {
return ExtensionNotificationDisplayHelperFactory::GetForProfile(GetProfile());
}
Profile* NotificationsApiFunction::GetProfile() const {
return Profile::FromBrowserContext(browser_context());
}
ExtensionFunction::ResponseAction NotificationsApiFunction::Run() {
if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) {
return RunNotificationsApi();
} else {
return RespondNow(Error(""));
}
}
message_center::NotificationType
NotificationsApiFunction::MapApiTemplateTypeToType(
api::notifications::TemplateType type) {
switch (type) {
case api::notifications::TemplateType::kNone:
case api::notifications::TemplateType::kBasic:
return message_center::NOTIFICATION_TYPE_SIMPLE;
case api::notifications::TemplateType::kImage:
return message_center::NOTIFICATION_TYPE_IMAGE;
case api::notifications::TemplateType::kList:
return message_center::NOTIFICATION_TYPE_MULTIPLE;
case api::notifications::TemplateType::kProgress:
return message_center::NOTIFICATION_TYPE_PROGRESS;
default:
// Gracefully handle newer application code that is running on an older
// runtime that doesn't recognize the requested template.
return message_center::NOTIFICATION_TYPE_SIMPLE;
}
}
NotificationsCreateFunction::NotificationsCreateFunction() = default;
NotificationsCreateFunction::~NotificationsCreateFunction() = default;
ExtensionFunction::ResponseAction
NotificationsCreateFunction::RunNotificationsApi() {
params_ = api::notifications::Create::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params_);
std::string notification_id;
if (params_->notification_id && !params_->notification_id->empty()) {
// If the caller provided a notificationId, use that.
notification_id = *params_->notification_id;
} else {
// Otherwise, use a randomly created GUID. In case that
// Uuid::GenerateRandomV4().AsLowercaseString returns the empty string,
// simply generate a random string.
notification_id = base::Uuid::GenerateRandomV4().AsLowercaseString();
if (notification_id.empty())
notification_id = base::RandBytesAsString(16);
}
// TODO(dewittj): Add more human-readable error strings if this fails.
std::string error;
if (!CreateNotification(notification_id, ¶ms_->options, &error)) {
return RespondNow(ErrorWithArgumentsDoNotUse(
api::notifications::Create::Results::Create(notification_id), error));
}
return RespondNow(WithArguments(notification_id));
}
NotificationsUpdateFunction::NotificationsUpdateFunction() = default;
NotificationsUpdateFunction::~NotificationsUpdateFunction() = default;
ExtensionFunction::ResponseAction
NotificationsUpdateFunction::RunNotificationsApi() {
params_ = api::notifications::Update::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params_);
// We are in update. If the ID doesn't exist, succeed but call the callback
// with "false".
const message_center::Notification* matched_notification =
GetDisplayHelper()->GetByNotificationId(
CreateScopedIdentifier(extension_->id(), params_->notification_id));
if (!matched_notification) {
return RespondNow(WithArguments(false));
}
// Copy the existing notification to get a writable version of it.
message_center::Notification notification = *matched_notification;
// If we have trouble updating the notification (could be improper use of API
// or some other reason), mark the function as failed, calling the callback
// with false.
// TODO(dewittj): Add more human-readable error strings if this fails.
std::string error;
bool could_update_notification = UpdateNotification(
params_->notification_id, ¶ms_->options, ¬ification, &error);
if (!could_update_notification) {
return RespondNow(ErrorWithArgumentsDoNotUse(
api::notifications::Update::Results::Create(false), error));
}
// No trouble, created the notification, send true to the callback and
// succeed.
return RespondNow(WithArguments(true));
}
NotificationsClearFunction::NotificationsClearFunction() = default;
NotificationsClearFunction::~NotificationsClearFunction() = default;
ExtensionFunction::ResponseAction
NotificationsClearFunction::RunNotificationsApi() {
params_ = api::notifications::Clear::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params_);
bool cancel_result = GetDisplayHelper()->Close(
CreateScopedIdentifier(extension_->id(), params_->notification_id));
return RespondNow(WithArguments(cancel_result));
}
NotificationsGetAllFunction::NotificationsGetAllFunction() = default;
NotificationsGetAllFunction::~NotificationsGetAllFunction() = default;
ExtensionFunction::ResponseAction
NotificationsGetAllFunction::RunNotificationsApi() {
std::set<std::string> notification_ids =
GetDisplayHelper()->GetNotificationIdsForExtension(extension_->url());
base::Value::Dict result;
for (const auto& entry : notification_ids) {
result.Set(StripScopeFromIdentifier(extension_->id(), entry), true);
}
return RespondNow(WithArguments(std::move(result)));
}
NotificationsGetPermissionLevelFunction::
NotificationsGetPermissionLevelFunction() = default;
NotificationsGetPermissionLevelFunction::
~NotificationsGetPermissionLevelFunction() = default;
bool NotificationsGetPermissionLevelFunction::CanRunWhileDisabled() const {
return true;
}
ExtensionFunction::ResponseAction
NotificationsGetPermissionLevelFunction::RunNotificationsApi() {
api::notifications::PermissionLevel result =
AreExtensionNotificationsAllowed()
? api::notifications::PermissionLevel::kGranted
: api::notifications::PermissionLevel::kDenied;
return RespondNow(WithArguments(api::notifications::ToString(result)));
}
} // namespace extensions
|