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

Side by Side Diff: ppapi/cpp/helper/dev/websocket_api_dev.cc

Issue 8956008: WebSocket Pepper API: C++ utility class implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix completion callback leaks? Created 9 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "ppapi/cpp/helper/dev/websocket_api_dev.h"
6
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/pp_macros.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/dev/websocket_dev.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/module_impl.h"
14 #include "ppapi/cpp/var.h"
15
16 namespace pp {
17
18 namespace helper {
brettw 2011/12/21 18:31:57 There was a thread on the Pepper mailing list and
Takashi Toyoshima 2011/12/22 07:56:29 OK. I fix it.
19
20 class WebSocketAPI_Dev::Implement : public WebSocket_Dev {
21 public:
22 Implement(Instance* instance, WebSocketAPI_Dev* api)
23 : WebSocket_Dev(instance),
24 api_(api),
25 callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
26 }
27
28 ~Implement() {}
29
30 int32_t Connect(const Var& url, const Var protocols[],
31 uint32_t protocol_count) {
32 CompletionCallback callback =
33 callback_factory_.NewOptionalCallback(&Implement::DidConnect);
brettw 2011/12/21 18:31:57 Sorry if I wasn't clear in my email, I think we wa
Takashi Toyoshima 2011/12/22 07:56:29 I understand that you afraid of calling Connect or
34 int32_t result =
35 WebSocket_Dev::Connect(url, protocols, protocol_count, callback);
36 if (result != PP_OK_COMPLETIONPENDING) {
37 // Consumes callback here and uses PP_ERROR_ABORTED instead of result
38 // in order to avoid side effects in DidConnect.
39 callback.Run(PP_ERROR_ABORTED);
40 }
41 return result;
42 }
43
44 int32_t Close(uint16_t code, const Var& reason) {
45 CompletionCallback callback =
46 callback_factory_.NewOptionalCallback(&Implement::DidClose);
47 int32_t result = WebSocket_Dev::Close(code, reason, callback);
48 if (result != PP_OK_COMPLETIONPENDING) {
49 // Consumes callback here and uses PP_ERROR_ABORTED instead of result
50 // in order to avoid side effects in DidClose.
51 callback.Run(PP_ERROR_ABORTED);
52 }
53 return result;
54 }
55
56 void Receive() {
57 int32_t result;
58 do {
59 CompletionCallback callback =
60 callback_factory_.NewOptionalCallback(&Implement::DidReceive);
61 result = WebSocket_Dev::ReceiveMessage(&receive_message_var_, callback);
62 if (result != PP_OK_COMPLETIONPENDING)
63 callback.Run(result);
64 } while (result == PP_OK);
65 }
66
67 void DidConnect(int32_t result) {
68 if (result == PP_OK) {
69 api_->OnOpen();
70 Receive();
71 } else if (result != PP_ERROR_ABORTED) {
72 DidClose(result);
73 }
74 }
75
76 void DidReceive(int32_t result) {
77 if (result == PP_OK) {
78 api_->OnMessage(receive_message_var_);
79 Receive();
80 } else if (result != PP_ERROR_ABORTED) {
81 DidClose(result);
82 }
83 }
84
85 void DidClose(int32_t result) {
86 if (result == PP_ERROR_ABORTED)
87 return;
88 bool was_clean = GetCloseWasClean() && result == PP_OK;
89 if (!was_clean)
90 api_->OnError();
brettw 2011/12/21 18:31:57 Maybe we can sent the Pepper error value to OnErro
Takashi Toyoshima 2011/12/22 07:56:29 To tell the truth, only PP_ERROR_ABORTED and PP_OK
91 api_->OnClose(was_clean, GetCloseCode(), GetCloseReason());
92 }
93
94 private:
95 WebSocketAPI_Dev* api_;
96 CompletionCallbackFactory<Implement> callback_factory_;
97 Var receive_message_var_;
98 };
99
100 WebSocketAPI_Dev::WebSocketAPI_Dev(Instance* instance)
101 : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
102 }
103
104 WebSocketAPI_Dev::~WebSocketAPI_Dev() {
105 delete impl_;
106 }
107
108 int32_t WebSocketAPI_Dev::Connect(const Var& url, const Var protocols[],
109 uint32_t protocol_count) {
110 return impl_->Connect(url, protocols, protocol_count);
111 }
112
113 int32_t WebSocketAPI_Dev::Close(uint16_t code, const Var& reason) {
114 return impl_->Close(code, reason);
115 }
116
117 int32_t WebSocketAPI_Dev::Send(const Var& data) {
118 return impl_->SendMessage(data);
119 }
120
121 uint64_t WebSocketAPI_Dev::GetBufferedAmount() {
122 return impl_->GetBufferedAmount();
123 }
124
125 Var WebSocketAPI_Dev::GetExtensions() {
126 return impl_->GetExtensions();
127 }
128
129 Var WebSocketAPI_Dev::GetProtocol() {
130 return impl_->GetProtocol();
131 }
132
133 PP_WebSocketReadyState_Dev WebSocketAPI_Dev::GetReadyState() {
134 return impl_->GetReadyState();
135 }
136
137 Var WebSocketAPI_Dev::GetURL() {
138 return impl_->GetURL();
139 }
140
141 } // namespace helper
142
143 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698