Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(749)

Side by Side Diff: content/renderer/push_messaging_dispatcher.cc

Issue 219653002: Push API: send and receive IPC messages for registration. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/renderer/push_messaging_dispatcher.h"
6
7 #include "content/common/push_messaging_messages.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/public/platform/WebPushRegistration.h"
10
11 using blink::WebString;
12
13 namespace content {
14
15 namespace {
16
17 const char* PushMessagingStatusToString(PushMessagingStatus status) {
18 switch (status) {
19 case PUSH_MESSAGING_STATUS_OK:
20 return "Successful operation";
21 case PUSH_MESSAGING_STATUS_INVALID_PARAMETER:
22 return "Invalid parameter";
23 case PUSH_MESSAGING_STATUS_NOT_SIGNED_IN:
24 return "Profile not signed in";
25 case PUSH_MESSAGING_STATUS_OPERATION_PENDING:
26 return "Previous asynchronous operation is still pending";
27 case PUSH_MESSAGING_STATUS_NETWORK_ERROR:
28 return "Network socket error";
29 case PUSH_MESSAGING_STATUS_SERVER_ERROR:
30 return "Server error";
31 case PUSH_MESSAGING_STATUS_TTL_EXCEEDED:
32 return "Exceeded the specified TTL during message sending";
33 case PUSH_MESSAGING_STATUS_UNKNOWN_ERROR:
34 return "Unknown error";
35 }
36 NOTREACHED();
37 return "";
38 }
39
40 } // namespace
41
42 PushMessagingDispatcher::PushMessagingDispatcher(RenderViewImpl* render_view)
43 : RenderViewObserver(render_view) {}
44
45 PushMessagingDispatcher::~PushMessagingDispatcher() {}
46
47 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) {
48 bool handled = true;
49 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message)
50 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess)
51 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError)
52 IPC_MESSAGE_UNHANDLED(handled = false)
53 IPC_END_MESSAGE_MAP()
54 return handled;
55 }
56
57 void PushMessagingDispatcher::registerPushMessaging(
58 const WebString& sender_id,
59 blink::WebPushRegistrationCallbacks* callbacks) {
60 DCHECK(callbacks);
61 int callbacks_id = registration_callbacks_.Add(callbacks);
62 Send(new PushMessagingHostMsg_Register(
63 routing_id(), callbacks_id, sender_id.utf8()));
64 }
65
66 void PushMessagingDispatcher::OnRegisterSuccess(
67 int32 callbacks_id,
68 const std::string& endpoint,
69 const std::string& registration_id) {
70 blink::WebPushRegistrationCallbacks* callbacks =
71 registration_callbacks_.Lookup(callbacks_id);
72 CHECK(callbacks);
73
74 scoped_ptr<blink::WebPushRegistration> registration(
75 new blink::WebPushRegistration(
76 WebString::fromUTF8(endpoint),
77 WebString::fromUTF8(registration_id)));
78 callbacks->onSuccess(registration.get());
79 registration_callbacks_.Remove(callbacks_id);
80 }
81
82 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id,
83 PushMessagingStatus status) {
84 blink::WebPushRegistrationCallbacks* callbacks =
85 registration_callbacks_.Lookup(callbacks_id);
86 CHECK(callbacks);
87
88 scoped_ptr<blink::WebPushError> error(
89 new blink::WebPushError(
90 blink::WebPushError::ErrorTypeAbort,
91 WebString::fromUTF8(PushMessagingStatusToString(status))));
92 callbacks->onError(error.get());
93 registration_callbacks_.Remove(callbacks_id);
94 }
95
96 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698