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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 DCHECK(profile_manager); | 211 DCHECK(profile_manager); |
206 | 212 |
207 profile_manager->LoadProfile( | 213 profile_manager->LoadProfile( |
208 profile_id, incognito, | 214 profile_id, incognito, |
209 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLOSE, | 215 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLOSE, |
210 NotificationCommon::PERSISTENT, | 216 NotificationCommon::PERSISTENT, |
211 ConvertJavaStringToUTF8(env, java_origin), notification_id, | 217 ConvertJavaStringToUTF8(env, java_origin), notification_id, |
212 -1 /* action index */, base::NullableString16() /* reply */)); | 218 -1 /* action index */, base::NullableString16() /* reply */)); |
213 } | 219 } |
214 | 220 |
221 void NotificationPlatformBridgeAndroid::OnQueryWebApkPackage( | |
222 JNIEnv* env, | |
223 const JavaParamRef<jobject>& java_object, | |
224 const JavaParamRef<jstring>& java_webapk_package, | |
225 const jlong jcallback_pointer) { | |
226 DCHECK(jcallback_pointer); | |
227 std::string webapk_package = | |
228 ConvertJavaStringToUTF8(env, java_webapk_package); | |
229 QueryWebApkPackageCallback* callback = | |
230 reinterpret_cast<QueryWebApkPackageCallback*>(jcallback_pointer); | |
231 callback->Run(webapk_package); | |
232 delete callback; | |
233 } | |
234 | |
215 void NotificationPlatformBridgeAndroid::Display( | 235 void NotificationPlatformBridgeAndroid::Display( |
216 NotificationCommon::Type notification_type, | 236 NotificationCommon::Type notification_type, |
217 const std::string& notification_id, | 237 const std::string& notification_id, |
218 const std::string& profile_id, | 238 const std::string& profile_id, |
219 bool incognito, | 239 bool incognito, |
220 const Notification& notification) { | 240 const Notification& notification) { |
221 JNIEnv* env = AttachCurrentThread(); | 241 JNIEnv* env = AttachCurrentThread(); |
222 // TODO(miguelg): Store the notification type in java instead of assuming it's | 242 // TODO(miguelg): Store the notification type in java instead of assuming it's |
223 // persistent once/if non persistent notifications are ever implemented on | 243 // persistent once/if non persistent notifications are ever implemented on |
224 // Android. | 244 // Android. |
225 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); | 245 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); |
226 | 246 |
227 GURL origin_url(notification.origin_url().GetOrigin()); | |
228 | |
229 GURL scope_url(notification.service_worker_scope()); | 247 GURL scope_url(notification.service_worker_scope()); |
230 if (!scope_url.is_valid()) | 248 if (!scope_url.is_valid()) |
231 scope_url = origin_url; | 249 scope_url = GURL(notification.origin_url().GetOrigin()); |
232 ScopedJavaLocalRef<jstring> j_scope_url = | 250 ScopedJavaLocalRef<jstring> j_scope_url = |
233 ConvertUTF8ToJavaString(env, scope_url.spec()); | 251 ConvertUTF8ToJavaString(env, scope_url.spec()); |
234 ScopedJavaLocalRef<jstring> webapk_package = | 252 uintptr_t callback_pointer = |
235 Java_NotificationPlatformBridge_queryWebApkPackage( | 253 reinterpret_cast<uintptr_t>(new QueryWebApkPackageCallback( |
236 env, java_object_, j_scope_url); | 254 base::Bind(&NotificationPlatformBridgeAndroid::DisplayInternal, |
255 weak_ptr_factory_.GetWeakPtr(), notification_id, | |
256 profile_id, incognito, notification))); | |
257 Java_NotificationPlatformBridge_queryWebApkPackage( | |
258 env, java_object_, j_scope_url, callback_pointer); | |
259 } | |
237 | 260 |
261 void NotificationPlatformBridgeAndroid::DisplayInternal( | |
pkotwicz
2017/07/20 01:32:05
.cc and .h order applies to DisplayInternal() too
Xi Han
2017/07/21 20:36:35
Done.
| |
262 const std::string& notification_id, | |
263 const std::string& profile_id, | |
264 bool incognito, | |
265 const Notification& notification, | |
266 const std::string& webapk_package) { | |
267 JNIEnv* env = AttachCurrentThread(); | |
238 ScopedJavaLocalRef<jstring> j_notification_id = | 268 ScopedJavaLocalRef<jstring> j_notification_id = |
239 ConvertUTF8ToJavaString(env, notification_id); | 269 ConvertUTF8ToJavaString(env, notification_id); |
270 | |
271 GURL origin_url(notification.origin_url().GetOrigin()); | |
240 ScopedJavaLocalRef<jstring> j_origin = | 272 ScopedJavaLocalRef<jstring> j_origin = |
241 ConvertUTF8ToJavaString(env, origin_url.spec()); | 273 ConvertUTF8ToJavaString(env, origin_url.spec()); |
242 ScopedJavaLocalRef<jstring> tag = | 274 ScopedJavaLocalRef<jstring> tag = |
243 ConvertUTF8ToJavaString(env, notification.tag()); | 275 ConvertUTF8ToJavaString(env, notification.tag()); |
244 ScopedJavaLocalRef<jstring> title = | 276 ScopedJavaLocalRef<jstring> title = |
245 ConvertUTF16ToJavaString(env, notification.title()); | 277 ConvertUTF16ToJavaString(env, notification.title()); |
246 ScopedJavaLocalRef<jstring> body = | 278 ScopedJavaLocalRef<jstring> body = |
247 ConvertUTF16ToJavaString(env, notification.message()); | 279 ConvertUTF16ToJavaString(env, notification.message()); |
248 | 280 |
249 ScopedJavaLocalRef<jobject> image; | 281 ScopedJavaLocalRef<jobject> image; |
(...skipping 12 matching lines...) Expand all Loading... | |
262 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); | 294 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); |
263 | 295 |
264 ScopedJavaLocalRef<jobjectArray> actions = | 296 ScopedJavaLocalRef<jobjectArray> actions = |
265 ConvertToJavaActionInfos(notification.buttons()); | 297 ConvertToJavaActionInfos(notification.buttons()); |
266 | 298 |
267 ScopedJavaLocalRef<jintArray> vibration_pattern = | 299 ScopedJavaLocalRef<jintArray> vibration_pattern = |
268 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 300 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
269 | 301 |
270 ScopedJavaLocalRef<jstring> j_profile_id = | 302 ScopedJavaLocalRef<jstring> j_profile_id = |
271 ConvertUTF8ToJavaString(env, profile_id); | 303 ConvertUTF8ToJavaString(env, profile_id); |
304 ScopedJavaLocalRef<jstring> j_webapk_package = | |
305 ConvertUTF8ToJavaString(env, webapk_package); | |
272 | 306 |
273 Java_NotificationPlatformBridge_displayNotification( | 307 Java_NotificationPlatformBridge_displayNotification( |
274 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, | 308 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, |
275 tag, webapk_package, title, body, image, notification_icon, badge, | 309 tag, j_webapk_package, title, body, image, notification_icon, badge, |
276 vibration_pattern, notification.timestamp().ToJavaTime(), | 310 vibration_pattern, notification.timestamp().ToJavaTime(), |
277 notification.renotify(), notification.silent(), actions); | 311 notification.renotify(), notification.silent(), actions); |
278 | 312 |
279 regenerated_notification_infos_[notification_id] = | 313 regenerated_notification_infos_[notification_id] = |
280 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), | 314 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
281 ConvertJavaStringToUTF8(env, webapk_package)); | 315 webapk_package); |
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 |