| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/completion_callback.h" | 5 #include "ppapi/cpp/completion_callback.h" |
| 6 #include "ppapi/cpp/instance.h" | 6 #include "ppapi/cpp/instance.h" |
| 7 #include "ppapi/cpp/module.h" | 7 #include "ppapi/cpp/module.h" |
| 8 #include "ppapi/cpp/var.h" | 8 #include "ppapi/cpp/var.h" |
| 9 #include "ppapi/cpp/var_array_buffer.h" | 9 #include "ppapi/cpp/var_array_buffer.h" |
| 10 #include "ppapi/cpp/websocket.h" | 10 #include "ppapi/cpp/websocket.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return false; | 70 return false; |
| 71 return true; | 71 return true; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void WebSocketInstance::Open(const std::string& url) { | 74 void WebSocketInstance::Open(const std::string& url) { |
| 75 pp::CompletionCallback callback(OnConnectCompletionCallback, this); | 75 pp::CompletionCallback callback(OnConnectCompletionCallback, this); |
| 76 websocket_ = new pp::WebSocket(this); | 76 websocket_ = new pp::WebSocket(this); |
| 77 if (!websocket_) | 77 if (!websocket_) |
| 78 return; | 78 return; |
| 79 websocket_->Connect(pp::Var(url), NULL, 0, callback); | 79 websocket_->Connect(pp::Var(url), NULL, 0, callback); |
| 80 PostMessage(pp::Var("log:connecting...")); | 80 PostMessage(pp::Var("connecting...")); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void WebSocketInstance::Close() { | 83 void WebSocketInstance::Close() { |
| 84 if (!IsConnected()) | 84 if (!IsConnected()) |
| 85 return; | 85 return; |
| 86 pp::CompletionCallback callback(OnCloseCompletionCallback, this); | 86 pp::CompletionCallback callback(OnCloseCompletionCallback, this); |
| 87 websocket_->Close( | 87 websocket_->Close( |
| 88 PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE, pp::Var("bye"), callback); | 88 PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE, pp::Var("bye"), callback); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void WebSocketInstance::Send(const std::string& message) { | 91 void WebSocketInstance::Send(const std::string& message) { |
| 92 if (!IsConnected()) | 92 if (!IsConnected()) |
| 93 return; | 93 return; |
| 94 websocket_->SendMessage(pp::Var(message)); | 94 websocket_->SendMessage(pp::Var(message)); |
| 95 PostMessage(pp::Var(std::string("log:send: ") + message)); | 95 PostMessage(pp::Var(std::string("send: ") + message)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void WebSocketInstance::Receive() { | 98 void WebSocketInstance::Receive() { |
| 99 pp::CompletionCallback callback(OnReceiveCompletionCallback, this); | 99 pp::CompletionCallback callback(OnReceiveCompletionCallback, this); |
| 100 // |receive_var_| must be valid until |callback| is invoked. | 100 // |receive_var_| must be valid until |callback| is invoked. |
| 101 // Just use a member variable. | 101 // Just use a member variable. |
| 102 websocket_->ReceiveMessage(&receive_var_, callback); | 102 websocket_->ReceiveMessage(&receive_var_, callback); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void WebSocketInstance::OnConnectCompletion(int32_t result) { | 105 void WebSocketInstance::OnConnectCompletion(int32_t result) { |
| 106 if (result != PP_OK) { | 106 if (result != PP_OK) { |
| 107 PostMessage(pp::Var("alert:connection failed")); | 107 PostMessage(pp::Var("connection failed")); |
| 108 return; | 108 return; |
| 109 } | 109 } |
| 110 PostMessage(pp::Var("log:connected")); | 110 PostMessage(pp::Var("connected")); |
| 111 Receive(); | 111 Receive(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void WebSocketInstance::OnCloseCompletion(int32_t result) { | 114 void WebSocketInstance::OnCloseCompletion(int32_t result) { |
| 115 PostMessage(pp::Var(PP_OK == result ? | 115 PostMessage(pp::Var(PP_OK == result ? |
| 116 "log:closed" : | 116 "closed" : |
| 117 "alert:abnormally closed")); | 117 "abnormally closed")); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void WebSocketInstance::OnReceiveCompletion(int32_t result) { | 120 void WebSocketInstance::OnReceiveCompletion(int32_t result) { |
| 121 if (result == PP_OK) { | 121 if (result == PP_OK) { |
| 122 if (receive_var_.is_array_buffer()) | 122 if (receive_var_.is_array_buffer()) |
| 123 PostMessage(pp::Var("log: receive: binary data")); | 123 PostMessage(pp::Var("receive: binary data")); |
| 124 else | 124 else |
| 125 PostMessage(pp::Var(std::string("log:receive: ") + | 125 PostMessage(pp::Var(std::string("receive: ") + |
| 126 receive_var_.AsString())); | 126 receive_var_.AsString())); |
| 127 } | 127 } |
| 128 Receive(); | 128 Receive(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void WebSocketInstance::OnConnectCompletionCallback(void* user_data, | 131 void WebSocketInstance::OnConnectCompletionCallback(void* user_data, |
| 132 int32_t result) { | 132 int32_t result) { |
| 133 WebSocketInstance* instance = static_cast<WebSocketInstance*>(user_data); | 133 WebSocketInstance* instance = static_cast<WebSocketInstance*>(user_data); |
| 134 instance->OnConnectCompletion(result); | 134 instance->OnConnectCompletion(result); |
| 135 } | 135 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 | 161 |
| 162 // Implement the required pp::CreateModule function that creates our specific | 162 // Implement the required pp::CreateModule function that creates our specific |
| 163 // kind of Module. | 163 // kind of Module. |
| 164 namespace pp { | 164 namespace pp { |
| 165 Module* CreateModule() { | 165 Module* CreateModule() { |
| 166 return new WebSocketModule(); | 166 return new WebSocketModule(); |
| 167 } | 167 } |
| 168 } // namespace pp | 168 } // namespace pp |
| OLD | NEW |