OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/notification/notification_api.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/extensions/event_names.h" | |
12 #include "chrome/browser/extensions/event_router.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" | |
16 #include "chrome/browser/notifications/notification.h" | |
17 #include "chrome/browser/notifications/notification_ui_manager.h" | |
18 #include "chrome/common/extensions/extension.h" | |
19 #include "googleurl/src/gurl.h" | |
20 | |
21 namespace extensions { | |
22 | |
23 namespace { | |
24 | |
25 const char kResultKey[] = "result"; | |
26 | |
27 class NotificationApiDelegate : public NotificationDelegate { | |
28 public: | |
29 NotificationApiDelegate(ApiFunction* api_function, | |
30 Profile* profile, | |
31 const std::string& extension_id, | |
32 const std::string& id) | |
33 : api_function_(api_function), | |
34 profile_(profile), | |
35 extension_id_(extension_id), | |
36 id_(id), | |
37 scoped_id_(CreateScopedIdentifier(extension_id, id)) { | |
38 DCHECK(api_function_); | |
39 } | |
40 | |
41 // Given an extension id and another id, returns an id that is unique | |
42 // relative to other extensions. | |
43 static std::string CreateScopedIdentifier(const std::string& extension_id, | |
44 const std::string& id) { | |
45 return extension_id + "-" + id; | |
46 } | |
47 | |
48 virtual void Display() OVERRIDE { | |
49 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | |
50 SendEvent(event_names::kOnNotificationDisplayed, args.Pass()); | |
51 } | |
52 | |
53 virtual void Error() OVERRIDE { | |
54 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | |
55 SendEvent(event_names::kOnNotificationError, args.Pass()); | |
56 } | |
57 | |
58 virtual void Close(bool by_user) OVERRIDE { | |
59 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | |
60 args->Append(Value::CreateBooleanValue(by_user)); | |
61 SendEvent(event_names::kOnNotificationClosed, args.Pass()); | |
62 } | |
63 | |
64 virtual void Click() OVERRIDE { | |
65 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | |
66 SendEvent(event_names::kOnNotificationClicked, args.Pass()); | |
67 } | |
68 | |
69 virtual void ButtonClick(int index) OVERRIDE { | |
70 scoped_ptr<ListValue> args(CreateBaseEventArgs()); | |
71 args->Append(Value::CreateIntegerValue(index)); | |
72 SendEvent(event_names::kOnNotificationButtonClicked, args.Pass()); | |
73 } | |
74 | |
75 virtual std::string id() const OVERRIDE { | |
76 return scoped_id_; | |
77 } | |
78 | |
79 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { | |
80 // We're holding a reference to api_function_, so we know it'll be valid as | |
81 // long as we are, and api_function_ (as a UIThreadExtensionFunction) | |
82 // listens to content::NOTIFICATION_RENDER_VIEW_HOST_DELETED and will | |
83 // properly zero out its copy of render_view_host when the RVH goes away. | |
84 return api_function_->render_view_host(); | |
85 } | |
86 | |
87 private: | |
88 virtual ~NotificationApiDelegate() {} | |
89 | |
90 void SendEvent(const std::string& name, scoped_ptr<ListValue> args) { | |
91 scoped_ptr<Event> event(new Event(name, args.Pass())); | |
92 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | |
93 extension_id_, event.Pass()); | |
94 } | |
95 | |
96 scoped_ptr<ListValue> CreateBaseEventArgs() { | |
97 scoped_ptr<ListValue> args(new ListValue()); | |
98 args->Append(Value::CreateStringValue(id_)); | |
99 return args.Pass(); | |
100 } | |
101 | |
102 scoped_refptr<ApiFunction> api_function_; | |
103 Profile* profile_; | |
104 const std::string extension_id_; | |
105 const std::string id_; | |
106 const std::string scoped_id_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(NotificationApiDelegate); | |
109 }; | |
110 | |
111 } // namespace | |
112 | |
113 NotificationApiFunction::NotificationApiFunction() { | |
114 } | |
115 | |
116 NotificationApiFunction::~NotificationApiFunction() { | |
117 } | |
118 | |
119 message_center::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 message_center::NOTIFICATION_TYPE_SIMPLE; | |
126 case api::experimental_notification::TEMPLATE_TYPE_BASIC: | |
127 return message_center::NOTIFICATION_TYPE_BASE_FORMAT; | |
128 case api::experimental_notification::TEMPLATE_TYPE_IMAGE: | |
129 return message_center::NOTIFICATION_TYPE_IMAGE; | |
130 case api::experimental_notification::TEMPLATE_TYPE_LIST: | |
131 return message_center::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 message_center::NOTIFICATION_TYPE_BASE_FORMAT; | |
136 } | |
137 } | |
138 | |
139 // If older notification runtime is used, MessageCenter is not built. | |
140 // Use simpler bridge then, ignoring all options. | |
141 #if !defined (ENABLE_MESSAGE_CENTER) | |
142 void NotificationApiFunction::CreateNotification( | |
143 const std::string& id, | |
144 api::experimental_notification::NotificationOptions* options) { | |
145 message_center::NotificationType type = | |
146 MapApiTemplateTypeToType(options->template_type); | |
147 GURL icon_url(UTF8ToUTF16(options->icon_url)); | |
148 string16 title(UTF8ToUTF16(options->title)); | |
149 string16 message(UTF8ToUTF16(options->message)); | |
150 | |
151 // Ignore options if running on the old notification runtime. | |
152 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | |
153 | |
154 NotificationApiDelegate* api_delegate(new NotificationApiDelegate( | |
155 this, | |
156 profile(), | |
157 extension_->id(), | |
158 id)); // ownership is passed to Notification | |
159 Notification notification(type, extension_->url(), icon_url, title, message, | |
160 WebKit::WebTextDirectionDefault, | |
161 string16(), UTF8ToUTF16(api_delegate->id()), | |
162 optional_fields.get(), api_delegate); | |
163 | |
164 g_browser_process->notification_ui_manager()->Add(notification, profile()); | |
165 } | |
166 #else // defined(ENABLE_MESSAGE_CENTER) | |
167 void NotificationApiFunction::CreateNotification( | |
168 const std::string& id, | |
169 api::experimental_notification::NotificationOptions* options) { | |
170 message_center::NotificationType type = | |
171 MapApiTemplateTypeToType(options->template_type); | |
172 GURL icon_url(UTF8ToUTF16(options->icon_url)); | |
173 string16 title(UTF8ToUTF16(options->title)); | |
174 string16 message(UTF8ToUTF16(options->message)); | |
175 | |
176 scoped_ptr<DictionaryValue> optional_fields(new DictionaryValue()); | |
177 | |
178 // For all notification types. | |
179 if (options->priority.get()) | |
180 optional_fields->SetInteger(message_center::kPriorityKey, | |
181 *options->priority); | |
182 if (options->event_time.get()) | |
183 optional_fields->SetDouble(message_center::kTimestampKey, | |
184 *options->event_time); | |
185 if (options->buttons.get()) { | |
186 if (options->buttons->size() > 0) { | |
187 linked_ptr<api::experimental_notification::NotificationButton> button = | |
188 (*options->buttons)[0]; | |
189 optional_fields->SetString(message_center::kButtonOneTitleKey, | |
190 UTF8ToUTF16(button->title)); | |
191 if (button->icon_url.get()) | |
192 optional_fields->SetString(message_center::kButtonOneIconUrlKey, | |
193 UTF8ToUTF16(*button->icon_url)); | |
194 } | |
195 if (options->buttons->size() > 1) { | |
196 linked_ptr<api::experimental_notification::NotificationButton> button = | |
197 (*options->buttons)[1]; | |
198 optional_fields->SetString(message_center::kButtonTwoTitleKey, | |
199 UTF8ToUTF16(button->title)); | |
200 if (button->icon_url.get()) | |
201 optional_fields->SetString(message_center::kButtonTwoIconUrlKey, | |
202 UTF8ToUTF16(*button->icon_url)); | |
203 } | |
204 } | |
205 if (options->expanded_message.get()) | |
206 optional_fields->SetString(message_center::kExpandedMessageKey, | |
207 UTF8ToUTF16(*options->expanded_message)); | |
208 | |
209 // For image notifications (type == 'image'). | |
210 // TODO(dharcourt): Fail if (type == 'image' && !options->image_url.get()) | |
211 // TODO(dharcourt): Fail if (type != 'image' && options->image_url.get()) | |
212 if (options->image_url.get()) | |
213 optional_fields->SetString(message_center::kImageUrlKey, | |
214 UTF8ToUTF16(*options->image_url)); | |
215 | |
216 // For list notifications (type == 'multiple'). | |
217 // TODO(dharcourt): Fail if (type == 'multiple' && !options->items.get()) | |
218 // TODO(dharcourt): Fail if (type != 'multiple' && options->items.get()) | |
219 if (options->items.get()) { | |
220 base::ListValue* items = new base::ListValue(); | |
221 std::vector< | |
222 linked_ptr< | |
223 api::experimental_notification::NotificationItem> >::iterator i; | |
224 for (i = options->items->begin(); i != options->items->end(); ++i) { | |
225 base::DictionaryValue* item = new base::DictionaryValue(); | |
226 item->SetString(message_center::kItemTitleKey, | |
227 UTF8ToUTF16(i->get()->title)); | |
228 item->SetString(message_center::kItemMessageKey, | |
229 UTF8ToUTF16(i->get()->message)); | |
230 items->Append(item); | |
231 } | |
232 optional_fields->Set(message_center::kItemsKey, items); | |
233 } | |
234 | |
235 NotificationApiDelegate* api_delegate(new NotificationApiDelegate( | |
236 this, | |
237 profile(), | |
238 extension_->id(), | |
239 id)); // ownership is passed to Notification | |
240 Notification notification(type, extension_->url(), icon_url, title, message, | |
241 WebKit::WebTextDirectionDefault, | |
242 string16(), UTF8ToUTF16(api_delegate->id()), | |
243 optional_fields.get(), api_delegate); | |
244 | |
245 g_browser_process->notification_ui_manager()->Add(notification, profile()); | |
246 } | |
247 #endif // !defined(ENABLE_MESSAGE_CENTER) | |
248 | |
249 bool NotificationApiFunction::IsNotificationApiEnabled() { | |
250 DesktopNotificationService* service = | |
251 DesktopNotificationServiceFactory::GetForProfile(profile()); | |
252 return service->IsExtensionEnabled(extension_->id()); | |
253 } | |
254 | |
255 bool NotificationApiFunction::RunImpl() { | |
256 if (!IsNotificationApiEnabled()) { | |
257 SendResponse(false); | |
258 return true; | |
259 } | |
260 | |
261 return RunNotificationApi(); | |
262 } | |
263 | |
264 const char kNotificationPrefix[] = "extension.api."; | |
265 | |
266 static uint64 next_id_ = 0; | |
267 | |
268 NotificationCreateFunction::NotificationCreateFunction() { | |
269 } | |
270 | |
271 NotificationCreateFunction::~NotificationCreateFunction() { | |
272 } | |
273 | |
274 bool NotificationCreateFunction::RunNotificationApi() { | |
275 params_ = api::experimental_notification::Create::Params::Create(*args_); | |
276 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
277 | |
278 // If the caller provided a notificationId, use that. Otherwise, generate | |
279 // one. Note that there's nothing stopping an app developer from passing in | |
280 // arbitrary "extension.api.999" notificationIds that will collide with | |
281 // future generated IDs. It doesn't seem necessary to try to prevent this; if | |
282 // developers want to hurt themselves, we'll let them. | |
283 const std::string extension_id(extension_->id()); | |
284 std::string notification_id; | |
285 if (!params_->notification_id.empty()) | |
286 notification_id = params_->notification_id; | |
287 else | |
288 notification_id = kNotificationPrefix + base::Uint64ToString(next_id_++); | |
289 | |
290 CreateNotification(notification_id, ¶ms_->options); | |
291 | |
292 SetResult(Value::CreateStringValue(notification_id)); | |
293 | |
294 SendResponse(true); | |
295 | |
296 return true; | |
297 } | |
298 | |
299 NotificationUpdateFunction::NotificationUpdateFunction() { | |
300 } | |
301 | |
302 NotificationUpdateFunction::~NotificationUpdateFunction() { | |
303 } | |
304 | |
305 bool NotificationUpdateFunction::RunNotificationApi() { | |
306 params_ = api::experimental_notification::Update::Params::Create(*args_); | |
307 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
308 | |
309 if (g_browser_process->notification_ui_manager()-> | |
310 DoesIdExist(NotificationApiDelegate::CreateScopedIdentifier( | |
311 extension_->id(), params_->notification_id))) { | |
312 CreateNotification(params_->notification_id, ¶ms_->options); | |
313 SetResult(Value::CreateBooleanValue(true)); | |
314 } else { | |
315 SetResult(Value::CreateBooleanValue(false)); | |
316 } | |
317 | |
318 SendResponse(true); | |
319 | |
320 return true; | |
321 } | |
322 | |
323 NotificationClearFunction::NotificationClearFunction() { | |
324 } | |
325 | |
326 NotificationClearFunction::~NotificationClearFunction() { | |
327 } | |
328 | |
329 bool NotificationClearFunction::RunNotificationApi() { | |
330 params_ = api::experimental_notification::Clear::Params::Create(*args_); | |
331 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
332 | |
333 bool cancel_result = g_browser_process->notification_ui_manager()-> | |
334 CancelById(NotificationApiDelegate::CreateScopedIdentifier( | |
335 extension_->id(), params_->notification_id)); | |
336 | |
337 SetResult(Value::CreateBooleanValue(cancel_result)); | |
338 SendResponse(true); | |
339 | |
340 return true; | |
341 } | |
342 | |
343 } // namespace extensions | |
OLD | NEW |