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" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 return ScopedJavaLocalRef<jobjectArray>(env, array); | 61 return ScopedJavaLocalRef<jobjectArray>(env, array); |
62 } | 62 } |
63 | 63 |
64 // Callback to run once the profile has been loaded in order to perform a | 64 // Callback to run once the profile has been loaded in order to perform a |
65 // given |operation| in a notification. | 65 // given |operation| in a notification. |
66 // TODO(miguelg) move it to notification_common? | 66 // TODO(miguelg) move it to notification_common? |
67 void ProfileLoadedCallback(NotificationCommon::Operation operation, | 67 void ProfileLoadedCallback(NotificationCommon::Operation operation, |
68 NotificationCommon::Type notification_type, | 68 NotificationCommon::Type notification_type, |
69 const std::string& origin, | 69 const std::string& origin, |
70 int64_t notification_id, | 70 const std::string& notification_id, |
71 int action_index, | 71 int action_index, |
72 Profile* profile) { | 72 Profile* profile) { |
73 if (!profile) { | 73 if (!profile) { |
74 // TODO(miguelg): Add UMA for this condition. | 74 // TODO(miguelg): Add UMA for this condition. |
75 // Perhaps propagate this through PersistentNotificationStatus. | 75 // Perhaps propagate this through PersistentNotificationStatus. |
76 LOG(WARNING) << "Profile not loaded correctly"; | 76 LOG(WARNING) << "Profile not loaded correctly"; |
77 return; | 77 return; |
78 } | 78 } |
79 | 79 |
80 NotificationDisplayService* display_service = | 80 NotificationDisplayService* display_service = |
81 NotificationDisplayServiceFactory::GetForProfile(profile); | 81 NotificationDisplayServiceFactory::GetForProfile(profile); |
82 | 82 |
83 static_cast<NativeNotificationDisplayService*>(display_service) | 83 static_cast<NativeNotificationDisplayService*>(display_service) |
84 ->ProcessNotificationOperation(operation, notification_type, origin, | 84 ->ProcessNotificationOperation(operation, notification_type, origin, |
85 base::Int64ToString(notification_id), | 85 notification_id, action_index); |
86 action_index); | |
87 } | 86 } |
88 | 87 |
89 } // namespace | 88 } // namespace |
90 | 89 |
91 // Called by the Java side when a notification event has been received, but the | 90 // Called by the Java side when a notification event has been received, but the |
92 // NotificationBridge has not been initialized yet. Enforce initialization of | 91 // NotificationBridge has not been initialized yet. Enforce initialization of |
93 // the class. | 92 // the class. |
94 static void InitializeNotificationPlatformBridge( | 93 static void InitializeNotificationPlatformBridge( |
95 JNIEnv* env, | 94 JNIEnv* env, |
96 const JavaParamRef<jclass>& clazz) { | 95 const JavaParamRef<jclass>& clazz) { |
(...skipping 11 matching lines...) Expand all Loading... |
108 } | 107 } |
109 | 108 |
110 NotificationPlatformBridgeAndroid::~NotificationPlatformBridgeAndroid() { | 109 NotificationPlatformBridgeAndroid::~NotificationPlatformBridgeAndroid() { |
111 Java_NotificationPlatformBridge_destroy(AttachCurrentThread(), | 110 Java_NotificationPlatformBridge_destroy(AttachCurrentThread(), |
112 java_object_.obj()); | 111 java_object_.obj()); |
113 } | 112 } |
114 | 113 |
115 void NotificationPlatformBridgeAndroid::OnNotificationClicked( | 114 void NotificationPlatformBridgeAndroid::OnNotificationClicked( |
116 JNIEnv* env, | 115 JNIEnv* env, |
117 const JavaParamRef<jobject>& java_object, | 116 const JavaParamRef<jobject>& java_object, |
118 jlong persistent_notification_id, | 117 const JavaParamRef<jstring>& java_notification_id, |
119 const JavaParamRef<jstring>& java_origin, | 118 const JavaParamRef<jstring>& java_origin, |
120 const JavaParamRef<jstring>& java_profile_id, | 119 const JavaParamRef<jstring>& java_profile_id, |
121 jboolean incognito, | 120 jboolean incognito, |
122 const JavaParamRef<jstring>& java_tag, | 121 const JavaParamRef<jstring>& java_tag, |
123 const JavaParamRef<jstring>& java_webapk_package, | 122 const JavaParamRef<jstring>& java_webapk_package, |
124 jint action_index) { | 123 jint action_index) { |
| 124 std::string notification_id = |
| 125 ConvertJavaStringToUTF8(env, java_notification_id); |
125 std::string tag = ConvertJavaStringToUTF8(env, java_tag); | 126 std::string tag = ConvertJavaStringToUTF8(env, java_tag); |
126 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); | 127 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); |
127 std::string webapk_package = | 128 std::string webapk_package = |
128 ConvertJavaStringToUTF8(env, java_webapk_package); | 129 ConvertJavaStringToUTF8(env, java_webapk_package); |
129 | 130 |
130 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); | 131 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); |
131 regenerated_notification_infos_[persistent_notification_id] = | 132 regenerated_notification_infos_[notification_id] = |
132 RegeneratedNotificationInfo(origin.spec(), tag, webapk_package); | 133 RegeneratedNotificationInfo(origin.spec(), tag, webapk_package); |
133 | 134 |
134 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 135 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
135 DCHECK(profile_manager); | 136 DCHECK(profile_manager); |
136 | 137 |
137 profile_manager->LoadProfile( | 138 profile_manager->LoadProfile( |
138 profile_id, incognito, | 139 profile_id, incognito, |
139 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLICK, | 140 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLICK, |
140 NotificationCommon::PERSISTENT, origin.spec(), | 141 NotificationCommon::PERSISTENT, origin.spec(), notification_id, |
141 persistent_notification_id, action_index)); | 142 action_index)); |
142 } | 143 } |
143 | 144 |
144 void NotificationPlatformBridgeAndroid::OnNotificationClosed( | 145 void NotificationPlatformBridgeAndroid::OnNotificationClosed( |
145 JNIEnv* env, | 146 JNIEnv* env, |
146 const JavaParamRef<jobject>& java_object, | 147 const JavaParamRef<jobject>& java_object, |
147 jlong persistent_notification_id, | 148 const JavaParamRef<jstring>& java_notification_id, |
148 const JavaParamRef<jstring>& java_origin, | 149 const JavaParamRef<jstring>& java_origin, |
149 const JavaParamRef<jstring>& java_profile_id, | 150 const JavaParamRef<jstring>& java_profile_id, |
150 jboolean incognito, | 151 jboolean incognito, |
151 const JavaParamRef<jstring>& java_tag, | 152 const JavaParamRef<jstring>& java_tag, |
152 jboolean by_user) { | 153 jboolean by_user) { |
153 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); | 154 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); |
| 155 std::string notification_id = |
| 156 ConvertJavaStringToUTF8(env, java_notification_id); |
154 | 157 |
155 // The notification was closed by the platform, so clear all local state. | 158 // The notification was closed by the platform, so clear all local state. |
156 regenerated_notification_infos_.erase(persistent_notification_id); | 159 regenerated_notification_infos_.erase(notification_id); |
157 | 160 |
158 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 161 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
159 DCHECK(profile_manager); | 162 DCHECK(profile_manager); |
160 | 163 |
161 profile_manager->LoadProfile( | 164 profile_manager->LoadProfile( |
162 profile_id, incognito, | 165 profile_id, incognito, |
163 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLOSE, | 166 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLOSE, |
164 NotificationCommon::PERSISTENT, | 167 NotificationCommon::PERSISTENT, |
165 ConvertJavaStringToUTF8(env, java_origin), | 168 ConvertJavaStringToUTF8(env, java_origin), notification_id, |
166 persistent_notification_id, -1 /* action index */)); | 169 -1 /* action index */)); |
167 } | 170 } |
168 | 171 |
169 void NotificationPlatformBridgeAndroid::Display( | 172 void NotificationPlatformBridgeAndroid::Display( |
170 NotificationCommon::Type notification_type, | 173 NotificationCommon::Type notification_type, |
171 const std::string& notification_id, | 174 const std::string& notification_id, |
172 const std::string& profile_id, | 175 const std::string& profile_id, |
173 bool incognito, | 176 bool incognito, |
174 const Notification& notification) { | 177 const Notification& notification) { |
175 JNIEnv* env = AttachCurrentThread(); | 178 JNIEnv* env = AttachCurrentThread(); |
176 // TODO(miguelg): Store the notification type in java instead of assuming it's | 179 // TODO(miguelg): Store the notification type in java instead of assuming it's |
177 // persistent once/if non persistent notifications are ever implemented on | 180 // persistent once/if non persistent notifications are ever implemented on |
178 // Android. | 181 // Android. |
179 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); | 182 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); |
180 | 183 |
181 // TODO(miguelg): Store the persistent notification in Java and the | |
182 // regenerated_notification_infos_ as a string instead of converting it back | |
183 // and forth to int64. | |
184 int64_t persistent_notification_id; | |
185 if (!base::StringToInt64(notification_id, &persistent_notification_id)) { | |
186 LOG(ERROR) << "Unable to convert notification ID: " << notification_id | |
187 << " to integer."; | |
188 return; | |
189 } | |
190 | |
191 GURL origin_url(notification.origin_url().GetOrigin()); | 184 GURL origin_url(notification.origin_url().GetOrigin()); |
192 | 185 |
193 ScopedJavaLocalRef<jstring> origin = | 186 ScopedJavaLocalRef<jstring> origin = |
194 ConvertUTF8ToJavaString(env, origin_url.spec()); | 187 ConvertUTF8ToJavaString(env, origin_url.spec()); |
195 | 188 |
196 ScopedJavaLocalRef<jstring> webapk_package; | 189 ScopedJavaLocalRef<jstring> webapk_package; |
197 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 190 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
198 switches::kEnableWebApk)) { | 191 switches::kEnableWebApk)) { |
199 webapk_package = Java_NotificationPlatformBridge_queryWebApkPackage( | 192 webapk_package = Java_NotificationPlatformBridge_queryWebApkPackage( |
200 env, java_object_.obj(), origin.obj()); | 193 env, java_object_.obj(), origin.obj()); |
201 } else { | 194 } else { |
202 webapk_package = ConvertUTF8ToJavaString(env, ""); | 195 webapk_package = ConvertUTF8ToJavaString(env, ""); |
203 } | 196 } |
204 | 197 |
| 198 ScopedJavaLocalRef<jstring> j_notification_id = |
| 199 ConvertUTF8ToJavaString(env, notification_id); |
205 ScopedJavaLocalRef<jstring> tag = | 200 ScopedJavaLocalRef<jstring> tag = |
206 ConvertUTF8ToJavaString(env, notification.tag()); | 201 ConvertUTF8ToJavaString(env, notification.tag()); |
207 ScopedJavaLocalRef<jstring> title = | 202 ScopedJavaLocalRef<jstring> title = |
208 ConvertUTF16ToJavaString(env, notification.title()); | 203 ConvertUTF16ToJavaString(env, notification.title()); |
209 ScopedJavaLocalRef<jstring> body = | 204 ScopedJavaLocalRef<jstring> body = |
210 ConvertUTF16ToJavaString(env, notification.message()); | 205 ConvertUTF16ToJavaString(env, notification.message()); |
211 | 206 |
212 ScopedJavaLocalRef<jobject> notification_icon; | 207 ScopedJavaLocalRef<jobject> notification_icon; |
213 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); | 208 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); |
214 if (!notification_icon_bitmap.drawsNothing()) | 209 if (!notification_icon_bitmap.drawsNothing()) |
(...skipping 13 matching lines...) Expand all Loading... |
228 ScopedJavaLocalRef<jobjectArray> action_icons = | 223 ScopedJavaLocalRef<jobjectArray> action_icons = |
229 ConvertToJavaBitmaps(notification.buttons()); | 224 ConvertToJavaBitmaps(notification.buttons()); |
230 | 225 |
231 ScopedJavaLocalRef<jintArray> vibration_pattern = | 226 ScopedJavaLocalRef<jintArray> vibration_pattern = |
232 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 227 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
233 | 228 |
234 ScopedJavaLocalRef<jstring> j_profile_id = | 229 ScopedJavaLocalRef<jstring> j_profile_id = |
235 ConvertUTF8ToJavaString(env, profile_id); | 230 ConvertUTF8ToJavaString(env, profile_id); |
236 | 231 |
237 Java_NotificationPlatformBridge_displayNotification( | 232 Java_NotificationPlatformBridge_displayNotification( |
238 env, java_object_.obj(), persistent_notification_id, origin.obj(), | 233 env, java_object_.obj(), j_notification_id.obj(), origin.obj(), |
239 j_profile_id.obj(), incognito, tag.obj(), webapk_package.obj(), | 234 j_profile_id.obj(), incognito, tag.obj(), webapk_package.obj(), |
240 title.obj(), body.obj(), notification_icon.obj(), badge.obj(), | 235 title.obj(), body.obj(), notification_icon.obj(), badge.obj(), |
241 vibration_pattern.obj(), notification.timestamp().ToJavaTime(), | 236 vibration_pattern.obj(), notification.timestamp().ToJavaTime(), |
242 notification.renotify(), notification.silent(), action_titles.obj(), | 237 notification.renotify(), notification.silent(), action_titles.obj(), |
243 action_icons.obj()); | 238 action_icons.obj()); |
244 | 239 |
245 regenerated_notification_infos_[persistent_notification_id] = | 240 regenerated_notification_infos_[notification_id] = |
246 RegeneratedNotificationInfo( | 241 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
247 origin_url.spec(), notification.tag(), | 242 ConvertJavaStringToUTF8(env, webapk_package)); |
248 ConvertJavaStringToUTF8(env, webapk_package)); | |
249 } | 243 } |
250 | 244 |
251 void NotificationPlatformBridgeAndroid::Close( | 245 void NotificationPlatformBridgeAndroid::Close( |
252 const std::string& profile_id, | 246 const std::string& profile_id, |
253 const std::string& notification_id) { | 247 const std::string& notification_id) { |
254 int64_t persistent_notification_id = 0; | 248 const auto iterator = regenerated_notification_infos_.find(notification_id); |
255 | |
256 // TODO(peter): Use the |delegate_id| directly when notification ids are being | |
257 // generated by content/ instead of us. | |
258 if (!base::StringToInt64(notification_id, &persistent_notification_id)) { | |
259 LOG(WARNING) << "Unable to decode notification_id " << notification_id; | |
260 return; | |
261 } | |
262 | |
263 const auto iterator = | |
264 regenerated_notification_infos_.find(persistent_notification_id); | |
265 if (iterator == regenerated_notification_infos_.end()) | 249 if (iterator == regenerated_notification_infos_.end()) |
266 return; | 250 return; |
267 | 251 |
268 const RegeneratedNotificationInfo& notification_info = iterator->second; | 252 const RegeneratedNotificationInfo& notification_info = iterator->second; |
269 | 253 |
270 JNIEnv* env = AttachCurrentThread(); | 254 JNIEnv* env = AttachCurrentThread(); |
271 | 255 |
| 256 ScopedJavaLocalRef<jstring> j_notification_id = |
| 257 ConvertUTF8ToJavaString(env, notification_id); |
272 ScopedJavaLocalRef<jstring> origin = | 258 ScopedJavaLocalRef<jstring> origin = |
273 ConvertUTF8ToJavaString(env, notification_info.origin); | 259 ConvertUTF8ToJavaString(env, notification_info.origin); |
274 ScopedJavaLocalRef<jstring> tag = | 260 ScopedJavaLocalRef<jstring> tag = |
275 ConvertUTF8ToJavaString(env, notification_info.tag); | 261 ConvertUTF8ToJavaString(env, notification_info.tag); |
276 ScopedJavaLocalRef<jstring> webapk_package = | 262 ScopedJavaLocalRef<jstring> webapk_package = |
277 ConvertUTF8ToJavaString(env, notification_info.webapk_package); | 263 ConvertUTF8ToJavaString(env, notification_info.webapk_package); |
278 | 264 |
279 ScopedJavaLocalRef<jstring> j_profile_id = | 265 ScopedJavaLocalRef<jstring> j_profile_id = |
280 ConvertUTF8ToJavaString(env, profile_id); | 266 ConvertUTF8ToJavaString(env, profile_id); |
281 | 267 |
282 regenerated_notification_infos_.erase(iterator); | 268 regenerated_notification_infos_.erase(iterator); |
283 | 269 |
284 Java_NotificationPlatformBridge_closeNotification( | 270 Java_NotificationPlatformBridge_closeNotification( |
285 env, java_object_.obj(), j_profile_id.obj(), persistent_notification_id, | 271 env, java_object_.obj(), j_profile_id.obj(), j_notification_id.obj(), |
286 origin.obj(), tag.obj(), webapk_package.obj()); | 272 origin.obj(), tag.obj(), webapk_package.obj()); |
287 } | 273 } |
288 | 274 |
289 bool NotificationPlatformBridgeAndroid::GetDisplayed( | 275 bool NotificationPlatformBridgeAndroid::GetDisplayed( |
290 const std::string& profile_id, | 276 const std::string& profile_id, |
291 bool incognito, | 277 bool incognito, |
292 std::set<std::string>* notifications) const { | 278 std::set<std::string>* notifications) const { |
293 // TODO(miguelg): This can actually be implemented for M+ | 279 // TODO(miguelg): This can actually be implemented for M+ |
294 return false; | 280 return false; |
295 } | 281 } |
(...skipping 18 matching lines...) Expand all Loading... |
314 RegeneratedNotificationInfo() {} | 300 RegeneratedNotificationInfo() {} |
315 | 301 |
316 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 302 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
317 RegeneratedNotificationInfo(const std::string& origin, | 303 RegeneratedNotificationInfo(const std::string& origin, |
318 const std::string& tag, | 304 const std::string& tag, |
319 const std::string& webapk_package) | 305 const std::string& webapk_package) |
320 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 306 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
321 | 307 |
322 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 308 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
323 ~RegeneratedNotificationInfo() {} | 309 ~RegeneratedNotificationInfo() {} |
OLD | NEW |