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
|
// Copyright 2014 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/extensions/api/notification_provider/notification_provider_api.h"
#include "base/callback.h"
#include "base/guid.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/common/chrome_version_info.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/extension.h"
#include "extensions/common/features/feature.h"
#include "ui/base/layout.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notifier_settings.h"
#include "url/gurl.h"
namespace extensions {
NotificationProviderEventRouter::NotificationProviderEventRouter(
Profile* profile)
: profile_(profile) {
}
NotificationProviderEventRouter::~NotificationProviderEventRouter() {
}
void NotificationProviderEventRouter::CreateNotification(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id,
const api::notifications::NotificationOptions& options) {
Create(notification_provider_id, sender_id, notification_id, options);
}
void NotificationProviderEventRouter::UpdateNotification(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id,
const api::notifications::NotificationOptions& options) {
Update(notification_provider_id, sender_id, notification_id, options);
}
void NotificationProviderEventRouter::ClearNotification(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id) {
Clear(notification_provider_id, sender_id, notification_id);
}
void NotificationProviderEventRouter::Create(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id,
const api::notifications::NotificationOptions& options) {
scoped_ptr<base::ListValue> args =
api::notification_provider::OnCreated::Create(
sender_id, notification_id, options);
scoped_ptr<Event> event(new Event(
api::notification_provider::OnCreated::kEventName, args.Pass()));
EventRouter::Get(profile_)
->DispatchEventToExtension(notification_provider_id, event.Pass());
}
void NotificationProviderEventRouter::Update(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id,
const api::notifications::NotificationOptions& options) {
scoped_ptr<base::ListValue> args =
api::notification_provider::OnUpdated::Create(
sender_id, notification_id, options);
scoped_ptr<Event> event(new Event(
api::notification_provider::OnUpdated::kEventName, args.Pass()));
EventRouter::Get(profile_)
->DispatchEventToExtension(notification_provider_id, event.Pass());
}
void NotificationProviderEventRouter::Clear(
const std::string& notification_provider_id,
const std::string& sender_id,
const std::string& notification_id) {
scoped_ptr<base::ListValue> args =
api::notification_provider::OnCleared::Create(sender_id, notification_id);
scoped_ptr<Event> event(new Event(
api::notification_provider::OnCleared::kEventName, args.Pass()));
EventRouter::Get(profile_)
->DispatchEventToExtension(notification_provider_id, event.Pass());
}
NotificationProviderNotifyOnClearedFunction::
NotificationProviderNotifyOnClearedFunction() {
}
NotificationProviderNotifyOnClearedFunction::
~NotificationProviderNotifyOnClearedFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderNotifyOnClearedFunction::Run() {
scoped_ptr<api::notification_provider::NotifyOnCleared::Params> params =
api::notification_provider::NotifyOnCleared::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
const Notification* notification =
g_browser_process->notification_ui_manager()->FindById(
params->notification_id,
NotificationUIManager::GetProfileID(GetProfile()));
bool found_notification = notification != NULL;
if (found_notification)
notification->delegate()->Close(true);
return RespondNow(
ArgumentList(api::notification_provider::NotifyOnCleared::Results::Create(
found_notification)));
}
NotificationProviderNotifyOnClickedFunction::
NotificationProviderNotifyOnClickedFunction() {
}
NotificationProviderNotifyOnClickedFunction::
~NotificationProviderNotifyOnClickedFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderNotifyOnClickedFunction::Run() {
scoped_ptr<api::notification_provider::NotifyOnClicked::Params> params =
api::notification_provider::NotifyOnClicked::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
const Notification* notification =
g_browser_process->notification_ui_manager()->FindById(
params->notification_id,
NotificationUIManager::GetProfileID(GetProfile()));
bool found_notification = notification != NULL;
if (found_notification)
notification->delegate()->Click();
return RespondNow(
ArgumentList(api::notification_provider::NotifyOnClicked::Results::Create(
found_notification)));
}
NotificationProviderNotifyOnButtonClickedFunction::
NotificationProviderNotifyOnButtonClickedFunction() {
}
NotificationProviderNotifyOnButtonClickedFunction::
~NotificationProviderNotifyOnButtonClickedFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderNotifyOnButtonClickedFunction::Run() {
scoped_ptr<api::notification_provider::NotifyOnButtonClicked::Params> params =
api::notification_provider::NotifyOnButtonClicked::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
const Notification* notification =
g_browser_process->notification_ui_manager()->FindById(
params->notification_id,
NotificationUIManager::GetProfileID(GetProfile()));
bool found_notification = notification != NULL;
if (found_notification)
notification->delegate()->ButtonClick(params->button_index);
return RespondNow(ArgumentList(
api::notification_provider::NotifyOnButtonClicked::Results::Create(
found_notification)));
}
NotificationProviderNotifyOnPermissionLevelChangedFunction::
NotificationProviderNotifyOnPermissionLevelChangedFunction() {
}
NotificationProviderNotifyOnPermissionLevelChangedFunction::
~NotificationProviderNotifyOnPermissionLevelChangedFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() {
scoped_ptr<api::notification_provider::NotifyOnPermissionLevelChanged::Params>
params = api::notification_provider::NotifyOnPermissionLevelChanged::
Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
// Third party apps/extensions with notification provider API will not be able
// to change permission levels of web notifiers, because the list of allowed
// websites should only be set in Chrome Settings manually by users. But they
// are able to change permission levels of application type notifiers.
bool is_application_type =
(params->notifier_type ==
api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION);
if (is_application_type) {
bool enabled =
(params->level == api::notification_provider::NotifierPermissionLevel::
NOTIFIER_PERMISSION_LEVEL_GRANTED);
DesktopNotificationService* desktop_notification_service =
DesktopNotificationServiceFactory::GetForProfile(GetProfile());
message_center::NotifierId notifier_id(
message_center::NotifierId::NotifierType::APPLICATION,
params->notifier_id);
desktop_notification_service->SetNotifierEnabled(notifier_id, enabled);
}
return RespondNow(
ArgumentList(api::notification_provider::NotifyOnPermissionLevelChanged::
Results::Create(is_application_type)));
}
NotificationProviderNotifyOnShowSettingsFunction::
NotificationProviderNotifyOnShowSettingsFunction() {
}
NotificationProviderNotifyOnShowSettingsFunction::
~NotificationProviderNotifyOnShowSettingsFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderNotifyOnShowSettingsFunction::Run() {
scoped_ptr<api::notification_provider::NotifyOnShowSettings::Params> params =
api::notification_provider::NotifyOnShowSettings::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
bool has_advanced_settings;
// Only application type notifiers have advanced settings.
if (params->notifier_type ==
api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION) {
// TODO(dewittj): Refactor NotificationUIManage API to have a getter of
// NotifierSettingsProvider, since it holds the settings provider.
message_center::NotifierSettingsProvider* settings_provider =
message_center::MessageCenter::Get()->GetNotifierSettingsProvider();
message_center::NotifierId notifier_id(
message_center::NotifierId::NotifierType::APPLICATION,
params->notifier_id);
has_advanced_settings =
settings_provider->NotifierHasAdvancedSettings(notifier_id);
if (has_advanced_settings)
settings_provider->OnNotifierAdvancedSettingsRequested(notifier_id, NULL);
} else {
has_advanced_settings = false;
}
return RespondNow(ArgumentList(
api::notification_provider::NotifyOnShowSettings::Results::Create(
has_advanced_settings)));
}
NotificationProviderGetNotifierFunction::
NotificationProviderGetNotifierFunction() {
}
NotificationProviderGetNotifierFunction::
~NotificationProviderGetNotifierFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderGetNotifierFunction::Run() {
api::notification_provider::Notifier notifier;
return RespondNow(ArgumentList(
api::notification_provider::GetNotifier::Results::Create(notifier)));
}
NotificationProviderGetAllNotifiersFunction::
NotificationProviderGetAllNotifiersFunction() {
}
NotificationProviderGetAllNotifiersFunction::
~NotificationProviderGetAllNotifiersFunction() {
}
ExtensionFunction::ResponseAction
NotificationProviderGetAllNotifiersFunction::Run() {
std::vector<linked_ptr<api::notification_provider::Notifier> > notifiers;
return RespondNow(ArgumentList(
api::notification_provider::GetAllNotifiers::Results::Create(notifiers)));
}
} // namespace extensions
|