OLD | NEW |
(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/instance.h" |
| 10 #include "ppapi/cpp/module.h" |
| 11 #include "ppapi/cpp/module_impl.h" |
| 12 #include "ppapi/cpp/var.h" |
| 13 #include "ppapi/cpp/websocket.h" |
| 14 #include "ppapi/utility/completion_callback_factory.h" |
| 15 |
| 16 namespace pp { |
| 17 |
| 18 class WebSocketAPI::Implement : public WebSocket { |
| 19 public: |
| 20 Implement(Instance* instance, WebSocketAPI* api) |
| 21 : WebSocket(instance), |
| 22 api_(api), |
| 23 callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 24 } |
| 25 |
| 26 virtual ~Implement() {} |
| 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::Connect(url, protocols, protocol_count, callback); |
| 34 if (result != PP_OK_COMPLETIONPENDING) { |
| 35 // In synchronous cases, consumes callback here and invokes callback |
| 36 // with PP_ERROR_ABORTED instead of result in order to avoid side effects |
| 37 // in DidConnect. DidConnect ignores this invocation and doesn't call |
| 38 // any delegate virtual method. |
| 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::Close(code, reason, callback); |
| 48 if (result != PP_OK_COMPLETIONPENDING) { |
| 49 // In synchronous cases, consumes callback here and invokes callback |
| 50 // with PP_ERROR_ABORTED instead of result in order to avoid side effects |
| 51 // in DidConnect. DidConnect ignores this invocation and doesn't call |
| 52 // any delegate virtual method. |
| 53 callback.Run(PP_ERROR_ABORTED); |
| 54 } |
| 55 return result; |
| 56 } |
| 57 |
| 58 void Receive() { |
| 59 int32_t result; |
| 60 do { |
| 61 CompletionCallback callback = |
| 62 callback_factory_.NewOptionalCallback(&Implement::DidReceive); |
| 63 result = WebSocket::ReceiveMessage(&receive_message_var_, callback); |
| 64 if (result != PP_OK_COMPLETIONPENDING) |
| 65 callback.Run(result); |
| 66 } while (result == PP_OK); |
| 67 } |
| 68 |
| 69 void DidConnect(int32_t result) { |
| 70 if (result == PP_OK) { |
| 71 api_->WebSocketDidOpen(); |
| 72 Receive(); |
| 73 } else if (result != PP_ERROR_ABORTED) { |
| 74 DidClose(result); |
| 75 } |
| 76 } |
| 77 |
| 78 void DidReceive(int32_t result) { |
| 79 if (result == PP_OK) { |
| 80 api_->HandleWebSocketMessage(receive_message_var_); |
| 81 Receive(); |
| 82 } else if (result != PP_ERROR_ABORTED) { |
| 83 DidClose(result); |
| 84 } |
| 85 } |
| 86 |
| 87 void DidClose(int32_t result) { |
| 88 if (result == PP_ERROR_ABORTED) |
| 89 return; |
| 90 bool was_clean = GetCloseWasClean(); |
| 91 if (!was_clean) |
| 92 api_->HandleWebSocketError(); |
| 93 api_->WebSocketDidClose(was_clean, GetCloseCode(), GetCloseReason()); |
| 94 } |
| 95 |
| 96 private: |
| 97 WebSocketAPI* api_; |
| 98 CompletionCallbackFactory<Implement> callback_factory_; |
| 99 Var receive_message_var_; |
| 100 }; |
| 101 |
| 102 WebSocketAPI::WebSocketAPI(Instance* instance) |
| 103 : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
| 104 } |
| 105 |
| 106 WebSocketAPI::~WebSocketAPI() { |
| 107 delete impl_; |
| 108 } |
| 109 |
| 110 int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[], |
| 111 uint32_t protocol_count) { |
| 112 return impl_->Connect(url, protocols, protocol_count); |
| 113 } |
| 114 |
| 115 int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) { |
| 116 return impl_->Close(code, reason); |
| 117 } |
| 118 |
| 119 int32_t WebSocketAPI::Send(const Var& data) { |
| 120 return impl_->SendMessage(data); |
| 121 } |
| 122 |
| 123 uint64_t WebSocketAPI::GetBufferedAmount() { |
| 124 return impl_->GetBufferedAmount(); |
| 125 } |
| 126 |
| 127 Var WebSocketAPI::GetExtensions() { |
| 128 return impl_->GetExtensions(); |
| 129 } |
| 130 |
| 131 Var WebSocketAPI::GetProtocol() { |
| 132 return impl_->GetProtocol(); |
| 133 } |
| 134 |
| 135 PP_WebSocketReadyState WebSocketAPI::GetReadyState() { |
| 136 return impl_->GetReadyState(); |
| 137 } |
| 138 |
| 139 Var WebSocketAPI::GetURL() { |
| 140 return impl_->GetURL(); |
| 141 } |
| 142 |
| 143 } // namespace pp |
OLD | NEW |