From e1c17416d0aa25b2646858f940fd89e461682fcd Mon Sep 17 00:00:00 2001 From: "mvanouwerkerk@chromium.org" Date: Tue, 8 Apr 2014 15:39:31 +0000 Subject: [PATCH] Push API: send and receive IPC messages for registration. 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 --- .../browser/push_messaging_message_filter.cc | 63 ++++++++++++++++ .../browser/push_messaging_message_filter.h | 41 +++++++++++ .../renderer_host/render_process_host_impl.cc | 2 + content/common/content_message_generator.h | 1 + content/common/push_messaging_messages.h | 28 +++++++ content/content_browser.gypi | 2 + content/content_common.gypi | 1 + content/content_renderer.gypi | 2 + content/renderer/push_messaging_dispatcher.cc | 73 +++++++++++++++++++ content/renderer/push_messaging_dispatcher.h | 56 ++++++++++++++ content/renderer/render_view_impl.cc | 8 ++ content/renderer/render_view_impl.h | 5 ++ ipc/ipc_message_start.h | 1 + 13 files changed, 283 insertions(+) create mode 100644 content/browser/push_messaging_message_filter.cc create mode 100644 content/browser/push_messaging_message_filter.h create mode 100644 content/common/push_messaging_messages.h create mode 100644 content/renderer/push_messaging_dispatcher.cc create mode 100644 content/renderer/push_messaging_dispatcher.h diff --git a/content/browser/push_messaging_message_filter.cc b/content/browser/push_messaging_message_filter.cc new file mode 100644 index 00000000000000..fa8e8a5286aaa5 --- /dev/null +++ b/content/browser/push_messaging_message_filter.cc @@ -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 + +#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 diff --git a/content/browser/push_messaging_message_filter.h b/content/browser/push_messaging_message_filter.h new file mode 100644 index 00000000000000..1eb70030d7f4e7 --- /dev/null +++ b/content/browser/push_messaging_message_filter.h @@ -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 + +#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_ diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc index e11e4c7a29ee5a..77248d43f2a04e 100644 --- a/content/browser/renderer_host/render_process_host_impl.cc +++ b/content/browser/renderer_host/render_process_host_impl.cc @@ -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" @@ -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() { diff --git a/content/common/content_message_generator.h b/content/common/content_message_generator.h index 31bd785da77f2d..f22c6586750d40 100644 --- a/content/common/content_message_generator.h +++ b/content/common/content_message_generator.h @@ -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" diff --git a/content/common/push_messaging_messages.h b/content/common/push_messaging_messages.h new file mode 100644 index 00000000000000..75b2b68472ac80 --- /dev/null +++ b/content/common/push_messaging_messages.h @@ -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 */) diff --git a/content/content_browser.gypi b/content/content_browser.gypi index e850c4b8d1c9e0..5ee4f05727e312 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -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', diff --git a/content/content_common.gypi b/content/content_common.gypi index 01b5f42ec555cb..c4096fff7f25c6 100644 --- a/content/content_common.gypi +++ b/content/content_common.gypi @@ -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', diff --git a/content/content_renderer.gypi b/content/content_renderer.gypi index 7250d835a764c8..0d523334fee435 100644 --- a/content/content_renderer.gypi +++ b/content/content_renderer.gypi @@ -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', diff --git a/content/renderer/push_messaging_dispatcher.cc b/content/renderer/push_messaging_dispatcher.cc new file mode 100644 index 00000000000000..136862caef2870 --- /dev/null +++ b/content/renderer/push_messaging_dispatcher.cc @@ -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 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 error( + new blink::WebPushError( + blink::WebPushError::ErrorTypeAbort, + WebString::fromUTF8(kAbortErrorReason))); + callbacks->onError(error.release()); + registration_callbacks_.Remove(callbacks_id); +} + +} // namespace content diff --git a/content/renderer/push_messaging_dispatcher.h b/content/renderer/push_messaging_dispatcher.h new file mode 100644 index 00000000000000..ad441b24f0363f --- /dev/null +++ b/content/renderer/push_messaging_dispatcher.h @@ -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 + +#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 + registration_callbacks_; + + DISALLOW_COPY_AND_ASSIGN(PushMessagingDispatcher); +}; + +} // namespace content + +#endif // CONTENT_RENDERER_PUSH_MESSAGING_DISPATCHER_H_ diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc index f99c6ff3c26fd7..0d4c45e0623669 100644 --- a/content/renderer/render_view_impl.cc +++ b/content/renderer/render_view_impl.cc @@ -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" @@ -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), @@ -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, diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h index e08d17d492a8af..1527bcad82de9e 100644 --- a/content/renderer/render_view_impl.h +++ b/content/renderer/render_view_impl.h @@ -150,6 +150,7 @@ class MouseLockDispatcher; class NavigationState; class NotificationProvider; class PepperPluginInstanceImpl; +class PushMessagingDispatcher; class RenderViewObserver; class RenderViewTest; class RendererAccessibility; @@ -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) @@ -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_; diff --git a/ipc/ipc_message_start.h b/ipc/ipc_message_start.h index 39a7851bcbe0d8..6474eabb723cfb 100644 --- a/ipc/ipc_message_start.h +++ b/ipc/ipc_message_start.h @@ -101,6 +101,7 @@ enum IPCMessageStart { ChromeExtensionMsgStart, MojoMsgStart, TranslateMsgStart, + PushMessagingMsgStart, LastIPCMsgStart // Must come last. };