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) { |
| 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 |
150 message_center::NotificationType type = | 174 message_center::NotificationType type = |
151 MapApiTemplateTypeToType(options->type); | 175 MapApiTemplateTypeToType(options->type); |
152 GURL icon_url(UTF8ToUTF16(options->icon_url)); | 176 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)); | |
179 string16 title(UTF8ToUTF16(options->title)); | 177 string16 title(UTF8ToUTF16(options->title)); |
180 string16 message(UTF8ToUTF16(options->message)); | 178 string16 message(UTF8ToUTF16(options->message)); |
181 | 179 |
182 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | 180 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); |
183 | 181 |
184 // For all notification types. | 182 // For all notification types. |
185 if (options->priority.get()) | 183 if (options->priority.get()) |
186 optional_fields->SetInteger(message_center::kPriorityKey, | 184 optional_fields->SetInteger(message_center::kPriorityKey, |
187 *options->priority); | 185 *options->priority); |
188 if (options->event_time.get()) | 186 if (options->event_time.get()) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 extension_->id(), | 242 extension_->id(), |
245 id)); // ownership is passed to Notification | 243 id)); // ownership is passed to Notification |
246 Notification notification(type, extension_->url(), icon_url, title, message, | 244 Notification notification(type, extension_->url(), icon_url, title, message, |
247 WebKit::WebTextDirectionDefault, | 245 WebKit::WebTextDirectionDefault, |
248 UTF8ToUTF16(extension_->name()), | 246 UTF8ToUTF16(extension_->name()), |
249 UTF8ToUTF16(api_delegate->id()), | 247 UTF8ToUTF16(api_delegate->id()), |
250 optional_fields.get(), api_delegate); | 248 optional_fields.get(), api_delegate); |
251 | 249 |
252 g_browser_process->notification_ui_manager()->Add(notification, profile()); | 250 g_browser_process->notification_ui_manager()->Add(notification, profile()); |
253 } | 251 } |
254 #endif // !defined(ENABLE_MESSAGE_CENTER) | |
255 | 252 |
256 bool NotificationsApiFunction::IsNotificationsApiEnabled() { | 253 bool NotificationsApiFunction::IsNotificationsApiEnabled() { |
257 DesktopNotificationService* service = | 254 DesktopNotificationService* service = |
258 DesktopNotificationServiceFactory::GetForProfile(profile()); | 255 DesktopNotificationServiceFactory::GetForProfile(profile()); |
259 return service->IsExtensionEnabled(extension_->id()); | 256 return service->IsExtensionEnabled(extension_->id()); |
260 } | 257 } |
261 | 258 |
262 bool NotificationsApiFunction::RunImpl() { | 259 bool NotificationsApiFunction::RunImpl() { |
263 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { | 260 if (IsNotificationsApiAvailable() && IsNotificationsApiEnabled()) { |
264 return RunNotificationsApi(); | 261 return RunNotificationsApi(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( | 358 CancelById(NotificationsApiDelegate::CreateScopedIdentifier( |
362 extension_->id(), params_->notification_id)); | 359 extension_->id(), params_->notification_id)); |
363 | 360 |
364 SetResult(Value::CreateBooleanValue(cancel_result)); | 361 SetResult(Value::CreateBooleanValue(cancel_result)); |
365 SendResponse(true); | 362 SendResponse(true); |
366 | 363 |
367 return true; | 364 return true; |
368 } | 365 } |
369 | 366 |
370 } // namespace extensions | 367 } // namespace extensions |
OLD | NEW |