Chromium Code Reviews| 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/dev/websocket_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/instance.h" | |
| 11 #include "ppapi/cpp/module.h" | |
| 12 #include "ppapi/cpp/module_impl.h" | |
| 13 #include "ppapi/cpp/var.h" | |
| 14 | |
| 15 // TODO(toyoshim): This macro is just for review until 8885012 is landed. | |
| 16 // I must remove it before landing. | |
| 17 #define PP_ALLOW_THIS_IN_INITIALIZER_LIST(x) x | |
| 18 | |
| 19 namespace pp { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 template <> const char* interface_name<PPB_WebSocket_Dev>() { | |
| 24 return PPB_WEBSOCKET_DEV_INTERFACE; | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 class WebSocket_Dev::WebSocketPrivate { | |
| 30 public: | |
| 31 WebSocketPrivate(WebSocket_Dev* ws) | |
| 32 : callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)), ws_(ws) { | |
| 33 } | |
| 34 | |
| 35 ~WebSocketPrivate() {} | |
| 36 | |
|
dmichael (off chromium)
2011/12/08 21:26:49
nit: I think everything below here except the 'New
| |
| 37 void Receive() { | |
| 38 int32_t result; | |
| 39 do { | |
| 40 result = get_interface<PPB_WebSocket_Dev>()->ReceiveMessage( | |
| 41 ws_->pp_resource(), | |
| 42 &receive_message_var_, | |
| 43 callback_factory_.NewOptionalCallback( | |
| 44 &WebSocketPrivate::DidReceive).pp_completion_callback()); | |
| 45 if (result == PP_OK) | |
| 46 ws_->OnMessage(Var(Var::PassRef(), receive_message_var_)); | |
| 47 } while (result == PP_OK); | |
| 48 if (result == PP_OK_COMPLETIONPENDING) | |
| 49 return; | |
| 50 if (result == PP_ERROR_BADARGUMENT) | |
| 51 return; | |
| 52 DidClose(result); | |
| 53 } | |
| 54 | |
| 55 void DidConnect(int32_t result) { | |
| 56 if (result == PP_OK) { | |
| 57 ws_->OnOpen(); | |
| 58 Receive(); | |
| 59 } else { | |
| 60 DidClose(result); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void DidReceive(int32_t result) { | |
| 65 if (result == PP_OK) { | |
| 66 ws_->OnMessage(Var(Var::PassRef(), receive_message_var_)); | |
| 67 Receive(); | |
| 68 } else { | |
| 69 DidClose(result); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void DidClose(int32_t result) { | |
| 74 PP_Resource resource = ws_->pp_resource(); | |
| 75 PP_Bool was_clean = | |
| 76 get_interface<PPB_WebSocket_Dev>()->GetCloseWasClean(resource); | |
| 77 uint16_t code = | |
| 78 get_interface<PPB_WebSocket_Dev>()->GetCloseCode(resource); | |
| 79 Var reason = Var(Var::PassRef(), | |
| 80 get_interface<PPB_WebSocket_Dev>()->GetCloseReason(resource)); | |
| 81 bool was_fully_clean = was_clean == PP_TRUE && result == PP_OK; | |
| 82 ws_->OnClose(was_fully_clean, code, reason); | |
| 83 } | |
| 84 | |
| 85 CompletionCallback NewConnectCallback() { | |
| 86 return callback_factory_.NewOptionalCallback( | |
| 87 &WebSocketPrivate::DidConnect); | |
| 88 } | |
| 89 | |
| 90 CompletionCallback NewCloseCallback() { | |
| 91 return callback_factory_.NewOptionalCallback(&WebSocketPrivate::DidClose); | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 pp::CompletionCallbackFactory<WebSocketPrivate> callback_factory_; | |
| 96 WebSocket_Dev* ws_; | |
| 97 PP_Var receive_message_var_; | |
| 98 }; | |
| 99 | |
| 100 WebSocket_Dev::WebSocket_Dev(Instance* instance) | |
| 101 : impl_(new WebSocketPrivate(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { | |
| 102 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 103 return; | |
| 104 PassRefFromConstructor(get_interface<PPB_WebSocket_Dev>()->Create( | |
| 105 instance->pp_instance())); | |
| 106 } | |
| 107 | |
| 108 WebSocket_Dev::~WebSocket_Dev() { | |
| 109 delete impl_; | |
|
dmichael (off chromium)
2011/12/08 21:26:49
Could you use scoped_ptr?
Takashi Toyoshima
2011/12/09 05:41:46
Can I use scoped_ptr in NaCl world?
brettw
2011/12/09 05:58:26
We don't use base from the C++ layer, so I'd avoid
| |
| 110 } | |
| 111 | |
| 112 int32_t WebSocket_Dev::Connect(const Var& url, const Var protocols[], | |
| 113 uint32_t protocol_count) { | |
| 114 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 115 return PP_ERROR_BADRESOURCE; | |
| 116 | |
| 117 // Convert protocols to C interface. | |
| 118 uint32_t allocation_protocol_count = protocol_count ? protocol_count : 1; | |
| 119 PP_Var *c_protocols = new PP_Var[allocation_protocol_count]; | |
| 120 if (!c_protocols) | |
| 121 return PP_ERROR_NOMEMORY; | |
| 122 for (uint32_t i = 0; i < protocol_count; ++i) | |
| 123 c_protocols[i] = protocols[i].pp_var(); | |
| 124 | |
| 125 int32_t result = get_interface<PPB_WebSocket_Dev>()->Connect( | |
| 126 pp_resource(), url.pp_var(), c_protocols, protocol_count, | |
| 127 impl_->NewConnectCallback().pp_completion_callback()); | |
| 128 delete[] c_protocols; | |
| 129 return result; | |
| 130 } | |
| 131 | |
| 132 int32_t WebSocket_Dev::Close(uint16_t code, const Var& reason) { | |
| 133 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 134 return PP_ERROR_BADRESOURCE; | |
| 135 | |
| 136 return get_interface<PPB_WebSocket_Dev>()->Close( | |
| 137 pp_resource(), code, reason.pp_var(), | |
| 138 impl_->NewCloseCallback().pp_completion_callback()); | |
| 139 } | |
| 140 | |
| 141 int32_t WebSocket_Dev::Send(const Var& data) { | |
| 142 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 143 return PP_ERROR_BADRESOURCE; | |
| 144 | |
| 145 return get_interface<PPB_WebSocket_Dev>()->SendMessage( | |
| 146 pp_resource(), data.pp_var()); | |
| 147 } | |
| 148 | |
| 149 uint64_t WebSocket_Dev::GetBufferedAmount() { | |
| 150 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 151 return PP_ERROR_BADRESOURCE; | |
|
dmichael (off chromium)
2011/12/08 21:26:49
Your return type is uint64_t; you probably want to
| |
| 152 | |
| 153 return get_interface<PPB_WebSocket_Dev>()->GetBufferedAmount(pp_resource()); | |
| 154 } | |
| 155 | |
| 156 Var WebSocket_Dev::GetExtensions() { | |
| 157 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 158 return Var(); | |
| 159 | |
| 160 return Var(Var::PassRef(), | |
| 161 get_interface<PPB_WebSocket_Dev>()->GetExtensions(pp_resource())); | |
| 162 } | |
| 163 | |
| 164 Var WebSocket_Dev::GetProtocol() { | |
| 165 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 166 return Var(); | |
| 167 | |
| 168 return Var(Var::PassRef(), | |
| 169 get_interface<PPB_WebSocket_Dev>()->GetProtocol(pp_resource())); | |
| 170 } | |
| 171 | |
| 172 PP_WebSocketReadyState_Dev WebSocket_Dev::GetReadyState() { | |
| 173 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 174 return PP_WEBSOCKETREADYSTATE_INVALID_DEV; | |
| 175 | |
| 176 return get_interface<PPB_WebSocket_Dev>()->GetReadyState(pp_resource()); | |
| 177 } | |
| 178 | |
| 179 Var WebSocket_Dev::GetURL() { | |
| 180 if (!has_interface<PPB_WebSocket_Dev>()) | |
| 181 return Var(); | |
| 182 | |
| 183 return Var(Var::PassRef(), | |
| 184 get_interface<PPB_WebSocket_Dev>()->GetURL(pp_resource())); | |
| 185 } | |
| 186 | |
| 187 } // namespace pp | |
| OLD | NEW |