| 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/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 17 #include "chrome/browser/notifications/notification.h" | 18 #include "chrome/browser/notifications/notification.h" |
| 18 #include "chrome/browser/notifications/notification_common.h" | 19 #include "chrome/browser/notifications/notification_common.h" |
| 20 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 19 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 21 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 20 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 22 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "content/public/common/persistent_notification_status.h" | 27 #include "content/public/common/persistent_notification_status.h" |
| 26 #include "content/public/common/platform_notification_data.h" | 28 #include "content/public/common/platform_notification_data.h" |
| 27 #include "jni/NotificationPlatformBridge_jni.h" | 29 #include "jni/NotificationPlatformBridge_jni.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 for (size_t i = 0; i < skbitmaps.size(); ++i) { | 54 for (size_t i = 0; i < skbitmaps.size(); ++i) { |
| 53 if (!skbitmaps[i].drawsNothing()) { | 55 if (!skbitmaps[i].drawsNothing()) { |
| 54 env->SetObjectArrayElement( | 56 env->SetObjectArrayElement( |
| 55 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); | 57 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 | 60 |
| 59 return ScopedJavaLocalRef<jobjectArray>(env, array); | 61 return ScopedJavaLocalRef<jobjectArray>(env, array); |
| 60 } | 62 } |
| 61 | 63 |
| 64 // Callback to run once the profile has been loaded in order to perform a |
| 65 // given |operation| in a notification. |
| 66 // TODO(miguelg) move it to notification_common? |
| 67 void ProfileLoadedCallback(NotificationCommon::Operation operation, |
| 68 NotificationCommon::Type notification_type, |
| 69 const std::string& origin, |
| 70 int64_t notification_id, |
| 71 int action_index, |
| 72 Profile* profile) { |
| 73 if (!profile) { |
| 74 // TODO(miguelg): Add UMA for this condition. |
| 75 // Perhaps propagate this through PersistentNotificationStatus. |
| 76 LOG(WARNING) << "Profile not loaded correctly"; |
| 77 return; |
| 78 } |
| 79 |
| 80 NotificationDisplayService* display_service = |
| 81 NotificationDisplayServiceFactory::GetForProfile(profile); |
| 82 |
| 83 static_cast<NativeNotificationDisplayService*>(display_service) |
| 84 ->ProcessNotificationOperation(operation, notification_type, origin, |
| 85 base::Int64ToString(notification_id), |
| 86 action_index); |
| 87 } |
| 88 |
| 62 } // namespace | 89 } // namespace |
| 63 | 90 |
| 64 // Called by the Java side when a notification event has been received, but the | 91 // Called by the Java side when a notification event has been received, but the |
| 65 // NotificationBridge has not been initialized yet. Enforce initialization of | 92 // NotificationBridge has not been initialized yet. Enforce initialization of |
| 66 // the class. | 93 // the class. |
| 67 static void InitializeNotificationPlatformBridge( | 94 static void InitializeNotificationPlatformBridge( |
| 68 JNIEnv* env, | 95 JNIEnv* env, |
| 69 const JavaParamRef<jclass>& clazz) { | 96 const JavaParamRef<jclass>& clazz) { |
| 70 g_browser_process->notification_platform_bridge(); | 97 g_browser_process->notification_platform_bridge(); |
| 71 } | 98 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 88 void NotificationPlatformBridgeAndroid::OnNotificationClicked( | 115 void NotificationPlatformBridgeAndroid::OnNotificationClicked( |
| 89 JNIEnv* env, | 116 JNIEnv* env, |
| 90 const JavaParamRef<jobject>& java_object, | 117 const JavaParamRef<jobject>& java_object, |
| 91 jlong persistent_notification_id, | 118 jlong persistent_notification_id, |
| 92 const JavaParamRef<jstring>& java_origin, | 119 const JavaParamRef<jstring>& java_origin, |
| 93 const JavaParamRef<jstring>& java_profile_id, | 120 const JavaParamRef<jstring>& java_profile_id, |
| 94 jboolean incognito, | 121 jboolean incognito, |
| 95 const JavaParamRef<jstring>& java_tag, | 122 const JavaParamRef<jstring>& java_tag, |
| 96 const JavaParamRef<jstring>& java_webapk_package, | 123 const JavaParamRef<jstring>& java_webapk_package, |
| 97 jint action_index) { | 124 jint action_index) { |
| 98 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); | |
| 99 std::string tag = ConvertJavaStringToUTF8(env, java_tag); | 125 std::string tag = ConvertJavaStringToUTF8(env, java_tag); |
| 100 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); | 126 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); |
| 101 std::string webapk_package = | 127 std::string webapk_package = |
| 102 ConvertJavaStringToUTF8(env, java_webapk_package); | 128 ConvertJavaStringToUTF8(env, java_webapk_package); |
| 103 | 129 |
| 130 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); |
| 104 regenerated_notification_infos_[persistent_notification_id] = | 131 regenerated_notification_infos_[persistent_notification_id] = |
| 105 RegeneratedNotificationInfo(origin.spec(), tag, webapk_package); | 132 RegeneratedNotificationInfo(origin.spec(), tag, webapk_package); |
| 106 | 133 |
| 107 PlatformNotificationServiceImpl::GetInstance() | 134 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 108 ->ProcessPersistentNotificationOperation( | 135 DCHECK(profile_manager); |
| 109 NotificationCommon::CLICK, profile_id, incognito, origin, | 136 |
| 110 persistent_notification_id, action_index); | 137 profile_manager->LoadProfile( |
| 138 profile_id, incognito, |
| 139 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLICK, |
| 140 NotificationCommon::PERSISTENT, origin.spec(), |
| 141 persistent_notification_id, action_index)); |
| 111 } | 142 } |
| 112 | 143 |
| 113 void NotificationPlatformBridgeAndroid::OnNotificationClosed( | 144 void NotificationPlatformBridgeAndroid::OnNotificationClosed( |
| 114 JNIEnv* env, | 145 JNIEnv* env, |
| 115 const JavaParamRef<jobject>& java_object, | 146 const JavaParamRef<jobject>& java_object, |
| 116 jlong persistent_notification_id, | 147 jlong persistent_notification_id, |
| 117 const JavaParamRef<jstring>& java_origin, | 148 const JavaParamRef<jstring>& java_origin, |
| 118 const JavaParamRef<jstring>& java_profile_id, | 149 const JavaParamRef<jstring>& java_profile_id, |
| 119 jboolean incognito, | 150 jboolean incognito, |
| 120 const JavaParamRef<jstring>& java_tag, | 151 const JavaParamRef<jstring>& java_tag, |
| 121 jboolean by_user) { | 152 jboolean by_user) { |
| 122 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); | |
| 123 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); | 153 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); |
| 124 std::string tag = ConvertJavaStringToUTF8(env, java_tag); | |
| 125 | 154 |
| 126 // The notification was closed by the platform, so clear all local state. | 155 // The notification was closed by the platform, so clear all local state. |
| 127 regenerated_notification_infos_.erase(persistent_notification_id); | 156 regenerated_notification_infos_.erase(persistent_notification_id); |
| 128 PlatformNotificationServiceImpl::GetInstance() | 157 |
| 129 ->ProcessPersistentNotificationOperation( | 158 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 130 NotificationCommon::CLOSE, profile_id, incognito, origin, | 159 DCHECK(profile_manager); |
| 131 persistent_notification_id, -1); | 160 |
| 161 profile_manager->LoadProfile( |
| 162 profile_id, incognito, |
| 163 base::Bind(&ProfileLoadedCallback, NotificationCommon::CLOSE, |
| 164 NotificationCommon::PERSISTENT, |
| 165 ConvertJavaStringToUTF8(env, java_origin), |
| 166 persistent_notification_id, -1 /* action index */)); |
| 132 } | 167 } |
| 133 | 168 |
| 134 void NotificationPlatformBridgeAndroid::Display( | 169 void NotificationPlatformBridgeAndroid::Display( |
| 170 NotificationCommon::Type notification_type, |
| 135 const std::string& notification_id, | 171 const std::string& notification_id, |
| 136 const std::string& profile_id, | 172 const std::string& profile_id, |
| 137 bool incognito, | 173 bool incognito, |
| 138 const Notification& notification) { | 174 const Notification& notification) { |
| 139 JNIEnv* env = AttachCurrentThread(); | 175 JNIEnv* env = AttachCurrentThread(); |
| 176 // TODO(miguelg): Store the notification type in java instead of assuming it's |
| 177 // persistent once/if non persistent notifications are ever implemented on |
| 178 // Android. |
| 179 DCHECK_EQ(notification_type, NotificationCommon::PERSISTENT); |
| 140 | 180 |
| 141 // The Android notification platform bridge only supports Web Notifications, | 181 // TODO(miguelg): Store the persistent notification in Java and the |
| 142 // which have a PersistentNotificationDelegate. The persistent id of the | 182 // regenerated_notification_infos_ as a string instead of converting it back |
| 143 // notification is exposed through it's interface. | 183 // and forth to int64. |
| 144 // | 184 int64_t persistent_notification_id; |
| 145 // TODO(peter): When content/ passes a message_center::Notification to the | 185 if (!base::StringToInt64(notification_id, &persistent_notification_id)) { |
| 146 // chrome/ layer, the persistent notification id should be captured as a | 186 LOG(ERROR) << "Unable to convert notification ID: " << notification_id |
| 147 // property on that object instead, making this cast unnecessary. | 187 << " to integer."; |
| 148 PersistentNotificationDelegate* delegate = | 188 return; |
| 149 static_cast<PersistentNotificationDelegate*>(notification.delegate()); | 189 } |
| 150 DCHECK(delegate); | |
| 151 | 190 |
| 152 int64_t persistent_notification_id = delegate->persistent_notification_id(); | |
| 153 GURL origin_url(notification.origin_url().GetOrigin()); | 191 GURL origin_url(notification.origin_url().GetOrigin()); |
| 154 | 192 |
| 155 ScopedJavaLocalRef<jstring> origin = | 193 ScopedJavaLocalRef<jstring> origin = |
| 156 ConvertUTF8ToJavaString(env, origin_url.spec()); | 194 ConvertUTF8ToJavaString(env, origin_url.spec()); |
| 157 | 195 |
| 158 ScopedJavaLocalRef<jstring> webapk_package; | 196 ScopedJavaLocalRef<jstring> webapk_package; |
| 159 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 197 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 160 switches::kEnableWebApk)) { | 198 switches::kEnableWebApk)) { |
| 161 webapk_package = Java_NotificationPlatformBridge_queryWebApkPackage( | 199 webapk_package = Java_NotificationPlatformBridge_queryWebApkPackage( |
| 162 env, java_object_.obj(), origin.obj()); | 200 env, java_object_.obj(), origin.obj()); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 RegeneratedNotificationInfo() {} | 314 RegeneratedNotificationInfo() {} |
| 277 | 315 |
| 278 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 316 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
| 279 RegeneratedNotificationInfo(const std::string& origin, | 317 RegeneratedNotificationInfo(const std::string& origin, |
| 280 const std::string& tag, | 318 const std::string& tag, |
| 281 const std::string& webapk_package) | 319 const std::string& webapk_package) |
| 282 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 320 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
| 283 | 321 |
| 284 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 322 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
| 285 ~RegeneratedNotificationInfo() {} | 323 ~RegeneratedNotificationInfo() {} |
| OLD | NEW |