Commit f6dd3c33 authored by peter's avatar peter Committed by Commit bot

Implement getting notifications up to the browser process.

This CL implements the ability to get notifications for a given Service
Worker up to the point where it hits the browser process, where there's
a NOTIMPLEMENTED() waiting for now. The browser process then replies
with an empty vector of open notifications.

The second part of the implementation depends on the Notification
Database, which is still an in-progress work.

This CL is part of a three-sided patch:
  [1] https://codereview.chromium.org/1018613003/
  [2] This CL.
  [3] https://codereview.chromium.org/1016843002/

BUG=442143

Review URL: https://codereview.chromium.org/1014073002

Cr-Commit-Position: refs/heads/master@{#321564}
parent 5444194b
......@@ -44,6 +44,8 @@ bool NotificationMessageFilter::OnMessageReceived(const IPC::Message& message) {
OnShowPlatformNotification)
IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_ShowPersistent,
OnShowPersistentNotification)
IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_GetNotifications,
OnGetNotifications)
IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_Close,
OnClosePlatformNotification)
IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_ClosePersistent,
......@@ -129,6 +131,22 @@ void NotificationMessageFilter::OnShowPersistentNotification(
icon, notification_data);
}
void NotificationMessageFilter::OnGetNotifications(
int request_id,
int64_t service_worker_registration_id,
const GURL& origin,
const std::string& filter_tag) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(peter): Implement retrieval of persistent Web Notifications from the
// database. Reply with an empty vector until this has been implemented.
// Tracked in https://crbug.com/442143.
Send(new PlatformNotificationMsg_DidGetNotifications(
request_id,
std::vector<PersistentNotificationInfo>()));
}
void NotificationMessageFilter::OnClosePlatformNotification(
int notification_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
......
......@@ -55,6 +55,10 @@ class NotificationMessageFilter : public BrowserMessageFilter {
const GURL& origin,
const SkBitmap& icon,
const PlatformNotificationData& notification_data);
void OnGetNotifications(int request_id,
int64_t service_worker_registration_id,
const GURL& origin,
const std::string& filter_tag);
void OnClosePlatformNotification(int notification_id);
void OnClosePersistentNotification(
const GURL& origin,
......
......@@ -4,20 +4,23 @@
#include "content/child/notifications/notification_dispatcher.h"
#include <limits>
#include "content/child/notifications/notification_manager.h"
#include "content/common/platform_notification_messages.h"
namespace content {
NotificationDispatcher::NotificationDispatcher(
ThreadSafeSender* thread_safe_sender)
: WorkerThreadMessageFilter(thread_safe_sender), next_notification_id_(0) {
: WorkerThreadMessageFilter(thread_safe_sender) {
}
NotificationDispatcher::~NotificationDispatcher() {}
int NotificationDispatcher::GenerateNotificationId(int thread_id) {
base::AutoLock lock(notification_id_map_lock_);
CHECK_LT(next_notification_id_, std::numeric_limits<int>::max());
notification_id_map_[next_notification_id_] = thread_id;
return next_notification_id_++;
}
......
......@@ -34,7 +34,7 @@ class NotificationDispatcher : public WorkerThreadMessageFilter {
base::Lock notification_id_map_lock_;
NotificationIdToThreadId notification_id_map_;
int next_notification_id_;
int next_notification_id_ = 0;
DISALLOW_COPY_AND_ASSIGN(NotificationDispatcher);
};
......
......@@ -13,7 +13,6 @@
#include "content/child/service_worker/web_service_worker_registration_impl.h"
#include "content/child/thread_safe_sender.h"
#include "content/child/worker_task_runner.h"
#include "content/common/platform_notification_messages.h"
#include "content/public/common/platform_notification_data.h"
#include "third_party/WebKit/public/platform/WebSerializedOrigin.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationDelegate.h"
......@@ -114,6 +113,36 @@ void NotificationManager::showPersistent(
base::Passed(&owned_callbacks)));
}
void NotificationManager::getNotifications(
const blink::WebString& filter_tag,
blink::WebServiceWorkerRegistration* service_worker_registration,
blink::WebNotificationGetCallbacks* callbacks) {
DCHECK(service_worker_registration);
DCHECK(callbacks);
WebServiceWorkerRegistrationImpl* service_worker_registration_impl =
static_cast<WebServiceWorkerRegistrationImpl*>(
service_worker_registration);
GURL origin = GURL(service_worker_registration_impl->scope()).GetOrigin();
int64 service_worker_registration_id =
service_worker_registration_impl->registration_id();
// TODO(peter): GenerateNotificationId is more of a request id. Consider
// renaming the method in the NotificationDispatcher if this makes sense.
int request_id =
notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
pending_get_notification_requests_.AddWithID(callbacks, request_id);
thread_safe_sender_->Send(
new PlatformNotificationHostMsg_GetNotifications(
request_id,
service_worker_registration_id,
origin,
base::UTF16ToUTF8(filter_tag)));
}
void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
if (pending_notifications_.CancelPageNotificationFetches(delegate))
return;
......@@ -171,6 +200,8 @@ bool NotificationManager::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidGetNotifications,
OnDidGetNotifications)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
......@@ -202,6 +233,34 @@ void NotificationManager::OnDidClick(int notification_id) {
iter->second->dispatchClickEvent();
}
void NotificationManager::OnDidGetNotifications(
int request_id,
const std::vector<PersistentNotificationInfo>& notification_infos) {
blink::WebNotificationGetCallbacks* callbacks =
pending_get_notification_requests_.Lookup(request_id);
DCHECK(callbacks);
if (!callbacks)
return;
scoped_ptr<blink::WebVector<blink::WebPersistentNotificationInfo>>
notifications(new blink::WebVector<blink::WebPersistentNotificationInfo>(
notification_infos.size()));
for (size_t i = 0; i < notification_infos.size(); ++i) {
blink::WebPersistentNotificationInfo web_notification_info;
web_notification_info.persistentNotificationId =
blink::WebString::fromUTF8(notification_infos[i].first);
web_notification_info.data =
ToWebNotificationData(notification_infos[i].second);
(*notifications)[i] = web_notification_info;
}
callbacks->onSuccess(notifications.release());
pending_get_notification_requests_.Remove(request_id);
}
void NotificationManager::DisplayPageNotification(
const blink::WebSerializedOrigin& origin,
const blink::WebNotificationData& notification_data,
......
......@@ -7,18 +7,22 @@
#include <map>
#include <set>
#include <vector>
#include "base/id_map.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "content/child/notifications/notification_dispatcher.h"
#include "content/child/notifications/pending_notifications_tracker.h"
#include "content/child/worker_task_runner.h"
#include "content/common/platform_notification_messages.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationManager.h"
class SkBitmap;
namespace content {
struct PlatformNotificationData;
class ThreadSafeSender;
class NotificationManager : public blink::WebNotificationManager,
......@@ -45,6 +49,10 @@ class NotificationManager : public blink::WebNotificationManager,
const blink::WebNotificationData& notification_data,
blink::WebServiceWorkerRegistration* service_worker_registration,
blink::WebNotificationShowCallbacks* callbacks);
virtual void getNotifications(
const blink::WebString& filter_tag,
blink::WebServiceWorkerRegistration* service_worker_registration,
blink::WebNotificationGetCallbacks* callbacks);
virtual void close(blink::WebNotificationDelegate* delegate);
virtual void closePersistent(
const blink::WebSerializedOrigin& origin,
......@@ -67,6 +75,9 @@ class NotificationManager : public blink::WebNotificationManager,
void OnDidShow(int notification_id);
void OnDidClose(int notification_id);
void OnDidClick(int notification_id);
void OnDidGetNotifications(
int request_id,
const std::vector<PersistentNotificationInfo>& notification_infos);
// To be called when a page notification is ready to be displayed. Will
// inform the browser process about all available data. The |delegate|,
......@@ -96,6 +107,10 @@ class NotificationManager : public blink::WebNotificationManager,
// ones, until all their associated resources have been fetched.
PendingNotificationsTracker pending_notifications_;
// Tracks pending requests for getting a list of notifications.
IDMap<blink::WebNotificationGetCallbacks, IDMapOwnPointer>
pending_get_notification_requests_;
// Map to store the delegate associated with a notification request Id.
std::map<int, blink::WebNotificationDelegate*> active_page_notifications_;
......
......@@ -5,11 +5,27 @@
// Messages for platform-native notifications using the Web Notification API.
// Multiply-included message file, hence no include guard.
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
#include "content/public/common/platform_notification_data.h"
#include "ipc/ipc_message_macros.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationPermission.h"
#include "third_party/skia/include/core/SkBitmap.h"
// Singly-included section for type definitions.
#ifndef CONTENT_COMMON_PLATFORM_NOTIFICATION_MESSAGES_H_
#define CONTENT_COMMON_PLATFORM_NOTIFICATION_MESSAGES_H_
// Defines the pair of [persistent notification id] => [notification data] used
// when getting the notifications for a given Service Worker registration.
using PersistentNotificationInfo =
std::pair<std::string, content::PlatformNotificationData>;
#endif // CONTENT_COMMON_PLATFORM_NOTIFICATION_MESSAGES_H_
#define IPC_MESSAGE_START PlatformNotificationMsgStart
IPC_ENUM_TRAITS_MAX_VALUE(blink::WebNotificationPermission,
......@@ -43,6 +59,13 @@ IPC_MESSAGE_CONTROL1(PlatformNotificationMsg_DidClose,
IPC_MESSAGE_CONTROL1(PlatformNotificationMsg_DidClick,
int /* notification_id */)
// Reply to PlatformNotificationHostMsg_GetNotifications sharing a vector of
// available notifications per the request's constraints.
IPC_MESSAGE_CONTROL2(PlatformNotificationMsg_DidGetNotifications,
int /* request_id */,
std::vector<PersistentNotificationInfo>
/* notifications */)
// Messages sent from the renderer to the browser.
IPC_MESSAGE_CONTROL4(PlatformNotificationHostMsg_Show,
......@@ -52,11 +75,17 @@ IPC_MESSAGE_CONTROL4(PlatformNotificationHostMsg_Show,
content::PlatformNotificationData /* notification_data */)
IPC_MESSAGE_CONTROL4(PlatformNotificationHostMsg_ShowPersistent,
int64 /* service_worker_provider_id */,
int64_t /* service_worker_registration_id */,
GURL /* origin */,
SkBitmap /* icon */,
content::PlatformNotificationData /* notification_data */)
IPC_MESSAGE_CONTROL4(PlatformNotificationHostMsg_GetNotifications,
int /* request_id */,
int64_t /* service_worker_registration_id */,
GURL /* origin */,
std::string /* filter_tag */)
IPC_MESSAGE_CONTROL1(PlatformNotificationHostMsg_Close,
int /* notification_id */)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment