summaryrefslogtreecommitdiffstats
path: root/ppapi/utility
diff options
context:
space:
mode:
authortoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-29 20:22:09 +0000
committertoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-29 20:22:09 +0000
commit2785cb9d787552852a68e3ac586607807dfa0d96 (patch)
tree376b552f7853c194d58f3e5fb3888e4de20c4aee /ppapi/utility
parent969229ae0e7187a5b786dd83de2079145346a7b7 (diff)
downloadchromium_src-2785cb9d787552852a68e3ac586607807dfa0d96.zip
chromium_src-2785cb9d787552852a68e3ac586607807dfa0d96.tar.gz
chromium_src-2785cb9d787552852a68e3ac586607807dfa0d96.tar.bz2
WebSocket Pepper API: an utility class implementation.
This class privide JS binding like API to Pepper C++ developers. BUG=87310 TEST=ui_tests --gtest_filter='PPAPI*Test.Websocket_Utility*' Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=115093 Review URL: http://codereview.chromium.org/8956008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/utility')
-rw-r--r--ppapi/utility/websocket/websocket_api.cc143
-rw-r--r--ppapi/utility/websocket/websocket_api.h130
2 files changed, 273 insertions, 0 deletions
diff --git a/ppapi/utility/websocket/websocket_api.cc b/ppapi/utility/websocket/websocket_api.cc
new file mode 100644
index 0000000..db4e01a
--- /dev/null
+++ b/ppapi/utility/websocket/websocket_api.cc
@@ -0,0 +1,143 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/utility/websocket/websocket_api.h"
+
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_macros.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/module_impl.h"
+#include "ppapi/cpp/var.h"
+#include "ppapi/cpp/websocket.h"
+#include "ppapi/utility/completion_callback_factory.h"
+
+namespace pp {
+
+class WebSocketAPI::Implement : public WebSocket {
+ public:
+ Implement(Instance* instance, WebSocketAPI* api)
+ : WebSocket(instance),
+ api_(api),
+ callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+ }
+
+ virtual ~Implement() {}
+
+ int32_t Connect(const Var& url, const Var protocols[],
+ uint32_t protocol_count) {
+ CompletionCallback callback =
+ callback_factory_.NewOptionalCallback(&Implement::DidConnect);
+ int32_t result =
+ WebSocket::Connect(url, protocols, protocol_count, callback);
+ if (result != PP_OK_COMPLETIONPENDING) {
+ // In synchronous cases, consumes callback here and invokes callback
+ // with PP_ERROR_ABORTED instead of result in order to avoid side effects
+ // in DidConnect. DidConnect ignores this invocation and doesn't call
+ // any delegate virtual method.
+ callback.Run(PP_ERROR_ABORTED);
+ }
+ return result;
+ }
+
+ int32_t Close(uint16_t code, const Var& reason) {
+ CompletionCallback callback =
+ callback_factory_.NewOptionalCallback(&Implement::DidClose);
+ int32_t result = WebSocket::Close(code, reason, callback);
+ if (result != PP_OK_COMPLETIONPENDING) {
+ // In synchronous cases, consumes callback here and invokes callback
+ // with PP_ERROR_ABORTED instead of result in order to avoid side effects
+ // in DidConnect. DidConnect ignores this invocation and doesn't call
+ // any delegate virtual method.
+ callback.Run(PP_ERROR_ABORTED);
+ }
+ return result;
+ }
+
+ void Receive() {
+ int32_t result;
+ do {
+ CompletionCallback callback =
+ callback_factory_.NewOptionalCallback(&Implement::DidReceive);
+ result = WebSocket::ReceiveMessage(&receive_message_var_, callback);
+ if (result != PP_OK_COMPLETIONPENDING)
+ callback.Run(result);
+ } while (result == PP_OK);
+ }
+
+ void DidConnect(int32_t result) {
+ if (result == PP_OK) {
+ api_->WebSocketDidOpen();
+ Receive();
+ } else if (result != PP_ERROR_ABORTED) {
+ DidClose(result);
+ }
+ }
+
+ void DidReceive(int32_t result) {
+ if (result == PP_OK) {
+ api_->HandleWebSocketMessage(receive_message_var_);
+ Receive();
+ } else if (result != PP_ERROR_ABORTED) {
+ DidClose(result);
+ }
+ }
+
+ void DidClose(int32_t result) {
+ if (result == PP_ERROR_ABORTED)
+ return;
+ bool was_clean = GetCloseWasClean();
+ if (!was_clean)
+ api_->HandleWebSocketError();
+ api_->WebSocketDidClose(was_clean, GetCloseCode(), GetCloseReason());
+ }
+
+ private:
+ WebSocketAPI* api_;
+ CompletionCallbackFactory<Implement> callback_factory_;
+ Var receive_message_var_;
+};
+
+WebSocketAPI::WebSocketAPI(Instance* instance)
+ : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
+}
+
+WebSocketAPI::~WebSocketAPI() {
+ delete impl_;
+}
+
+int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[],
+ uint32_t protocol_count) {
+ return impl_->Connect(url, protocols, protocol_count);
+}
+
+int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) {
+ return impl_->Close(code, reason);
+}
+
+int32_t WebSocketAPI::Send(const Var& data) {
+ return impl_->SendMessage(data);
+}
+
+uint64_t WebSocketAPI::GetBufferedAmount() {
+ return impl_->GetBufferedAmount();
+}
+
+Var WebSocketAPI::GetExtensions() {
+ return impl_->GetExtensions();
+}
+
+Var WebSocketAPI::GetProtocol() {
+ return impl_->GetProtocol();
+}
+
+PP_WebSocketReadyState WebSocketAPI::GetReadyState() {
+ return impl_->GetReadyState();
+}
+
+Var WebSocketAPI::GetURL() {
+ return impl_->GetURL();
+}
+
+} // namespace pp
diff --git a/ppapi/utility/websocket/websocket_api.h b/ppapi/utility/websocket/websocket_api.h
new file mode 100644
index 0000000..f733b1c
--- /dev/null
+++ b/ppapi/utility/websocket/websocket_api.h
@@ -0,0 +1,130 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_UTILITY_WEBSOCKET_WEBSOCKET_API_H_
+#define PPAPI_UTILITY_WEBSOCKET_WEBSOCKET_API_H_
+
+#include "ppapi/c/ppb_websocket.h"
+
+/// @file
+/// This file defines the WebSocketAPI interface.
+
+namespace pp {
+
+class CompletionCallback;
+class Instance;
+class Var;
+
+/// The <code>WebSocketAPI</code> class
+class WebSocketAPI {
+ public:
+ /// Constructs a WebSocketAPI object.
+ explicit WebSocketAPI(Instance* instance);
+
+ /// Destructs a WebSocketAPI object.
+ virtual ~WebSocketAPI();
+
+ /// Connect() connects to the specified WebSocket server. Caller can call
+ /// this method at most once.
+ ///
+ /// @param[in] url A <code>Var</code> of string type representing a WebSocket
+ /// server URL.
+ /// @param[in] protocols A pointer to an array of string type
+ /// <code>Var</code> specifying sub-protocols. Each <code>Var</code>
+ /// represents one sub-protocol and its <code>PP_VarType</code> must be
+ /// <code>PP_VARTYPE_STRING</code>. This argument can be null only if
+ /// <code>protocol_count</code> is 0.
+ /// @param[in] protocol_count The number of sub-protocols in
+ /// <code>protocols</code>.
+ ///
+ /// @return An int32_t containing an error code from
+ /// <code>pp_errors.h</code>.
+ /// See also <code>pp::WebSocket::Connect</code>.
+ int32_t Connect(const Var& url, const Var protocols[],
+ uint32_t protocol_count);
+
+ /// Close() closes the specified WebSocket connection by specifying
+ /// <code>code</code> and <code>reason</code>.
+ ///
+ /// @param[in] code The WebSocket close code. Ignored if it is 0.
+ /// @param[in] reason A <code>Var</code> of string type which represents the
+ /// WebSocket close reason. Ignored if it is undefined type.
+ ///
+ /// @return An int32_t containing an error code from
+ /// <code>pp_errors.h</code>.
+ /// See also <code>pp::WebSocket::Close</code>.
+ int32_t Close(uint16_t code, const Var& reason);
+
+ /// Send() sends a message to the WebSocket server.
+ ///
+ /// @param[in] data A message to send. The message is copied to internal
+ /// buffer. So caller can free <code>data</code> safely after returning
+ /// from the function.
+ ///
+ /// @return An int32_t containing an error code from
+ /// <code>pp_errors.h</code>.
+ /// See also <code>pp::WebSocket::SendMessage</code>.
+ int32_t Send(const Var& data);
+
+ /// GetBufferedAmount() returns the number of bytes of text and binary
+ /// messages that have been queued for the WebSocket connection to send but
+ /// have not been transmitted to the network yet.
+ ///
+ /// @return Returns the number of bytes.
+ uint64_t GetBufferedAmount();
+
+ /// GetExtensions() returns the extensions selected by the server for the
+ /// specified WebSocket connection.
+ ///
+ /// @return Returns a <code>Var</code> of string type. If called before the
+ /// connection is established, its data is empty string.
+ /// Currently its data is always an empty string.
+ Var GetExtensions();
+
+ /// GetProtocol() returns the sub-protocol chosen by the server for the
+ /// specified WebSocket connection.
+ ///
+ /// @return Returns a <code>Var</code> of string type. If called before the
+ /// connection is established, it contains the empty string.
+ Var GetProtocol();
+
+ /// GetReadyState() returns the ready state of the specified WebSocket
+ /// connection.
+ ///
+ /// @return Returns <code>PP_WEBSOCKETREADYSTATE_INVALID</code> if called
+ /// before connect() is called.
+ PP_WebSocketReadyState GetReadyState();
+
+ /// GetURL() returns the URL associated with specified WebSocket connection.
+ ///
+ /// @return Returns a <code>Var</code> of string type. If called before the
+ /// connection is established, it contains the empty string.
+ Var GetURL();
+
+ /// WebSocketDidOpen() is invoked when the connection is established by
+ /// Connect().
+ virtual void WebSocketDidOpen() = 0;
+
+ /// WebSocketDidClose() is invoked when the connection is closed by errors or
+ /// Close().
+ virtual void WebSocketDidClose(bool wasClean,
+ uint16_t code,
+ const Var& reason) = 0;
+
+ /// HandleWebSocketMessage() is invoked when a message is received.
+ virtual void HandleWebSocketMessage(const Var& message) = 0;
+
+ /// HandleWebSocketError() is invoked if the user agent was required to fail
+ /// the WebSocket connection or the WebSocket connection is closed with
+ /// prejudice. DidClose() always follows HandleError().
+ virtual void HandleWebSocketError() = 0;
+
+ private:
+ class Implement;
+ Implement* impl_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_WEBSOCKET_WEBSOCKET_API_H_