Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/notification/notification_api.h" | 5 #include "chrome/browser/extensions/api/notification/notification_api.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/event_names.h" | 11 #include "chrome/browser/extensions/event_names.h" |
| 12 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
| 14 #include "chrome/browser/notifications/desktop_notification_service.h" | |
| 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 14 #include "chrome/browser/notifications/notification.h" | 16 #include "chrome/browser/notifications/notification.h" |
| 15 #include "chrome/browser/notifications/notification_ui_manager.h" | 17 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 16 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 17 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 18 #include "ui/notifications/notification_types.h" | 20 #include "ui/notifications/notification_types.h" |
| 19 | 21 |
| 20 namespace extensions { | 22 namespace extensions { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 extension_->id(), | 189 extension_->id(), |
| 188 id)); // ownership is passed to Notification | 190 id)); // ownership is passed to Notification |
| 189 Notification notification(type, extension_->url(), icon_url, title, message, | 191 Notification notification(type, extension_->url(), icon_url, title, message, |
| 190 WebKit::WebTextDirectionDefault, | 192 WebKit::WebTextDirectionDefault, |
| 191 string16(), UTF8ToUTF16(api_delegate->id()), | 193 string16(), UTF8ToUTF16(api_delegate->id()), |
| 192 optional_fields.get(), api_delegate); | 194 optional_fields.get(), api_delegate); |
| 193 | 195 |
| 194 g_browser_process->notification_ui_manager()->Add(notification, profile()); | 196 g_browser_process->notification_ui_manager()->Add(notification, profile()); |
| 195 } | 197 } |
| 196 | 198 |
| 199 bool NotificationApiFunction::IsNotificationApiEnabled() { | |
| 200 DesktopNotificationService* service = | |
| 201 DesktopNotificationServiceFactory::GetForProfile(profile()); | |
| 202 return service->IsExtensionEnabled(extension_->id()); | |
| 203 } | |
| 204 | |
| 197 const char kNotificationPrefix[] = "extension.api."; | 205 const char kNotificationPrefix[] = "extension.api."; |
| 198 | 206 |
| 199 static uint64 next_id_ = 0; | 207 static uint64 next_id_ = 0; |
| 200 | 208 |
| 201 NotificationCreateFunction::NotificationCreateFunction() { | 209 NotificationCreateFunction::NotificationCreateFunction() { |
| 202 } | 210 } |
| 203 | 211 |
| 204 NotificationCreateFunction::~NotificationCreateFunction() { | 212 NotificationCreateFunction::~NotificationCreateFunction() { |
| 205 } | 213 } |
| 206 | 214 |
| 207 bool NotificationCreateFunction::RunImpl() { | 215 bool NotificationCreateFunction::RunImpl() { |
| 208 params_ = api::experimental_notification::Create::Params::Create(*args_); | 216 params_ = api::experimental_notification::Create::Params::Create(*args_); |
| 209 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 217 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 210 | 218 |
| 219 if (!IsNotificationApiEnabled()) { | |
|
miket_OOO
2013/02/11 23:04:20
If you think this should be enforced for all Notif
Jun Mukai
2013/02/12 01:09:02
That sounds reasonable. Fixed the code based on yo
| |
| 220 SendResponse(false); | |
| 221 return true; | |
| 222 } | |
| 223 | |
| 211 // If the caller provided a notificationId, use that. Otherwise, generate | 224 // If the caller provided a notificationId, use that. Otherwise, generate |
| 212 // one. Note that there's nothing stopping an app developer from passing in | 225 // one. Note that there's nothing stopping an app developer from passing in |
| 213 // arbitrary "extension.api.999" notificationIds that will collide with | 226 // arbitrary "extension.api.999" notificationIds that will collide with |
| 214 // future generated IDs. It doesn't seem necessary to try to prevent this; if | 227 // future generated IDs. It doesn't seem necessary to try to prevent this; if |
| 215 // developers want to hurt themselves, we'll let them. | 228 // developers want to hurt themselves, we'll let them. |
| 216 const std::string extension_id(extension_->id()); | 229 const std::string extension_id(extension_->id()); |
| 217 std::string notification_id; | 230 std::string notification_id; |
| 218 if (!params_->notification_id.empty()) | 231 if (!params_->notification_id.empty()) |
| 219 notification_id = params_->notification_id; | 232 notification_id = params_->notification_id; |
| 220 else | 233 else |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 232 NotificationUpdateFunction::NotificationUpdateFunction() { | 245 NotificationUpdateFunction::NotificationUpdateFunction() { |
| 233 } | 246 } |
| 234 | 247 |
| 235 NotificationUpdateFunction::~NotificationUpdateFunction() { | 248 NotificationUpdateFunction::~NotificationUpdateFunction() { |
| 236 } | 249 } |
| 237 | 250 |
| 238 bool NotificationUpdateFunction::RunImpl() { | 251 bool NotificationUpdateFunction::RunImpl() { |
| 239 params_ = api::experimental_notification::Update::Params::Create(*args_); | 252 params_ = api::experimental_notification::Update::Params::Create(*args_); |
| 240 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 253 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 241 | 254 |
| 255 if (!IsNotificationApiEnabled()) { | |
| 256 SendResponse(false); | |
| 257 return true; | |
| 258 } | |
| 259 | |
| 242 if (g_browser_process->notification_ui_manager()-> | 260 if (g_browser_process->notification_ui_manager()-> |
| 243 DoesIdExist(NotificationApiDelegate::CreateScopedIdentifier( | 261 DoesIdExist(NotificationApiDelegate::CreateScopedIdentifier( |
| 244 extension_->id(), params_->notification_id))) { | 262 extension_->id(), params_->notification_id))) { |
| 245 CreateNotification(params_->notification_id, ¶ms_->options); | 263 CreateNotification(params_->notification_id, ¶ms_->options); |
| 246 SetResult(Value::CreateBooleanValue(true)); | 264 SetResult(Value::CreateBooleanValue(true)); |
| 247 } else { | 265 } else { |
| 248 SetResult(Value::CreateBooleanValue(false)); | 266 SetResult(Value::CreateBooleanValue(false)); |
| 249 } | 267 } |
| 250 | 268 |
| 251 SendResponse(true); | 269 SendResponse(true); |
| 252 | 270 |
| 253 return true; | 271 return true; |
| 254 } | 272 } |
| 255 | 273 |
| 256 NotificationDeleteFunction::NotificationDeleteFunction() { | 274 NotificationDeleteFunction::NotificationDeleteFunction() { |
| 257 } | 275 } |
| 258 | 276 |
| 259 NotificationDeleteFunction::~NotificationDeleteFunction() { | 277 NotificationDeleteFunction::~NotificationDeleteFunction() { |
| 260 } | 278 } |
| 261 | 279 |
| 262 bool NotificationDeleteFunction::RunImpl() { | 280 bool NotificationDeleteFunction::RunImpl() { |
| 263 params_ = api::experimental_notification::Delete::Params::Create(*args_); | 281 params_ = api::experimental_notification::Delete::Params::Create(*args_); |
| 264 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 282 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 265 | 283 |
| 284 if (!IsNotificationApiEnabled()) { | |
| 285 SendResponse(false); | |
| 286 return true; | |
| 287 } | |
| 288 | |
| 266 bool cancel_result = g_browser_process->notification_ui_manager()-> | 289 bool cancel_result = g_browser_process->notification_ui_manager()-> |
| 267 CancelById(NotificationApiDelegate::CreateScopedIdentifier( | 290 CancelById(NotificationApiDelegate::CreateScopedIdentifier( |
| 268 extension_->id(), params_->notification_id)); | 291 extension_->id(), params_->notification_id)); |
| 269 | 292 |
| 270 SetResult(Value::CreateBooleanValue(cancel_result)); | 293 SetResult(Value::CreateBooleanValue(cancel_result)); |
| 271 SendResponse(true); | 294 SendResponse(true); |
| 272 | 295 |
| 273 return true; | 296 return true; |
| 274 } | 297 } |
| 275 | 298 |
| 276 } // namespace extensions | 299 } // namespace extensions |
| OLD | NEW |