OLD | NEW |
| (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 { | |
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 return WebSocket_Dev::Connect(url, protocols, protocol_count, | |
33 callback_factory_.NewOptionalCallback(&Implement::DidConnect)); | |
34 } | |
35 | |
36 int32_t Close(uint16_t code, const Var& reason) { | |
37 return WebSocket_Dev::Close(code, reason, | |
38 callback_factory_.NewOptionalCallback(&Implement::DidClose)); | |
39 } | |
40 | |
41 void Receive() { | |
42 WebSocket_Dev::ReceiveMessage(&receive_message_var_, | |
43 callback_factory_.NewCallback(&Implement::DidReceive)); | |
44 } | |
45 | |
46 void DidConnect(int32_t result) { | |
47 if (result == PP_OK) { | |
48 api_->OnOpen(); | |
49 Receive(); | |
50 } else if (result != PP_ERROR_ABORTED) { | |
51 DidClose(result); | |
52 } | |
53 } | |
54 | |
55 void DidReceive(int32_t result) { | |
56 if (result == PP_OK) { | |
57 api_->OnMessage(receive_message_var_); | |
58 Receive(); | |
59 } else if (result != PP_ERROR_ABORTED) { | |
60 DidClose(result); | |
61 } | |
62 } | |
63 | |
64 void DidClose(int32_t result) { | |
65 bool was_clean = GetCloseWasClean() && result == PP_OK; | |
66 if (!was_clean) | |
67 api_->OnError(); | |
68 api_->OnClose(was_clean, GetCloseCode(), GetCloseReason()); | |
69 } | |
70 | |
71 private: | |
72 WebSocketAPI_Dev* api_; | |
73 CompletionCallbackFactory<Implement> callback_factory_; | |
74 Var receive_message_var_; | |
75 }; | |
76 | |
77 WebSocketAPI_Dev::WebSocketAPI_Dev(Instance* instance) | |
78 : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { | |
79 } | |
80 | |
81 WebSocketAPI_Dev::~WebSocketAPI_Dev() { | |
82 delete impl_; | |
83 } | |
84 | |
85 int32_t WebSocketAPI_Dev::Connect(const Var& url, const Var protocols[], | |
86 uint32_t protocol_count) { | |
87 return impl_->Connect(url, protocols, protocol_count); | |
88 } | |
89 | |
90 int32_t WebSocketAPI_Dev::Close(uint16_t code, const Var& reason) { | |
91 return impl_->Close(code, reason); | |
92 } | |
93 | |
94 int32_t WebSocketAPI_Dev::Send(const Var& data) { | |
95 return impl_->SendMessage(data); | |
96 } | |
97 | |
98 uint64_t WebSocketAPI_Dev::GetBufferedAmount() { | |
99 return impl_->GetBufferedAmount(); | |
100 } | |
101 | |
102 Var WebSocketAPI_Dev::GetExtensions() { | |
103 return impl_->GetExtensions(); | |
104 } | |
105 | |
106 Var WebSocketAPI_Dev::GetProtocol() { | |
107 return impl_->GetProtocol(); | |
108 } | |
109 | |
110 PP_WebSocketReadyState_Dev WebSocketAPI_Dev::GetReadyState() { | |
111 return impl_->GetReadyState(); | |
112 } | |
113 | |
114 Var WebSocketAPI_Dev::GetURL() { | |
115 return impl_->GetURL(); | |
116 } | |
117 | |
118 } // namespace helper | |
119 | |
120 } // namespace pp | |
OLD | NEW |