OLD | NEW |
---|---|
(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 namespace { | |
12 | |
13 const char* PushMessagingStatusToString(content::PushMessagingStatus status) { | |
14 switch (status) { | |
15 case content::PUSH_MESSAGING_STATUS_OK: | |
16 return "Successful operation"; | |
17 case content::PUSH_MESSAGING_STATUS_INVALID_PARAMETER: | |
18 return "Invalid parameter"; | |
19 case content::PUSH_MESSAGING_STATUS_NOT_SIGNED_IN: | |
20 return "Profile not signed in"; | |
21 case content::PUSH_MESSAGING_STATUS_OPERATION_PENDING: | |
22 return "Previous asynchronous operation is still pending"; | |
23 case content::PUSH_MESSAGING_STATUS_NETWORK_ERROR: | |
24 return "Network socket error"; | |
25 case content::PUSH_MESSAGING_STATUS_SERVER_ERROR: | |
26 return "Server error"; | |
27 case content::PUSH_MESSAGING_STATUS_TTL_EXCEEDED: | |
28 return "Exceeded the specified TTL during message sending"; | |
29 case content::PUSH_MESSAGING_STATUS_UNKNOWN_ERROR: | |
30 return "Unknown error"; | |
31 } | |
32 NOTREACHED(); | |
33 return ""; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 namespace content { | |
39 | |
40 PushMessagingDispatcher::PushMessagingDispatcher(RenderViewImpl* render_view) | |
41 : RenderViewObserver(render_view) {} | |
42 | |
43 PushMessagingDispatcher::~PushMessagingDispatcher() {} | |
44 | |
45 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { | |
46 bool handled = true; | |
47 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) | |
48 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess) | |
49 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError) | |
50 IPC_MESSAGE_UNHANDLED(handled = false) | |
51 IPC_END_MESSAGE_MAP() | |
52 return handled; | |
53 } | |
54 | |
55 void PushMessagingDispatcher::registerPushMessaging( | |
56 const blink::WebString& sender_id, | |
57 blink::WebPushRegistrationCallbacks* callbacks) { | |
58 DCHECK(callbacks); | |
59 int callbacks_id = registration_callbacks_.Add(callbacks); | |
60 Send(new PushMessagingHostMsg_Register( | |
61 routing_id(), callbacks_id, sender_id.utf8())); | |
62 } | |
63 | |
64 void PushMessagingDispatcher::OnRegisterSuccess( | |
65 int32 callbacks_id, | |
66 const std::string& endpoint, | |
67 const std::string& registration_id) { | |
68 blink::WebPushRegistrationCallbacks* callbacks = | |
69 registration_callbacks_.Lookup(callbacks_id); | |
70 CHECK(callbacks); | |
71 if (!callbacks) | |
72 return; | |
73 | |
74 scoped_ptr<blink::WebPushRegistration> registration( | |
75 new blink::WebPushRegistration( | |
76 blink::WebString::fromUTF8(endpoint), | |
77 blink::WebString::fromUTF8(registration_id))); | |
Peter Beverloo
2014/04/02 14:46:05
You could use "using blink::WebString" to avoid a
Michael van Ouwerkerk
2014/04/02 15:49:30
Done.
| |
78 callbacks->onSuccess(registration.release()); | |
Peter Beverloo
2014/04/02 14:46:05
You want to use .get() here, since .release() will
Michael van Ouwerkerk
2014/04/02 15:49:30
Done.
| |
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 DCHECK(callbacks); | |
Peter Beverloo
2014/04/02 14:46:05
Same comment as I left on line 70 in my previous r
Michael van Ouwerkerk
2014/04/02 15:49:30
Done.
| |
87 if (!callbacks) | |
88 return; | |
89 | |
90 scoped_ptr<blink::WebPushError> error( | |
91 new blink::WebPushError( | |
92 blink::WebPushError::ErrorTypeAbort, | |
93 blink::WebString::fromUTF8(PushMessagingStatusToString(status)))); | |
94 callbacks->onError(error.release()); | |
95 registration_callbacks_.Remove(callbacks_id); | |
96 } | |
97 | |
98 } // namespace content | |
OLD | NEW |