Remove Observer methods in RTCVideoEncoderFactory.
[chromium-blink-merge.git] / content / renderer / push_messaging_dispatcher.cc
blob1de8da99d2d277d4daa1a63a0488f13dbf8a597c
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
3 // found in the LICENSE file.
5 #include "content/renderer/push_messaging_dispatcher.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/child/service_worker/web_service_worker_provider_impl.h"
9 #include "content/common/push_messaging_messages.h"
10 #include "content/renderer/manifest/manifest_manager.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "ipc/ipc_message.h"
13 #include "third_party/WebKit/public/platform/WebPushError.h"
14 #include "third_party/WebKit/public/platform/WebPushRegistration.h"
15 #include "third_party/WebKit/public/platform/WebServiceWorkerProvider.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
18 #include "url/gurl.h"
20 using blink::WebString;
22 namespace content {
24 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame)
25 : RenderFrameObserver(render_frame) {
28 PushMessagingDispatcher::~PushMessagingDispatcher() {}
30 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) {
31 bool handled = true;
32 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message)
33 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess)
34 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError)
35 IPC_MESSAGE_UNHANDLED(handled = false)
36 IPC_END_MESSAGE_MAP()
37 return handled;
40 void PushMessagingDispatcher::registerPushMessaging(
41 const WebString& sender_id,
42 blink::WebPushRegistrationCallbacks* callbacks,
43 blink::WebServiceWorkerProvider* service_worker_provider) {
44 RenderFrameImpl::FromRoutingID(routing_id())->manifest_manager()->GetManifest(
45 base::Bind(&PushMessagingDispatcher::DoRegister,
46 base::Unretained(this),
47 sender_id.utf8(),
48 callbacks,
49 service_worker_provider));
52 void PushMessagingDispatcher::DoRegister(
53 const std::string& sender_id,
54 blink::WebPushRegistrationCallbacks* callbacks,
55 blink::WebServiceWorkerProvider* service_worker_provider,
56 const Manifest& manifest) {
57 DCHECK(callbacks);
58 int callbacks_id = registration_callbacks_.Add(callbacks);
59 int service_worker_provider_id = static_cast<WebServiceWorkerProviderImpl*>(
60 service_worker_provider)->provider_id();
61 Send(new PushMessagingHostMsg_Register(
62 routing_id(),
63 callbacks_id,
64 manifest.gcm_sender_id.is_null()
65 ? sender_id
66 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()),
67 blink::WebUserGestureIndicator::isProcessingUserGesture(),
68 service_worker_provider_id));
71 void PushMessagingDispatcher::OnRegisterSuccess(
72 int32 callbacks_id,
73 const GURL& endpoint,
74 const std::string& registration_id) {
75 blink::WebPushRegistrationCallbacks* callbacks =
76 registration_callbacks_.Lookup(callbacks_id);
77 CHECK(callbacks);
79 scoped_ptr<blink::WebPushRegistration> registration(
80 new blink::WebPushRegistration(
81 WebString::fromUTF8(endpoint.spec()),
82 WebString::fromUTF8(registration_id)));
83 callbacks->onSuccess(registration.release());
84 registration_callbacks_.Remove(callbacks_id);
87 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id,
88 PushMessagingStatus status) {
89 blink::WebPushRegistrationCallbacks* callbacks =
90 registration_callbacks_.Lookup(callbacks_id);
91 CHECK(callbacks);
93 scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
94 blink::WebPushError::ErrorTypeAbort,
95 WebString::fromUTF8(PushMessagingStatusToString(status))));
96 callbacks->onError(error.release());
97 registration_callbacks_.Remove(callbacks_id);
100 } // namespace content