From b838eeae926063bc732785681300e086bdf2a3db Mon Sep 17 00:00:00 2001 From: "yzshen@chromium.org" Date: Sat, 22 Jun 2013 22:17:22 +0000 Subject: Move PPB_TCPSocket out of dev. BUG=247225 TEST=None Review URL: https://chromiumcodereview.appspot.com/17314012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@208094 0039d316-1c4b-4281-b951-d872f2087c98 --- ppapi/cpp/dev/tcp_socket_dev.cc | 117 ---------------------------- ppapi/cpp/dev/tcp_socket_dev.h | 166 ---------------------------------------- ppapi/cpp/tcp_socket.cc | 116 ++++++++++++++++++++++++++++ ppapi/cpp/tcp_socket.h | 166 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 282 insertions(+), 283 deletions(-) delete mode 100644 ppapi/cpp/dev/tcp_socket_dev.cc delete mode 100644 ppapi/cpp/dev/tcp_socket_dev.h create mode 100644 ppapi/cpp/tcp_socket.cc create mode 100644 ppapi/cpp/tcp_socket.h (limited to 'ppapi/cpp') diff --git a/ppapi/cpp/dev/tcp_socket_dev.cc b/ppapi/cpp/dev/tcp_socket_dev.cc deleted file mode 100644 index 03628a8..0000000 --- a/ppapi/cpp/dev/tcp_socket_dev.cc +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2013 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/cpp/dev/tcp_socket_dev.h" - -#include "ppapi/c/pp_errors.h" -#include "ppapi/cpp/completion_callback.h" -#include "ppapi/cpp/instance_handle.h" -#include "ppapi/cpp/module_impl.h" - -namespace pp { - -namespace { - -template <> const char* interface_name() { - return PPB_TCPSOCKET_DEV_INTERFACE_0_1; -} - -} // namespace - -TCPSocket_Dev::TCPSocket_Dev() { -} - -TCPSocket_Dev::TCPSocket_Dev(const InstanceHandle& instance) { - if (has_interface()) { - PassRefFromConstructor(get_interface()->Create( - instance.pp_instance())); - } -} - -TCPSocket_Dev::TCPSocket_Dev(PassRef, PP_Resource resource) - : Resource(PASS_REF, resource) { -} - -TCPSocket_Dev::TCPSocket_Dev(const TCPSocket_Dev& other) : Resource(other) { -} - -TCPSocket_Dev::~TCPSocket_Dev() { -} - -TCPSocket_Dev& TCPSocket_Dev::operator=(const TCPSocket_Dev& other) { - Resource::operator=(other); - return *this; -} - -// static -bool TCPSocket_Dev::IsAvailable() { - return has_interface(); -} - -int32_t TCPSocket_Dev::Connect(const NetAddress& addr, - const CompletionCallback& callback) { - if (has_interface()) { - return get_interface()->Connect( - pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); - } - return callback.MayForce(PP_ERROR_NOINTERFACE); -} - -NetAddress TCPSocket_Dev::GetLocalAddress() const { - if (has_interface()) { - return NetAddress( - PASS_REF, - get_interface()->GetLocalAddress(pp_resource())); - } - return NetAddress(); -} - -NetAddress TCPSocket_Dev::GetRemoteAddress() const { - if (has_interface()) { - return NetAddress( - PASS_REF, - get_interface()->GetRemoteAddress( - pp_resource())); - } - return NetAddress(); -} - -int32_t TCPSocket_Dev::Read(char* buffer, - int32_t bytes_to_read, - const CompletionCallback& callback) { - if (has_interface()) { - return get_interface()->Read( - pp_resource(), buffer, bytes_to_read, - callback.pp_completion_callback()); - } - return callback.MayForce(PP_ERROR_NOINTERFACE); -} - -int32_t TCPSocket_Dev::Write(const char* buffer, - int32_t bytes_to_write, - const CompletionCallback& callback) { - if (has_interface()) { - return get_interface()->Write( - pp_resource(), buffer, bytes_to_write, - callback.pp_completion_callback()); - } - return callback.MayForce(PP_ERROR_NOINTERFACE); -} - -void TCPSocket_Dev::Close() { - if (has_interface()) - get_interface()->Close(pp_resource()); -} - -int32_t TCPSocket_Dev::SetOption(PP_TCPSocket_Option_Dev name, - const Var& value, - const CompletionCallback& callback) { - if (has_interface()) { - return get_interface()->SetOption( - pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); - } - return callback.MayForce(PP_ERROR_NOINTERFACE); -} - -} // namespace pp diff --git a/ppapi/cpp/dev/tcp_socket_dev.h b/ppapi/cpp/dev/tcp_socket_dev.h deleted file mode 100644 index 4067bf0..0000000 --- a/ppapi/cpp/dev/tcp_socket_dev.h +++ /dev/null @@ -1,166 +0,0 @@ -// Copyright 2013 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_CPP_DEV_TCP_SOCKET_DEV_H_ -#define PPAPI_CPP_DEV_TCP_SOCKET_DEV_H_ - -#include "ppapi/c/dev/ppb_tcp_socket_dev.h" -#include "ppapi/cpp/net_address.h" -#include "ppapi/cpp/pass_ref.h" -#include "ppapi/cpp/resource.h" - -namespace pp { - -class CompletionCallback; -class InstanceHandle; - -/// The TCPSocket_Dev class provides TCP socket operations. -/// -/// Permissions: Apps permission socket with subrule -/// tcp-connect is required for Connect(). -/// For more details about network communication permissions, please see: -/// http://developer.chrome.com/apps/app_network.html -class TCPSocket_Dev: public Resource { - public: - /// Default constructor for creating an is_null() TCPSocket_Dev - /// object. - TCPSocket_Dev(); - - /// A constructor used to create a TCPSocket_Dev object. - /// - /// @param[in] instance The instance with which this resource will be - /// associated. - explicit TCPSocket_Dev(const InstanceHandle& instance); - - /// A constructor used when you have received a PP_Resource as a - /// return value that has had 1 ref added for you. - /// - /// @param[in] resource A PPB_TCPSocket_Dev resource. - TCPSocket_Dev(PassRef, PP_Resource resource); - - /// The copy constructor for TCPSocket_Dev. - /// - /// @param[in] other A reference to another TCPSocket_Dev. - TCPSocket_Dev(const TCPSocket_Dev& other); - - /// The destructor. - virtual ~TCPSocket_Dev(); - - /// The assignment operator for TCPSocket_Dev. - /// - /// @param[in] other A reference to another TCPSocket_Dev. - /// - /// @return A reference to this TCPSocket_Dev object. - TCPSocket_Dev& operator=(const TCPSocket_Dev& other); - - /// Static function for determining whether the browser supports the - /// PPB_TCPSocket_Dev interface. - /// - /// @return true if the interface is available, false otherwise. - static bool IsAvailable(); - - /// Connects the socket to the given address. - /// - /// @param[in] addr A NetAddress object. - /// @param[in] callback A CompletionCallback to be called upon - /// completion. - /// - /// @return An int32_t containing an error code from pp_errors.h, - /// including (but not limited to): - /// - PP_ERROR_NOACCESS: the caller doesn't have required - /// permissions. - /// - PP_ERROR_ADDRESS_UNREACHABLE: addr is - /// unreachable. - /// - PP_ERROR_CONNECTION_REFUSED: the connection attempt was - /// refused. - /// - PP_ERROR_CONNECTION_FAILED: the connection attempt failed. - /// - PP_ERROR_CONNECTION_TIMEDOUT: the connection attempt timed - /// out. - int32_t Connect(const NetAddress& addr, - const CompletionCallback& callback); - - /// Gets the local address of the socket, if it is connected. - /// - /// @return A NetAddress object. The object will be null - /// (i.e., is_null() returns true) on failure. - NetAddress GetLocalAddress() const; - - /// Gets the remote address of the socket, if it is connected. - /// - /// @return A NetAddress object. The object will be null - /// (i.e., is_null() returns true) on failure. - NetAddress GetRemoteAddress() const; - - /// Reads data from the socket. The socket must be connected. It may perform a - /// partial read. - /// - /// Caveat: You should be careful about the lifetime of - /// buffer. Typically you will use a - /// CompletionCallbackFactory to scope callbacks to the lifetime - /// of your class. When your class goes out of scope, the callback factory - /// will not actually cancel the operation, but will rather just skip issuing - /// the callback on your class. This means that if the underlying - /// PPB_TCPSocket_Dev resource outlives your class, the browser - /// will still try to write into your buffer when the operation completes. - /// The buffer must be kept valid until then to avoid memory corruption. - /// If you want to release the buffer while the Read() call is - /// still pending, you should call Close() to ensure that the - /// buffer won't be accessed in the future. - /// - /// @param[out] buffer The buffer to store the received data on success. It - /// must be at least as large as bytes_to_read. - /// @param[in] bytes_to_read The number of bytes to read. - /// @param[in] callback A CompletionCallback to be called upon - /// completion. - /// - /// @return A non-negative number on success to indicate how many bytes have - /// been read, 0 means that end-of-file was reached; otherwise, an error code - /// from pp_errors.h. - int32_t Read(char* buffer, - int32_t bytes_to_read, - const CompletionCallback& callback); - - /// Writes data to the socket. The socket must be connected. It may perform a - /// partial write. - /// - /// @param[in] buffer The buffer containing the data to write. - /// @param[in] bytes_to_write The number of bytes to write. - /// @param[in] callback A CompletionCallback to be called upon - /// completion. - /// - /// @return A non-negative number on success to indicate how many bytes have - /// been written; otherwise, an error code from pp_errors.h. - int32_t Write(const char* buffer, - int32_t bytes_to_write, - const CompletionCallback& callback); - - /// Cancels all pending reads and writes and disconnects the socket. Any - /// pending callbacks will still run, reporting PP_ERROR_ABORTED - /// if pending IO was interrupted. After a call to this method, no output - /// buffer pointers passed into previous Read() calls will be - /// accessed. It is not valid to call Connect() again. - /// - /// The socket is implicitly closed if it is destroyed, so you are not - /// required to call this method. - void Close(); - - /// Sets a socket option on the TCP socket. - /// Please see the PP_TCPSocket_Option_Dev description for option - /// names, value types and allowed values. - /// - /// @param[in] name The option to set. - /// @param[in] value The option value to set. - /// @param[in] callback A CompletionCallback to be called upon - /// completion. - /// - /// @return An int32_t containing an error code from pp_errors.h. - //// - int32_t SetOption(PP_TCPSocket_Option_Dev name, - const Var& value, - const CompletionCallback& callback); -}; - -} // namespace pp - -#endif // PPAPI_CPP_DEV_TCP_SOCKET__DevDEV_H_ diff --git a/ppapi/cpp/tcp_socket.cc b/ppapi/cpp/tcp_socket.cc new file mode 100644 index 0000000..f0002fa --- /dev/null +++ b/ppapi/cpp/tcp_socket.cc @@ -0,0 +1,116 @@ +// Copyright 2013 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/cpp/tcp_socket.h" + +#include "ppapi/c/pp_errors.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/instance_handle.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name() { + return PPB_TCPSOCKET_INTERFACE_1_0; +} + +} // namespace + +TCPSocket::TCPSocket() { +} + +TCPSocket::TCPSocket(const InstanceHandle& instance) { + if (has_interface()) { + PassRefFromConstructor(get_interface()->Create( + instance.pp_instance())); + } +} + +TCPSocket::TCPSocket(PassRef, PP_Resource resource) + : Resource(PASS_REF, resource) { +} + +TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { +} + +TCPSocket::~TCPSocket() { +} + +TCPSocket& TCPSocket::operator=(const TCPSocket& other) { + Resource::operator=(other); + return *this; +} + +// static +bool TCPSocket::IsAvailable() { + return has_interface(); +} + +int32_t TCPSocket::Connect(const NetAddress& addr, + const CompletionCallback& callback) { + if (has_interface()) { + return get_interface()->Connect( + pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); + } + return callback.MayForce(PP_ERROR_NOINTERFACE); +} + +NetAddress TCPSocket::GetLocalAddress() const { + if (has_interface()) { + return NetAddress( + PASS_REF, + get_interface()->GetLocalAddress(pp_resource())); + } + return NetAddress(); +} + +NetAddress TCPSocket::GetRemoteAddress() const { + if (has_interface()) { + return NetAddress( + PASS_REF, + get_interface()->GetRemoteAddress(pp_resource())); + } + return NetAddress(); +} + +int32_t TCPSocket::Read(char* buffer, + int32_t bytes_to_read, + const CompletionCallback& callback) { + if (has_interface()) { + return get_interface()->Read( + pp_resource(), buffer, bytes_to_read, + callback.pp_completion_callback()); + } + return callback.MayForce(PP_ERROR_NOINTERFACE); +} + +int32_t TCPSocket::Write(const char* buffer, + int32_t bytes_to_write, + const CompletionCallback& callback) { + if (has_interface()) { + return get_interface()->Write( + pp_resource(), buffer, bytes_to_write, + callback.pp_completion_callback()); + } + return callback.MayForce(PP_ERROR_NOINTERFACE); +} + +void TCPSocket::Close() { + if (has_interface()) + get_interface()->Close(pp_resource()); +} + +int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, + const Var& value, + const CompletionCallback& callback) { + if (has_interface()) { + return get_interface()->SetOption( + pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); + } + return callback.MayForce(PP_ERROR_NOINTERFACE); +} + +} // namespace pp diff --git a/ppapi/cpp/tcp_socket.h b/ppapi/cpp/tcp_socket.h new file mode 100644 index 0000000..5a5e0f4 --- /dev/null +++ b/ppapi/cpp/tcp_socket.h @@ -0,0 +1,166 @@ +// Copyright 2013 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_CPP_TCP_SOCKET_H_ +#define PPAPI_CPP_TCP_SOCKET_H_ + +#include "ppapi/c/ppb_tcp_socket.h" +#include "ppapi/cpp/net_address.h" +#include "ppapi/cpp/pass_ref.h" +#include "ppapi/cpp/resource.h" + +namespace pp { + +class CompletionCallback; +class InstanceHandle; + +/// The TCPSocket class provides TCP socket operations. +/// +/// Permissions: Apps permission socket with subrule +/// tcp-connect is required for Connect(). +/// For more details about network communication permissions, please see: +/// http://developer.chrome.com/apps/app_network.html +class TCPSocket : public Resource { + public: + /// Default constructor for creating an is_null() TCPSocket + /// object. + TCPSocket(); + + /// A constructor used to create a TCPSocket object. + /// + /// @param[in] instance The instance with which this resource will be + /// associated. + explicit TCPSocket(const InstanceHandle& instance); + + /// A constructor used when you have received a PP_Resource as a + /// return value that has had 1 ref added for you. + /// + /// @param[in] resource A PPB_TCPSocket resource. + TCPSocket(PassRef, PP_Resource resource); + + /// The copy constructor for TCPSocket. + /// + /// @param[in] other A reference to another TCPSocket. + TCPSocket(const TCPSocket& other); + + /// The destructor. + virtual ~TCPSocket(); + + /// The assignment operator for TCPSocket. + /// + /// @param[in] other A reference to another TCPSocket. + /// + /// @return A reference to this TCPSocket object. + TCPSocket& operator=(const TCPSocket& other); + + /// Static function for determining whether the browser supports the + /// PPB_TCPSocket interface. + /// + /// @return true if the interface is available, false otherwise. + static bool IsAvailable(); + + /// Connects the socket to the given address. + /// + /// @param[in] addr A NetAddress object. + /// @param[in] callback A CompletionCallback to be called upon + /// completion. + /// + /// @return An int32_t containing an error code from pp_errors.h, + /// including (but not limited to): + /// - PP_ERROR_NOACCESS: the caller doesn't have required + /// permissions. + /// - PP_ERROR_ADDRESS_UNREACHABLE: addr is + /// unreachable. + /// - PP_ERROR_CONNECTION_REFUSED: the connection attempt was + /// refused. + /// - PP_ERROR_CONNECTION_FAILED: the connection attempt failed. + /// - PP_ERROR_CONNECTION_TIMEDOUT: the connection attempt timed + /// out. + int32_t Connect(const NetAddress& addr, + const CompletionCallback& callback); + + /// Gets the local address of the socket, if it is connected. + /// + /// @return A NetAddress object. The object will be null + /// (i.e., is_null() returns true) on failure. + NetAddress GetLocalAddress() const; + + /// Gets the remote address of the socket, if it is connected. + /// + /// @return A NetAddress object. The object will be null + /// (i.e., is_null() returns true) on failure. + NetAddress GetRemoteAddress() const; + + /// Reads data from the socket. The socket must be connected. It may perform a + /// partial read. + /// + /// Caveat: You should be careful about the lifetime of + /// buffer. Typically you will use a + /// CompletionCallbackFactory to scope callbacks to the lifetime + /// of your class. When your class goes out of scope, the callback factory + /// will not actually cancel the operation, but will rather just skip issuing + /// the callback on your class. This means that if the underlying + /// PPB_TCPSocket resource outlives your class, the browser + /// will still try to write into your buffer when the operation completes. + /// The buffer must be kept valid until then to avoid memory corruption. + /// If you want to release the buffer while the Read() call is + /// still pending, you should call Close() to ensure that the + /// buffer won't be accessed in the future. + /// + /// @param[out] buffer The buffer to store the received data on success. It + /// must be at least as large as bytes_to_read. + /// @param[in] bytes_to_read The number of bytes to read. + /// @param[in] callback A CompletionCallback to be called upon + /// completion. + /// + /// @return A non-negative number on success to indicate how many bytes have + /// been read, 0 means that end-of-file was reached; otherwise, an error code + /// from pp_errors.h. + int32_t Read(char* buffer, + int32_t bytes_to_read, + const CompletionCallback& callback); + + /// Writes data to the socket. The socket must be connected. It may perform a + /// partial write. + /// + /// @param[in] buffer The buffer containing the data to write. + /// @param[in] bytes_to_write The number of bytes to write. + /// @param[in] callback A CompletionCallback to be called upon + /// completion. + /// + /// @return A non-negative number on success to indicate how many bytes have + /// been written; otherwise, an error code from pp_errors.h. + int32_t Write(const char* buffer, + int32_t bytes_to_write, + const CompletionCallback& callback); + + /// Cancels all pending reads and writes and disconnects the socket. Any + /// pending callbacks will still run, reporting PP_ERROR_ABORTED + /// if pending IO was interrupted. After a call to this method, no output + /// buffer pointers passed into previous Read() calls will be + /// accessed. It is not valid to call Connect() again. + /// + /// The socket is implicitly closed if it is destroyed, so you are not + /// required to call this method. + void Close(); + + /// Sets a socket option on the TCP socket. + /// Please see the PP_TCPSocket_Option description for option + /// names, value types and allowed values. + /// + /// @param[in] name The option to set. + /// @param[in] value The option value to set. + /// @param[in] callback A CompletionCallback to be called upon + /// completion. + /// + /// @return An int32_t containing an error code from pp_errors.h. + //// + int32_t SetOption(PP_TCPSocket_Option name, + const Var& value, + const CompletionCallback& callback); +}; + +} // namespace pp + +#endif // PPAPI_CPP_TCP_SOCKET_H_ -- cgit v1.1