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/guid.h" | 8 #include "base/guid.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 float image_scale = | 176 float image_scale = |
177 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 177 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
178 | 178 |
179 // Extract required fields: type, title, message, and icon. | 179 // Extract required fields: type, title, message, and icon. |
180 message_center::NotificationType type = | 180 message_center::NotificationType type = |
181 MapApiTemplateTypeToType(options->type); | 181 MapApiTemplateTypeToType(options->type); |
182 const base::string16 title(base::UTF8ToUTF16(*options->title)); | 182 const base::string16 title(base::UTF8ToUTF16(*options->title)); |
183 const base::string16 message(base::UTF8ToUTF16(*options->message)); | 183 const base::string16 message(base::UTF8ToUTF16(*options->message)); |
184 gfx::Image icon; | 184 gfx::Image icon; |
185 | 185 |
186 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 186 if (options->icon_bitmap.get() && |
187 image_scale, | 187 !NotificationConversionHelper::NotificationBitmapToGfxImage( |
188 bitmap_sizes.icon_size, | 188 image_scale, bitmap_sizes.icon_size, options->icon_bitmap.get(), |
189 options->icon_bitmap.get(), | |
190 &icon)) { | 189 &icon)) { |
191 SetError(kUnableToDecodeIconError); | 190 SetError(kUnableToDecodeIconError); |
192 return false; | 191 return false; |
193 } | 192 } |
194 | 193 |
195 // Then, handle any optional data that's been provided. | 194 // Then, handle any optional data that's been provided. |
196 message_center::RichNotificationData optional_fields; | 195 message_center::RichNotificationData optional_fields; |
197 if (options->app_icon_mask_url.get()) { | 196 if (options->app_icon_mask_bitmap.get() && |
198 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( | 197 !NotificationConversionHelper::NotificationBitmapToGfxImage( |
199 image_scale, | 198 image_scale, bitmap_sizes.app_icon_mask_size, |
200 bitmap_sizes.app_icon_mask_size, | 199 options->app_icon_mask_bitmap.get(), &optional_fields.small_image)) { |
201 options->app_icon_mask_bitmap.get(), | |
202 &optional_fields.small_image)) { | |
203 SetError(kUnableToDecodeIconError); | 200 SetError(kUnableToDecodeIconError); |
204 return false; | 201 return false; |
205 } | |
206 } | 202 } |
207 | 203 |
208 if (options->priority.get()) | 204 if (options->priority.get()) |
209 optional_fields.priority = *options->priority; | 205 optional_fields.priority = *options->priority; |
210 | 206 |
211 if (options->event_time.get()) | 207 if (options->event_time.get()) |
212 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); | 208 optional_fields.timestamp = base::Time::FromJsTime(*options->event_time); |
213 | 209 |
214 if (options->buttons.get()) { | 210 if (options->buttons.get()) { |
215 // Currently we allow up to 2 buttons. | 211 // Currently we allow up to 2 buttons. |
216 size_t number_of_buttons = options->buttons->size(); | 212 size_t number_of_buttons = options->buttons->size(); |
217 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 213 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
218 | 214 |
219 for (size_t i = 0; i < number_of_buttons; i++) { | 215 for (size_t i = 0; i < number_of_buttons; i++) { |
220 message_center::ButtonInfo info( | 216 message_center::ButtonInfo info( |
221 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 217 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
222 NotificationConversionHelper::NotificationBitmapToGfxImage( | 218 if ((*options->buttons)[i]->icon_bitmap.get()) { |
223 image_scale, | 219 NotificationConversionHelper::NotificationBitmapToGfxImage( |
224 bitmap_sizes.button_icon_size, | 220 image_scale, bitmap_sizes.button_icon_size, |
225 (*options->buttons)[i]->icon_bitmap.get(), | 221 (*options->buttons)[i]->icon_bitmap.get(), &info.icon); |
226 &info.icon); | 222 optional_fields.buttons.push_back(info); |
227 optional_fields.buttons.push_back(info); | 223 } |
228 } | 224 } |
229 } | 225 } |
230 | 226 |
231 if (options->context_message) { | 227 if (options->context_message) { |
232 optional_fields.context_message = | 228 optional_fields.context_message = |
233 base::UTF8ToUTF16(*options->context_message); | 229 base::UTF8ToUTF16(*options->context_message); |
234 } | 230 } |
235 | 231 |
236 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 232 if (options->image_bitmap.get()) { |
237 image_scale, | 233 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( |
238 bitmap_sizes.image_size, | 234 image_scale, bitmap_sizes.image_size, options->image_bitmap.get(), |
239 options->image_bitmap.get(), | 235 &optional_fields.image); |
240 &optional_fields.image); | 236 // We should have an image if and only if the type is an image type. |
241 // We should have an image if and only if the type is an image type. | 237 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { |
242 if (has_image != (type == message_center::NOTIFICATION_TYPE_IMAGE)) { | 238 SetError(kExtraImageProvided); |
243 SetError(kExtraImageProvided); | 239 return false; |
244 return false; | 240 } |
245 } | 241 } |
246 | 242 |
247 // We should have list items if and only if the type is a multiple type. | 243 // We should have list items if and only if the type is a multiple type. |
248 bool has_list_items = options->items.get() && options->items->size() > 0; | 244 bool has_list_items = options->items.get() && options->items->size() > 0; |
249 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { | 245 if (has_list_items != (type == message_center::NOTIFICATION_TYPE_MULTIPLE)) { |
250 SetError(kExtraListItemsProvided); | 246 SetError(kExtraListItemsProvided); |
251 return false; | 247 return false; |
252 } | 248 } |
253 | 249 |
254 if (options->progress.get() != NULL) { | 250 if (options->progress.get() != NULL) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); | 304 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactors().back()); |
309 | 305 |
310 // Update optional fields if provided. | 306 // Update optional fields if provided. |
311 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) | 307 if (options->type != api::notifications::TEMPLATE_TYPE_NONE) |
312 notification->set_type(MapApiTemplateTypeToType(options->type)); | 308 notification->set_type(MapApiTemplateTypeToType(options->type)); |
313 if (options->title) | 309 if (options->title) |
314 notification->set_title(base::UTF8ToUTF16(*options->title)); | 310 notification->set_title(base::UTF8ToUTF16(*options->title)); |
315 if (options->message) | 311 if (options->message) |
316 notification->set_message(base::UTF8ToUTF16(*options->message)); | 312 notification->set_message(base::UTF8ToUTF16(*options->message)); |
317 | 313 |
318 // TODO(dewittj): Return error if this fails. | 314 if (options->icon_bitmap.get()) { |
dewittj
2015/05/22 16:42:23
icon is required, we need to set an error if there
Deepak
2015/05/23 14:03:40
When I return error code on failing of this if (op
| |
319 if (options->icon_bitmap) { | |
320 gfx::Image icon; | 315 gfx::Image icon; |
321 NotificationConversionHelper::NotificationBitmapToGfxImage( | 316 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
322 image_scale, bitmap_sizes.icon_size, options->icon_bitmap.get(), &icon); | 317 image_scale, bitmap_sizes.icon_size, options->icon_bitmap.get(), |
318 &icon)) { | |
319 SetError(kUnableToDecodeIconError); | |
320 return false; | |
321 } | |
323 notification->set_icon(icon); | 322 notification->set_icon(icon); |
324 } | 323 } |
325 | 324 |
326 gfx::Image app_icon_mask; | 325 if (options->app_icon_mask_bitmap.get()) { |
327 if (NotificationConversionHelper::NotificationBitmapToGfxImage( | 326 gfx::Image app_icon_mask; |
328 image_scale, | 327 if (!NotificationConversionHelper::NotificationBitmapToGfxImage( |
329 bitmap_sizes.app_icon_mask_size, | 328 image_scale, bitmap_sizes.app_icon_mask_size, |
330 options->app_icon_mask_bitmap.get(), | 329 options->app_icon_mask_bitmap.get(), &app_icon_mask)) { |
331 &app_icon_mask)) { | 330 SetError(kUnableToDecodeIconError); |
331 return false; | |
332 } | |
332 notification->set_small_image(app_icon_mask); | 333 notification->set_small_image(app_icon_mask); |
333 } | 334 } |
334 | 335 |
335 if (options->priority) | 336 if (options->priority) |
336 notification->set_priority(*options->priority); | 337 notification->set_priority(*options->priority); |
337 | 338 |
338 if (options->event_time) | 339 if (options->event_time) |
339 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); | 340 notification->set_timestamp(base::Time::FromJsTime(*options->event_time)); |
340 | 341 |
341 if (options->buttons) { | 342 if (options->buttons) { |
342 // Currently we allow up to 2 buttons. | 343 // Currently we allow up to 2 buttons. |
343 size_t number_of_buttons = options->buttons->size(); | 344 size_t number_of_buttons = options->buttons->size(); |
344 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; | 345 number_of_buttons = number_of_buttons > 2 ? 2 : number_of_buttons; |
345 | 346 |
346 std::vector<message_center::ButtonInfo> buttons; | 347 std::vector<message_center::ButtonInfo> buttons; |
347 for (size_t i = 0; i < number_of_buttons; i++) { | 348 for (size_t i = 0; i < number_of_buttons; i++) { |
348 message_center::ButtonInfo button( | 349 message_center::ButtonInfo button( |
349 base::UTF8ToUTF16((*options->buttons)[i]->title)); | 350 base::UTF8ToUTF16((*options->buttons)[i]->title)); |
350 NotificationConversionHelper::NotificationBitmapToGfxImage( | 351 if ((*options->buttons)[i]->icon_bitmap.get()) { |
351 image_scale, | 352 NotificationConversionHelper::NotificationBitmapToGfxImage( |
352 bitmap_sizes.button_icon_size, | 353 image_scale, bitmap_sizes.button_icon_size, |
353 (*options->buttons)[i]->icon_bitmap.get(), | 354 (*options->buttons)[i]->icon_bitmap.get(), &button.icon); |
354 &button.icon); | 355 buttons.push_back(button); |
355 buttons.push_back(button); | 356 } |
356 } | 357 } |
357 notification->set_buttons(buttons); | 358 notification->set_buttons(buttons); |
358 } | 359 } |
359 | 360 |
360 if (options->context_message) { | 361 if (options->context_message) { |
361 notification->set_context_message( | 362 notification->set_context_message( |
362 base::UTF8ToUTF16(*options->context_message)); | 363 base::UTF8ToUTF16(*options->context_message)); |
363 } | 364 } |
364 | 365 |
365 gfx::Image image; | 366 if (options->image_bitmap.get()) { |
dewittj
2015/05/22 16:42:23
If the type is 'image', then an image bitmap is re
Deepak
2015/05/23 14:03:40
Done.
| |
366 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( | 367 gfx::Image image; |
367 image_scale, | 368 bool has_image = NotificationConversionHelper::NotificationBitmapToGfxImage( |
368 bitmap_sizes.image_size, | 369 image_scale, bitmap_sizes.image_size, options->image_bitmap.get(), |
369 options->image_bitmap.get(), | 370 &image); |
370 &image); | 371 if (!has_image) { |
371 if (has_image) { | 372 SetError(kUnableToDecodeIconError); |
373 return false; | |
374 } | |
372 // We should have an image if and only if the type is an image type. | 375 // We should have an image if and only if the type is an image type. |
373 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { | 376 if (notification->type() != message_center::NOTIFICATION_TYPE_IMAGE) { |
374 SetError(kExtraImageProvided); | 377 SetError(kExtraImageProvided); |
375 return false; | 378 return false; |
376 } | 379 } |
377 notification->set_image(image); | 380 notification->set_image(image); |
378 } | 381 } |
379 | 382 |
380 if (options->progress) { | 383 if (options->progress) { |
381 // We should have progress if and only if the type is a progress type. | 384 // We should have progress if and only if the type is a progress type. |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 ? api::notifications::PERMISSION_LEVEL_GRANTED | 603 ? api::notifications::PERMISSION_LEVEL_GRANTED |
601 : api::notifications::PERMISSION_LEVEL_DENIED; | 604 : api::notifications::PERMISSION_LEVEL_DENIED; |
602 | 605 |
603 SetResult(new base::StringValue(api::notifications::ToString(result))); | 606 SetResult(new base::StringValue(api::notifications::ToString(result))); |
604 SendResponse(true); | 607 SendResponse(true); |
605 | 608 |
606 return true; | 609 return true; |
607 } | 610 } |
608 | 611 |
609 } // namespace extensions | 612 } // namespace extensions |
OLD | NEW |