Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/notifications/notification_platform_bridge_android.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_android.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/nullable_string16.h" | 15 #include "base/strings/nullable_string16.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/notifications/native_notification_display_service.h" | 18 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 19 #include "chrome/browser/notifications/notification.h" | 19 #include "chrome/browser/notifications/notification.h" |
| 20 #include "chrome/browser/notifications/notification_common.h" | 20 #include "chrome/browser/notifications/notification_common.h" |
| 21 #include "chrome/browser/notifications/notification_display_service_factory.h" | 21 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 22 #include "chrome/browser/notifications/notification_platform_types_android.h" | |
| 22 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 23 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 23 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 24 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 24 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" | 28 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "content/public/common/persistent_notification_status.h" | 29 #include "content/public/common/persistent_notification_status.h" |
| 29 #include "content/public/common/platform_notification_data.h" | 30 #include "content/public/common/platform_notification_data.h" |
| 31 #include "jni/ActionInfo_jni.h" | |
| 30 #include "jni/NotificationPlatformBridge_jni.h" | 32 #include "jni/NotificationPlatformBridge_jni.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 33 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "ui/gfx/android/java_bitmap.h" | 34 #include "ui/gfx/android/java_bitmap.h" |
| 33 #include "ui/gfx/image/image.h" | 35 #include "ui/gfx/image/image.h" |
| 34 | 36 |
| 35 using base::android::AttachCurrentThread; | 37 using base::android::AttachCurrentThread; |
| 36 using base::android::ConvertJavaStringToUTF8; | 38 using base::android::ConvertJavaStringToUTF8; |
| 37 using base::android::ConvertJavaStringToUTF16; | 39 using base::android::ConvertJavaStringToUTF16; |
| 38 using base::android::ConvertUTF16ToJavaString; | 40 using base::android::ConvertUTF16ToJavaString; |
| 39 using base::android::ConvertUTF8ToJavaString; | 41 using base::android::ConvertUTF8ToJavaString; |
| 40 using base::android::JavaParamRef; | 42 using base::android::JavaParamRef; |
| 41 using base::android::ScopedJavaLocalRef; | 43 using base::android::ScopedJavaLocalRef; |
| 42 | 44 |
| 43 namespace { | 45 namespace { |
| 44 | 46 |
| 45 ScopedJavaLocalRef<jobjectArray> ConvertToJavaBitmaps( | 47 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(JNIEnv* env, |
| 48 const gfx::Image& icon) { | |
| 49 SkBitmap skbitmap = icon.AsBitmap(); | |
| 50 ScopedJavaLocalRef<jobject> j_bitmap = NULL; | |
|
Peter Beverloo
2016/10/20 15:36:20
nit: no need to assign NULL - that's the default c
awdf
2016/10/20 16:05:39
Done.
| |
| 51 if (!skbitmap.drawsNothing()) { | |
|
Peter Beverloo
2016/10/20 15:36:20
micro nit: no {} for one line statements
awdf
2016/10/20 16:05:39
Done.
| |
| 52 j_bitmap = gfx::ConvertToJavaBitmap(&skbitmap); | |
| 53 } | |
| 54 return j_bitmap; | |
| 55 } | |
| 56 | |
| 57 NotificationActionType GetNotificationActionType( | |
| 58 message_center::ButtonInfo button) { | |
| 59 switch (button.type) { | |
| 60 case message_center::ButtonType::BUTTON: | |
| 61 return NotificationActionType::BUTTON; | |
| 62 case message_center::ButtonType::TEXT: | |
| 63 return NotificationActionType::TEXT; | |
| 64 } | |
|
Peter Beverloo
2016/10/20 15:36:20
Not all compilers like this. Nothing stops a stati
awdf
2016/10/20 16:05:39
Done.
| |
| 65 } | |
| 66 | |
| 67 ScopedJavaLocalRef<jobjectArray> ConvertToJavaActionInfos( | |
| 46 const std::vector<message_center::ButtonInfo>& buttons) { | 68 const std::vector<message_center::ButtonInfo>& buttons) { |
| 47 std::vector<SkBitmap> skbitmaps; | |
| 48 for (const message_center::ButtonInfo& button : buttons) | |
| 49 skbitmaps.push_back(button.icon.AsBitmap()); | |
| 50 | |
| 51 JNIEnv* env = AttachCurrentThread(); | 69 JNIEnv* env = AttachCurrentThread(); |
| 52 ScopedJavaLocalRef<jclass> clazz = | 70 ScopedJavaLocalRef<jclass> clazz = base::android::GetClass( |
| 53 base::android::GetClass(env, "android/graphics/Bitmap"); | 71 env, "org/chromium/chrome/browser/notifications/ActionInfo"); |
| 54 jobjectArray array = env->NewObjectArray(skbitmaps.size(), clazz.obj(), | 72 jobjectArray array = env->NewObjectArray(buttons.size(), clazz.obj(), |
|
Peter Beverloo
2016/10/20 15:36:20
nit: s/array/actions/?
awdf
2016/10/20 16:05:39
Done.
| |
| 55 nullptr /* initialElement */); | 73 nullptr /* initialElement */); |
| 56 base::android::CheckException(env); | 74 base::android::CheckException(env); |
| 57 | 75 |
| 58 for (size_t i = 0; i < skbitmaps.size(); ++i) { | 76 for (size_t i = 0; i < buttons.size(); ++i) { |
| 59 if (!skbitmaps[i].drawsNothing()) { | 77 ScopedJavaLocalRef<jstring> title = |
|
Peter Beverloo
2016/10/20 15:36:20
nit: consider having a const reference to the butt
awdf
2016/10/20 16:05:39
Done.
| |
| 60 env->SetObjectArrayElement( | 78 base::android::ConvertUTF16ToJavaString(env, buttons[i].title); |
| 61 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); | 79 int type = GetNotificationActionType(buttons[i]); |
| 62 } | 80 ScopedJavaLocalRef<jstring> placeholder = |
| 81 base::android::ConvertUTF16ToJavaString(env, buttons[i].placeholder); | |
| 82 ScopedJavaLocalRef<jobject> icon = | |
| 83 ConvertToJavaBitmap(env, buttons[i].icon); | |
| 84 ScopedJavaLocalRef<jobject> action_info = | |
| 85 chrome::Java_ActionInfo_createActionInfo(AttachCurrentThread(), | |
| 86 title.obj(), icon.obj(), type, | |
| 87 placeholder.obj()); | |
| 88 env->SetObjectArrayElement(array, i, action_info.obj()); | |
| 63 } | 89 } |
| 64 | 90 |
| 65 return ScopedJavaLocalRef<jobjectArray>(env, array); | 91 return ScopedJavaLocalRef<jobjectArray>(env, array); |
| 66 } | 92 } |
| 67 | 93 |
| 68 // Callback to run once the profile has been loaded in order to perform a | 94 // Callback to run once the profile has been loaded in order to perform a |
| 69 // given |operation| in a notification. | 95 // given |operation| in a notification. |
| 70 // TODO(miguelg) move it to notification_common? | 96 // TODO(miguelg) move it to notification_common? |
| 71 void ProfileLoadedCallback(NotificationCommon::Operation operation, | 97 void ProfileLoadedCallback(NotificationCommon::Operation operation, |
| 72 NotificationCommon::Type notification_type, | 98 NotificationCommon::Type notification_type, |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 ScopedJavaLocalRef<jobject> notification_icon; | 247 ScopedJavaLocalRef<jobject> notification_icon; |
| 222 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); | 248 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); |
| 223 if (!notification_icon_bitmap.drawsNothing()) | 249 if (!notification_icon_bitmap.drawsNothing()) |
| 224 notification_icon = gfx::ConvertToJavaBitmap(¬ification_icon_bitmap); | 250 notification_icon = gfx::ConvertToJavaBitmap(¬ification_icon_bitmap); |
| 225 | 251 |
| 226 ScopedJavaLocalRef<jobject> badge; | 252 ScopedJavaLocalRef<jobject> badge; |
| 227 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); | 253 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); |
| 228 if (!badge_bitmap.drawsNothing()) | 254 if (!badge_bitmap.drawsNothing()) |
| 229 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); | 255 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); |
| 230 | 256 |
| 231 // TODO(crbug.com/650302): Combine these action_* vectors into a single vector | 257 // TODO(crbug.com/650302): Combine these action_* vectors into a single vector |
|
awdf
2016/10/20 15:20:34
Argh, commented out code - thought I removed this.
| |
| 232 // of objects. | 258 // of objects. |
| 259 /* | |
| 233 std::vector<base::string16> action_titles_vector; | 260 std::vector<base::string16> action_titles_vector; |
| 234 std::vector<base::string16> action_types_vector; | 261 std::vector<base::string16> action_types_vector; |
| 235 std::vector<base::string16> action_placeholders_vector; | 262 std::vector<base::string16> action_placeholders_vector; |
| 236 for (const message_center::ButtonInfo& button : notification.buttons()) { | 263 for (const message_center::ButtonInfo& button : notification.buttons()) { |
| 237 action_titles_vector.push_back(button.title); | 264 action_titles_vector.push_back(button.title); |
| 238 action_placeholders_vector.push_back(button.placeholder); | 265 action_placeholders_vector.push_back(button.placeholder); |
| 239 base::string16 type; | 266 base::string16 type; |
| 240 switch (button.type) { | 267 switch (button.type) { |
| 241 case message_center::ButtonType::BUTTON: | 268 case message_center::ButtonType::BUTTON: |
| 242 type = base::ASCIIToUTF16("button"); | 269 type = base::ASCIIToUTF16("button"); |
| 243 break; | 270 break; |
| 244 case message_center::ButtonType::TEXT: | 271 case message_center::ButtonType::TEXT: |
| 245 type = base::ASCIIToUTF16("text"); | 272 type = base::ASCIIToUTF16("text"); |
| 246 break; | 273 break; |
| 247 } | 274 } |
| 248 action_types_vector.push_back(std::move(type)); | 275 action_types_vector.push_back(std::move(type)); |
| 249 } | 276 } |
| 250 ScopedJavaLocalRef<jobjectArray> action_titles = | 277 ScopedJavaLocalRef<jobjectArray> action_titles = |
| 251 base::android::ToJavaArrayOfStrings(env, action_titles_vector); | 278 base::android::ToJavaArrayOfStrings(env, action_titles_vector); |
| 252 | 279 |
| 253 ScopedJavaLocalRef<jobjectArray> action_types = | 280 ScopedJavaLocalRef<jobjectArray> action_types = |
| 254 base::android::ToJavaArrayOfStrings(env, action_types_vector); | 281 base::android::ToJavaArrayOfStrings(env, action_types_vector); |
| 255 | 282 |
| 256 ScopedJavaLocalRef<jobjectArray> action_placeholders = | 283 ScopedJavaLocalRef<jobjectArray> action_placeholders = |
| 257 base::android::ToJavaArrayOfStrings(env, action_placeholders_vector); | 284 base::android::ToJavaArrayOfStrings(env, action_placeholders_vector); |
| 258 | 285 |
| 259 ScopedJavaLocalRef<jobjectArray> action_icons = | 286 ScopedJavaLocalRef<jobjectArray> action_icons = |
| 260 ConvertToJavaBitmaps(notification.buttons()); | 287 ConvertToJavaBitmaps(notification.buttons()); |
| 288 */ | |
| 289 | |
| 290 ScopedJavaLocalRef<jobjectArray> action_infos = | |
| 291 ConvertToJavaActionInfos(notification.buttons()); | |
| 261 | 292 |
| 262 ScopedJavaLocalRef<jintArray> vibration_pattern = | 293 ScopedJavaLocalRef<jintArray> vibration_pattern = |
| 263 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 294 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
| 264 | 295 |
| 265 ScopedJavaLocalRef<jstring> j_profile_id = | 296 ScopedJavaLocalRef<jstring> j_profile_id = |
| 266 ConvertUTF8ToJavaString(env, profile_id); | 297 ConvertUTF8ToJavaString(env, profile_id); |
| 267 | 298 |
| 268 Java_NotificationPlatformBridge_displayNotification( | 299 Java_NotificationPlatformBridge_displayNotification( |
| 269 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, | 300 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, |
| 270 tag, webapk_package, title, body, image, notification_icon, badge, | 301 tag, webapk_package, title, body, image, notification_icon, badge, |
| 271 vibration_pattern, notification.timestamp().ToJavaTime(), | 302 vibration_pattern, notification.timestamp().ToJavaTime(), |
| 272 notification.renotify(), notification.silent(), action_titles, | 303 notification.renotify(), notification.silent(), action_infos); |
| 273 action_icons, action_types, action_placeholders); | |
| 274 | 304 |
| 275 regenerated_notification_infos_[notification_id] = | 305 regenerated_notification_infos_[notification_id] = |
| 276 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), | 306 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
| 277 ConvertJavaStringToUTF8(env, webapk_package)); | 307 ConvertJavaStringToUTF8(env, webapk_package)); |
| 278 } | 308 } |
| 279 | 309 |
| 280 void NotificationPlatformBridgeAndroid::Close( | 310 void NotificationPlatformBridgeAndroid::Close( |
| 281 const std::string& profile_id, | 311 const std::string& profile_id, |
| 282 const std::string& notification_id) { | 312 const std::string& notification_id) { |
| 283 const auto iterator = regenerated_notification_infos_.find(notification_id); | 313 const auto iterator = regenerated_notification_infos_.find(notification_id); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 RegeneratedNotificationInfo() {} | 361 RegeneratedNotificationInfo() {} |
| 332 | 362 |
| 333 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 363 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
| 334 RegeneratedNotificationInfo(const std::string& origin, | 364 RegeneratedNotificationInfo(const std::string& origin, |
| 335 const std::string& tag, | 365 const std::string& tag, |
| 336 const std::string& webapk_package) | 366 const std::string& webapk_package) |
| 337 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 367 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
| 338 | 368 |
| 339 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 369 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
| 340 ~RegeneratedNotificationInfo() {} | 370 ~RegeneratedNotificationInfo() {} |
| OLD | NEW |