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#ifdef SendMessage
17#undef SendMessage
18#endif
19
20namespace pp {
21
22class WebSocketAPI::Implement : public WebSocket {
23 public:
24  Implement(Instance* instance, WebSocketAPI* api)
25      : WebSocket(instance),
26        api_(api),
27        callback_factory_(this) {
28  }
29
30  virtual ~Implement() {}
31
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);
44    }
45    return result;
46  }
47
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);
58    }
59    return result;
60  }
61
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);
71  }
72
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);
79    }
80  }
81
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);
88    }
89  }
90
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());
98  }
99
100 private:
101  WebSocketAPI* api_;
102  CompletionCallbackFactory<Implement> callback_factory_;
103  Var receive_message_var_;
104};
105
106WebSocketAPI::WebSocketAPI(Instance* instance)
107    : impl_(new Implement(instance, this)) {
108}
109
110WebSocketAPI::~WebSocketAPI() {
111  delete impl_;
112}
113
114int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[],
115                              uint32_t protocol_count) {
116  return impl_->Connect(url, protocols, protocol_count);
117}
118
119int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) {
120  return impl_->Close(code, reason);
121}
122
123int32_t WebSocketAPI::Send(const Var& data) {
124  return impl_->SendMessage(data);
125}
126
127uint64_t WebSocketAPI::GetBufferedAmount() {
128  return impl_->GetBufferedAmount();
129}
130
131Var WebSocketAPI::GetExtensions() {
132  return impl_->GetExtensions();
133}
134
135Var WebSocketAPI::GetProtocol() {
136  return impl_->GetProtocol();
137}
138
139PP_WebSocketReadyState WebSocketAPI::GetReadyState() {
140  return impl_->GetReadyState();
141}
142
143Var WebSocketAPI::GetURL() {
144  return impl_->GetURL();
145}
146
147}  // namespace pp
148