Skip to content

Commit

Permalink
Push API: send and receive IPC messages for registration.
Browse files Browse the repository at this point in the history
BUG=350378

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262419 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mvanouwerkerk@chromium.org committed Apr 8, 2014
1 parent fbe8f04 commit e1c1741
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 0 deletions.
63 changes: 63 additions & 0 deletions content/browser/push_messaging_message_filter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 "content/browser/push_messaging_message_filter.h"

#include <string>

#include "content/common/push_messaging_messages.h"
#include "content/public/browser/browser_thread.h"

namespace content {

PushMessagingMessageFilter::PushMessagingMessageFilter()
: BrowserMessageFilter(PushMessagingMsgStart) {}

PushMessagingMessageFilter::~PushMessagingMessageFilter() {}

bool PushMessagingMessageFilter::OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(PushMessagingMessageFilter, message, *message_was_ok)
IPC_MESSAGE_HANDLER(PushMessagingHostMsg_Register, OnRegister)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}

void PushMessagingMessageFilter::OnRegister(int routing_id,
int callbacks_id,
const std::string& sender_id) {
// TODO(mvanouwerkerk): Really implement, the below simply returns an error.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
GURL endpoint = GURL("https://android.googleapis.com/gcm/send");
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&PushMessagingMessageFilter::DidRegister,
this,
routing_id,
callbacks_id,
endpoint,
"",
true));

}

void PushMessagingMessageFilter::DidRegister(int routing_id,
int callbacks_id,
const GURL& endpoint,
const std::string& registration_id,
bool error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!error) {
Send(new PushMessagingMsg_RegisterSuccess(routing_id,
callbacks_id,
endpoint,
registration_id));
} else {
Send(new PushMessagingMsg_RegisterError(routing_id, callbacks_id));
}
}

} // namespace content
41 changes: 41 additions & 0 deletions content/browser/push_messaging_message_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

#ifndef CONTENT_BROWSER_PUSH_MESSAGING_MESSAGE_FILTER_H_
#define CONTENT_BROWSER_PUSH_MESSAGING_MESSAGE_FILTER_H_

#include <string>

#include "content/public/browser/browser_message_filter.h"
#include "url/gurl.h"

namespace content {

class PushMessagingMessageFilter : public BrowserMessageFilter {
public:
PushMessagingMessageFilter();

private:
virtual ~PushMessagingMessageFilter();

// BrowserMessageFilter implementation.
virtual bool OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) OVERRIDE;

void OnRegister(int routing_id,
int callbacks_id,
const std::string& sender_id);

void DidRegister(int routing_id,
int callbacks_id,
const GURL& endpoint,
const std::string& registration_id,
bool error);

DISALLOW_COPY_AND_ASSIGN(PushMessagingMessageFilter);
};

} // namespace content

#endif // CONTENT_BROWSER_PUSH_MESSAGING_MESSAGE_FILTER_H_
2 changes: 2 additions & 0 deletions content/browser/renderer_host/render_process_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include "content/browser/mime_registry_message_filter.h"
#include "content/browser/plugin_service_impl.h"
#include "content/browser/profiler_message_filter.h"
#include "content/browser/push_messaging_message_filter.h"
#include "content/browser/quota_dispatcher_host.h"
#include "content/browser/renderer_host/clipboard_message_filter.h"
#include "content/browser/renderer_host/database_message_filter.h"
Expand Down Expand Up @@ -834,6 +835,7 @@ void RenderProcessHostImpl::CreateMessageFilters() {
AddFilter(new VibrationMessageFilter());
screen_orientation_dispatcher_host_ = new ScreenOrientationDispatcherHost();
AddFilter(screen_orientation_dispatcher_host_);
AddFilter(new PushMessagingMessageFilter());
}

int RenderProcessHostImpl::GetNextRoutingID() {
Expand Down
1 change: 1 addition & 0 deletions content/common/content_message_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "content/common/pepper_messages.h"
#include "content/common/plugin_process_messages.h"
#include "content/common/power_monitor_messages.h"
#include "content/common/push_messaging_messages.h"
#include "content/common/quota_messages.h"
#include "content/common/resource_messages.h"
#include "content/common/screen_orientation_messages.h"
Expand Down
28 changes: 28 additions & 0 deletions content/common/push_messaging_messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.

// IPC messages for push messaging.
// Multiply-included message file, hence no include guard.

#include "ipc/ipc_message_macros.h"
#include "url/gurl.h"

#define IPC_MESSAGE_START PushMessagingMsgStart

// Messages sent from the browser to the renderer.

IPC_MESSAGE_ROUTED3(PushMessagingMsg_RegisterSuccess,
int32 /* callbacks_id */,
GURL /* endpoint */,
std::string /* registration_id */)

IPC_MESSAGE_ROUTED1(PushMessagingMsg_RegisterError,
int32 /* callbacks_id */)

// Messages sent from the renderer to the browser.

IPC_MESSAGE_CONTROL3(PushMessagingHostMsg_Register,
int32 /* routing_id */,
int32 /* callbacks_id */,
std::string /* sender_id */)
2 changes: 2 additions & 0 deletions content/content_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,8 @@
'browser/profiler_controller_impl.h',
'browser/profiler_message_filter.cc',
'browser/profiler_message_filter.h',
'browser/push_messaging_message_filter.cc',
'browser/push_messaging_message_filter.h',
'browser/quota_dispatcher_host.cc',
'browser/quota_dispatcher_host.h',
'browser/renderer_data_memoizing_store.h',
Expand Down
1 change: 1 addition & 0 deletions content/content_common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@
'common/plugin_process_messages.h',
'common/power_monitor_messages.h',
'common/process_type.cc',
'common/push_messaging_messages.h',
'common/quota_messages.h',
'common/resource_messages.cc',
'common/resource_messages.h',
Expand Down
2 changes: 2 additions & 0 deletions content/content_renderer.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@
'renderer/pepper/usb_key_code_conversion_win.cc',
'renderer/pepper/v8_var_converter.cc',
'renderer/pepper/v8_var_converter.h',
'renderer/push_messaging_dispatcher.cc',
'renderer/push_messaging_dispatcher.h',
'renderer/render_frame_impl.cc',
'renderer/render_frame_impl.h',
'renderer/render_process.h',
Expand Down
73 changes: 73 additions & 0 deletions content/renderer/push_messaging_dispatcher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 "content/renderer/push_messaging_dispatcher.h"

#include "content/common/push_messaging_messages.h"
#include "content/renderer/render_view_impl.h"
#include "ipc/ipc_message.h"
#include "third_party/WebKit/public/platform/WebPushError.h"
#include "third_party/WebKit/public/platform/WebPushRegistration.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "url/gurl.h"

using blink::WebString;

namespace content {

PushMessagingDispatcher::PushMessagingDispatcher(RenderViewImpl* render_view)
: RenderViewObserver(render_view) {}

PushMessagingDispatcher::~PushMessagingDispatcher() {}

bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message)
IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess)
IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}

void PushMessagingDispatcher::registerPushMessaging(
const WebString& sender_id,
blink::WebPushRegistrationCallbacks* callbacks) {
DCHECK(callbacks);
int callbacks_id = registration_callbacks_.Add(callbacks);
Send(new PushMessagingHostMsg_Register(
routing_id(), callbacks_id, sender_id.utf8()));
}

void PushMessagingDispatcher::OnRegisterSuccess(
int32 callbacks_id,
const GURL& endpoint,
const std::string& registration_id) {
blink::WebPushRegistrationCallbacks* callbacks =
registration_callbacks_.Lookup(callbacks_id);
CHECK(callbacks);

scoped_ptr<blink::WebPushRegistration> registration(
new blink::WebPushRegistration(
WebString::fromUTF8(endpoint.spec()),
WebString::fromUTF8(registration_id)));
callbacks->onSuccess(registration.release());
registration_callbacks_.Remove(callbacks_id);
}

void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id) {
const std::string kAbortErrorReason = "Registration failed.";
blink::WebPushRegistrationCallbacks* callbacks =
registration_callbacks_.Lookup(callbacks_id);
CHECK(callbacks);

scoped_ptr<blink::WebPushError> error(
new blink::WebPushError(
blink::WebPushError::ErrorTypeAbort,
WebString::fromUTF8(kAbortErrorReason)));
callbacks->onError(error.release());
registration_callbacks_.Remove(callbacks_id);
}

} // namespace content
56 changes: 56 additions & 0 deletions content/renderer/push_messaging_dispatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.

#ifndef CONTENT_RENDERER_PUSH_MESSAGING_DISPATCHER_H_
#define CONTENT_RENDERER_PUSH_MESSAGING_DISPATCHER_H_

#include <string>

#include "base/id_map.h"
#include "content/public/renderer/render_view_observer.h"
#include "third_party/WebKit/public/platform/WebPushClient.h"

class GURL;

namespace IPC {
class Message;
} // namespace IPC

namespace blink {
class WebString;
} // namespace blink

namespace content {
class RenderViewImpl;

class PushMessagingDispatcher : public RenderViewObserver,
public blink::WebPushClient {
public:
explicit PushMessagingDispatcher(RenderViewImpl* render_view);
virtual ~PushMessagingDispatcher();

private:
// RenderView::Observer implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;

// WebPushClient implementation.
virtual void registerPushMessaging(
const blink::WebString& sender_id,
blink::WebPushRegistrationCallbacks* callbacks) OVERRIDE;

void OnRegisterSuccess(int32 callbacks_id,
const GURL& endpoint,
const std::string& registration_id);

void OnRegisterError(int32 callbacks_id);

IDMap<blink::WebPushRegistrationCallbacks, IDMapOwnPointer>
registration_callbacks_;

DISALLOW_COPY_AND_ASSIGN(PushMessagingDispatcher);
};

} // namespace content

#endif // CONTENT_RENDERER_PUSH_MESSAGING_DISPATCHER_H_
8 changes: 8 additions & 0 deletions content/renderer/render_view_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#include "content/renderer/memory_benchmarking_extension.h"
#include "content/renderer/mhtml_generator.h"
#include "content/renderer/notification_provider.h"
#include "content/renderer/push_messaging_dispatcher.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_process.h"
#include "content/renderer/render_thread_impl.h"
Expand Down Expand Up @@ -666,6 +667,7 @@ RenderViewImpl::RenderViewImpl(RenderViewImplParams* params)
cached_has_main_frame_vertical_scrollbar_(false),
has_scrolled_focused_editable_node_into_rect_(false),
notification_provider_(NULL),
push_messaging_dispatcher_(NULL),
geolocation_dispatcher_(NULL),
input_tag_speech_dispatcher_(NULL),
speech_recognition_dispatcher_(NULL),
Expand Down Expand Up @@ -4394,6 +4396,12 @@ blink::WebMIDIClient* RenderViewImpl::webMIDIClient() {
return midi_dispatcher_;
}

blink::WebPushClient* RenderViewImpl::webPushClient() {
if (!push_messaging_dispatcher_)
push_messaging_dispatcher_ = new PushMessagingDispatcher(this);
return push_messaging_dispatcher_;
}

void RenderViewImpl::draggableRegionsChanged() {
FOR_EACH_OBSERVER(
RenderViewObserver,
Expand Down
5 changes: 5 additions & 0 deletions content/renderer/render_view_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class MouseLockDispatcher;
class NavigationState;
class NotificationProvider;
class PepperPluginInstanceImpl;
class PushMessagingDispatcher;
class RenderViewObserver;
class RenderViewTest;
class RendererAccessibility;
Expand Down Expand Up @@ -503,6 +504,7 @@ class CONTENT_EXPORT RenderViewImpl
virtual blink::WebPageVisibilityState visibilityState() const;
virtual blink::WebUserMediaClient* userMediaClient();
virtual blink::WebMIDIClient* webMIDIClient();
virtual blink::WebPushClient* webPushClient();
virtual void draggableRegionsChanged();

#if defined(OS_ANDROID)
Expand Down Expand Up @@ -1244,6 +1246,9 @@ class CONTENT_EXPORT RenderViewImpl
// Holds a reference to the service which provides desktop notifications.
NotificationProvider* notification_provider_;

// The push messaging dispatcher attached to this view, lazily initialized.
PushMessagingDispatcher* push_messaging_dispatcher_;

// The geolocation dispatcher attached to this view, lazily initialized.
GeolocationDispatcher* geolocation_dispatcher_;

Expand Down
1 change: 1 addition & 0 deletions ipc/ipc_message_start.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ enum IPCMessageStart {
ChromeExtensionMsgStart,
MojoMsgStart,
TranslateMsgStart,
PushMessagingMsgStart,
LastIPCMsgStart // Must come last.
};

Expand Down

0 comments on commit e1c1741

Please sign in to comment.