hunspell: Cleanup to fix the header include guards under google/ directory.
[chromium-blink-merge.git] / ppapi / utility / websocket / websocket_api.cc
blob82a320e5adb68d618d85a75c47e2909f8dc932ae
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.
5 #include "ppapi/utility/websocket/websocket_api.h"
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"
16 #ifdef SendMessage
17 #undef SendMessage
18 #endif
20 namespace pp {
22 class WebSocketAPI::Implement : public WebSocket {
23 public:
24 Implement(Instance* instance, WebSocketAPI* api)
25 : WebSocket(instance),
26 api_(api),
27 callback_factory_(this) {
30 virtual ~Implement() {}
32 int32_t Connect(const Var& url, const Var protocols[],
33 uint32_t protocol_count) {
34 CompletionCallback callback =
35 callback_factory_.NewOptionalCallback(&Implement::DidConnect);
36 int32_t result =
37 WebSocket::Connect(url, protocols, protocol_count, callback);
38 if (result != PP_OK_COMPLETIONPENDING) {
39 // In synchronous cases, consumes callback here and invokes callback
40 // with PP_ERROR_ABORTED instead of result in order to avoid side effects
41 // in DidConnect. DidConnect ignores this invocation and doesn't call
42 // any delegate virtual method.
43 callback.Run(PP_ERROR_ABORTED);
45 return result;
48 int32_t Close(uint16_t code, const Var& reason) {
49 CompletionCallback callback =
50 callback_factory_.NewOptionalCallback(&Implement::DidClose);
51 int32_t result = WebSocket::Close(code, reason, callback);
52 if (result != PP_OK_COMPLETIONPENDING) {
53 // In synchronous cases, consumes callback here and invokes callback
54 // with PP_ERROR_ABORTED instead of result in order to avoid side effects
55 // in DidConnect. DidConnect ignores this invocation and doesn't call
56 // any delegate virtual method.
57 callback.Run(PP_ERROR_ABORTED);
59 return result;
62 void Receive() {
63 int32_t result;
64 do {
65 CompletionCallback callback =
66 callback_factory_.NewOptionalCallback(&Implement::DidReceive);
67 result = WebSocket::ReceiveMessage(&receive_message_var_, callback);
68 if (result != PP_OK_COMPLETIONPENDING)
69 callback.Run(result);
70 } while (result == PP_OK);
73 void DidConnect(int32_t result) {
74 if (result == PP_OK) {
75 api_->WebSocketDidOpen();
76 Receive();
77 } else if (result != PP_ERROR_ABORTED) {
78 DidClose(result);
82 void DidReceive(int32_t result) {
83 if (result == PP_OK) {
84 api_->HandleWebSocketMessage(receive_message_var_);
85 Receive();
86 } else if (result != PP_ERROR_ABORTED) {
87 DidClose(result);
91 void DidClose(int32_t result) {
92 if (result == PP_ERROR_ABORTED)
93 return;
94 bool was_clean = GetCloseWasClean();
95 if (!was_clean)
96 api_->HandleWebSocketError();
97 api_->WebSocketDidClose(was_clean, GetCloseCode(), GetCloseReason());
100 private:
101 WebSocketAPI* api_;
102 CompletionCallbackFactory<Implement> callback_factory_;
103 Var receive_message_var_;
106 WebSocketAPI::WebSocketAPI(Instance* instance)
107 : impl_(new Implement(instance, this)) {
110 WebSocketAPI::~WebSocketAPI() {
111 delete impl_;
114 int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[],
115 uint32_t protocol_count) {
116 return impl_->Connect(url, protocols, protocol_count);
119 int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) {
120 return impl_->Close(code, reason);
123 int32_t WebSocketAPI::Send(const Var& data) {
124 return impl_->SendMessage(data);
127 uint64_t WebSocketAPI::GetBufferedAmount() {
128 return impl_->GetBufferedAmount();
131 Var WebSocketAPI::GetExtensions() {
132 return impl_->GetExtensions();
135 Var WebSocketAPI::GetProtocol() {
136 return impl_->GetProtocol();
139 PP_WebSocketReadyState WebSocketAPI::GetReadyState() {
140 return impl_->GetReadyState();
143 Var WebSocketAPI::GetURL() {
144 return impl_->GetURL();
147 } // namespace pp