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_ui_manager_android.h" | 5 #include "chrome/browser/notifications/notification_ui_manager_android.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/android/jni_array.h" | 9 #include "base/android/jni_array.h" |
8 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/pickle.h" | 12 #include "base/pickle.h" |
11 #include "base/strings/string16.h" | |
12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 14 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
14 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 15 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
15 #include "chrome/browser/notifications/profile_notification.h" | 16 #include "chrome/browser/notifications/profile_notification.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "content/public/common/persistent_notification_status.h" | 18 #include "content/public/common/persistent_notification_status.h" |
18 #include "content/public/common/platform_notification_data.h" | 19 #include "content/public/common/platform_notification_data.h" |
19 #include "jni/NotificationUIManager_jni.h" | 20 #include "jni/NotificationUIManager_jni.h" |
20 #include "ui/gfx/android/java_bitmap.h" | 21 #include "ui/gfx/android/java_bitmap.h" |
21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 content::PlatformNotificationData notification_data; | 179 content::PlatformNotificationData notification_data; |
179 GURL origin; | 180 GURL origin; |
180 int64 service_worker_registration_id; | 181 int64 service_worker_registration_id; |
181 | 182 |
182 Pickle pickle(reinterpret_cast<const char*>(&bytes[0]), bytes.size()); | 183 Pickle pickle(reinterpret_cast<const char*>(&bytes[0]), bytes.size()); |
183 if (!UnserializePersistentNotification(pickle, ¬ification_data, &origin, | 184 if (!UnserializePersistentNotification(pickle, ¬ification_data, &origin, |
184 &service_worker_registration_id)) { | 185 &service_worker_registration_id)) { |
185 return false; | 186 return false; |
186 } | 187 } |
187 | 188 |
188 // Store the platform Id of this notification so that it can be closed. | 189 // Store the platform id, tag, and origin of this notification so that it can |
189 platform_notifications_[id] = platform_id; | 190 // be closed. |
| 191 RegeneratedNotificationInfo notification_info(notification_data.tag, |
| 192 platform_id, origin.spec()); |
| 193 regenerated_notification_infos_.insert(std::make_pair(id, notification_info)); |
190 | 194 |
191 PlatformNotificationServiceImpl* service = | 195 PlatformNotificationServiceImpl* service = |
192 PlatformNotificationServiceImpl::GetInstance(); | 196 PlatformNotificationServiceImpl::GetInstance(); |
193 | 197 |
194 // TODO(peter): Rather than assuming that the last used profile is the | 198 // TODO(peter): Rather than assuming that the last used profile is the |
195 // appropriate one for this notification, the used profile should be | 199 // appropriate one for this notification, the used profile should be |
196 // stored as part of the notification's data. See https://crbug.com/437574. | 200 // stored as part of the notification's data. See https://crbug.com/437574. |
197 service->OnPersistentNotificationClick( | 201 service->OnPersistentNotificationClick( |
198 ProfileManager::GetLastUsedProfile(), | 202 ProfileManager::GetLastUsedProfile(), |
199 service_worker_registration_id, | 203 service_worker_registration_id, |
(...skipping 24 matching lines...) Expand all Loading... |
224 return; | 228 return; |
225 | 229 |
226 ProfileNotification* profile_notification = | 230 ProfileNotification* profile_notification = |
227 new ProfileNotification(profile, notification); | 231 new ProfileNotification(profile, notification); |
228 | 232 |
229 // Takes ownership of |profile_notification|. | 233 // Takes ownership of |profile_notification|. |
230 AddProfileNotification(profile_notification); | 234 AddProfileNotification(profile_notification); |
231 | 235 |
232 JNIEnv* env = AttachCurrentThread(); | 236 JNIEnv* env = AttachCurrentThread(); |
233 | 237 |
| 238 ScopedJavaLocalRef<jstring> tag = |
| 239 ConvertUTF16ToJavaString(env, notification.replace_id()); |
234 ScopedJavaLocalRef<jstring> id = ConvertUTF8ToJavaString( | 240 ScopedJavaLocalRef<jstring> id = ConvertUTF8ToJavaString( |
235 env, profile_notification->notification().id()); | 241 env, profile_notification->notification().id()); |
236 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString( | 242 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString( |
237 env, notification.title()); | 243 env, notification.title()); |
238 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString( | 244 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString( |
239 env, notification.message()); | 245 env, notification.message()); |
240 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString( | 246 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString( |
241 env, notification.origin_url().GetOrigin().spec()); | 247 env, notification.origin_url().GetOrigin().spec()); |
242 | 248 |
243 ScopedJavaLocalRef<jobject> icon; | 249 ScopedJavaLocalRef<jobject> icon; |
(...skipping 16 matching lines...) Expand all Loading... |
260 "Unable to serialize the notification, payload too large (max 1MB)."; | 266 "Unable to serialize the notification, payload too large (max 1MB)."; |
261 RemoveProfileNotification(profile_notification); | 267 RemoveProfileNotification(profile_notification); |
262 return; | 268 return; |
263 } | 269 } |
264 | 270 |
265 notification_data = base::android::ToJavaByteArray( | 271 notification_data = base::android::ToJavaByteArray( |
266 env, static_cast<const uint8*>(pickle->data()), pickle->size()); | 272 env, static_cast<const uint8*>(pickle->data()), pickle->size()); |
267 } | 273 } |
268 | 274 |
269 int platform_id = Java_NotificationUIManager_displayNotification( | 275 int platform_id = Java_NotificationUIManager_displayNotification( |
270 env, java_object_.obj(), id.obj(), title.obj(), body.obj(), icon.obj(), | 276 env, java_object_.obj(), tag.obj(), id.obj(), title.obj(), body.obj(), |
271 origin.obj(), notification_data.obj()); | 277 icon.obj(), origin.obj(), notification_data.obj()); |
272 | 278 |
273 std::string notification_id = profile_notification->notification().id(); | 279 RegeneratedNotificationInfo notification_info( |
274 platform_notifications_[notification_id] = platform_id; | 280 notification.replace_id(), platform_id, |
| 281 notification.origin_url().GetOrigin().spec()); |
| 282 regenerated_notification_infos_.insert(std::make_pair( |
| 283 profile_notification->notification().id(), notification_info)); |
275 | 284 |
276 notification.delegate()->Display(); | 285 notification.delegate()->Display(); |
277 } | 286 } |
278 | 287 |
279 bool NotificationUIManagerAndroid::Update(const Notification& notification, | 288 bool NotificationUIManagerAndroid::Update(const Notification& notification, |
280 Profile* profile) { | 289 Profile* profile) { |
281 const base::string16& replace_id = notification.replace_id(); | 290 const base::string16& replace_id = notification.replace_id(); |
282 if (replace_id.empty()) | 291 if (replace_id.empty()) |
283 return false; | 292 return false; |
284 | 293 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 408 |
400 profile_notifications_.clear(); | 409 profile_notifications_.clear(); |
401 } | 410 } |
402 | 411 |
403 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) { | 412 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) { |
404 return RegisterNativesImpl(env); | 413 return RegisterNativesImpl(env); |
405 } | 414 } |
406 | 415 |
407 void NotificationUIManagerAndroid::PlatformCloseNotification( | 416 void NotificationUIManagerAndroid::PlatformCloseNotification( |
408 const std::string& notification_id) { | 417 const std::string& notification_id) { |
409 auto iterator = platform_notifications_.find(notification_id); | 418 auto iterator = regenerated_notification_infos_.find(notification_id); |
410 if (iterator == platform_notifications_.end()) | 419 if (iterator == regenerated_notification_infos_.end()) |
411 return; | 420 return; |
412 | 421 |
413 int platform_id = iterator->second; | 422 RegeneratedNotificationInfo notification_info = iterator->second; |
414 platform_notifications_.erase(notification_id); | 423 int platform_id = notification_info.platform_id; |
| 424 JNIEnv* env = AttachCurrentThread(); |
415 | 425 |
416 Java_NotificationUIManager_closeNotification(AttachCurrentThread(), | 426 ScopedJavaLocalRef<jstring> tag = |
417 java_object_.obj(), | 427 ConvertUTF16ToJavaString(env, notification_info.tag); |
418 platform_id); | 428 ScopedJavaLocalRef<jstring> origin = |
| 429 ConvertUTF8ToJavaString(env, notification_info.origin); |
| 430 |
| 431 regenerated_notification_infos_.erase(notification_id); |
| 432 |
| 433 Java_NotificationUIManager_closeNotification( |
| 434 env, java_object_.obj(), tag.obj(), platform_id, origin.obj()); |
419 } | 435 } |
420 | 436 |
421 void NotificationUIManagerAndroid::AddProfileNotification( | 437 void NotificationUIManagerAndroid::AddProfileNotification( |
422 ProfileNotification* profile_notification) { | 438 ProfileNotification* profile_notification) { |
423 std::string id = profile_notification->notification().id(); | 439 std::string id = profile_notification->notification().id(); |
424 | 440 |
425 // Notification ids should be unique. | 441 // Notification ids should be unique. |
426 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); | 442 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
427 | 443 |
428 profile_notifications_[id] = profile_notification; | 444 profile_notifications_[id] = profile_notification; |
(...skipping 10 matching lines...) Expand all Loading... |
439 | 455 |
440 ProfileNotification* NotificationUIManagerAndroid::FindProfileNotification( | 456 ProfileNotification* NotificationUIManagerAndroid::FindProfileNotification( |
441 const std::string& id) const { | 457 const std::string& id) const { |
442 auto iter = profile_notifications_.find(id); | 458 auto iter = profile_notifications_.find(id); |
443 if (iter == profile_notifications_.end()) | 459 if (iter == profile_notifications_.end()) |
444 return nullptr; | 460 return nullptr; |
445 | 461 |
446 return iter->second; | 462 return iter->second; |
447 } | 463 } |
448 | 464 |
OLD | NEW |