Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_android.cc

Issue 2440483002: Combine action parameters sent to Android displayNotification (Closed)
Patch Set: Comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
51 if (!skbitmap.drawsNothing())
52 j_bitmap = gfx::ConvertToJavaBitmap(&skbitmap);
53 return j_bitmap;
54 }
55
56 NotificationActionType GetNotificationActionType(
57 message_center::ButtonInfo button) {
58 switch (button.type) {
59 case message_center::ButtonType::BUTTON:
60 return NotificationActionType::BUTTON;
61 case message_center::ButtonType::TEXT:
62 return NotificationActionType::TEXT;
63 default:
Peter Beverloo 2016/10/20 16:13:38 Nah, sorry - don't use "default". Basically, if y
64 NOTREACHED();
65 return NotificationActionType::TEXT;
66 }
67 }
68
69 ScopedJavaLocalRef<jobjectArray> ConvertToJavaActionInfos(
46 const std::vector<message_center::ButtonInfo>& buttons) { 70 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(); 71 JNIEnv* env = AttachCurrentThread();
52 ScopedJavaLocalRef<jclass> clazz = 72 ScopedJavaLocalRef<jclass> clazz = base::android::GetClass(
53 base::android::GetClass(env, "android/graphics/Bitmap"); 73 env, "org/chromium/chrome/browser/notifications/ActionInfo");
54 jobjectArray array = env->NewObjectArray(skbitmaps.size(), clazz.obj(), 74 jobjectArray actions = env->NewObjectArray(buttons.size(), clazz.obj(),
55 nullptr /* initialElement */); 75 nullptr /* initialElement */);
56 base::android::CheckException(env); 76 base::android::CheckException(env);
57 77
58 for (size_t i = 0; i < skbitmaps.size(); ++i) { 78 for (size_t i = 0; i < buttons.size(); ++i) {
59 if (!skbitmaps[i].drawsNothing()) { 79 const auto& button = buttons[i];
60 env->SetObjectArrayElement( 80 ScopedJavaLocalRef<jstring> title =
61 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); 81 base::android::ConvertUTF16ToJavaString(env, button.title);
62 } 82 int type = GetNotificationActionType(button);
83 ScopedJavaLocalRef<jstring> placeholder =
84 base::android::ConvertUTF16ToJavaString(env, button.placeholder);
85 ScopedJavaLocalRef<jobject> icon = ConvertToJavaBitmap(env, button.icon);
86 ScopedJavaLocalRef<jobject> action_info =
87 Java_ActionInfo_createActionInfo(AttachCurrentThread(), title.obj(),
88 icon.obj(), type, placeholder.obj());
89 env->SetObjectArrayElement(actions, i, action_info.obj());
63 } 90 }
64 91
65 return ScopedJavaLocalRef<jobjectArray>(env, array); 92 return ScopedJavaLocalRef<jobjectArray>(env, actions);
66 } 93 }
67 94
68 // Callback to run once the profile has been loaded in order to perform a 95 // Callback to run once the profile has been loaded in order to perform a
69 // given |operation| in a notification. 96 // given |operation| in a notification.
70 // TODO(miguelg) move it to notification_common? 97 // TODO(miguelg) move it to notification_common?
71 void ProfileLoadedCallback(NotificationCommon::Operation operation, 98 void ProfileLoadedCallback(NotificationCommon::Operation operation,
72 NotificationCommon::Type notification_type, 99 NotificationCommon::Type notification_type,
73 const std::string& origin, 100 const std::string& origin,
74 const std::string& notification_id, 101 const std::string& notification_id,
75 int action_index, 102 int action_index,
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 ScopedJavaLocalRef<jobject> notification_icon; 248 ScopedJavaLocalRef<jobject> notification_icon;
222 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); 249 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap();
223 if (!notification_icon_bitmap.drawsNothing()) 250 if (!notification_icon_bitmap.drawsNothing())
224 notification_icon = gfx::ConvertToJavaBitmap(&notification_icon_bitmap); 251 notification_icon = gfx::ConvertToJavaBitmap(&notification_icon_bitmap);
225 252
226 ScopedJavaLocalRef<jobject> badge; 253 ScopedJavaLocalRef<jobject> badge;
227 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); 254 SkBitmap badge_bitmap = notification.small_image().AsBitmap();
228 if (!badge_bitmap.drawsNothing()) 255 if (!badge_bitmap.drawsNothing())
229 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); 256 badge = gfx::ConvertToJavaBitmap(&badge_bitmap);
230 257
231 // TODO(crbug.com/650302): Combine these action_* vectors into a single vector 258 ScopedJavaLocalRef<jobjectArray> actions =
232 // of objects. 259 ConvertToJavaActionInfos(notification.buttons());
233 std::vector<base::string16> action_titles_vector;
234 std::vector<base::string16> action_types_vector;
235 std::vector<base::string16> action_placeholders_vector;
236 for (const message_center::ButtonInfo& button : notification.buttons()) {
237 action_titles_vector.push_back(button.title);
238 action_placeholders_vector.push_back(button.placeholder);
239 base::string16 type;
240 switch (button.type) {
241 case message_center::ButtonType::BUTTON:
242 type = base::ASCIIToUTF16("button");
243 break;
244 case message_center::ButtonType::TEXT:
245 type = base::ASCIIToUTF16("text");
246 break;
247 }
248 action_types_vector.push_back(std::move(type));
249 }
250 ScopedJavaLocalRef<jobjectArray> action_titles =
251 base::android::ToJavaArrayOfStrings(env, action_titles_vector);
252
253 ScopedJavaLocalRef<jobjectArray> action_types =
254 base::android::ToJavaArrayOfStrings(env, action_types_vector);
255
256 ScopedJavaLocalRef<jobjectArray> action_placeholders =
257 base::android::ToJavaArrayOfStrings(env, action_placeholders_vector);
258
259 ScopedJavaLocalRef<jobjectArray> action_icons =
260 ConvertToJavaBitmaps(notification.buttons());
261 260
262 ScopedJavaLocalRef<jintArray> vibration_pattern = 261 ScopedJavaLocalRef<jintArray> vibration_pattern =
263 base::android::ToJavaIntArray(env, notification.vibration_pattern()); 262 base::android::ToJavaIntArray(env, notification.vibration_pattern());
264 263
265 ScopedJavaLocalRef<jstring> j_profile_id = 264 ScopedJavaLocalRef<jstring> j_profile_id =
266 ConvertUTF8ToJavaString(env, profile_id); 265 ConvertUTF8ToJavaString(env, profile_id);
267 266
268 Java_NotificationPlatformBridge_displayNotification( 267 Java_NotificationPlatformBridge_displayNotification(
269 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, 268 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito,
270 tag, webapk_package, title, body, image, notification_icon, badge, 269 tag, webapk_package, title, body, image, notification_icon, badge,
271 vibration_pattern, notification.timestamp().ToJavaTime(), 270 vibration_pattern, notification.timestamp().ToJavaTime(),
272 notification.renotify(), notification.silent(), action_titles, 271 notification.renotify(), notification.silent(), actions);
273 action_icons, action_types, action_placeholders);
274 272
275 regenerated_notification_infos_[notification_id] = 273 regenerated_notification_infos_[notification_id] =
276 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), 274 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(),
277 ConvertJavaStringToUTF8(env, webapk_package)); 275 ConvertJavaStringToUTF8(env, webapk_package));
278 } 276 }
279 277
280 void NotificationPlatformBridgeAndroid::Close( 278 void NotificationPlatformBridgeAndroid::Close(
281 const std::string& profile_id, 279 const std::string& profile_id,
282 const std::string& notification_id) { 280 const std::string& notification_id) {
283 const auto iterator = regenerated_notification_infos_.find(notification_id); 281 const auto iterator = regenerated_notification_infos_.find(notification_id);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 RegeneratedNotificationInfo() {} 329 RegeneratedNotificationInfo() {}
332 330
333 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: 331 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo::
334 RegeneratedNotificationInfo(const std::string& origin, 332 RegeneratedNotificationInfo(const std::string& origin,
335 const std::string& tag, 333 const std::string& tag,
336 const std::string& webapk_package) 334 const std::string& webapk_package)
337 : origin(origin), tag(tag), webapk_package(webapk_package) {} 335 : origin(origin), tag(tag), webapk_package(webapk_package) {}
338 336
339 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: 337 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo::
340 ~RegeneratedNotificationInfo() {} 338 ~RegeneratedNotificationInfo() {}
OLDNEW
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/notifications/notification_platform_types_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698