1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_ui_manager_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/logging.h"
#include "base/pickle.h"
#include "base/strings/string16.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/persistent_notification_delegate.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/browser/notifications/profile_notification.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/common/persistent_notification_status.h"
#include "content/public/common/platform_notification_data.h"
#include "jni/NotificationUIManager_jni.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
namespace {
// The maximum size of the serialized pickle that carries a notification's meta
// information. Notifications carrying more data will be silently dropped - with
// an error being written to the log.
const int kMaximumSerializedNotificationSizeBytes = 1024 * 1024;
// Persistent notifications are likely to outlive the browser process they were
// created by on Android. In order to be able to re-surface the notification
// when the user interacts with them, all relevant notification data needs to
// be serialized with the notification itself.
//
// In the near future, as more features get added to Chrome's notification
// implementation, this will be done by storing the persistent notification data
// in a database. However, to support launching Chrome for Android from a
// notification event until that exists, serialize the data in the Intent.
//
// TODO(peter): Move towards storing notification data in a database rather than
// as a serialized Intent extra.
scoped_ptr<Pickle> SerializePersistentNotification(
const content::PlatformNotificationData& notification_data,
const GURL& origin,
int64 service_worker_registration_id) {
scoped_ptr<Pickle> pickle(new Pickle);
// content::PlatformNotificationData
pickle->WriteString16(notification_data.title);
pickle->WriteInt(static_cast<int>(notification_data.direction));
pickle->WriteString(notification_data.lang);
pickle->WriteString16(notification_data.body);
pickle->WriteString16(notification_data.tag);
pickle->WriteString(notification_data.icon.spec());
// The origin which is displaying the notification.
pickle->WriteString(origin.spec());
// The Service Worker registration this notification is associated with.
pickle->WriteInt64(service_worker_registration_id);
if (pickle->size() > kMaximumSerializedNotificationSizeBytes)
return nullptr;
return pickle.Pass();
}
bool UnserializePersistentNotification(
const Pickle& pickle,
content::PlatformNotificationData* notification_data,
GURL* origin,
int64* service_worker_registration_id) {
DCHECK(notification_data && origin && service_worker_registration_id);
PickleIterator iterator(pickle);
std::string icon_url, origin_url;
int direction_value;
// Unpack content::PlatformNotificationData.
if (!iterator.ReadString16(¬ification_data->title) ||
!iterator.ReadInt(&direction_value) ||
!iterator.ReadString(¬ification_data->lang) ||
!iterator.ReadString16(¬ification_data->body) ||
!iterator.ReadString16(¬ification_data->tag) ||
!iterator.ReadString(&icon_url)) {
return false;
}
notification_data->direction =
static_cast<content::PlatformNotificationData::NotificationDirection>(
direction_value);
notification_data->icon = GURL(icon_url);
// Unpack the origin which displayed this notification.
if (!iterator.ReadString(&origin_url))
return false;
*origin = GURL(origin_url);
// Unpack the Service Worker registration id.
if (!iterator.ReadInt64(service_worker_registration_id))
return false;
return true;
}
// Called when the "notificationclick" event in the Service Worker has finished
// executing for a notification that was created in a previous instance of the
// browser.
void OnEventDispatchComplete(content::PersistentNotificationStatus status) {
// TODO(peter): Add UMA statistics based on |status|.
// TODO(peter): Decide if we want to forcefully shut down the browser process
// if we're confident it was created for delivering this event.
}
} // namespace
// Called by the Java side when a notification event has been received, but the
// NotificationUIManager has not been initialized yet. Enforce initialization of
// the class.
static void InitializeNotificationUIManager(JNIEnv* env, jclass clazz) {
g_browser_process->notification_ui_manager();
}
// static
NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) {
return new NotificationUIManagerAndroid();
}
NotificationUIManagerAndroid::NotificationUIManagerAndroid() {
java_object_.Reset(
Java_NotificationUIManager_create(
AttachCurrentThread(),
reinterpret_cast<intptr_t>(this),
base::android::GetApplicationContext()));
// TODO(peter): Synchronize notifications with the Java side.
}
NotificationUIManagerAndroid::~NotificationUIManagerAndroid() {
Java_NotificationUIManager_destroy(AttachCurrentThread(),
java_object_.obj());
}
bool NotificationUIManagerAndroid::OnNotificationClicked(
JNIEnv* env,
jobject java_object,
jstring notification_id,
jint platform_id,
jbyteArray serialized_notification_data) {
std::string id = ConvertJavaStringToUTF8(env, notification_id);
auto iter = profile_notifications_.find(id);
if (iter != profile_notifications_.end()) {
const Notification& notification = iter->second->notification();
notification.delegate()->Click();
return true;
}
// If the Notification were not found, it may be a persistent notification
// that outlived the Chrome browser process. In this case, try to
// unserialize the notification's serialized data and trigger the click
// event manually.
std::vector<uint8> bytes;
base::android::JavaByteArrayToByteVector(env, serialized_notification_data,
&bytes);
if (!bytes.size())
return false;
content::PlatformNotificationData notification_data;
GURL origin;
int64 service_worker_registration_id;
Pickle pickle(reinterpret_cast<const char*>(&bytes[0]), bytes.size());
if (!UnserializePersistentNotification(pickle, ¬ification_data, &origin,
&service_worker_registration_id)) {
return false;
}
// Store the platform Id of this notification so that it can be closed.
platform_notifications_[id] = platform_id;
PlatformNotificationServiceImpl* service =
PlatformNotificationServiceImpl::GetInstance();
// TODO(peter): Rather than assuming that the last used profile is the
// appropriate one for this notification, the used profile should be
// stored as part of the notification's data. See https://crbug.com/437574.
service->OnPersistentNotificationClick(
ProfileManager::GetLastUsedProfile(),
service_worker_registration_id,
id,
origin,
notification_data,
base::Bind(&OnEventDispatchComplete));
return true;
}
bool NotificationUIManagerAndroid::OnNotificationClosed(
JNIEnv* env, jobject java_object, jstring notification_id) {
std::string id = ConvertJavaStringToUTF8(env, notification_id);
auto iter = profile_notifications_.find(id);
if (iter == profile_notifications_.end())
return false;
const Notification& notification = iter->second->notification();
notification.delegate()->Close(true /** by_user **/);
return true;
}
void NotificationUIManagerAndroid::Add(const Notification& notification,
Profile* profile) {
if (Update(notification, profile))
return;
ProfileNotification* profile_notification =
new ProfileNotification(profile, notification);
// Takes ownership of |profile_notification|.
AddProfileNotification(profile_notification);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> id = ConvertUTF8ToJavaString(
env, profile_notification->notification().id());
ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString(
env, notification.title());
ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString(
env, notification.message());
ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString(
env, notification.origin_url().GetOrigin().spec());
ScopedJavaLocalRef<jobject> icon;
SkBitmap icon_bitmap = notification.icon().AsBitmap();
if (!icon_bitmap.isNull())
icon = gfx::ConvertToJavaBitmap(&icon_bitmap);
ScopedJavaLocalRef<jbyteArray> notification_data;
if (true /** is_persistent_notification **/) {
PersistentNotificationDelegate* delegate =
static_cast<PersistentNotificationDelegate*>(notification.delegate());
scoped_ptr<Pickle> pickle = SerializePersistentNotification(
delegate->notification_data(),
notification.origin_url(),
delegate->service_worker_registration_id());
if (!pickle) {
LOG(ERROR) <<
"Unable to serialize the notification, payload too large (max 1MB).";
RemoveProfileNotification(profile_notification);
return;
}
notification_data = base::android::ToJavaByteArray(
env, static_cast<const uint8*>(pickle->data()), pickle->size());
}
int platform_id = Java_NotificationUIManager_displayNotification(
env, java_object_.obj(), id.obj(), title.obj(), body.obj(), icon.obj(),
origin.obj(), notification_data.obj());
std::string notification_id = profile_notification->notification().id();
platform_notifications_[notification_id] = platform_id;
notification.delegate()->Display();
}
bool NotificationUIManagerAndroid::Update(const Notification& notification,
Profile* profile) {
const base::string16& replace_id = notification.replace_id();
if (replace_id.empty())
return false;
const GURL origin_url = notification.origin_url();
DCHECK(origin_url.is_valid());
for (const auto& iterator : profile_notifications_) {
ProfileNotification* profile_notification = iterator.second;
if (profile_notification->notification().replace_id() != replace_id ||
profile_notification->notification().origin_url() != origin_url ||
profile_notification->profile() != profile)
continue;
std::string notification_id = profile_notification->notification().id();
// TODO(peter): Use Android's native notification replacement mechanism.
// Right now FALSE is returned from this function even when we would be
// able to update the notification, so that Add() creates a new one.
RemoveProfileNotification(profile_notification);
break;
}
return false;
}
const Notification* NotificationUIManagerAndroid::FindById(
const std::string& delegate_id,
ProfileID profile_id) const {
std::string profile_notification_id =
ProfileNotification::GetProfileNotificationId(delegate_id, profile_id);
ProfileNotification* profile_notification =
FindProfileNotification(profile_notification_id);
if (!profile_notification)
return 0;
return &profile_notification->notification();
}
bool NotificationUIManagerAndroid::CancelById(const std::string& delegate_id,
ProfileID profile_id) {
std::string profile_notification_id =
ProfileNotification::GetProfileNotificationId(delegate_id, profile_id);
ProfileNotification* profile_notification =
FindProfileNotification(profile_notification_id);
if (profile_notification) {
RemoveProfileNotification(profile_notification);
return true;
}
// On Android, it's still possible that the notification can be closed in case
// the platform Id is known, even if no delegate exists. This happens when the
// browser process is started because of interaction with a Notification.
PlatformCloseNotification(delegate_id);
return true;
}
std::set<std::string>
NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
// |profile| may be invalid, so no calls must be made based on the instance.
std::set<std::string> delegate_ids;
for (auto iterator : profile_notifications_) {
ProfileNotification* profile_notification = iterator.second;
if (profile_notification->notification().origin_url() == source &&
profile_notification->profile() == profile)
delegate_ids.insert(profile_notification->notification().id());
}
return delegate_ids;
}
bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
const GURL& source_origin) {
bool removed = true;
for (auto iterator = profile_notifications_.begin();
iterator != profile_notifications_.end();) {
auto current_iterator = iterator++;
ProfileNotification* profile_notification = current_iterator->second;
if (profile_notification->notification().origin_url() == source_origin) {
RemoveProfileNotification(profile_notification);
removed = true;
}
}
return removed;
}
bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id) {
bool removed = true;
for (auto iterator = profile_notifications_.begin();
iterator != profile_notifications_.end();) {
auto current_iterator = iterator++;
ProfileNotification* profile_notification = current_iterator->second;
ProfileID current_profile_id =
NotificationUIManager::GetProfileID(profile_notification->profile());
if (current_profile_id == profile_id) {
RemoveProfileNotification(profile_notification);
removed = true;
}
}
return removed;
}
void NotificationUIManagerAndroid::CancelAll() {
for (auto iterator : profile_notifications_) {
ProfileNotification* profile_notification = iterator.second;
PlatformCloseNotification(profile_notification->notification().id());
delete profile_notification;
}
profile_notifications_.clear();
}
bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) {
return RegisterNativesImpl(env);
}
void NotificationUIManagerAndroid::PlatformCloseNotification(
const std::string& notification_id) {
auto iterator = platform_notifications_.find(notification_id);
if (iterator == platform_notifications_.end())
return;
int platform_id = iterator->second;
platform_notifications_.erase(notification_id);
Java_NotificationUIManager_closeNotification(AttachCurrentThread(),
java_object_.obj(),
platform_id);
}
void NotificationUIManagerAndroid::AddProfileNotification(
ProfileNotification* profile_notification) {
std::string id = profile_notification->notification().id();
// Notification ids should be unique.
DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
profile_notifications_[id] = profile_notification;
}
void NotificationUIManagerAndroid::RemoveProfileNotification(
ProfileNotification* profile_notification) {
std::string notification_id = profile_notification->notification().id();
PlatformCloseNotification(notification_id);
profile_notifications_.erase(notification_id);
delete profile_notification;
}
ProfileNotification* NotificationUIManagerAndroid::FindProfileNotification(
const std::string& id) const {
auto iter = profile_notifications_.find(id);
if (iter == profile_notifications_.end())
return nullptr;
return iter->second;
}
|