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" | 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/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
20 #include "ui/notifications/notification_types.h" | |
21 | 20 |
22 namespace extensions { | 21 namespace extensions { |
23 | 22 |
24 namespace { | 23 namespace { |
25 | 24 |
26 const char kResultKey[] = "result"; | 25 const char kResultKey[] = "result"; |
27 | 26 |
28 class NotificationApiDelegate : public NotificationDelegate { | 27 class NotificationApiDelegate : public NotificationDelegate { |
29 public: | 28 public: |
30 NotificationApiDelegate(ApiFunction* api_function, | 29 NotificationApiDelegate(ApiFunction* api_function, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 }; | 109 }; |
111 | 110 |
112 } // namespace | 111 } // namespace |
113 | 112 |
114 NotificationApiFunction::NotificationApiFunction() { | 113 NotificationApiFunction::NotificationApiFunction() { |
115 } | 114 } |
116 | 115 |
117 NotificationApiFunction::~NotificationApiFunction() { | 116 NotificationApiFunction::~NotificationApiFunction() { |
118 } | 117 } |
119 | 118 |
| 119 ui::notifications::NotificationType |
| 120 NotificationApiFunction::MapApiTemplateTypeToType( |
| 121 api::experimental_notification::TemplateType type) { |
| 122 switch (type) { |
| 123 case api::experimental_notification::TEMPLATE_TYPE_NONE: |
| 124 case api::experimental_notification::TEMPLATE_TYPE_SIMPLE: |
| 125 return ui::notifications::NOTIFICATION_TYPE_SIMPLE; |
| 126 case api::experimental_notification::TEMPLATE_TYPE_BASIC: |
| 127 return ui::notifications::NOTIFICATION_TYPE_BASE_FORMAT; |
| 128 case api::experimental_notification::TEMPLATE_TYPE_IMAGE: |
| 129 return ui::notifications::NOTIFICATION_TYPE_IMAGE; |
| 130 case api::experimental_notification::TEMPLATE_TYPE_LIST: |
| 131 return ui::notifications::NOTIFICATION_TYPE_MULTIPLE; |
| 132 default: |
| 133 // Gracefully handle newer application code that is running on an older |
| 134 // runtime that doesn't recognize the requested template. |
| 135 return ui::notifications::NOTIFICATION_TYPE_BASE_FORMAT; |
| 136 } |
| 137 } |
| 138 |
120 void NotificationApiFunction::CreateNotification( | 139 void NotificationApiFunction::CreateNotification( |
121 const std::string& id, | 140 const std::string& id, |
122 api::experimental_notification::NotificationOptions* options) { | 141 api::experimental_notification::NotificationOptions* options) { |
123 ui::notifications::NotificationType type = | 142 ui::notifications::NotificationType type = MapApiTemplateTypeToType( |
124 ui::notifications::StringToNotificationType(options->type); | 143 options->template_type); |
125 GURL icon_url(UTF8ToUTF16(options->icon_url)); | 144 GURL icon_url(UTF8ToUTF16(options->icon_url)); |
126 string16 title(UTF8ToUTF16(options->title)); | 145 string16 title(UTF8ToUTF16(options->title)); |
127 string16 message(UTF8ToUTF16(options->message)); | 146 string16 message(UTF8ToUTF16(options->message)); |
128 | 147 |
129 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | 148 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); |
130 | 149 |
131 // For all notification types. | 150 // For all notification types. |
132 if (options->priority.get()) | 151 if (options->priority.get()) |
133 optional_fields->SetInteger(ui::notifications::kPriorityKey, | 152 optional_fields->SetInteger(ui::notifications::kPriorityKey, |
134 *options->priority); | 153 *options->priority); |
135 if (options->timestamp.get()) | 154 if (options->event_time.get()) |
136 optional_fields->SetString(ui::notifications::kTimestampKey, | 155 optional_fields->SetString(ui::notifications::kTimestampKey, |
137 *options->timestamp); | 156 *options->event_time); |
138 if (options->unread_count.get()) | 157 if (options->buttons.get()) { |
139 optional_fields->SetInteger(ui::notifications::kUnreadCountKey, | 158 if (options->buttons->size() > 0) { |
140 *options->unread_count); | 159 linked_ptr<api::experimental_notification::NotificationButton> button = |
141 if (options->button_one_title.get()) | 160 (*options->buttons)[0]; |
142 optional_fields->SetString(ui::notifications::kButtonOneTitleKey, | 161 optional_fields->SetString(ui::notifications::kButtonOneTitleKey, |
143 UTF8ToUTF16(*options->button_one_title)); | 162 UTF8ToUTF16(button->title)); |
144 if (options->button_one_icon_url.get()) | 163 if (button->icon_url.get()) |
145 optional_fields->SetString(ui::notifications::kButtonOneIconUrlKey, | 164 optional_fields->SetString(ui::notifications::kButtonOneIconUrlKey, |
146 UTF8ToUTF16(*options->button_one_icon_url)); | 165 UTF8ToUTF16(*button->icon_url)); |
147 // TODO(dharcourt): Fail if: | 166 } |
148 // (options->button_two_title.get() || options->button_two_icon_url.get()) && | 167 if (options->buttons->size() > 1) { |
149 // !(options->button_one_title.get() || options->button_one_icon_url.get()) | 168 linked_ptr<api::experimental_notification::NotificationButton> button = |
150 if (options->button_two_title.get()) | 169 (*options->buttons)[1]; |
151 optional_fields->SetString(ui::notifications::kButtonTwoTitleKey, | 170 optional_fields->SetString(ui::notifications::kButtonTwoTitleKey, |
152 UTF8ToUTF16(*options->button_two_title)); | 171 UTF8ToUTF16(button->title)); |
153 if (options->button_two_icon_url.get()) | 172 if (button->icon_url.get()) |
154 optional_fields->SetString(ui::notifications::kButtonTwoIconUrlKey, | 173 optional_fields->SetString(ui::notifications::kButtonTwoIconUrlKey, |
155 UTF8ToUTF16(*options->button_two_icon_url)); | 174 UTF8ToUTF16(*button->icon_url)); |
| 175 } |
| 176 } |
156 if (options->expanded_message.get()) | 177 if (options->expanded_message.get()) |
157 optional_fields->SetString(ui::notifications::kExpandedMessageKey, | 178 optional_fields->SetString(ui::notifications::kExpandedMessageKey, |
158 UTF8ToUTF16(*options->expanded_message)); | 179 UTF8ToUTF16(*options->expanded_message)); |
159 | 180 |
160 // For image notifications (type == 'image'). | 181 // For image notifications (type == 'image'). |
161 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) | 182 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) |
162 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) | 183 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) |
163 if (options->image_url.get()) | 184 if (options->image_url.get()) |
164 optional_fields->SetString(ui::notifications::kImageUrlKey, | 185 optional_fields->SetString(ui::notifications::kImageUrlKey, |
165 UTF8ToUTF16(*options->image_url)); | 186 UTF8ToUTF16(*options->image_url)); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 CancelById(NotificationApiDelegate::CreateScopedIdentifier( | 305 CancelById(NotificationApiDelegate::CreateScopedIdentifier( |
285 extension_->id(), params_->notification_id)); | 306 extension_->id(), params_->notification_id)); |
286 | 307 |
287 SetResult(Value::CreateBooleanValue(cancel_result)); | 308 SetResult(Value::CreateBooleanValue(cancel_result)); |
288 SendResponse(true); | 309 SendResponse(true); |
289 | 310 |
290 return true; | 311 return true; |
291 } | 312 } |
292 | 313 |
293 } // namespace extensions | 314 } // namespace extensions |
OLD | NEW |