Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_android.cc

Issue 1919183003: Rename NotificationUIManager to NotificationPlatformBridge (1/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_platform_bridge_android.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/android/context_utils.h" 10 #include "base/android/context_utils.h"
11 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.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/notification.h" 17 #include "chrome/browser/notifications/notification.h"
18 #include "chrome/browser/notifications/persistent_notification_delegate.h" 18 #include "chrome/browser/notifications/persistent_notification_delegate.h"
19 #include "chrome/browser/notifications/platform_notification_service_impl.h" 19 #include "chrome/browser/notifications/platform_notification_service_impl.h"
20 #include "chrome/browser/profiles/profile_manager.h" 20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "content/public/common/persistent_notification_status.h" 21 #include "content/public/common/persistent_notification_status.h"
22 #include "content/public/common/platform_notification_data.h" 22 #include "content/public/common/platform_notification_data.h"
23 #include "jni/NotificationUIManager_jni.h" 23 #include "jni/NotificationPlatformBridge_jni.h"
24 #include "third_party/skia/include/core/SkBitmap.h" 24 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/gfx/android/java_bitmap.h" 25 #include "ui/gfx/android/java_bitmap.h"
26 #include "ui/gfx/image/image.h" 26 #include "ui/gfx/image/image.h"
27 27
28 using base::android::AttachCurrentThread; 28 using base::android::AttachCurrentThread;
29 using base::android::ConvertJavaStringToUTF8; 29 using base::android::ConvertJavaStringToUTF8;
30 using base::android::ConvertUTF16ToJavaString; 30 using base::android::ConvertUTF16ToJavaString;
31 using base::android::ConvertUTF8ToJavaString; 31 using base::android::ConvertUTF8ToJavaString;
32 32
33 namespace { 33 namespace {
(...skipping 17 matching lines...) Expand all
51 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); 51 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj());
52 } 52 }
53 } 53 }
54 54
55 return ScopedJavaLocalRef<jobjectArray>(env, array); 55 return ScopedJavaLocalRef<jobjectArray>(env, array);
56 } 56 }
57 57
58 } // namespace 58 } // namespace
59 59
60 // Called by the Java side when a notification event has been received, but the 60 // Called by the Java side when a notification event has been received, but the
61 // NotificationUIManager has not been initialized yet. Enforce initialization of 61 // NotificationBridge has not been initialized yet. Enforce initialization of
62 // the class. 62 // the class.
63 static void InitializeNotificationUIManager(JNIEnv* env, 63 static void InitializeNotificationPlatformBridge(
64 const JavaParamRef<jclass>& clazz) { 64 JNIEnv* env,
65 const JavaParamRef<jclass>& clazz) {
65 g_browser_process->notification_platform_bridge(); 66 g_browser_process->notification_platform_bridge();
66 } 67 }
67 68
68 // static 69 // static
69 NotificationPlatformBridge* NotificationPlatformBridge::Create() { 70 NotificationPlatformBridge* NotificationPlatformBridge::Create() {
70 return new NotificationUIManagerAndroid(); 71 return new NotificationPlatformBridgeAndroid();
71 } 72 }
72 73
73 NotificationUIManagerAndroid::NotificationUIManagerAndroid() { 74 NotificationPlatformBridgeAndroid::NotificationPlatformBridgeAndroid() {
74 java_object_.Reset( 75 java_object_.Reset(Java_NotificationPlatformBridge_create(
75 Java_NotificationUIManager_create( 76 AttachCurrentThread(), reinterpret_cast<intptr_t>(this),
76 AttachCurrentThread(), 77 base::android::GetApplicationContext()));
77 reinterpret_cast<intptr_t>(this),
78 base::android::GetApplicationContext()));
79 } 78 }
80 79
81 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() { 80 NotificationPlatformBridgeAndroid::~NotificationPlatformBridgeAndroid() {
82 Java_NotificationUIManager_destroy(AttachCurrentThread(), 81 Java_NotificationPlatformBridge_destroy(AttachCurrentThread(),
83 java_object_.obj()); 82 java_object_.obj());
84 } 83 }
85 84
86 void NotificationUIManagerAndroid::OnNotificationClicked( 85 void NotificationPlatformBridgeAndroid::OnNotificationClicked(
87 JNIEnv* env, 86 JNIEnv* env,
88 const JavaParamRef<jobject>& java_object, 87 const JavaParamRef<jobject>& java_object,
89 jlong persistent_notification_id, 88 jlong persistent_notification_id,
90 const JavaParamRef<jstring>& java_origin, 89 const JavaParamRef<jstring>& java_origin,
91 const JavaParamRef<jstring>& java_profile_id, 90 const JavaParamRef<jstring>& java_profile_id,
92 jboolean incognito, 91 jboolean incognito,
93 const JavaParamRef<jstring>& java_tag, 92 const JavaParamRef<jstring>& java_tag,
94 jint action_index) { 93 jint action_index) {
95 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); 94 GURL origin(ConvertJavaStringToUTF8(env, java_origin));
96 std::string tag = ConvertJavaStringToUTF8(env, java_tag); 95 std::string tag = ConvertJavaStringToUTF8(env, java_tag);
97 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); 96 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id);
98 97
99 regenerated_notification_infos_[persistent_notification_id] = 98 regenerated_notification_infos_[persistent_notification_id] =
100 std::make_pair(origin.spec(), tag); 99 std::make_pair(origin.spec(), tag);
101 100
102 PlatformNotificationServiceImpl::GetInstance() 101 PlatformNotificationServiceImpl::GetInstance()
103 ->ProcessPersistentNotificationOperation( 102 ->ProcessPersistentNotificationOperation(
104 PlatformNotificationServiceImpl::NOTIFICATION_CLICK, profile_id, 103 PlatformNotificationServiceImpl::NOTIFICATION_CLICK, profile_id,
105 incognito, origin, persistent_notification_id, action_index); 104 incognito, origin, persistent_notification_id, action_index);
106 } 105 }
107 106
108 void NotificationUIManagerAndroid::OnNotificationClosed( 107 void NotificationPlatformBridgeAndroid::OnNotificationClosed(
109 JNIEnv* env, 108 JNIEnv* env,
110 const JavaParamRef<jobject>& java_object, 109 const JavaParamRef<jobject>& java_object,
111 jlong persistent_notification_id, 110 jlong persistent_notification_id,
112 const JavaParamRef<jstring>& java_origin, 111 const JavaParamRef<jstring>& java_origin,
113 const JavaParamRef<jstring>& java_profile_id, 112 const JavaParamRef<jstring>& java_profile_id,
114 jboolean incognito, 113 jboolean incognito,
115 const JavaParamRef<jstring>& java_tag, 114 const JavaParamRef<jstring>& java_tag,
116 jboolean by_user) { 115 jboolean by_user) {
117 GURL origin(ConvertJavaStringToUTF8(env, java_origin)); 116 GURL origin(ConvertJavaStringToUTF8(env, java_origin));
118 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id); 117 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id);
119 std::string tag = ConvertJavaStringToUTF8(env, java_tag); 118 std::string tag = ConvertJavaStringToUTF8(env, java_tag);
120 119
121 // The notification was closed by the platform, so clear all local state. 120 // The notification was closed by the platform, so clear all local state.
122 regenerated_notification_infos_.erase(persistent_notification_id); 121 regenerated_notification_infos_.erase(persistent_notification_id);
123 PlatformNotificationServiceImpl::GetInstance() 122 PlatformNotificationServiceImpl::GetInstance()
124 ->ProcessPersistentNotificationOperation( 123 ->ProcessPersistentNotificationOperation(
125 PlatformNotificationServiceImpl::NOTIFICATION_CLOSE, profile_id, 124 PlatformNotificationServiceImpl::NOTIFICATION_CLOSE, profile_id,
126 incognito, origin, persistent_notification_id, -1); 125 incognito, origin, persistent_notification_id, -1);
127 } 126 }
128 127
129 void NotificationUIManagerAndroid::Display(const std::string& notification_id, 128 void NotificationPlatformBridgeAndroid::Display(
130 const std::string& profile_id, 129 const std::string& notification_id,
131 bool incognito, 130 const std::string& profile_id,
132 const Notification& notification) { 131 bool incognito,
132 const Notification& notification) {
133 JNIEnv* env = AttachCurrentThread(); 133 JNIEnv* env = AttachCurrentThread();
134 134
135 // The Android notification UI manager only supports Web Notifications, which 135 // The Android notification platform bridge only supports Web Notifications,
136 // have a PersistentNotificationDelegate. The persistent id of the 136 // which have a PersistentNotificationDelegate. The persistent id of the
137 // notification is exposed through it's interface. 137 // notification is exposed through it's interface.
138 // 138 //
139 // TODO(peter): When content/ passes a message_center::Notification to the 139 // TODO(peter): When content/ passes a message_center::Notification to the
140 // chrome/ layer, the persistent notification id should be captured as a 140 // chrome/ layer, the persistent notification id should be captured as a
141 // property on that object instead, making this cast unnecessary. 141 // property on that object instead, making this cast unnecessary.
142 PersistentNotificationDelegate* delegate = 142 PersistentNotificationDelegate* delegate =
143 static_cast<PersistentNotificationDelegate*>(notification.delegate()); 143 static_cast<PersistentNotificationDelegate*>(notification.delegate());
144 DCHECK(delegate); 144 DCHECK(delegate);
145 145
146 int64_t persistent_notification_id = delegate->persistent_notification_id(); 146 int64_t persistent_notification_id = delegate->persistent_notification_id();
147 GURL origin_url(notification.origin_url().GetOrigin()); 147 GURL origin_url(notification.origin_url().GetOrigin());
148 148
149 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString( 149 ScopedJavaLocalRef<jstring> origin =
150 env, origin_url.spec()); 150 ConvertUTF8ToJavaString(env, origin_url.spec());
151 ScopedJavaLocalRef<jstring> tag = 151 ScopedJavaLocalRef<jstring> tag =
152 ConvertUTF8ToJavaString(env, notification.tag()); 152 ConvertUTF8ToJavaString(env, notification.tag());
153 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString( 153 ScopedJavaLocalRef<jstring> title =
154 env, notification.title()); 154 ConvertUTF16ToJavaString(env, notification.title());
155 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString( 155 ScopedJavaLocalRef<jstring> body =
156 env, notification.message()); 156 ConvertUTF16ToJavaString(env, notification.message());
157 157
158 ScopedJavaLocalRef<jobject> notification_icon; 158 ScopedJavaLocalRef<jobject> notification_icon;
159 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); 159 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap();
160 if (!notification_icon_bitmap.drawsNothing()) 160 if (!notification_icon_bitmap.drawsNothing())
161 notification_icon = gfx::ConvertToJavaBitmap(&notification_icon_bitmap); 161 notification_icon = gfx::ConvertToJavaBitmap(&notification_icon_bitmap);
162 162
163 ScopedJavaLocalRef<jobject> badge; 163 ScopedJavaLocalRef<jobject> badge;
164 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); 164 SkBitmap badge_bitmap = notification.small_image().AsBitmap();
165 if (!badge_bitmap.drawsNothing()) 165 if (!badge_bitmap.drawsNothing())
166 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); 166 badge = gfx::ConvertToJavaBitmap(&badge_bitmap);
167 167
168 std::vector<base::string16> action_titles_vector; 168 std::vector<base::string16> action_titles_vector;
169 for (const message_center::ButtonInfo& button : notification.buttons()) 169 for (const message_center::ButtonInfo& button : notification.buttons())
170 action_titles_vector.push_back(button.title); 170 action_titles_vector.push_back(button.title);
171 ScopedJavaLocalRef<jobjectArray> action_titles = 171 ScopedJavaLocalRef<jobjectArray> action_titles =
172 base::android::ToJavaArrayOfStrings(env, action_titles_vector); 172 base::android::ToJavaArrayOfStrings(env, action_titles_vector);
173 173
174 ScopedJavaLocalRef<jobjectArray> action_icons = 174 ScopedJavaLocalRef<jobjectArray> action_icons =
175 ConvertToJavaBitmaps(notification.buttons()); 175 ConvertToJavaBitmaps(notification.buttons());
176 176
177 ScopedJavaLocalRef<jintArray> vibration_pattern = 177 ScopedJavaLocalRef<jintArray> vibration_pattern =
178 base::android::ToJavaIntArray(env, notification.vibration_pattern()); 178 base::android::ToJavaIntArray(env, notification.vibration_pattern());
179 179
180 ScopedJavaLocalRef<jstring> j_profile_id = 180 ScopedJavaLocalRef<jstring> j_profile_id =
181 ConvertUTF8ToJavaString(env, profile_id); 181 ConvertUTF8ToJavaString(env, profile_id);
182 182
183 Java_NotificationUIManager_displayNotification( 183 Java_NotificationPlatformBridge_displayNotification(
184 env, java_object_.obj(), persistent_notification_id, origin.obj(), 184 env, java_object_.obj(), persistent_notification_id, origin.obj(),
185 j_profile_id.obj(), incognito, tag.obj(), title.obj(), body.obj(), 185 j_profile_id.obj(), incognito, tag.obj(), title.obj(), body.obj(),
186 notification_icon.obj(), badge.obj(), vibration_pattern.obj(), 186 notification_icon.obj(), badge.obj(), vibration_pattern.obj(),
187 notification.timestamp().ToJavaTime(), notification.renotify(), 187 notification.timestamp().ToJavaTime(), notification.renotify(),
188 notification.silent(), action_titles.obj(), action_icons.obj()); 188 notification.silent(), action_titles.obj(), action_icons.obj());
189 189
190 regenerated_notification_infos_[persistent_notification_id] = 190 regenerated_notification_infos_[persistent_notification_id] =
191 std::make_pair(origin_url.spec(), notification.tag()); 191 std::make_pair(origin_url.spec(), notification.tag());
192 } 192 }
193 193
194 void NotificationUIManagerAndroid::Close(const std::string& profile_id, 194 void NotificationPlatformBridgeAndroid::Close(
195 const std::string& notification_id) { 195 const std::string& profile_id,
196 const std::string& notification_id) {
196 int64_t persistent_notification_id = 0; 197 int64_t persistent_notification_id = 0;
197 198
198 // TODO(peter): Use the |delegate_id| directly when notification ids are being 199 // TODO(peter): Use the |delegate_id| directly when notification ids are being
199 // generated by content/ instead of us. 200 // generated by content/ instead of us.
200 if (!base::StringToInt64(notification_id, &persistent_notification_id)) { 201 if (!base::StringToInt64(notification_id, &persistent_notification_id)) {
201 LOG(WARNING) << "Unable to decode notification_id " << notification_id; 202 LOG(WARNING) << "Unable to decode notification_id " << notification_id;
202 return; 203 return;
203 } 204 }
204 205
205 const auto iterator = 206 const auto iterator =
206 regenerated_notification_infos_.find(persistent_notification_id); 207 regenerated_notification_infos_.find(persistent_notification_id);
207 if (iterator == regenerated_notification_infos_.end()) 208 if (iterator == regenerated_notification_infos_.end())
208 return; 209 return;
209 210
210 const RegeneratedNotificationInfo& notification_info = iterator->second; 211 const RegeneratedNotificationInfo& notification_info = iterator->second;
211 212
212 JNIEnv* env = AttachCurrentThread(); 213 JNIEnv* env = AttachCurrentThread();
213 214
214 ScopedJavaLocalRef<jstring> origin = 215 ScopedJavaLocalRef<jstring> origin =
215 ConvertUTF8ToJavaString(env, notification_info.first); 216 ConvertUTF8ToJavaString(env, notification_info.first);
216 ScopedJavaLocalRef<jstring> tag = 217 ScopedJavaLocalRef<jstring> tag =
217 ConvertUTF8ToJavaString(env, notification_info.second); 218 ConvertUTF8ToJavaString(env, notification_info.second);
218 ScopedJavaLocalRef<jstring> j_profile_id = 219 ScopedJavaLocalRef<jstring> j_profile_id =
219 ConvertUTF8ToJavaString(env, profile_id); 220 ConvertUTF8ToJavaString(env, profile_id);
220 221
221 regenerated_notification_infos_.erase(iterator); 222 regenerated_notification_infos_.erase(iterator);
222 223
223 Java_NotificationUIManager_closeNotification( 224 Java_NotificationPlatformBridge_closeNotification(
224 env, java_object_.obj(), j_profile_id.obj(), persistent_notification_id, 225 env, java_object_.obj(), j_profile_id.obj(), persistent_notification_id,
225 origin.obj(), tag.obj()); 226 origin.obj(), tag.obj());
226 } 227 }
227 228
228 bool NotificationUIManagerAndroid::GetDisplayed( 229 bool NotificationPlatformBridgeAndroid::GetDisplayed(
229 const std::string& profile_id, 230 const std::string& profile_id,
230 bool incognito, 231 bool incognito,
231 std::set<std::string>* notifications) const { 232 std::set<std::string>* notifications) const {
232 // TODO(miguelg): This can actually be implemented for M+ 233 // TODO(miguelg): This can actually be implemented for M+
233 return false; 234 return false;
234 } 235 }
235 236
236 bool NotificationUIManagerAndroid::SupportsNotificationCenter() const { 237 bool NotificationPlatformBridgeAndroid::SupportsNotificationCenter() const {
237 return true; 238 return true;
238 } 239 }
239 240
240 bool NotificationUIManagerAndroid::RegisterNotificationPlatformBridge( 241 bool NotificationPlatformBridgeAndroid::RegisterNotificationPlatformBridge(
241 JNIEnv* env) { 242 JNIEnv* env) {
242 return RegisterNativesImpl(env); 243 return RegisterNativesImpl(env);
243 } 244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698