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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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()); | 233 GURL origin_url(notification.origin_url().GetOrigin()); |
228 | 234 |
229 GURL scope_url(notification.service_worker_scope()); | 235 GURL scope_url(notification.service_worker_scope()); |
230 if (!scope_url.is_valid()) | 236 if (!scope_url.is_valid()) |
231 scope_url = origin_url; | 237 scope_url = origin_url; |
pkotwicz
2017/07/10 19:06:06
Nit: Can you remove |origin_url| and initialize |s
Xi Han
2017/07/12 13:49:14
Done.
| |
232 ScopedJavaLocalRef<jstring> j_scope_url = | 238 ScopedJavaLocalRef<jstring> j_scope_url = |
233 ConvertUTF8ToJavaString(env, scope_url.spec()); | 239 ConvertUTF8ToJavaString(env, scope_url.spec()); |
234 ScopedJavaLocalRef<jstring> webapk_package = | 240 uintptr_t callback_pointer = |
235 Java_NotificationPlatformBridge_queryWebApkPackage( | 241 reinterpret_cast<uintptr_t>(new QueryWebApkPackageCallback(base::Bind( |
236 env, java_object_, j_scope_url); | 242 &NotificationPlatformBridgeAndroid::OnQueryWebApkPackageInternal, |
243 weak_ptr_factory_.GetWeakPtr(), notification_id, profile_id, | |
244 incognito, notification))); | |
245 Java_NotificationPlatformBridge_queryWebApkPackage( | |
246 env, java_object_, j_scope_url, callback_pointer); | |
247 } | |
237 | 248 |
249 void NotificationPlatformBridgeAndroid::OnQueryWebApkPackageInternal( | |
250 const std::string& notification_id, | |
251 const std::string& profile_id, | |
252 bool incognito, | |
253 const Notification& notification, | |
254 const std::string& webapk_package) { | |
255 JNIEnv* env = AttachCurrentThread(); | |
238 ScopedJavaLocalRef<jstring> j_notification_id = | 256 ScopedJavaLocalRef<jstring> j_notification_id = |
239 ConvertUTF8ToJavaString(env, notification_id); | 257 ConvertUTF8ToJavaString(env, notification_id); |
258 | |
259 GURL origin_url(notification.origin_url().GetOrigin()); | |
240 ScopedJavaLocalRef<jstring> j_origin = | 260 ScopedJavaLocalRef<jstring> j_origin = |
241 ConvertUTF8ToJavaString(env, origin_url.spec()); | 261 ConvertUTF8ToJavaString(env, origin_url.spec()); |
242 ScopedJavaLocalRef<jstring> tag = | 262 ScopedJavaLocalRef<jstring> tag = |
243 ConvertUTF8ToJavaString(env, notification.tag()); | 263 ConvertUTF8ToJavaString(env, notification.tag()); |
244 ScopedJavaLocalRef<jstring> title = | 264 ScopedJavaLocalRef<jstring> title = |
245 ConvertUTF16ToJavaString(env, notification.title()); | 265 ConvertUTF16ToJavaString(env, notification.title()); |
246 ScopedJavaLocalRef<jstring> body = | 266 ScopedJavaLocalRef<jstring> body = |
247 ConvertUTF16ToJavaString(env, notification.message()); | 267 ConvertUTF16ToJavaString(env, notification.message()); |
248 | 268 |
249 ScopedJavaLocalRef<jobject> image; | 269 ScopedJavaLocalRef<jobject> image; |
(...skipping 12 matching lines...) Expand all Loading... | |
262 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); | 282 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); |
263 | 283 |
264 ScopedJavaLocalRef<jobjectArray> actions = | 284 ScopedJavaLocalRef<jobjectArray> actions = |
265 ConvertToJavaActionInfos(notification.buttons()); | 285 ConvertToJavaActionInfos(notification.buttons()); |
266 | 286 |
267 ScopedJavaLocalRef<jintArray> vibration_pattern = | 287 ScopedJavaLocalRef<jintArray> vibration_pattern = |
268 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 288 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
269 | 289 |
270 ScopedJavaLocalRef<jstring> j_profile_id = | 290 ScopedJavaLocalRef<jstring> j_profile_id = |
271 ConvertUTF8ToJavaString(env, profile_id); | 291 ConvertUTF8ToJavaString(env, profile_id); |
292 ScopedJavaLocalRef<jstring> j_webapk_package = | |
293 ConvertUTF8ToJavaString(env, webapk_package); | |
272 | 294 |
273 Java_NotificationPlatformBridge_displayNotification( | 295 Java_NotificationPlatformBridge_displayNotification( |
274 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, | 296 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, |
275 tag, webapk_package, title, body, image, notification_icon, badge, | 297 tag, j_webapk_package, title, body, image, notification_icon, badge, |
276 vibration_pattern, notification.timestamp().ToJavaTime(), | 298 vibration_pattern, notification.timestamp().ToJavaTime(), |
277 notification.renotify(), notification.silent(), actions); | 299 notification.renotify(), notification.silent(), actions); |
278 | 300 |
279 regenerated_notification_infos_[notification_id] = | 301 regenerated_notification_infos_[notification_id] = |
280 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), | 302 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
281 ConvertJavaStringToUTF8(env, webapk_package)); | 303 webapk_package); |
304 } | |
305 | |
306 void NotificationPlatformBridgeAndroid::OnQueryWebApkPackage( | |
307 JNIEnv* env, | |
308 const JavaParamRef<jobject>& java_object, | |
309 const JavaParamRef<jstring>& java_webapk_package, | |
310 const jlong jcallback_pointer) { | |
311 DCHECK(jcallback_pointer); | |
312 std::string webapk_package = | |
313 ConvertJavaStringToUTF8(env, java_webapk_package); | |
314 QueryWebApkPackageCallback* callback = | |
315 reinterpret_cast<QueryWebApkPackageCallback*>(jcallback_pointer); | |
316 callback->Run(webapk_package); | |
317 delete callback; | |
282 } | 318 } |
283 | 319 |
284 void NotificationPlatformBridgeAndroid::Close( | 320 void NotificationPlatformBridgeAndroid::Close( |
285 const std::string& profile_id, | 321 const std::string& profile_id, |
286 const std::string& notification_id) { | 322 const std::string& notification_id) { |
287 const auto iterator = regenerated_notification_infos_.find(notification_id); | 323 const auto iterator = regenerated_notification_infos_.find(notification_id); |
288 if (iterator == regenerated_notification_infos_.end()) | 324 if (iterator == regenerated_notification_infos_.end()) |
289 return; | 325 return; |
290 | 326 |
291 const RegeneratedNotificationInfo& notification_info = iterator->second; | 327 const RegeneratedNotificationInfo& notification_info = iterator->second; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 RegeneratedNotificationInfo() {} | 379 RegeneratedNotificationInfo() {} |
344 | 380 |
345 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 381 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
346 RegeneratedNotificationInfo(const std::string& origin, | 382 RegeneratedNotificationInfo(const std::string& origin, |
347 const std::string& tag, | 383 const std::string& tag, |
348 const std::string& webapk_package) | 384 const std::string& webapk_package) |
349 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 385 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
350 | 386 |
351 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 387 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
352 ~RegeneratedNotificationInfo() {} | 388 ~RegeneratedNotificationInfo() {} |
OLD | NEW |