| 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/notifications/notifications_api.h" | 5 #include "chrome/browser/extensions/api/notifications/notifications_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" | 14 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 16 #include "chrome/browser/notifications/notification.h" | 16 #include "chrome/browser/notifications/notification.h" |
| 17 #include "chrome/browser/notifications/notification_ui_manager.h" | 17 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 18 #include "chrome/common/chrome_version_info.h" | 18 #include "chrome/common/chrome_version_info.h" |
| 19 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/features/feature.h" | 20 #include "chrome/common/extensions/features/feature.h" |
| 21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "ui/message_center/message_center_util.h" | |
| 25 | 24 |
| 26 namespace extensions { | 25 namespace extensions { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 const char kResultKey[] = "result"; | 29 const char kResultKey[] = "result"; |
| 31 | 30 |
| 32 class NotificationsApiDelegate : public NotificationDelegate { | 31 class NotificationsApiDelegate : public NotificationDelegate { |
| 33 public: | 32 public: |
| 34 NotificationsApiDelegate(ApiFunction* api_function, | 33 NotificationsApiDelegate(ApiFunction* api_function, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 134 |
| 136 return true; | 135 return true; |
| 137 } | 136 } |
| 138 | 137 |
| 139 NotificationsApiFunction::NotificationsApiFunction() { | 138 NotificationsApiFunction::NotificationsApiFunction() { |
| 140 } | 139 } |
| 141 | 140 |
| 142 NotificationsApiFunction::~NotificationsApiFunction() { | 141 NotificationsApiFunction::~NotificationsApiFunction() { |
| 143 } | 142 } |
| 144 | 143 |
| 144 // If older notification runtime is used, MessageCenter is not built. |
| 145 // Use simpler bridge then, ignoring all options. |
| 146 #if !defined (ENABLE_MESSAGE_CENTER) |
| 145 void NotificationsApiFunction::CreateNotification( | 147 void NotificationsApiFunction::CreateNotification( |
| 146 const std::string& id, | 148 const std::string& id, |
| 147 api::notifications::NotificationOptions* options) { | 149 api::notifications::NotificationOptions* options) { |
| 148 // If older notification runtime is used, use simpler bridge. | |
| 149 if (!message_center::IsRichNotificationEnabled()) { | |
| 150 message_center::NotificationType type = | |
| 151 MapApiTemplateTypeToType(options->type); | |
| 152 GURL icon_url(UTF8ToUTF16(options->icon_url)); | |
| 153 string16 title(UTF8ToUTF16(options->title)); | |
| 154 string16 message(UTF8ToUTF16(options->message)); | |
| 155 | |
| 156 // Ignore options if running on the old notification runtime. | |
| 157 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | |
| 158 | |
| 159 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | |
| 160 this, | |
| 161 profile(), | |
| 162 extension_->id(), | |
| 163 id)); // ownership is passed to Notification | |
| 164 Notification notification(type, extension_->url(), icon_url, title, message, | |
| 165 WebKit::WebTextDirectionDefault, | |
| 166 UTF8ToUTF16(extension_->name()), | |
| 167 UTF8ToUTF16(api_delegate->id()), | |
| 168 optional_fields.get(), api_delegate); | |
| 169 | |
| 170 g_browser_process->notification_ui_manager()->Add(notification, profile()); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 message_center::NotificationType type = | 150 message_center::NotificationType type = |
| 175 MapApiTemplateTypeToType(options->type); | 151 MapApiTemplateTypeToType(options->type); |
| 176 GURL icon_url(UTF8ToUTF16(options->icon_url)); | 152 GURL icon_url(UTF8ToUTF16(options->icon_url)); |
| 153 string16 title(UTF8ToUTF16(options->title)); |
| 154 string16 message(UTF8ToUTF16(options->message)); |
| 155 |
| 156 // Ignore options if running on the old notification runtime. |
| 157 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); |
| 158 |
| 159 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( |
| 160 this, |
| 161 profile(), |
| 162 extension_->id(), |
| 163 id)); // ownership is passed to Notification |
| 164 Notification notification(type, extension_->url(), icon_url, title, message, |
| 165 WebKit::WebTextDirectionDefault, |
| 166 UTF8ToUTF16(extension_->name()), |
| 167 UTF8ToUTF16(api_delegate->id()), |
| 168 optional_fields.get(), api_delegate); |
| 169 |
| 170 g_browser_process->notification_ui_manager()->Add(notification, profile()); |
| 171 } |
| 172 #else // defined(ENABLE_MESSAGE_CENTER) |
| 173 void NotificationsApiFunction::CreateNotification( |
| 174 const std::string& id, |
| 175 api::notifications::NotificationOptions* options) { |
| 176 message_center::NotificationType type = |
| 177 MapApiTemplateTypeToType(options->type); |
| 178 GURL icon_url(UTF8ToUTF16(options->icon_url)); |
| 177 string16 title(UTF8ToUTF16(options->title)); | 179 string16 title(UTF8ToUTF16(options->title)); |
| 178 string16 message(UTF8ToUTF16(options->message)); | 180 string16 message(UTF8ToUTF16(options->message)); |
| 179 | 181 |
| 180 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | 182 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); |
| 181 | 183 |
| 182 // For all notification types. | 184 // For all notification types. |
| 183 if (options->priority.get()) | 185 if (options->priority.get()) |
| 184 optional_fields->SetInteger(message_center::kPriorityKey, | 186 optional_fields->SetInteger(message_center::kPriorityKey, |
| 185 *options->priority); | 187 *options->priority); |
| 186 if (options->event_time.get()) | 188 if (options->event_time.get()) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 extension_->id(), | 244 extension_->id(), |
| 243 id)); // ownership is passed to Notification | 245 id)); // ownership is passed to Notification |
| 244 Notification notification(type, extension_->url(), icon_url, title, message, | 246 Notification notification(type, extension_->url(), icon_url, title, message, |
| 245 WebKit::WebTextDirectionDefault, | 247 WebKit::WebTextDirectionDefault, |
| 246 UTF8ToUTF16(extension_->name()), | 248 UTF8ToUTF16(extension_->name()), |
| 247 UTF8ToUTF16(api_delegate->id()), | 249 UTF8ToUTF16(api_delegate->id()), |
| 248 optional_fields.get(), api_delegate); | 250 optional_fields.get(), api_delegate); |
| 249 | 251 |
| 250 g_browser_process->notification_ui_manager()->Add(notification, profile()); | 252 g_browser_process->notification_ui_manager()->Add(notification, profile()); |
| 251 } | 253 } |
| 254 #endif // !defined(ENABLE_MESSAGE_CENTER) |
| 252 | 255 |
| 253 bool NotificationsApiFunction::IsNotificationsApiEnabled() { | 256 bool NotificationsApiFunction::IsNotificationsApiEnabled() { |
| 254 DesktopNotificationService* service = | 257 DesktopNotificationService* service = |
| 255 DesktopNotificationServiceFactory::GetForProfile(profile()); | 258 DesktopNotificationServiceFactory::GetForProfile(profile()); |
| 256 return service->IsExtensionEnabled(extension_->id()); | 259 return service->IsExtensionEnabled(extension_->id()); |
| 257 } | 260 } |
| 258 | 261 |
| 259 bool NotificationsApiFunction::RunImpl() { | 262 bool NotificationsApiFunction::RunImpl() { |
| 260 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { | 263 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { |
| 261 return RunNotificationsApi(); | 264 return RunNotificationsApi(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( | 361 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( |
| 359 extension_->id(), params_->notification_id)); | 362 extension_->id(), params_->notification_id)); |
| 360 | 363 |
| 361 SetResult(Value::CreateBooleanValue(cancel_result)); | 364 SetResult(Value::CreateBooleanValue(cancel_result)); |
| 362 SendResponse(true); | 365 SendResponse(true); |
| 363 | 366 |
| 364 return true; | 367 return true; |
| 365 } | 368 } |
| 366 | 369 |
| 367 } // namespace extensions | 370 } // namespace extensions |
| OLD | NEW |