blob: cdc0a378defc4f5ff21fb46ebaf857a94385dc05 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_platform_bridge_chromeos.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/notifications/chrome_ash_message_center_client.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/app_icon_loader.h"
#include "ui/gfx/image/image.h"
namespace {
// TODO(estade): remove this function. NotificationPlatformBridge should either
// get Profile* pointers or, longer term, all profile management should be moved
// up a layer to NativeNotificationDisplayService.
Profile* GetProfileFromId(const std::string& profile_id, bool incognito) {
ProfileManager* manager = g_browser_process->profile_manager();
Profile* profile =
manager->GetProfile(manager->user_data_dir().AppendASCII(profile_id));
return incognito ? profile->GetOffTheRecordProfile() : profile;
}
} // namespace
// static
NotificationPlatformBridge* NotificationPlatformBridge::Create() {
return new NotificationPlatformBridgeChromeOs();
}
// static
bool NotificationPlatformBridge::CanHandleType(
NotificationHandler::Type notification_type) {
return true;
}
NotificationPlatformBridgeChromeOs::NotificationPlatformBridgeChromeOs()
: impl_(std::make_unique<ChromeAshMessageCenterClient>(this)) {}
NotificationPlatformBridgeChromeOs::~NotificationPlatformBridgeChromeOs() {}
void NotificationPlatformBridgeChromeOs::Display(
NotificationHandler::Type notification_type,
const std::string& profile_id,
bool is_incognito,
const message_center::Notification& notification,
std::unique_ptr<NotificationCommon::Metadata> metadata) {
auto active_notification = std::make_unique<ProfileNotification>(
GetProfileFromId(profile_id, is_incognito), notification,
notification_type);
impl_->Display(NotificationHandler::Type::MAX, std::string(), false,
active_notification->notification(), std::move(metadata));
std::string profile_notification_id =
active_notification->notification().id();
active_notifications_.emplace(profile_notification_id,
std::move(active_notification));
}
void NotificationPlatformBridgeChromeOs::Close(
const std::string& profile_id,
const std::string& notification_id) {
impl_->Close(profile_id, notification_id);
}
void NotificationPlatformBridgeChromeOs::GetDisplayed(
const std::string& profile_id,
bool incognito,
const GetDisplayedNotificationsCallback& callback) const {
impl_->GetDisplayed(profile_id, incognito, callback);
}
void NotificationPlatformBridgeChromeOs::SetReadyCallback(
NotificationBridgeReadyCallback callback) {
impl_->SetReadyCallback(std::move(callback));
}
void NotificationPlatformBridgeChromeOs::HandleNotificationClosed(
const std::string& id,
bool by_user) {
auto iter = active_notifications_.find(id);
DCHECK(iter != active_notifications_.end());
ProfileNotification* notification = iter->second.get();
if (notification->type() == NotificationHandler::Type::TRANSIENT) {
notification->notification().delegate()->Close(by_user);
} else {
NotificationDisplayServiceImpl::GetForProfile(notification->profile())
->ProcessNotificationOperation(
NotificationCommon::CLOSE, notification->type(),
notification->notification().origin_url(),
notification->original_id(), base::nullopt, base::nullopt, by_user);
}
active_notifications_.erase(iter);
}
void NotificationPlatformBridgeChromeOs::HandleNotificationClicked(
const std::string& id) {
ProfileNotification* notification = GetProfileNotification(id);
if (notification->type() == NotificationHandler::Type::TRANSIENT) {
notification->notification().delegate()->Click();
} else {
NotificationDisplayServiceImpl::GetForProfile(notification->profile())
->ProcessNotificationOperation(
NotificationCommon::CLICK, notification->type(),
notification->notification().origin_url(),
notification->original_id(), base::nullopt, base::nullopt,
base::nullopt);
}
}
void NotificationPlatformBridgeChromeOs::HandleNotificationButtonClicked(
const std::string& id,
int button_index,
const base::Optional<base::string16>& reply) {
ProfileNotification* notification = GetProfileNotification(id);
if (notification->type() == NotificationHandler::Type::TRANSIENT) {
if (reply) {
notification->notification().delegate()->ButtonClickWithReply(
button_index, *reply);
} else {
notification->notification().delegate()->ButtonClick(button_index);
}
} else {
NotificationDisplayServiceImpl::GetForProfile(notification->profile())
->ProcessNotificationOperation(
NotificationCommon::CLICK, notification->type(),
notification->notification().origin_url(),
notification->original_id(), button_index, reply, base::nullopt);
}
}
void NotificationPlatformBridgeChromeOs::
HandleNotificationSettingsButtonClicked(const std::string& id) {
ProfileNotification* notification = GetProfileNotification(id);
if (notification->type() == NotificationHandler::Type::TRANSIENT) {
notification->notification().delegate()->SettingsClick();
} else {
NotificationDisplayServiceImpl::GetForProfile(notification->profile())
->ProcessNotificationOperation(
NotificationCommon::SETTINGS, notification->type(),
notification->notification().origin_url(),
notification->original_id(), base::nullopt, base::nullopt,
base::nullopt);
}
}
void NotificationPlatformBridgeChromeOs::DisableNotification(
const std::string& id) {
ProfileNotification* notification = GetProfileNotification(id);
DCHECK_NE(NotificationHandler::Type::TRANSIENT, notification->type());
NotificationDisplayServiceImpl::GetForProfile(notification->profile())
->ProcessNotificationOperation(NotificationCommon::DISABLE_PERMISSION,
notification->type(),
notification->notification().origin_url(),
notification->original_id(), base::nullopt,
base::nullopt, base::nullopt);
}
ProfileNotification* NotificationPlatformBridgeChromeOs::GetProfileNotification(
const std::string& profile_notification_id) {
auto iter = active_notifications_.find(profile_notification_id);
DCHECK(iter != active_notifications_.end());
return iter->second.get();
}