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/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" | |
| 24 | 25 |
| 25 namespace extensions { | 26 namespace extensions { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const char kResultKey[] = "result"; | 30 const char kResultKey[] = "result"; |
| 30 | 31 |
| 31 class NotificationsApiDelegate : public NotificationDelegate { | 32 class NotificationsApiDelegate : public NotificationDelegate { |
| 32 public: | 33 public: |
| 33 NotificationsApiDelegate(ApiFunction* api_function, | 34 NotificationsApiDelegate(ApiFunction* api_function, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 | 135 |
| 135 return true; | 136 return true; |
| 136 } | 137 } |
| 137 | 138 |
| 138 NotificationsApiFunction::NotificationsApiFunction() { | 139 NotificationsApiFunction::NotificationsApiFunction() { |
| 139 } | 140 } |
| 140 | 141 |
| 141 NotificationsApiFunction::~NotificationsApiFunction() { | 142 NotificationsApiFunction::~NotificationsApiFunction() { |
| 142 } | 143 } |
| 143 | 144 |
| 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) | |
| 147 void NotificationsApiFunction::CreateNotification( | 145 void NotificationsApiFunction::CreateNotification( |
| 148 const std::string& id, | 146 const std::string& id, |
| 149 api::notifications::NotificationOptions* options) { | 147 api::notifications::NotificationOptions* options) { |
| 150 message_center::NotificationType type = | 148 // If older notification runtime is used, use simpler bridge. |
| 151 MapApiTemplateTypeToType(options->type); | 149 if (message_center::IsRichNotificationEnabled()) { |
|
dewittj
2013/05/08 18:53:01
need a "!" in the conditional?
Dmitry Titov
2013/05/08 23:03:26
oops. Done. Tested that Galore works :-)
| |
| 152 GURL icon_url(UTF8ToUTF16(options->icon_url)); | 150 message_center::NotificationType type = |
| 153 string16 title(UTF8ToUTF16(options->title)); | 151 MapApiTemplateTypeToType(options->type); |
| 154 string16 message(UTF8ToUTF16(options->message)); | 152 GURL icon_url(UTF8ToUTF16(options->icon_url)); |
| 153 string16 title(UTF8ToUTF16(options->title)); | |
| 154 string16 message(UTF8ToUTF16(options->message)); | |
| 155 | 155 |
| 156 // Ignore options if running on the old notification runtime. | 156 // Ignore options if running on the old notification runtime. |
| 157 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | 157 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); |
| 158 | 158 |
| 159 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | 159 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( |
| 160 this, | 160 this, |
| 161 profile(), | 161 profile(), |
| 162 extension_->id(), | 162 extension_->id(), |
| 163 id)); // ownership is passed to Notification | 163 id)); // ownership is passed to Notification |
| 164 Notification notification(type, extension_->url(), icon_url, title, message, | 164 Notification notification(type, extension_->url(), icon_url, title, message, |
| 165 WebKit::WebTextDirectionDefault, | 165 WebKit::WebTextDirectionDefault, |
| 166 UTF8ToUTF16(extension_->name()), | 166 UTF8ToUTF16(extension_->name()), |
| 167 UTF8ToUTF16(api_delegate->id()), | 167 UTF8ToUTF16(api_delegate->id()), |
| 168 optional_fields.get(), api_delegate); | 168 optional_fields.get(), api_delegate); |
| 169 | 169 |
| 170 g_browser_process->notification_ui_manager()->Add(notification, profile()); | 170 g_browser_process->notification_ui_manager()->Add(notification, profile()); |
|
dewittj
2013/05/08 18:53:01
put a return here to buy one level of indentation
Dmitry Titov
2013/05/08 23:03:26
Done.
| |
| 171 } else { | |
| 172 message_center::NotificationType type = | |
| 173 MapApiTemplateTypeToType(options->type); | |
| 174 GURL icon_url(UTF8ToUTF16(options->icon_url)); | |
| 175 string16 title(UTF8ToUTF16(options->title)); | |
| 176 string16 message(UTF8ToUTF16(options->message)); | |
| 177 | |
| 178 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | |
| 179 | |
| 180 // For all notification types. | |
| 181 if (options->priority.get()) | |
| 182 optional_fields->SetInteger(message_center::kPriorityKey, | |
| 183 *options->priority); | |
| 184 if (options->event_time.get()) | |
| 185 optional_fields->SetDouble(message_center::kTimestampKey, | |
| 186 *options->event_time); | |
| 187 if (options->buttons.get()) { | |
| 188 if (options->buttons->size() > 0) { | |
| 189 linked_ptr<api::notifications::NotificationButton> button = | |
| 190 (*options->buttons)[0]; | |
| 191 optional_fields->SetString(message_center::kButtonOneTitleKey, | |
| 192 UTF8ToUTF16(button->title)); | |
| 193 if (button->icon_url.get()) | |
| 194 optional_fields->SetString(message_center::kButtonOneIconUrlKey, | |
| 195 UTF8ToUTF16(*button->icon_url)); | |
| 196 } | |
| 197 if (options->buttons->size() > 1) { | |
| 198 linked_ptr<api::notifications::NotificationButton> button = | |
| 199 (*options->buttons)[1]; | |
| 200 optional_fields->SetString(message_center::kButtonTwoTitleKey, | |
| 201 UTF8ToUTF16(button->title)); | |
| 202 if (button->icon_url.get()) | |
| 203 optional_fields->SetString(message_center::kButtonTwoIconUrlKey, | |
| 204 UTF8ToUTF16(*button->icon_url)); | |
| 205 } | |
| 206 } | |
| 207 if (options->expanded_message.get()) | |
| 208 optional_fields->SetString(message_center::kExpandedMessageKey, | |
| 209 UTF8ToUTF16(*options->expanded_message)); | |
| 210 | |
| 211 // For image notifications (type == 'image'). | |
| 212 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) | |
| 213 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) | |
| 214 if (options->image_url.get()) | |
| 215 optional_fields->SetString(message_center::kImageUrlKey, | |
| 216 UTF8ToUTF16(*options->image_url)); | |
| 217 | |
| 218 // For list notifications (type == 'multiple'). | |
| 219 // TODO(dharcourt): Fail if (type == 'multiple' && !options->items.get()) | |
| 220 // TODO(dharcourt): Fail if (type != 'multiple' && options->items.get()) | |
| 221 if (options->items.get()) { | |
| 222 base::ListValue* items = new base::ListValue(); | |
| 223 std::vector< | |
| 224 linked_ptr< | |
| 225 api::notifications::NotificationItem> >::iterator i; | |
| 226 for (i = options->items->begin(); i != options->items->end(); ++i) { | |
| 227 base::DictionaryValue* item = new base::DictionaryValue(); | |
| 228 item->SetString(message_center::kItemTitleKey, | |
| 229 UTF8ToUTF16(i->get()->title)); | |
| 230 item->SetString(message_center::kItemMessageKey, | |
| 231 UTF8ToUTF16(i->get()->message)); | |
| 232 items->Append(item); | |
| 233 } | |
| 234 optional_fields->Set(message_center::kItemsKey, items); | |
| 235 } | |
| 236 | |
| 237 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | |
| 238 this, | |
| 239 profile(), | |
| 240 extension_->id(), | |
| 241 id)); // ownership is passed to Notification | |
| 242 Notification notification(type, extension_->url(), icon_url, title, message, | |
| 243 WebKit::WebTextDirectionDefault, | |
| 244 UTF8ToUTF16(extension_->name()), | |
| 245 UTF8ToUTF16(api_delegate->id()), | |
| 246 optional_fields.get(), api_delegate); | |
| 247 | |
| 248 g_browser_process->notification_ui_manager()->Add(notification, profile()); | |
| 249 } | |
| 171 } | 250 } |
| 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)); | |
| 179 string16 title(UTF8ToUTF16(options->title)); | |
| 180 string16 message(UTF8ToUTF16(options->message)); | |
| 181 | |
| 182 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | |
| 183 | |
| 184 // For all notification types. | |
| 185 if (options->priority.get()) | |
| 186 optional_fields->SetInteger(message_center::kPriorityKey, | |
| 187 *options->priority); | |
| 188 if (options->event_time.get()) | |
| 189 optional_fields->SetDouble(message_center::kTimestampKey, | |
| 190 *options->event_time); | |
| 191 if (options->buttons.get()) { | |
| 192 if (options->buttons->size() > 0) { | |
| 193 linked_ptr<api::notifications::NotificationButton> button = | |
| 194 (*options->buttons)[0]; | |
| 195 optional_fields->SetString(message_center::kButtonOneTitleKey, | |
| 196 UTF8ToUTF16(button->title)); | |
| 197 if (button->icon_url.get()) | |
| 198 optional_fields->SetString(message_center::kButtonOneIconUrlKey, | |
| 199 UTF8ToUTF16(*button->icon_url)); | |
| 200 } | |
| 201 if (options->buttons->size() > 1) { | |
| 202 linked_ptr<api::notifications::NotificationButton> button = | |
| 203 (*options->buttons)[1]; | |
| 204 optional_fields->SetString(message_center::kButtonTwoTitleKey, | |
| 205 UTF8ToUTF16(button->title)); | |
| 206 if (button->icon_url.get()) | |
| 207 optional_fields->SetString(message_center::kButtonTwoIconUrlKey, | |
| 208 UTF8ToUTF16(*button->icon_url)); | |
| 209 } | |
| 210 } | |
| 211 if (options->expanded_message.get()) | |
| 212 optional_fields->SetString(message_center::kExpandedMessageKey, | |
| 213 UTF8ToUTF16(*options->expanded_message)); | |
| 214 | |
| 215 // For image notifications (type == 'image'). | |
| 216 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) | |
| 217 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) | |
| 218 if (options->image_url.get()) | |
| 219 optional_fields->SetString(message_center::kImageUrlKey, | |
| 220 UTF8ToUTF16(*options->image_url)); | |
| 221 | |
| 222 // For list notifications (type == 'multiple'). | |
| 223 // TODO(dharcourt): Fail if (type == 'multiple' && !options->items.get()) | |
| 224 // TODO(dharcourt): Fail if (type != 'multiple' && options->items.get()) | |
| 225 if (options->items.get()) { | |
| 226 base::ListValue* items = new base::ListValue(); | |
| 227 std::vector< | |
| 228 linked_ptr< | |
| 229 api::notifications::NotificationItem> >::iterator i; | |
| 230 for (i = options->items->begin(); i != options->items->end(); ++i) { | |
| 231 base::DictionaryValue* item = new base::DictionaryValue(); | |
| 232 item->SetString(message_center::kItemTitleKey, | |
| 233 UTF8ToUTF16(i->get()->title)); | |
| 234 item->SetString(message_center::kItemMessageKey, | |
| 235 UTF8ToUTF16(i->get()->message)); | |
| 236 items->Append(item); | |
| 237 } | |
| 238 optional_fields->Set(message_center::kItemsKey, items); | |
| 239 } | |
| 240 | |
| 241 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | |
| 242 this, | |
| 243 profile(), | |
| 244 extension_->id(), | |
| 245 id)); // ownership is passed to Notification | |
| 246 Notification notification(type, extension_->url(), icon_url, title, message, | |
| 247 WebKit::WebTextDirectionDefault, | |
| 248 UTF8ToUTF16(extension_->name()), | |
| 249 UTF8ToUTF16(api_delegate->id()), | |
| 250 optional_fields.get(), api_delegate); | |
| 251 | |
| 252 g_browser_process->notification_ui_manager()->Add(notification, profile()); | |
| 253 } | |
| 254 #endif // !defined(ENABLE_MESSAGE_CENTER) | |
| 255 | 251 |
| 256 bool NotificationsApiFunction::IsNotificationsApiEnabled() { | 252 bool NotificationsApiFunction::IsNotificationsApiEnabled() { |
| 257 DesktopNotificationService* service = | 253 DesktopNotificationService* service = |
| 258 DesktopNotificationServiceFactory::GetForProfile(profile()); | 254 DesktopNotificationServiceFactory::GetForProfile(profile()); |
| 259 return service->IsExtensionEnabled(extension_->id()); | 255 return service->IsExtensionEnabled(extension_->id()); |
| 260 } | 256 } |
| 261 | 257 |
| 262 bool NotificationsApiFunction::RunImpl() { | 258 bool NotificationsApiFunction::RunImpl() { |
| 263 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { | 259 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { |
| 264 return RunNotificationsApi(); | 260 return RunNotificationsApi(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( | 357 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( |
| 362 extension_->id(), params_->notification_id)); | 358 extension_->id(), params_->notification_id)); |
| 363 | 359 |
| 364 SetResult(Value::CreateBooleanValue(cancel_result)); | 360 SetResult(Value::CreateBooleanValue(cancel_result)); |
| 365 SendResponse(true); | 361 SendResponse(true); |
| 366 | 362 |
| 367 return true; | 363 return true; |
| 368 } | 364 } |
| 369 | 365 |
| 370 } // namespace extensions | 366 } // namespace extensions |
| OLD | NEW |