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> | 7 #include <utility> |
8 | 8 |
9 #include "base/android/jni_array.h" | 9 #include "base/android/jni_array.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/pickle.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/notifications/notification.h" | 14 #include "chrome/browser/notifications/notification.h" |
15 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 15 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
16 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 16 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
17 #include "chrome/browser/notifications/profile_notification.h" | 17 #include "chrome/browser/notifications/profile_notification.h" |
18 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
19 #include "content/public/common/persistent_notification_status.h" | 19 #include "content/public/common/persistent_notification_status.h" |
20 #include "content/public/common/platform_notification_data.h" | 20 #include "content/public/common/platform_notification_data.h" |
21 #include "jni/NotificationUIManager_jni.h" | 21 #include "jni/NotificationUIManager_jni.h" |
22 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
24 | 24 |
25 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
26 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
27 using base::android::ConvertUTF16ToJavaString; | 27 using base::android::ConvertUTF16ToJavaString; |
28 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // The maximum size of the serialized pickle that carries a notification's meta | |
33 // information. Notifications carrying more data will be silently dropped - with | |
34 // an error being written to the log. | |
35 const int kMaximumSerializedNotificationSizeBytes = 1024 * 1024; | |
36 | |
37 // Persistent notifications are likely to outlive the browser process they were | |
38 // created by on Android. In order to be able to re-surface the notification | |
39 // when the user interacts with them, all relevant notification data needs to | |
40 // be serialized with the notification itself. | |
41 // | |
42 // In the near future, as more features get added to Chrome's notification | |
43 // implementation, this will be done by storing the persistent notification data | |
44 // in a database. However, to support launching Chrome for Android from a | |
45 // notification event until that exists, serialize the data in the Intent. | |
46 // | |
47 // TODO(peter): Move towards storing notification data in a database rather than | |
48 // as a serialized Intent extra. | |
49 | |
50 scoped_ptr<Pickle> SerializePersistentNotification( | |
51 const content::PlatformNotificationData& notification_data, | |
52 const GURL& origin, | |
53 int64 service_worker_registration_id) { | |
54 scoped_ptr<Pickle> pickle(new Pickle); | |
55 | |
56 // content::PlatformNotificationData | |
57 pickle->WriteString16(notification_data.title); | |
58 pickle->WriteInt(static_cast<int>(notification_data.direction)); | |
59 pickle->WriteString(notification_data.lang); | |
60 pickle->WriteString16(notification_data.body); | |
61 pickle->WriteString(notification_data.tag); | |
62 pickle->WriteString(notification_data.icon.spec()); | |
63 pickle->WriteBool(notification_data.silent); | |
64 | |
65 // The origin which is displaying the notification. | |
66 pickle->WriteString(origin.spec()); | |
67 | |
68 // The Service Worker registration this notification is associated with. | |
69 pickle->WriteInt64(service_worker_registration_id); | |
70 | |
71 if (pickle->size() > kMaximumSerializedNotificationSizeBytes) | |
72 return nullptr; | |
73 | |
74 return pickle.Pass(); | |
75 } | |
76 | |
77 bool UnserializePersistentNotification( | |
78 const Pickle& pickle, | |
79 content::PlatformNotificationData* notification_data, | |
80 GURL* origin, | |
81 int64* service_worker_registration_id) { | |
82 DCHECK(notification_data && origin && service_worker_registration_id); | |
83 PickleIterator iterator(pickle); | |
84 | |
85 std::string icon_url, origin_url; | |
86 int direction_value; | |
87 | |
88 // Unpack content::PlatformNotificationData. | |
89 if (!iterator.ReadString16(¬ification_data->title) || | |
90 !iterator.ReadInt(&direction_value) || | |
91 !iterator.ReadString(¬ification_data->lang) || | |
92 !iterator.ReadString16(¬ification_data->body) || | |
93 !iterator.ReadString(¬ification_data->tag) || | |
94 !iterator.ReadString(&icon_url) || | |
95 !iterator.ReadBool(¬ification_data->silent)) { | |
96 return false; | |
97 } | |
98 | |
99 notification_data->direction = | |
100 static_cast<content::PlatformNotificationData::NotificationDirection>( | |
101 direction_value); | |
102 notification_data->icon = GURL(icon_url); | |
103 | |
104 // Unpack the origin which displayed this notification. | |
105 if (!iterator.ReadString(&origin_url)) | |
106 return false; | |
107 | |
108 *origin = GURL(origin_url); | |
109 | |
110 // Unpack the Service Worker registration id. | |
111 if (!iterator.ReadInt64(service_worker_registration_id)) | |
112 return false; | |
113 | |
114 return true; | |
115 } | |
116 | |
117 // Called when the "notificationclick" event in the Service Worker has finished | 32 // Called when the "notificationclick" event in the Service Worker has finished |
118 // executing for a notification that was created in a previous instance of the | 33 // executing for a notification that was created in a previous instance of the |
119 // browser. | 34 // browser. |
120 void OnEventDispatchComplete(content::PersistentNotificationStatus status) { | 35 void OnEventDispatchComplete(content::PersistentNotificationStatus status) { |
121 // TODO(peter): Add UMA statistics based on |status|. | 36 // TODO(peter): Add UMA statistics based on |status|. |
122 // TODO(peter): Decide if we want to forcefully shut down the browser process | 37 // TODO(peter): Decide if we want to forcefully shut down the browser process |
123 // if we're confident it was created for delivering this event. | 38 // if we're confident it was created for delivering this event. |
124 } | 39 } |
125 | 40 |
126 } // namespace | 41 } // namespace |
127 | 42 |
128 // Called by the Java side when a notification event has been received, but the | 43 // Called by the Java side when a notification event has been received, but the |
129 // NotificationUIManager has not been initialized yet. Enforce initialization of | 44 // NotificationUIManager has not been initialized yet. Enforce initialization of |
130 // the class. | 45 // the class. |
131 static void InitializeNotificationUIManager(JNIEnv* env, jclass clazz) { | 46 static void InitializeNotificationUIManager(JNIEnv* env, jclass clazz) { |
132 g_browser_process->notification_ui_manager(); | 47 g_browser_process->notification_ui_manager(); |
133 } | 48 } |
134 | 49 |
135 // static | 50 // static |
136 NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) { | 51 NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) { |
137 return new NotificationUIManagerAndroid(); | 52 return new NotificationUIManagerAndroid(); |
138 } | 53 } |
139 | 54 |
140 NotificationUIManagerAndroid::NotificationUIManagerAndroid() { | 55 NotificationUIManagerAndroid::NotificationUIManagerAndroid() { |
141 java_object_.Reset( | 56 java_object_.Reset( |
142 Java_NotificationUIManager_create( | 57 Java_NotificationUIManager_create( |
143 AttachCurrentThread(), | 58 AttachCurrentThread(), |
144 reinterpret_cast<intptr_t>(this), | 59 reinterpret_cast<intptr_t>(this), |
145 base::android::GetApplicationContext())); | 60 base::android::GetApplicationContext())); |
146 | |
147 // TODO(peter): Synchronize notifications with the Java side. | |
148 } | 61 } |
149 | 62 |
150 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() { | 63 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() { |
151 Java_NotificationUIManager_destroy(AttachCurrentThread(), | 64 Java_NotificationUIManager_destroy(AttachCurrentThread(), |
152 java_object_.obj()); | 65 java_object_.obj()); |
153 } | 66 } |
154 | 67 |
155 bool NotificationUIManagerAndroid::OnNotificationClicked( | 68 bool NotificationUIManagerAndroid::OnNotificationClicked( |
156 JNIEnv* env, | 69 JNIEnv* env, |
157 jobject java_object, | 70 jobject java_object, |
158 jstring notification_id, | 71 jlong persistent_notification_id, |
159 jbyteArray serialized_notification_data) { | 72 jstring java_origin, |
160 std::string id = ConvertJavaStringToUTF8(env, notification_id); | 73 jstring java_tag) { |
| 74 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); |
| 75 std::string tag = ConvertJavaStringToUTF8(env, java_tag); |
161 | 76 |
162 auto iter = profile_notifications_.find(id); | 77 regenerated_notification_infos_[persistent_notification_id] = |
163 if (iter != profile_notifications_.end()) { | 78 std::make_pair(origin.spec(), tag); |
164 const Notification& notification = iter->second->notification(); | |
165 notification.delegate()->Click(); | |
166 | |
167 return true; | |
168 } | |
169 | |
170 // If the Notification were not found, it may be a persistent notification | |
171 // that outlived the Chrome browser process. In this case, try to | |
172 // unserialize the notification's serialized data and trigger the click | |
173 // event manually. | |
174 | |
175 std::vector<uint8> bytes; | |
176 base::android::JavaByteArrayToByteVector(env, serialized_notification_data, | |
177 &bytes); | |
178 if (!bytes.size()) | |
179 return false; | |
180 | |
181 content::PlatformNotificationData notification_data; | |
182 GURL origin; | |
183 int64 service_worker_registration_id; | |
184 | |
185 Pickle pickle(reinterpret_cast<const char*>(&bytes[0]), bytes.size()); | |
186 if (!UnserializePersistentNotification(pickle, ¬ification_data, &origin, | |
187 &service_worker_registration_id)) { | |
188 return false; | |
189 } | |
190 | |
191 // Store the tag and origin of this notification so that it can later be | |
192 // closed using these details. | |
193 regenerated_notification_infos_[id] = | |
194 std::make_pair(notification_data.tag, origin.spec()); | |
195 | |
196 PlatformNotificationServiceImpl* service = | |
197 PlatformNotificationServiceImpl::GetInstance(); | |
198 | 79 |
199 // TODO(peter): Rather than assuming that the last used profile is the | 80 // TODO(peter): Rather than assuming that the last used profile is the |
200 // appropriate one for this notification, the used profile should be | 81 // appropriate one for this notification, the used profile should be |
201 // stored as part of the notification's data. See https://crbug.com/437574. | 82 // stored as part of the notification's data. See https://crbug.com/437574. |
202 service->OnPersistentNotificationClick( | 83 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClick( |
203 ProfileManager::GetLastUsedProfile(), | 84 ProfileManager::GetLastUsedProfile(), |
204 service_worker_registration_id, | 85 persistent_notification_id, |
205 id, | |
206 origin, | 86 origin, |
207 notification_data, | |
208 base::Bind(&OnEventDispatchComplete)); | 87 base::Bind(&OnEventDispatchComplete)); |
209 | 88 |
210 return true; | 89 return true; |
211 } | 90 } |
212 | 91 |
213 bool NotificationUIManagerAndroid::OnNotificationClosed( | 92 bool NotificationUIManagerAndroid::OnNotificationClosed( |
214 JNIEnv* env, jobject java_object, jstring notification_id) { | 93 JNIEnv* env, |
215 std::string id = ConvertJavaStringToUTF8(env, notification_id); | 94 jobject java_object, |
216 | 95 jlong persistent_notification_id, |
217 auto iter = profile_notifications_.find(id); | 96 jstring java_origin, |
218 if (iter == profile_notifications_.end()) | 97 jstring java_tag) { |
219 return false; | 98 // TODO(peter): Implement handling when a notification has been closed. The |
220 | 99 // notification database has to reflect this in its own state. |
221 const Notification& notification = iter->second->notification(); | |
222 notification.delegate()->Close(true /** by_user **/); | |
223 RemoveProfileNotification(iter->second, true /* close */); | |
224 return true; | 100 return true; |
225 } | 101 } |
226 | 102 |
227 void NotificationUIManagerAndroid::Add(const Notification& notification, | 103 void NotificationUIManagerAndroid::Add(const Notification& notification, |
228 Profile* profile) { | 104 Profile* profile) { |
229 // If the given notification is replacing an older one, drop its associated | 105 // If the given notification is replacing an older one, drop its associated |
230 // profile notification object without closing the platform notification. | 106 // profile notification object without closing the platform notification. |
231 // We'll use the native Android system to perform a smoother replacement. | 107 // We'll use the native Android system to perform a smoother replacement. |
232 ProfileNotification* notification_to_replace = | 108 ProfileNotification* notification_to_replace = |
233 FindNotificationToReplace(notification, profile); | 109 FindNotificationToReplace(notification, profile); |
234 if (notification_to_replace) | 110 if (notification_to_replace) |
235 RemoveProfileNotification(notification_to_replace, false /* close */); | 111 RemoveProfileNotification(notification_to_replace, false /* close */); |
236 | 112 |
237 ProfileNotification* profile_notification = | 113 ProfileNotification* profile_notification = |
238 new ProfileNotification(profile, notification); | 114 new ProfileNotification(profile, notification); |
239 | 115 |
240 // Takes ownership of |profile_notification|. | 116 // Takes ownership of |profile_notification|. |
241 AddProfileNotification(profile_notification); | 117 AddProfileNotification(profile_notification); |
242 | 118 |
243 JNIEnv* env = AttachCurrentThread(); | 119 JNIEnv* env = AttachCurrentThread(); |
244 | 120 |
| 121 PersistentNotificationDelegate* delegate = |
| 122 static_cast<PersistentNotificationDelegate*>(notification.delegate()); |
| 123 DCHECK(delegate); |
| 124 |
| 125 int64_t persistent_notification_id = delegate->persistent_notification_id(); |
| 126 GURL origin_url(notification.origin_url().GetOrigin()); |
| 127 |
| 128 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString( |
| 129 env, origin_url.spec()); |
245 ScopedJavaLocalRef<jstring> tag = | 130 ScopedJavaLocalRef<jstring> tag = |
246 ConvertUTF8ToJavaString(env, notification.tag()); | 131 ConvertUTF8ToJavaString(env, notification.tag()); |
247 ScopedJavaLocalRef<jstring> id = ConvertUTF8ToJavaString( | |
248 env, profile_notification->notification().id()); | |
249 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString( | 132 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString( |
250 env, notification.title()); | 133 env, notification.title()); |
251 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString( | 134 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString( |
252 env, notification.message()); | 135 env, notification.message()); |
253 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString( | |
254 env, notification.origin_url().GetOrigin().spec()); | |
255 | 136 |
256 ScopedJavaLocalRef<jobject> icon; | 137 ScopedJavaLocalRef<jobject> icon; |
257 | 138 |
258 SkBitmap icon_bitmap = notification.icon().AsBitmap(); | 139 SkBitmap icon_bitmap = notification.icon().AsBitmap(); |
259 if (!icon_bitmap.isNull()) | 140 if (!icon_bitmap.isNull()) |
260 icon = gfx::ConvertToJavaBitmap(&icon_bitmap); | 141 icon = gfx::ConvertToJavaBitmap(&icon_bitmap); |
261 | 142 |
262 ScopedJavaLocalRef<jbyteArray> notification_data; | |
263 if (true /** is_persistent_notification **/) { | |
264 PersistentNotificationDelegate* delegate = | |
265 static_cast<PersistentNotificationDelegate*>(notification.delegate()); | |
266 scoped_ptr<Pickle> pickle = SerializePersistentNotification( | |
267 delegate->notification_data(), | |
268 notification.origin_url(), | |
269 delegate->service_worker_registration_id()); | |
270 | |
271 if (!pickle) { | |
272 LOG(ERROR) << | |
273 "Unable to serialize the notification, payload too large (max 1MB)."; | |
274 RemoveProfileNotification(profile_notification, true /* close */); | |
275 return; | |
276 } | |
277 | |
278 notification_data = base::android::ToJavaByteArray( | |
279 env, static_cast<const uint8*>(pickle->data()), pickle->size()); | |
280 } | |
281 | |
282 Java_NotificationUIManager_displayNotification( | 143 Java_NotificationUIManager_displayNotification( |
283 env, | 144 env, |
284 java_object_.obj(), | 145 java_object_.obj(), |
| 146 persistent_notification_id, |
| 147 origin.obj(), |
285 tag.obj(), | 148 tag.obj(), |
286 id.obj(), | |
287 title.obj(), | 149 title.obj(), |
288 body.obj(), | 150 body.obj(), |
289 icon.obj(), | 151 icon.obj(), |
290 origin.obj(), | 152 notification.silent()); |
291 notification.silent(), | |
292 notification_data.obj()); | |
293 | 153 |
294 regenerated_notification_infos_[profile_notification->notification().id()] = | 154 regenerated_notification_infos_[persistent_notification_id] = |
295 std::make_pair(notification.tag(), | 155 std::make_pair(origin_url.spec(), notification.tag()); |
296 notification.origin_url().GetOrigin().spec()); | |
297 | 156 |
298 notification.delegate()->Display(); | 157 notification.delegate()->Display(); |
299 } | 158 } |
300 | 159 |
301 bool NotificationUIManagerAndroid::Update(const Notification& notification, | 160 bool NotificationUIManagerAndroid::Update(const Notification& notification, |
302 Profile* profile) { | 161 Profile* profile) { |
303 // This method is currently only called from extensions and local discovery, | 162 // This method is currently only called from extensions and local discovery, |
304 // both of which are not supported on Android. | 163 // both of which are not supported on Android. |
305 NOTIMPLEMENTED(); | 164 NOTIMPLEMENTED(); |
306 return false; | 165 return false; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 260 |
402 profile_notifications_.clear(); | 261 profile_notifications_.clear(); |
403 } | 262 } |
404 | 263 |
405 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) { | 264 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) { |
406 return RegisterNativesImpl(env); | 265 return RegisterNativesImpl(env); |
407 } | 266 } |
408 | 267 |
409 void NotificationUIManagerAndroid::PlatformCloseNotification( | 268 void NotificationUIManagerAndroid::PlatformCloseNotification( |
410 const std::string& notification_id) { | 269 const std::string& notification_id) { |
411 auto iterator = regenerated_notification_infos_.find(notification_id); | 270 int64_t persistent_notification_id = 0; |
| 271 if (!base::StringToInt64(notification_id, &persistent_notification_id)) |
| 272 return; |
| 273 |
| 274 const auto iterator = |
| 275 regenerated_notification_infos_.find(persistent_notification_id); |
412 if (iterator == regenerated_notification_infos_.end()) | 276 if (iterator == regenerated_notification_infos_.end()) |
413 return; | 277 return; |
414 | 278 |
415 RegeneratedNotificationInfo notification_info = iterator->second; | 279 RegeneratedNotificationInfo notification_info = iterator->second; |
416 JNIEnv* env = AttachCurrentThread(); | 280 JNIEnv* env = AttachCurrentThread(); |
417 | 281 |
| 282 ScopedJavaLocalRef<jstring> origin = |
| 283 ConvertUTF8ToJavaString(env, notification_info.first); |
418 ScopedJavaLocalRef<jstring> tag = | 284 ScopedJavaLocalRef<jstring> tag = |
419 ConvertUTF8ToJavaString(env, notification_info.first); | |
420 ScopedJavaLocalRef<jstring> origin = | |
421 ConvertUTF8ToJavaString(env, notification_info.second); | 285 ConvertUTF8ToJavaString(env, notification_info.second); |
422 ScopedJavaLocalRef<jstring> java_notification_id = | |
423 ConvertUTF8ToJavaString(env, notification_id); | |
424 | 286 |
425 regenerated_notification_infos_.erase(notification_id); | 287 regenerated_notification_infos_.erase(iterator); |
426 | 288 |
427 Java_NotificationUIManager_closeNotification( | 289 Java_NotificationUIManager_closeNotification(env, |
428 env, java_object_.obj(), tag.obj(), java_notification_id.obj(), | 290 java_object_.obj(), |
429 origin.obj()); | 291 persistent_notification_id, |
| 292 origin.obj(), |
| 293 tag.obj()); |
430 } | 294 } |
431 | 295 |
432 void NotificationUIManagerAndroid::AddProfileNotification( | 296 void NotificationUIManagerAndroid::AddProfileNotification( |
433 ProfileNotification* profile_notification) { | 297 ProfileNotification* profile_notification) { |
434 std::string id = profile_notification->notification().id(); | 298 std::string id = profile_notification->notification().id(); |
435 | 299 |
436 // Notification ids should be unique. | 300 // Notification ids should be unique. |
437 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); | 301 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
438 | 302 |
439 profile_notifications_[id] = profile_notification; | 303 profile_notifications_[id] = profile_notification; |
(...skipping 30 matching lines...) Expand all Loading... |
470 for (const auto& iterator : profile_notifications_) { | 334 for (const auto& iterator : profile_notifications_) { |
471 ProfileNotification* profile_notification = iterator.second; | 335 ProfileNotification* profile_notification = iterator.second; |
472 if (profile_notification->notification().tag() == tag || | 336 if (profile_notification->notification().tag() == tag || |
473 profile_notification->notification().origin_url() == origin_url || | 337 profile_notification->notification().origin_url() == origin_url || |
474 profile_notification->profile() == profile) { | 338 profile_notification->profile() == profile) { |
475 return profile_notification; | 339 return profile_notification; |
476 } | 340 } |
477 } | 341 } |
478 return nullptr; | 342 return nullptr; |
479 } | 343 } |
OLD | NEW |