| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/notification_provider/notification_provi
der_api.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/guid.h" | |
| 11 #include "base/rand_util.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/notifications/notification.h" | |
| 16 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 17 #include "chrome/browser/notifications/notifier_state_tracker.h" | |
| 18 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" | |
| 19 #include "extensions/browser/event_router.h" | |
| 20 #include "extensions/common/extension.h" | |
| 21 #include "extensions/common/features/feature.h" | |
| 22 #include "ui/base/layout.h" | |
| 23 #include "ui/message_center/message_center.h" | |
| 24 #include "ui/message_center/notifier_settings.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 namespace extensions { | |
| 28 | |
| 29 NotificationProviderEventRouter::NotificationProviderEventRouter( | |
| 30 Profile* profile) | |
| 31 : profile_(profile) { | |
| 32 } | |
| 33 | |
| 34 NotificationProviderEventRouter::~NotificationProviderEventRouter() { | |
| 35 } | |
| 36 | |
| 37 void NotificationProviderEventRouter::CreateNotification( | |
| 38 const std::string& notification_provider_id, | |
| 39 const std::string& sender_id, | |
| 40 const std::string& notification_id, | |
| 41 const api::notifications::NotificationOptions& options) { | |
| 42 Create(notification_provider_id, sender_id, notification_id, options); | |
| 43 } | |
| 44 | |
| 45 void NotificationProviderEventRouter::UpdateNotification( | |
| 46 const std::string& notification_provider_id, | |
| 47 const std::string& sender_id, | |
| 48 const std::string& notification_id, | |
| 49 const api::notifications::NotificationOptions& options) { | |
| 50 Update(notification_provider_id, sender_id, notification_id, options); | |
| 51 } | |
| 52 void NotificationProviderEventRouter::ClearNotification( | |
| 53 const std::string& notification_provider_id, | |
| 54 const std::string& sender_id, | |
| 55 const std::string& notification_id) { | |
| 56 Clear(notification_provider_id, sender_id, notification_id); | |
| 57 } | |
| 58 | |
| 59 void NotificationProviderEventRouter::Create( | |
| 60 const std::string& notification_provider_id, | |
| 61 const std::string& sender_id, | |
| 62 const std::string& notification_id, | |
| 63 const api::notifications::NotificationOptions& options) { | |
| 64 std::unique_ptr<base::ListValue> args = | |
| 65 api::notification_provider::OnCreated::Create(sender_id, notification_id, | |
| 66 options); | |
| 67 | |
| 68 std::unique_ptr<Event> event(new Event( | |
| 69 events::NOTIFICATION_PROVIDER_ON_CREATED, | |
| 70 api::notification_provider::OnCreated::kEventName, std::move(args))); | |
| 71 | |
| 72 EventRouter::Get(profile_) | |
| 73 ->DispatchEventToExtension(notification_provider_id, std::move(event)); | |
| 74 } | |
| 75 | |
| 76 void NotificationProviderEventRouter::Update( | |
| 77 const std::string& notification_provider_id, | |
| 78 const std::string& sender_id, | |
| 79 const std::string& notification_id, | |
| 80 const api::notifications::NotificationOptions& options) { | |
| 81 std::unique_ptr<base::ListValue> args = | |
| 82 api::notification_provider::OnUpdated::Create(sender_id, notification_id, | |
| 83 options); | |
| 84 | |
| 85 std::unique_ptr<Event> event(new Event( | |
| 86 events::NOTIFICATION_PROVIDER_ON_UPDATED, | |
| 87 api::notification_provider::OnUpdated::kEventName, std::move(args))); | |
| 88 | |
| 89 EventRouter::Get(profile_) | |
| 90 ->DispatchEventToExtension(notification_provider_id, std::move(event)); | |
| 91 } | |
| 92 | |
| 93 void NotificationProviderEventRouter::Clear( | |
| 94 const std::string& notification_provider_id, | |
| 95 const std::string& sender_id, | |
| 96 const std::string& notification_id) { | |
| 97 std::unique_ptr<base::ListValue> args = | |
| 98 api::notification_provider::OnCleared::Create(sender_id, notification_id); | |
| 99 | |
| 100 std::unique_ptr<Event> event(new Event( | |
| 101 events::NOTIFICATION_PROVIDER_ON_CLEARED, | |
| 102 api::notification_provider::OnCleared::kEventName, std::move(args))); | |
| 103 | |
| 104 EventRouter::Get(profile_) | |
| 105 ->DispatchEventToExtension(notification_provider_id, std::move(event)); | |
| 106 } | |
| 107 | |
| 108 NotificationProviderNotifyOnClearedFunction:: | |
| 109 NotificationProviderNotifyOnClearedFunction() { | |
| 110 } | |
| 111 | |
| 112 NotificationProviderNotifyOnClearedFunction:: | |
| 113 ~NotificationProviderNotifyOnClearedFunction() { | |
| 114 } | |
| 115 | |
| 116 ExtensionFunction::ResponseAction | |
| 117 NotificationProviderNotifyOnClearedFunction::Run() { | |
| 118 std::unique_ptr<api::notification_provider::NotifyOnCleared::Params> params = | |
| 119 api::notification_provider::NotifyOnCleared::Params::Create(*args_); | |
| 120 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 121 | |
| 122 const Notification* notification = | |
| 123 g_browser_process->notification_ui_manager()->FindById( | |
| 124 params->notification_id, | |
| 125 NotificationUIManager::GetProfileID(GetProfile())); | |
| 126 | |
| 127 bool found_notification = notification != NULL; | |
| 128 if (found_notification) | |
| 129 notification->delegate()->Close(true); | |
| 130 | |
| 131 return RespondNow( | |
| 132 ArgumentList(api::notification_provider::NotifyOnCleared::Results::Create( | |
| 133 found_notification))); | |
| 134 } | |
| 135 | |
| 136 NotificationProviderNotifyOnClickedFunction:: | |
| 137 NotificationProviderNotifyOnClickedFunction() { | |
| 138 } | |
| 139 | |
| 140 NotificationProviderNotifyOnClickedFunction:: | |
| 141 ~NotificationProviderNotifyOnClickedFunction() { | |
| 142 } | |
| 143 | |
| 144 ExtensionFunction::ResponseAction | |
| 145 NotificationProviderNotifyOnClickedFunction::Run() { | |
| 146 std::unique_ptr<api::notification_provider::NotifyOnClicked::Params> params = | |
| 147 api::notification_provider::NotifyOnClicked::Params::Create(*args_); | |
| 148 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 149 | |
| 150 const Notification* notification = | |
| 151 g_browser_process->notification_ui_manager()->FindById( | |
| 152 params->notification_id, | |
| 153 NotificationUIManager::GetProfileID(GetProfile())); | |
| 154 | |
| 155 bool found_notification = notification != NULL; | |
| 156 if (found_notification) | |
| 157 notification->delegate()->Click(); | |
| 158 | |
| 159 return RespondNow( | |
| 160 ArgumentList(api::notification_provider::NotifyOnClicked::Results::Create( | |
| 161 found_notification))); | |
| 162 } | |
| 163 | |
| 164 NotificationProviderNotifyOnButtonClickedFunction:: | |
| 165 NotificationProviderNotifyOnButtonClickedFunction() { | |
| 166 } | |
| 167 | |
| 168 NotificationProviderNotifyOnButtonClickedFunction:: | |
| 169 ~NotificationProviderNotifyOnButtonClickedFunction() { | |
| 170 } | |
| 171 | |
| 172 ExtensionFunction::ResponseAction | |
| 173 NotificationProviderNotifyOnButtonClickedFunction::Run() { | |
| 174 std::unique_ptr<api::notification_provider::NotifyOnButtonClicked::Params> | |
| 175 params = | |
| 176 api::notification_provider::NotifyOnButtonClicked::Params::Create( | |
| 177 *args_); | |
| 178 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 179 | |
| 180 const Notification* notification = | |
| 181 g_browser_process->notification_ui_manager()->FindById( | |
| 182 params->notification_id, | |
| 183 NotificationUIManager::GetProfileID(GetProfile())); | |
| 184 | |
| 185 bool found_notification = notification != NULL; | |
| 186 if (found_notification) | |
| 187 notification->delegate()->ButtonClick(params->button_index); | |
| 188 | |
| 189 return RespondNow(ArgumentList( | |
| 190 api::notification_provider::NotifyOnButtonClicked::Results::Create( | |
| 191 found_notification))); | |
| 192 } | |
| 193 | |
| 194 NotificationProviderNotifyOnPermissionLevelChangedFunction:: | |
| 195 NotificationProviderNotifyOnPermissionLevelChangedFunction() { | |
| 196 } | |
| 197 | |
| 198 NotificationProviderNotifyOnPermissionLevelChangedFunction:: | |
| 199 ~NotificationProviderNotifyOnPermissionLevelChangedFunction() { | |
| 200 } | |
| 201 | |
| 202 ExtensionFunction::ResponseAction | |
| 203 NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() { | |
| 204 std::unique_ptr< | |
| 205 api::notification_provider::NotifyOnPermissionLevelChanged::Params> | |
| 206 params = api::notification_provider::NotifyOnPermissionLevelChanged:: | |
| 207 Params::Create(*args_); | |
| 208 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 209 | |
| 210 // Third party apps/extensions with notification provider API will not be able | |
| 211 // to change permission levels of web notifiers, because the list of allowed | |
| 212 // websites should only be set in Chrome Settings manually by users. But they | |
| 213 // are able to change permission levels of application type notifiers. | |
| 214 bool is_application_type = | |
| 215 (params->notifier_type == | |
| 216 api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION); | |
| 217 if (is_application_type) { | |
| 218 bool enabled = | |
| 219 (params->level == api::notification_provider::NotifierPermissionLevel:: | |
| 220 NOTIFIER_PERMISSION_LEVEL_GRANTED); | |
| 221 | |
| 222 NotifierStateTracker* notifier_state_tracker = | |
| 223 NotifierStateTrackerFactory::GetForProfile(GetProfile()); | |
| 224 | |
| 225 message_center::NotifierId notifier_id( | |
| 226 message_center::NotifierId::NotifierType::APPLICATION, | |
| 227 params->notifier_id); | |
| 228 | |
| 229 notifier_state_tracker->SetNotifierEnabled(notifier_id, enabled); | |
| 230 } | |
| 231 | |
| 232 return RespondNow( | |
| 233 ArgumentList(api::notification_provider::NotifyOnPermissionLevelChanged:: | |
| 234 Results::Create(is_application_type))); | |
| 235 } | |
| 236 | |
| 237 NotificationProviderNotifyOnShowSettingsFunction:: | |
| 238 NotificationProviderNotifyOnShowSettingsFunction() { | |
| 239 } | |
| 240 | |
| 241 NotificationProviderNotifyOnShowSettingsFunction:: | |
| 242 ~NotificationProviderNotifyOnShowSettingsFunction() { | |
| 243 } | |
| 244 | |
| 245 ExtensionFunction::ResponseAction | |
| 246 NotificationProviderNotifyOnShowSettingsFunction::Run() { | |
| 247 std::unique_ptr<api::notification_provider::NotifyOnShowSettings::Params> | |
| 248 params = api::notification_provider::NotifyOnShowSettings::Params::Create( | |
| 249 *args_); | |
| 250 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 251 | |
| 252 bool has_advanced_settings; | |
| 253 // Only application type notifiers have advanced settings. | |
| 254 if (params->notifier_type == | |
| 255 api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION) { | |
| 256 // TODO(dewittj): Refactor NotificationUIManage API to have a getter of | |
| 257 // NotifierSettingsProvider, since it holds the settings provider. | |
| 258 message_center::NotifierSettingsProvider* settings_provider = | |
| 259 message_center::MessageCenter::Get()->GetNotifierSettingsProvider(); | |
| 260 | |
| 261 message_center::NotifierId notifier_id( | |
| 262 message_center::NotifierId::NotifierType::APPLICATION, | |
| 263 params->notifier_id); | |
| 264 | |
| 265 has_advanced_settings = | |
| 266 settings_provider->NotifierHasAdvancedSettings(notifier_id); | |
| 267 if (has_advanced_settings) | |
| 268 settings_provider->OnNotifierAdvancedSettingsRequested(notifier_id, NULL); | |
| 269 } else { | |
| 270 has_advanced_settings = false; | |
| 271 } | |
| 272 | |
| 273 return RespondNow(ArgumentList( | |
| 274 api::notification_provider::NotifyOnShowSettings::Results::Create( | |
| 275 has_advanced_settings))); | |
| 276 } | |
| 277 | |
| 278 NotificationProviderGetNotifierFunction:: | |
| 279 NotificationProviderGetNotifierFunction() { | |
| 280 } | |
| 281 | |
| 282 NotificationProviderGetNotifierFunction:: | |
| 283 ~NotificationProviderGetNotifierFunction() { | |
| 284 } | |
| 285 | |
| 286 ExtensionFunction::ResponseAction | |
| 287 NotificationProviderGetNotifierFunction::Run() { | |
| 288 api::notification_provider::Notifier notifier; | |
| 289 | |
| 290 return RespondNow(ArgumentList( | |
| 291 api::notification_provider::GetNotifier::Results::Create(notifier))); | |
| 292 } | |
| 293 | |
| 294 NotificationProviderGetAllNotifiersFunction:: | |
| 295 NotificationProviderGetAllNotifiersFunction() { | |
| 296 } | |
| 297 | |
| 298 NotificationProviderGetAllNotifiersFunction:: | |
| 299 ~NotificationProviderGetAllNotifiersFunction() { | |
| 300 } | |
| 301 | |
| 302 ExtensionFunction::ResponseAction | |
| 303 NotificationProviderGetAllNotifiersFunction::Run() { | |
| 304 std::vector<api::notification_provider::Notifier> notifiers; | |
| 305 | |
| 306 return RespondNow(ArgumentList( | |
| 307 api::notification_provider::GetAllNotifiers::Results::Create(notifiers))); | |
| 308 } | |
| 309 | |
| 310 } // namespace extensions | |
| OLD | NEW |