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

Side by Side Diff: ppapi/utility/websocket/websocket_api.cc

Issue 8956008: WebSocket Pepper API: C++ utility class implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add unit tests Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/utility/websocket/websocket_api.h"
6
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/pp_macros.h"
9 #include "ppapi/cpp/dev/websocket_dev.h"
10 #include "ppapi/cpp/instance.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/module_impl.h"
13 #include "ppapi/cpp/var.h"
14 #include "ppapi/utility/completion_callback_factory.h"
15
16 namespace pp {
17
18 class WebSocketAPI::Implement : public WebSocket_Dev {
19 public:
20 Implement(Instance* instance, WebSocketAPI* api)
21 : WebSocket_Dev(instance),
22 api_(api),
23 callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
24 }
25
26 ~Implement() {}
dmichael (off chromium) 2012/02/01 23:05:18 style nit: virtual
Takashi Toyoshima 2012/02/03 07:58:27 Done.
27
28 int32_t Connect(const Var& url, const Var protocols[],
29 uint32_t protocol_count) {
30 CompletionCallback callback =
31 callback_factory_.NewOptionalCallback(&Implement::DidConnect);
32 int32_t result =
33 WebSocket_Dev::Connect(url, protocols, protocol_count, callback);
34 if (result != PP_OK_COMPLETIONPENDING) {
dmichael (off chromium) 2012/02/01 23:05:18 PP_OK is not possible here, right? If this class d
Takashi Toyoshima 2012/02/03 07:58:27 Yes. In successful cases it always returns asynchr
35 // Consumes callback here and uses PP_ERROR_ABORTED instead of result
36 // in order to avoid side effects in DidConnect.
37 callback.Run(PP_ERROR_ABORTED);
dmichael (off chromium) 2012/02/01 23:05:18 Do all your errors that are reported immediately h
Takashi Toyoshima 2012/02/03 07:58:27 Caller catches the error code as a return value of
38 }
39 return result;
40 }
41
42 int32_t Close(uint16_t code, const Var& reason) {
43 CompletionCallback callback =
44 callback_factory_.NewOptionalCallback(&Implement::DidClose);
45 int32_t result = WebSocket_Dev::Close(code, reason, callback);
46 if (result != PP_OK_COMPLETIONPENDING) {
dmichael (off chromium) 2012/02/01 23:05:18 ditto about documenting that PP_OK won't happen.
Takashi Toyoshima 2012/02/03 07:58:27 Ditto. I revise these comments to describe more cl
47 // Consumes callback here and uses PP_ERROR_ABORTED instead of result
48 // in order to avoid side effects in DidClose.
49 callback.Run(PP_ERROR_ABORTED);
50 }
51 return result;
52 }
53
54 void Receive() {
55 int32_t result;
56 do {
57 CompletionCallback callback =
58 callback_factory_.NewOptionalCallback(&Implement::DidReceive);
59 result = WebSocket_Dev::ReceiveMessage(&receive_message_var_, callback);
60 if (result != PP_OK_COMPLETIONPENDING)
61 callback.Run(result);
62 } while (result == PP_OK);
63 }
64
65 void DidConnect(int32_t result) {
66 if (result == PP_OK) {
67 api_->OnOpen();
68 Receive();
69 } else if (result != PP_ERROR_ABORTED) {
70 DidClose(result);
71 }
72 }
73
74 void DidReceive(int32_t result) {
75 if (result == PP_OK) {
76 api_->OnMessage(receive_message_var_);
77 Receive();
78 } else if (result != PP_ERROR_ABORTED) {
79 DidClose(result);
80 }
81 }
82
83 void DidClose(int32_t result) {
84 if (result == PP_ERROR_ABORTED)
85 return;
86 bool was_clean = GetCloseWasClean();
87 if (!was_clean)
88 api_->OnError();
89 api_->OnClose(was_clean, GetCloseCode(), GetCloseReason());
90 }
91
92 private:
93 WebSocketAPI* api_;
94 CompletionCallbackFactory<Implement> callback_factory_;
95 Var receive_message_var_;
96 };
97
98 WebSocketAPI::WebSocketAPI(Instance* instance)
99 : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
100 }
101
102 WebSocketAPI::~WebSocketAPI() {
103 delete impl_;
104 }
105
106 int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[],
107 uint32_t protocol_count) {
108 return impl_->Connect(url, protocols, protocol_count);
109 }
110
111 int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) {
112 return impl_->Close(code, reason);
113 }
114
115 int32_t WebSocketAPI::Send(const Var& data) {
116 return impl_->SendMessage(data);
117 }
118
119 uint64_t WebSocketAPI::GetBufferedAmount() {
120 return impl_->GetBufferedAmount();
121 }
122
123 Var WebSocketAPI::GetExtensions() {
124 return impl_->GetExtensions();
125 }
126
127 Var WebSocketAPI::GetProtocol() {
128 return impl_->GetProtocol();
129 }
130
131 PP_WebSocketReadyState_Dev WebSocketAPI::GetReadyState() {
132 return impl_->GetReadyState();
133 }
134
135 Var WebSocketAPI::GetURL() {
136 return impl_->GetURL();
137 }
138
139 bool WebSocketAPI::SetBinaryType(PP_WebSocketBinaryType_Dev binary_type) {
140 return impl_->SetBinaryType(binary_type);
141 }
142
143 PP_WebSocketBinaryType_Dev WebSocketAPI::GetBinaryType() {
144 return impl_->GetBinaryType();
145 }
146
147 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698