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/bind.h" |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/strings/nullable_string16.h" | 16 #include "base/strings/nullable_string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/notifications/notification.h" | 19 #include "chrome/browser/notifications/notification.h" |
19 #include "chrome/browser/notifications/notification_common.h" | 20 #include "chrome/browser/notifications/notification_common.h" |
20 #include "chrome/browser/notifications/notification_display_service.h" | 21 #include "chrome/browser/notifications/notification_display_service.h" |
21 #include "chrome/browser/notifications/notification_display_service_factory.h" | 22 #include "chrome/browser/notifications/notification_display_service_factory.h" |
(...skipping 14 matching lines...) Expand all Loading... |
36 using base::android::AttachCurrentThread; | 37 using base::android::AttachCurrentThread; |
37 using base::android::ConvertJavaStringToUTF8; | 38 using base::android::ConvertJavaStringToUTF8; |
38 using base::android::ConvertJavaStringToUTF16; | 39 using base::android::ConvertJavaStringToUTF16; |
39 using base::android::ConvertUTF16ToJavaString; | 40 using base::android::ConvertUTF16ToJavaString; |
40 using base::android::ConvertUTF8ToJavaString; | 41 using base::android::ConvertUTF8ToJavaString; |
41 using base::android::JavaParamRef; | 42 using base::android::JavaParamRef; |
42 using base::android::ScopedJavaLocalRef; | 43 using base::android::ScopedJavaLocalRef; |
43 | 44 |
44 namespace { | 45 namespace { |
45 | 46 |
| 47 // Callback to call after querying WebAPK package name is done. |
| 48 // The parameter is the package name of the WebAPK that matches the query. |
| 49 using QueryWebApkPackageCallback = base::Callback<void(const std::string&)>; |
| 50 |
46 // A Java counterpart will be generated for this enum. | 51 // A Java counterpart will be generated for this enum. |
47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications | 52 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications |
48 enum NotificationActionType { | 53 enum NotificationActionType { |
49 // NB. Making this a one-line enum breaks code generation! crbug.com/657847 | 54 // NB. Making this a one-line enum breaks code generation! crbug.com/657847 |
50 BUTTON, | 55 BUTTON, |
51 TEXT | 56 TEXT |
52 }; | 57 }; |
53 | 58 |
54 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(JNIEnv* env, | 59 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(JNIEnv* env, |
55 const gfx::Image& icon) { | 60 const gfx::Image& icon) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 JNIEnv* env, | 137 JNIEnv* env, |
133 const JavaParamRef<jclass>& clazz) { | 138 const JavaParamRef<jclass>& clazz) { |
134 g_browser_process->notification_platform_bridge(); | 139 g_browser_process->notification_platform_bridge(); |
135 } | 140 } |
136 | 141 |
137 // static | 142 // static |
138 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 143 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
139 return new NotificationPlatformBridgeAndroid(); | 144 return new NotificationPlatformBridgeAndroid(); |
140 } | 145 } |
141 | 146 |
142 NotificationPlatformBridgeAndroid::NotificationPlatformBridgeAndroid() { | 147 NotificationPlatformBridgeAndroid::NotificationPlatformBridgeAndroid() |
| 148 : weak_ptr_factory_(this) { |
143 java_object_.Reset(Java_NotificationPlatformBridge_create( | 149 java_object_.Reset(Java_NotificationPlatformBridge_create( |
144 AttachCurrentThread(), reinterpret_cast<intptr_t>(this))); | 150 AttachCurrentThread(), reinterpret_cast<intptr_t>(this))); |
145 } | 151 } |
146 | 152 |
147 NotificationPlatformBridgeAndroid::~NotificationPlatformBridgeAndroid() { | 153 NotificationPlatformBridgeAndroid::~NotificationPlatformBridgeAndroid() { |
148 Java_NotificationPlatformBridge_destroy(AttachCurrentThread(), java_object_); | 154 Java_NotificationPlatformBridge_destroy(AttachCurrentThread(), java_object_); |
149 } | 155 } |
150 | 156 |
151 void NotificationPlatformBridgeAndroid::OnNotificationClicked( | 157 void NotificationPlatformBridgeAndroid::OnNotificationClicked( |
152 JNIEnv* env, | 158 JNIEnv* env, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 const std::string& notification_id, | 223 const std::string& notification_id, |
218 const std::string& profile_id, | 224 const std::string& profile_id, |
219 bool incognito, | 225 bool incognito, |
220 const Notification& notification) { | 226 const Notification& notification) { |
221 JNIEnv* env = AttachCurrentThread(); | 227 JNIEnv* env = AttachCurrentThread(); |
222 // TODO(miguelg): Store the notification type in java instead of assuming it's | 228 // TODO(miguelg): Store the notification type in java instead of assuming it's |
223 // persistent once/if non persistent notifications are ever implemented on | 229 // persistent once/if non persistent notifications are ever implemented on |
224 // Android. | 230 // Android. |
225 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); | 231 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); |
226 | 232 |
227 GURL origin_url(notification.origin_url().GetOrigin()); | |
228 | |
229 GURL scope_url(notification.service_worker_scope()); | 233 GURL scope_url(notification.service_worker_scope()); |
230 if (!scope_url.is_valid()) | 234 if (!scope_url.is_valid()) |
231 scope_url = origin_url; | 235 scope_url = GURL(notification.origin_url().GetOrigin()); |
232 ScopedJavaLocalRef<jstring> j_scope_url = | 236 ScopedJavaLocalRef<jstring> j_scope_url = |
233 ConvertUTF8ToJavaString(env, scope_url.spec()); | 237 ConvertUTF8ToJavaString(env, scope_url.spec()); |
234 ScopedJavaLocalRef<jstring> webapk_package = | 238 uintptr_t callback_pointer = |
235 Java_NotificationPlatformBridge_queryWebApkPackage( | 239 reinterpret_cast<uintptr_t>(new QueryWebApkPackageCallback(base::Bind( |
236 env, java_object_, j_scope_url); | 240 &NotificationPlatformBridgeAndroid::OnQueryWebApkPackageInternal, |
| 241 weak_ptr_factory_.GetWeakPtr(), notification_id, profile_id, |
| 242 incognito, notification))); |
| 243 Java_NotificationPlatformBridge_queryWebApkPackage( |
| 244 env, java_object_, j_scope_url, callback_pointer); |
| 245 } |
237 | 246 |
| 247 void NotificationPlatformBridgeAndroid::OnQueryWebApkPackageInternal( |
| 248 const std::string& notification_id, |
| 249 const std::string& profile_id, |
| 250 bool incognito, |
| 251 const Notification& notification, |
| 252 const std::string& webapk_package) { |
| 253 JNIEnv* env = AttachCurrentThread(); |
238 ScopedJavaLocalRef<jstring> j_notification_id = | 254 ScopedJavaLocalRef<jstring> j_notification_id = |
239 ConvertUTF8ToJavaString(env, notification_id); | 255 ConvertUTF8ToJavaString(env, notification_id); |
| 256 |
| 257 GURL origin_url(notification.origin_url().GetOrigin()); |
240 ScopedJavaLocalRef<jstring> j_origin = | 258 ScopedJavaLocalRef<jstring> j_origin = |
241 ConvertUTF8ToJavaString(env, origin_url.spec()); | 259 ConvertUTF8ToJavaString(env, origin_url.spec()); |
242 ScopedJavaLocalRef<jstring> tag = | 260 ScopedJavaLocalRef<jstring> tag = |
243 ConvertUTF8ToJavaString(env, notification.tag()); | 261 ConvertUTF8ToJavaString(env, notification.tag()); |
244 ScopedJavaLocalRef<jstring> title = | 262 ScopedJavaLocalRef<jstring> title = |
245 ConvertUTF16ToJavaString(env, notification.title()); | 263 ConvertUTF16ToJavaString(env, notification.title()); |
246 ScopedJavaLocalRef<jstring> body = | 264 ScopedJavaLocalRef<jstring> body = |
247 ConvertUTF16ToJavaString(env, notification.message()); | 265 ConvertUTF16ToJavaString(env, notification.message()); |
248 | 266 |
249 ScopedJavaLocalRef<jobject> image; | 267 ScopedJavaLocalRef<jobject> image; |
(...skipping 12 matching lines...) Expand all Loading... |
262 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); | 280 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); |
263 | 281 |
264 ScopedJavaLocalRef<jobjectArray> actions = | 282 ScopedJavaLocalRef<jobjectArray> actions = |
265 ConvertToJavaActionInfos(notification.buttons()); | 283 ConvertToJavaActionInfos(notification.buttons()); |
266 | 284 |
267 ScopedJavaLocalRef<jintArray> vibration_pattern = | 285 ScopedJavaLocalRef<jintArray> vibration_pattern = |
268 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 286 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
269 | 287 |
270 ScopedJavaLocalRef<jstring> j_profile_id = | 288 ScopedJavaLocalRef<jstring> j_profile_id = |
271 ConvertUTF8ToJavaString(env, profile_id); | 289 ConvertUTF8ToJavaString(env, profile_id); |
| 290 ScopedJavaLocalRef<jstring> j_webapk_package = |
| 291 ConvertUTF8ToJavaString(env, webapk_package); |
272 | 292 |
273 Java_NotificationPlatformBridge_displayNotification( | 293 Java_NotificationPlatformBridge_displayNotification( |
274 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, | 294 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, |
275 tag, webapk_package, title, body, image, notification_icon, badge, | 295 tag, j_webapk_package, title, body, image, notification_icon, badge, |
276 vibration_pattern, notification.timestamp().ToJavaTime(), | 296 vibration_pattern, notification.timestamp().ToJavaTime(), |
277 notification.renotify(), notification.silent(), actions); | 297 notification.renotify(), notification.silent(), actions); |
278 | 298 |
279 regenerated_notification_infos_[notification_id] = | 299 regenerated_notification_infos_[notification_id] = |
280 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), | 300 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
281 ConvertJavaStringToUTF8(env, webapk_package)); | 301 webapk_package); |
| 302 } |
| 303 |
| 304 void NotificationPlatformBridgeAndroid::OnQueryWebApkPackage( |
| 305 JNIEnv* env, |
| 306 const JavaParamRef<jobject>& java_object, |
| 307 const JavaParamRef<jstring>& java_webapk_package, |
| 308 const jlong jcallback_pointer) { |
| 309 DCHECK(jcallback_pointer); |
| 310 std::string webapk_package = |
| 311 ConvertJavaStringToUTF8(env, java_webapk_package); |
| 312 QueryWebApkPackageCallback* callback = |
| 313 reinterpret_cast<QueryWebApkPackageCallback*>(jcallback_pointer); |
| 314 callback->Run(webapk_package); |
| 315 delete callback; |
282 } | 316 } |
283 | 317 |
284 void NotificationPlatformBridgeAndroid::Close( | 318 void NotificationPlatformBridgeAndroid::Close( |
285 const std::string& profile_id, | 319 const std::string& profile_id, |
286 const std::string& notification_id) { | 320 const std::string& notification_id) { |
287 const auto iterator = regenerated_notification_infos_.find(notification_id); | 321 const auto iterator = regenerated_notification_infos_.find(notification_id); |
288 if (iterator == regenerated_notification_infos_.end()) | 322 if (iterator == regenerated_notification_infos_.end()) |
289 return; | 323 return; |
290 | 324 |
291 const RegeneratedNotificationInfo& notification_info = iterator->second; | 325 const RegeneratedNotificationInfo& notification_info = iterator->second; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 RegeneratedNotificationInfo() {} | 377 RegeneratedNotificationInfo() {} |
344 | 378 |
345 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 379 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
346 RegeneratedNotificationInfo(const std::string& origin, | 380 RegeneratedNotificationInfo(const std::string& origin, |
347 const std::string& tag, | 381 const std::string& tag, |
348 const std::string& webapk_package) | 382 const std::string& webapk_package) |
349 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 383 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
350 | 384 |
351 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 385 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
352 ~RegeneratedNotificationInfo() {} | 386 ~RegeneratedNotificationInfo() {} |
OLD | NEW |