summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--content/browser/renderer_host/pepper_message_filter.cc538
-rw-r--r--content/browser/renderer_host/pepper_message_filter.h5
-rw-r--r--content/common/pepper_messages.cc35
-rw-r--r--content/common/pepper_messages.h20
-rw-r--r--content/ppapi_plugin/ppapi_thread.cc44
-rw-r--r--content/ppapi_plugin/ppapi_thread.h10
-rw-r--r--ppapi/c/private/ppb_flash_net_connector.h7
-rw-r--r--ppapi/c/private/ppb_flash_tcp_socket.h88
-rw-r--r--ppapi/cpp/private/flash_tcp_socket.cc102
-rw-r--r--ppapi/cpp/private/flash_tcp_socket.h48
-rw-r--r--ppapi/ppapi_cpp.gypi1
-rw-r--r--ppapi/ppapi_proxy.gypi2
-rw-r--r--ppapi/ppapi_shared.gypi2
-rw-r--r--ppapi/proxy/dispatcher.cc3
-rw-r--r--ppapi/proxy/interface_id.h1
-rw-r--r--ppapi/proxy/plugin_dispatcher.cc6
-rw-r--r--ppapi/proxy/plugin_dispatcher.h11
-rw-r--r--ppapi/proxy/ppapi_messages.h40
-rw-r--r--ppapi/proxy/ppapi_param_traits.cc35
-rw-r--r--ppapi/proxy/ppapi_param_traits.h9
-rw-r--r--ppapi/proxy/ppb_flash_tcp_socket_proxy.cc438
-rw-r--r--ppapi/proxy/ppb_flash_tcp_socket_proxy.h61
-rw-r--r--ppapi/proxy/resource_creation_proxy.cc6
-rw-r--r--ppapi/proxy/resource_creation_proxy.h1
-rw-r--r--ppapi/shared_impl/resource_object_base.h1
-rw-r--r--ppapi/tests/all_c_includes.h1
-rw-r--r--ppapi/thunk/ppb_flash_tcp_socket_api.h38
-rw-r--r--ppapi/thunk/ppb_flash_tcp_socket_thunk.cc121
-rw-r--r--ppapi/thunk/resource_creation_api.h1
-rw-r--r--ppapi/thunk/thunk.h2
-rw-r--r--webkit/plugins/ppapi/plugin_module.cc3
-rw-r--r--webkit/plugins/ppapi/resource_creation_impl.cc6
-rw-r--r--webkit/plugins/ppapi/resource_creation_impl.h1
33 files changed, 1585 insertions, 102 deletions
diff --git a/content/browser/renderer_host/pepper_message_filter.cc b/content/browser/renderer_host/pepper_message_filter.cc
index 46d0095..55ec1f9 100644
--- a/content/browser/renderer_host/pepper_message_filter.cc
+++ b/content/browser/renderer_host/pepper_message_filter.cc
@@ -4,8 +4,17 @@
#include "content/browser/renderer_host/pepper_message_filter.h"
+#include <string.h>
+
+#include <limits>
+#include <string>
+
#include "base/basictypes.h"
#include "base/bind.h"
+#include "base/compiler_specific.h"
+#include "base/memory/linked_ptr.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/threading/worker_pool.h"
#include "base/values.h"
@@ -15,40 +24,497 @@
#include "content/browser/resource_context.h"
#include "content/common/pepper_messages.h"
#include "net/base/address_list.h"
+#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/host_port_pair.h"
#include "net/base/host_resolver.h"
+#include "net/base/io_buffer.h"
#include "net/base/single_request_host_resolver.h"
+#include "net/base/sys_addrinfo.h"
+#include "net/socket/tcp_client_socket.h"
#include "net/url_request/url_request_context.h"
#include "ppapi/c/private/ppb_flash_net_connector.h"
#include "ppapi/proxy/ppapi_messages.h"
+#include "ppapi/proxy/ppb_flash_tcp_socket_proxy.h"
#include "webkit/plugins/ppapi/ppb_flash_net_connector_impl.h"
#if defined(ENABLE_FLAPPER_HACKS)
-#include <netdb.h>
-#include <string.h>
-#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
+#endif // ENABLE_FLAPPER_HACKS
+
+namespace {
+
+bool ValidateNetAddress(const PP_Flash_NetAddress& addr) {
+ if (addr.size < sizeof(reinterpret_cast<sockaddr*>(0)->sa_family))
+ return false;
+
+ // TODO(viettrungluu): more careful validation?
+ // Just do a size check for AF_INET.
+ if (reinterpret_cast<const sockaddr*>(addr.data)->sa_family == AF_INET &&
+ addr.size >= sizeof(sockaddr_in))
+ return true;
+
+ // Ditto for AF_INET6.
+ if (reinterpret_cast<const sockaddr*>(addr.data)->sa_family == AF_INET6 &&
+ addr.size >= sizeof(sockaddr_in6))
+ return true;
+
+ // Reject everything else.
+ return false;
+}
+
+bool SockaddrToNetAddress(const sockaddr* sa,
+ socklen_t sa_length,
+ PP_Flash_NetAddress* net_addr) {
+ if (!sa || sa_length <= 0 || !net_addr)
+ return false;
+
+ CHECK_LE(static_cast<uint32_t>(sa_length), sizeof(net_addr->data));
+ net_addr->size = sa_length;
+ memcpy(net_addr->data, sa, net_addr->size);
+ return true;
+}
+
+bool IPEndPointToNetAddress(const net::IPEndPoint& ip,
+ PP_Flash_NetAddress* net_addr) {
+ sockaddr_storage storage = { 0 };
+ size_t length = sizeof(storage);
+
+ return ip.ToSockAddr(reinterpret_cast<sockaddr*>(&storage), &length) &&
+ SockaddrToNetAddress(reinterpret_cast<const sockaddr*>(&storage),
+ length, net_addr);
+}
+
+// Converts the first address to a PP_Flash_NetAddress.
+bool AddressListToNetAddress(const net::AddressList& address_list,
+ PP_Flash_NetAddress* net_addr) {
+ const addrinfo* head = address_list.head();
+ return head &&
+ SockaddrToNetAddress(head->ai_addr, head->ai_addrlen, net_addr);
+}
+
+bool NetAddressToAddressList(const PP_Flash_NetAddress& net_addr,
+ net::AddressList* address_list) {
+ if (!address_list || !ValidateNetAddress(net_addr))
+ return false;
+
+ net::IPEndPoint ip_end_point;
+ if (!ip_end_point.FromSockAddr(
+ reinterpret_cast<const sockaddr*>(net_addr.data), net_addr.size)) {
+ return false;
+ }
+ *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(),
+ ip_end_point.port());
+ return true;
+}
+
+} // namespace
// Make sure the storage in |PP_Flash_NetAddress| is big enough. (Do it here
// since the data is opaque elsewhere.)
COMPILE_ASSERT(sizeof(reinterpret_cast<PP_Flash_NetAddress*>(0)->data) >=
sizeof(sockaddr_storage), PP_Flash_NetAddress_data_too_small);
-#endif // ENABLE_FLAPPER_HACKS
const PP_Flash_NetAddress kInvalidNetAddress = { 0 };
+class PepperMessageFilter::FlashTCPSocket {
+ public:
+ FlashTCPSocket(PepperMessageFilter* pepper_message_filter,
+ int32 routing_id,
+ uint32 plugin_dispatcher_id,
+ uint32 socket_id);
+ ~FlashTCPSocket();
+
+ void Connect(const std::string& host, uint16_t port);
+ void ConnectWithNetAddress(const PP_Flash_NetAddress& net_addr);
+ void Read(int32 bytes_to_read);
+ void Write(const std::string& data);
+
+ private:
+ void StartConnect(const net::AddressList& addresses);
+
+ void SendConnectACKError();
+ void SendReadACKError();
+ void SendWriteACKError();
+
+ void OnResolveCompleted(int result);
+ void OnConnectCompleted(int result);
+ void OnReadCompleted(int result);
+ void OnWriteCompleted(int result);
+
+ PepperMessageFilter* pepper_message_filter_;
+ int32 routing_id_;
+ uint32 plugin_dispatcher_id_;
+ uint32 socket_id_;
+
+ bool connect_in_progress_;
+ bool connected_;
+ bool end_of_file_reached_;
+
+ net::CompletionCallbackImpl<FlashTCPSocket> resolve_callback_;
+ net::CompletionCallbackImpl<FlashTCPSocket> connect_callback_;
+ net::CompletionCallbackImpl<FlashTCPSocket> read_callback_;
+ net::CompletionCallbackImpl<FlashTCPSocket> write_callback_;
+
+ scoped_ptr<net::SingleRequestHostResolver> resolver_;
+ net::AddressList address_list_;
+
+ scoped_ptr<net::StreamSocket> socket_;
+
+ scoped_refptr<net::IOBuffer> read_buffer_;
+ scoped_refptr<net::IOBuffer> write_buffer_;
+
+ DISALLOW_COPY_AND_ASSIGN(FlashTCPSocket);
+};
+
+PepperMessageFilter::FlashTCPSocket::FlashTCPSocket(
+ PepperMessageFilter* pepper_message_filter,
+ int32 routing_id,
+ uint32 plugin_dispatcher_id,
+ uint32 socket_id)
+ : pepper_message_filter_(pepper_message_filter),
+ routing_id_(routing_id),
+ plugin_dispatcher_id_(plugin_dispatcher_id),
+ socket_id_(socket_id),
+ connect_in_progress_(false),
+ connected_(false),
+ end_of_file_reached_(false),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ resolve_callback_(this, &FlashTCPSocket::OnResolveCompleted)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ connect_callback_(this, &FlashTCPSocket::OnConnectCompleted)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ read_callback_(this, &FlashTCPSocket::OnReadCompleted)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ write_callback_(this, &FlashTCPSocket::OnWriteCompleted)) {
+ DCHECK(pepper_message_filter);
+}
+
+PepperMessageFilter::FlashTCPSocket::~FlashTCPSocket() {
+ // Make sure no further callbacks from socket_.
+ if (socket_.get())
+ socket_->Disconnect();
+}
+
+void PepperMessageFilter::FlashTCPSocket::Connect(const std::string& host,
+ uint16_t port) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ if (connect_in_progress_ || connected_) {
+ SendConnectACKError();
+ return;
+ }
+
+ connect_in_progress_ = true;
+ net::HostResolver::RequestInfo request_info(net::HostPortPair(host, port));
+ resolver_.reset(new net::SingleRequestHostResolver(
+ pepper_message_filter_->GetHostResolver()));
+ int result = resolver_->Resolve(request_info, &address_list_,
+ &resolve_callback_, net::BoundNetLog());
+ if (result != net::ERR_IO_PENDING)
+ OnResolveCompleted(result);
+}
+
+void PepperMessageFilter::FlashTCPSocket::ConnectWithNetAddress(
+ const PP_Flash_NetAddress& net_addr) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ if (connect_in_progress_ || connected_ ||
+ !NetAddressToAddressList(net_addr, &address_list_)) {
+ SendConnectACKError();
+ return;
+ }
+
+ connect_in_progress_ = true;
+ StartConnect(address_list_);
+}
+
+void PepperMessageFilter::FlashTCPSocket::Read(int32 bytes_to_read) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ if (!connected_ || end_of_file_reached_ || read_buffer_.get() ||
+ bytes_to_read <= 0) {
+ SendReadACKError();
+ return;
+ }
+
+ if (bytes_to_read > pp::proxy::kFlashTCPSocketMaxReadSize) {
+ NOTREACHED();
+ bytes_to_read = pp::proxy::kFlashTCPSocketMaxReadSize;
+ }
+
+ read_buffer_ = new net::IOBuffer(bytes_to_read);
+ int result = socket_->Read(read_buffer_, bytes_to_read, &read_callback_);
+ if (result != net::ERR_IO_PENDING)
+ OnReadCompleted(result);
+}
+
+void PepperMessageFilter::FlashTCPSocket::Write(const std::string& data) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ if (!connected_ || write_buffer_.get() || data.empty()) {
+ SendWriteACKError();
+ return;
+ }
+
+ int data_size = data.size();
+ if (data_size > pp::proxy::kFlashTCPSocketMaxWriteSize) {
+ NOTREACHED();
+ data_size = pp::proxy::kFlashTCPSocketMaxWriteSize;
+ }
+
+ write_buffer_ = new net::IOBuffer(data_size);
+ memcpy(write_buffer_->data(), data.c_str(), data_size);
+ int result = socket_->Write(write_buffer_, data.size(), &write_callback_);
+ if (result != net::ERR_IO_PENDING)
+ OnWriteCompleted(result);
+}
+
+void PepperMessageFilter::FlashTCPSocket::StartConnect(
+ const net::AddressList& addresses) {
+ DCHECK(connect_in_progress_);
+
+ socket_.reset(
+ new net::TCPClientSocket(addresses, NULL, net::NetLog::Source()));
+ int result = socket_->Connect(&connect_callback_);
+ if (result != net::ERR_IO_PENDING)
+ OnConnectCompleted(result);
+}
+
+void PepperMessageFilter::FlashTCPSocket::SendConnectACKError() {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_ConnectACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, false,
+ kInvalidNetAddress, kInvalidNetAddress));
+}
+
+void PepperMessageFilter::FlashTCPSocket::SendReadACKError() {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_ReadACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, false,
+ std::string()));
+}
+
+void PepperMessageFilter::FlashTCPSocket::SendWriteACKError() {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_WriteACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, false, 0));
+}
+
+void PepperMessageFilter::FlashTCPSocket::OnResolveCompleted(int result) {
+ DCHECK(connect_in_progress_);
+
+ if (result != net::OK) {
+ SendConnectACKError();
+ connect_in_progress_ = false;
+ return;
+ }
+
+ StartConnect(address_list_);
+}
+
+void PepperMessageFilter::FlashTCPSocket::OnConnectCompleted(int result) {
+ DCHECK(connect_in_progress_ && socket_.get());
+
+ if (result != net::OK) {
+ SendConnectACKError();
+ } else {
+ net::IPEndPoint ip_end_point;
+ net::AddressList address_list;
+ PP_Flash_NetAddress local_addr = kInvalidNetAddress;
+ PP_Flash_NetAddress remote_addr = kInvalidNetAddress;
+
+ if (socket_->GetLocalAddress(&ip_end_point) != net::OK ||
+ !IPEndPointToNetAddress(ip_end_point, &local_addr) ||
+ socket_->GetPeerAddress(&address_list) != net::OK ||
+ !AddressListToNetAddress(address_list, &remote_addr)) {
+ SendConnectACKError();
+ } else {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_ConnectACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, true,
+ local_addr, remote_addr));
+ connected_ = true;
+ }
+ }
+ connect_in_progress_ = false;
+}
+
+void PepperMessageFilter::FlashTCPSocket::OnReadCompleted(int result) {
+ DCHECK(read_buffer_.get());
+
+ if (result > 0) {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_ReadACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, true,
+ std::string(read_buffer_->data(), result)));
+ } else if (result == 0) {
+ end_of_file_reached_ = true;
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_ReadACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, true,
+ std::string()));
+ } else {
+ SendReadACKError();
+ }
+ read_buffer_ = NULL;
+}
+
+void PepperMessageFilter::FlashTCPSocket::OnWriteCompleted(int result) {
+ DCHECK(write_buffer_.get());
+
+ if (result >= 0) {
+ pepper_message_filter_->Send(
+ new PpapiMsg_PPBFlashTCPSocket_WriteACK(
+ routing_id_, plugin_dispatcher_id_, socket_id_, true, result));
+ } else {
+ SendWriteACKError();
+ }
+ write_buffer_ = NULL;
+}
+
+// FlashTCPSocketManager manages the mapping from socket IDs to FlashTCPSocket
+// instances.
+class PepperMessageFilter::FlashTCPSocketManager {
+ public:
+ explicit FlashTCPSocketManager(PepperMessageFilter* pepper_message_filter);
+
+ void OnMsgCreate(int32 routing_id,
+ uint32 plugin_dispatcher_id,
+ uint32* socket_id);
+ void OnMsgConnect(uint32 socket_id,
+ const std::string& host,
+ uint16_t port);
+ void OnMsgConnectWithNetAddress(uint32 socket_id,
+ const PP_Flash_NetAddress& net_addr);
+ void OnMsgRead(uint32 socket_id, int32_t bytes_to_read);
+ void OnMsgWrite(uint32 socket_id, const std::string& data);
+ void OnMsgDisconnect(uint32 socket_id);
+
+ private:
+ typedef std::map<uint32, linked_ptr<FlashTCPSocket> > SocketMap;
+ SocketMap sockets_;
+
+ uint32 next_socket_id_;
+
+ PepperMessageFilter* pepper_message_filter_;
+ DISALLOW_COPY_AND_ASSIGN(FlashTCPSocketManager);
+};
+
+PepperMessageFilter::FlashTCPSocketManager::FlashTCPSocketManager(
+ PepperMessageFilter* pepper_message_filter)
+ : next_socket_id_(1),
+ pepper_message_filter_(pepper_message_filter) {
+ DCHECK(pepper_message_filter);
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgCreate(
+ int32 routing_id,
+ uint32 plugin_dispatcher_id,
+ uint32* socket_id) {
+ // Generate a socket ID. For each process which sends us socket requests, IDs
+ // of living sockets must be unique.
+ //
+ // However, it is safe to generate IDs based on the internal state of a single
+ // FlashTCPSocketManager object, because for each plugin or renderer process,
+ // there is at most one PepperMessageFilter (in other words, at most one
+ // FlashTCPSocketManager) talking to it.
+
+ DCHECK(socket_id);
+ if (sockets_.size() >= std::numeric_limits<uint32>::max()) {
+ // All valid IDs are being used.
+ *socket_id = 0;
+ return;
+ }
+ do {
+ // Although it is unlikely, make sure that we won't cause any trouble when
+ // the counter overflows.
+ *socket_id = next_socket_id_++;
+ } while (*socket_id == 0 ||
+ sockets_.find(*socket_id) != sockets_.end());
+ sockets_[*socket_id] = linked_ptr<FlashTCPSocket>(
+ new FlashTCPSocket(pepper_message_filter_, routing_id,
+ plugin_dispatcher_id, *socket_id));
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgConnect(
+ uint32 socket_id,
+ const std::string& host,
+ uint16_t port) {
+ SocketMap::iterator iter = sockets_.find(socket_id);
+ if (iter == sockets_.end()) {
+ NOTREACHED();
+ return;
+ }
+
+ iter->second->Connect(host, port);
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgConnectWithNetAddress(
+ uint32 socket_id,
+ const PP_Flash_NetAddress& net_addr) {
+ SocketMap::iterator iter = sockets_.find(socket_id);
+ if (iter == sockets_.end()) {
+ NOTREACHED();
+ return;
+ }
+
+ iter->second->ConnectWithNetAddress(net_addr);
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgRead(
+ uint32 socket_id,
+ int32_t bytes_to_read) {
+ SocketMap::iterator iter = sockets_.find(socket_id);
+ if (iter == sockets_.end()) {
+ NOTREACHED();
+ return;
+ }
+
+ iter->second->Read(bytes_to_read);
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgWrite(
+ uint32 socket_id,
+ const std::string& data) {
+ SocketMap::iterator iter = sockets_.find(socket_id);
+ if (iter == sockets_.end()) {
+ NOTREACHED();
+ return;
+ }
+
+ iter->second->Write(data);
+}
+
+void PepperMessageFilter::FlashTCPSocketManager::OnMsgDisconnect(
+ uint32 socket_id) {
+ SocketMap::iterator iter = sockets_.find(socket_id);
+ if (iter == sockets_.end()) {
+ NOTREACHED();
+ return;
+ }
+
+ // Destroy the FlashTCPSocket instance will cancel any pending completion
+ // callback. From this point on, there won't be any messages associated with
+ // this socket sent to the plugin side.
+ sockets_.erase(iter);
+}
+
PepperMessageFilter::PepperMessageFilter(
const content::ResourceContext* resource_context)
: resource_context_(resource_context),
- host_resolver_(NULL) {
+ host_resolver_(NULL),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ socket_manager_(new FlashTCPSocketManager(this))) {
DCHECK(resource_context_);
}
PepperMessageFilter::PepperMessageFilter(net::HostResolver* host_resolver)
: resource_context_(NULL),
- host_resolver_(host_resolver) {
+ host_resolver_(host_resolver),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ socket_manager_(new FlashTCPSocketManager(this))) {
DCHECK(host_resolver);
}
@@ -66,6 +532,25 @@ bool PepperMessageFilter::OnMessageReceived(const IPC::Message& msg,
OnGetLocalTimeZoneOffset)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_PPBFont_GetFontFamilies,
OnGetFontFamilies)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_Create,
+ socket_manager_.get(), FlashTCPSocketManager::OnMsgCreate)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_Connect,
+ socket_manager_.get(), FlashTCPSocketManager::OnMsgConnect)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_ConnectWithNetAddress,
+ socket_manager_.get(),
+ FlashTCPSocketManager::OnMsgConnectWithNetAddress)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_Read,
+ socket_manager_.get(), FlashTCPSocketManager::OnMsgRead)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_Write,
+ socket_manager_.get(), FlashTCPSocketManager::OnMsgWrite)
+ IPC_MESSAGE_FORWARD(
+ PpapiHostMsg_PPBFlashTCPSocket_Disconnect,
+ socket_manager_.get(), FlashTCPSocketManager::OnMsgDisconnect)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
return handled;
@@ -75,34 +560,6 @@ bool PepperMessageFilter::OnMessageReceived(const IPC::Message& msg,
namespace {
-bool ValidateNetAddress(const PP_Flash_NetAddress& addr) {
- if (addr.size < sizeof(sa_family_t))
- return false;
-
- // TODO(viettrungluu): more careful validation?
- // Just do a size check for AF_INET.
- if (reinterpret_cast<const sockaddr*>(addr.data)->sa_family == AF_INET &&
- addr.size >= sizeof(sockaddr_in))
- return true;
-
- // Ditto for AF_INET6.
- if (reinterpret_cast<const sockaddr*>(addr.data)->sa_family == AF_INET6 &&
- addr.size >= sizeof(sockaddr_in6))
- return true;
-
- // Reject everything else.
- return false;
-}
-
-PP_Flash_NetAddress SockaddrToNetAddress(const struct sockaddr* sa,
- socklen_t sa_length) {
- PP_Flash_NetAddress addr;
- CHECK_LE(sa_length, sizeof(addr.data));
- addr.size = sa_length;
- memcpy(addr.data, sa, addr.size);
- return addr;
-}
-
int ConnectTcpSocket(const PP_Flash_NetAddress& addr,
PP_Flash_NetAddress* local_addr_out,
PP_Flash_NetAddress* remote_addr_out) {
@@ -249,14 +706,15 @@ void PepperMessageFilter::ConnectTcpOnWorkerThread(int routing_id,
IPC::InvalidPlatformFileForTransit();
PP_Flash_NetAddress local_addr = kInvalidNetAddress;
PP_Flash_NetAddress remote_addr = kInvalidNetAddress;
+ PP_Flash_NetAddress addr = kInvalidNetAddress;
for (const struct addrinfo* ai = addresses.head(); ai; ai = ai->ai_next) {
- PP_Flash_NetAddress addr = SockaddrToNetAddress(ai->ai_addr,
- ai->ai_addrlen);
- int fd = ConnectTcpSocket(addr, &local_addr, &remote_addr);
- if (fd != -1) {
- socket_for_transit = base::FileDescriptor(fd, true);
- break;
+ if (SockaddrToNetAddress(ai->ai_addr, ai->ai_addrlen, &addr)) {
+ int fd = ConnectTcpSocket(addr, &local_addr, &remote_addr);
+ if (fd != -1) {
+ socket_for_transit = base::FileDescriptor(fd, true);
+ break;
+ }
}
}
diff --git a/content/browser/renderer_host/pepper_message_filter.h b/content/browser/renderer_host/pepper_message_filter.h
index f047ec9..e9cd398 100644
--- a/content/browser/renderer_host/pepper_message_filter.h
+++ b/content/browser/renderer_host/pepper_message_filter.h
@@ -41,6 +41,9 @@ class PepperMessageFilter : public BrowserMessageFilter {
bool* message_was_ok);
private:
+ class FlashTCPSocket;
+ class FlashTCPSocketManager;
+
#if defined(ENABLE_FLAPPER_HACKS)
// Message handlers.
void OnConnectTcp(int routing_id,
@@ -91,6 +94,8 @@ class PepperMessageFilter : public BrowserMessageFilter {
// GetHostResolver instead of accessing directly.
net::HostResolver* host_resolver_;
+ scoped_ptr<FlashTCPSocketManager> socket_manager_;
+
DISALLOW_COPY_AND_ASSIGN(PepperMessageFilter);
};
diff --git a/content/common/pepper_messages.cc b/content/common/pepper_messages.cc
index b2a8f63..4d7602b 100644
--- a/content/common/pepper_messages.cc
+++ b/content/common/pepper_messages.cc
@@ -2,40 +2,5 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "ppapi/c/private/ppb_flash_net_connector.h"
-
#define IPC_MESSAGE_IMPL
#include "content/common/pepper_messages.h"
-
-namespace IPC {
-
-void ParamTraits<PP_Flash_NetAddress>::Write(Message* m, const param_type& p) {
- WriteParam(m, p.size);
- m->WriteBytes(p.data, static_cast<int>(p.size));
-}
-
-bool ParamTraits<PP_Flash_NetAddress>::Read(const Message* m,
- void** iter,
- param_type* p) {
- uint16 size;
- if (!ReadParam(m, iter, &size))
- return false;
- if (size > sizeof(p->data))
- return false;
- p->size = size;
-
- const char* data;
- if (!m->ReadBytes(iter, &data, size))
- return false;
- memcpy(p->data, data, size);
- return true;
-}
-
-void ParamTraits<PP_Flash_NetAddress>::Log(const param_type& p,
- std::string* l) {
- l->append("<PP_Flash_NetAddress (");
- LogParam(p.size, l);
- l->append(" bytes)>");
-}
-
-} // namespace IPC
diff --git a/content/common/pepper_messages.h b/content/common/pepper_messages.h
index 5a84626..b89705e 100644
--- a/content/common/pepper_messages.h
+++ b/content/common/pepper_messages.h
@@ -4,27 +4,9 @@
// Multiply-included message file, no traditional include guard
#include "ipc/ipc_message_macros.h"
-#include "ipc/ipc_param_traits.h"
#include "ipc/ipc_platform_file.h"
#include "ppapi/c/private/ppb_flash_net_connector.h"
-
-// Singly-included section, not yet converted.
-#ifndef CONTENT_COMMON_PEPPER_MESSAGES_H_
-#define CONTENT_COMMON_PEPPER_MESSAGES_H_
-
-namespace IPC {
-
-template <>
-struct ParamTraits<PP_Flash_NetAddress> {
- typedef PP_Flash_NetAddress param_type;
- static void Write(Message* m, const param_type& p);
- static bool Read(const Message* m, void** iter, param_type* p);
- static void Log(const param_type& p, std::string* l);
-};
-
-} // namespace IPC
-
-#endif // CONTENT_COMMON_PEPPER_MESSAGES_H_
+#include "ppapi/proxy/ppapi_param_traits.h"
#define IPC_MESSAGE_START PepperMsgStart
diff --git a/content/ppapi_plugin/ppapi_thread.cc b/content/ppapi_plugin/ppapi_thread.cc
index 9b3c846..c3b42ed 100644
--- a/content/ppapi_plugin/ppapi_thread.cc
+++ b/content/ppapi_plugin/ppapi_thread.cc
@@ -41,7 +41,8 @@ PpapiThread::PpapiThread(bool is_broker)
get_plugin_interface_(NULL),
connect_instance_func_(NULL),
local_pp_module_(
- base::RandInt(0, std::numeric_limits<PP_Module>::max())) {
+ base::RandInt(0, std::numeric_limits<PP_Module>::max())),
+ next_plugin_dispatcher_id_(1) {
}
PpapiThread::~PpapiThread() {
@@ -70,6 +71,12 @@ bool PpapiThread::OnMessageReceived(const IPC::Message& msg) {
IPC_BEGIN_MESSAGE_MAP(PpapiThread, msg)
IPC_MESSAGE_HANDLER(PpapiMsg_LoadPlugin, OnMsgLoadPlugin)
IPC_MESSAGE_HANDLER(PpapiMsg_CreateChannel, OnMsgCreateChannel)
+ IPC_MESSAGE_HANDLER_GENERIC(PpapiMsg_PPBFlashTCPSocket_ConnectACK,
+ OnPluginDispatcherMessageReceived(msg))
+ IPC_MESSAGE_HANDLER_GENERIC(PpapiMsg_PPBFlashTCPSocket_ReadACK,
+ OnPluginDispatcherMessageReceived(msg))
+ IPC_MESSAGE_HANDLER_GENERIC(PpapiMsg_PPBFlashTCPSocket_WriteACK,
+ OnPluginDispatcherMessageReceived(msg))
IPC_END_MESSAGE_MAP()
return true;
}
@@ -103,6 +110,27 @@ bool PpapiThread::SendToBrowser(IPC::Message* msg) {
return Send(msg);
}
+uint32 PpapiThread::Register(pp::proxy::PluginDispatcher* plugin_dispatcher) {
+ if (!plugin_dispatcher ||
+ plugin_dispatchers_.size() >= std::numeric_limits<uint32>::max()) {
+ return 0;
+ }
+
+ uint32 id = 0;
+ do {
+ // Although it is unlikely, make sure that we won't cause any trouble when
+ // the counter overflows.
+ id = next_plugin_dispatcher_id_++;
+ } while (id == 0 ||
+ plugin_dispatchers_.find(id) != plugin_dispatchers_.end());
+ plugin_dispatchers_[id] = plugin_dispatcher;
+ return id;
+}
+
+void PpapiThread::Unregister(uint32 plugin_dispatcher_id) {
+ plugin_dispatchers_.erase(plugin_dispatcher_id);
+}
+
void PpapiThread::OnMsgLoadPlugin(const FilePath& path) {
base::ScopedNativeLibrary library(base::LoadNativeLibrary(path, NULL));
@@ -192,6 +220,20 @@ void PpapiThread::OnMsgCreateChannel(base::ProcessHandle host_process_handle,
Send(new PpapiHostMsg_ChannelCreated(channel_handle));
}
+void PpapiThread::OnPluginDispatcherMessageReceived(const IPC::Message& msg) {
+ // The first parameter should be a plugin dispatcher ID.
+ void* iter = NULL;
+ uint32 id = 0;
+ if (!msg.ReadUInt32(&iter, &id)) {
+ NOTREACHED();
+ return;
+ }
+ std::map<uint32, pp::proxy::PluginDispatcher*>::iterator dispatcher =
+ plugin_dispatchers_.find(id);
+ if (dispatcher != plugin_dispatchers_.end())
+ dispatcher->second->OnMessageReceived(msg);
+}
+
bool PpapiThread::SetupRendererChannel(base::ProcessHandle host_process_handle,
int renderer_id,
IPC::ChannelHandle* handle) {
diff --git a/content/ppapi_plugin/ppapi_thread.h b/content/ppapi_plugin/ppapi_thread.h
index 3d20387..5b77f24 100644
--- a/content/ppapi_plugin/ppapi_thread.h
+++ b/content/ppapi_plugin/ppapi_thread.h
@@ -6,6 +6,8 @@
#define CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
#pragma once
+#include <map>
+
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
@@ -42,11 +44,15 @@ class PpapiThread : public ChildThread,
virtual void PostToWebKitThread(const tracked_objects::Location& from_here,
const base::Closure& task) OVERRIDE;
virtual bool SendToBrowser(IPC::Message* msg) OVERRIDE;
+ virtual uint32 Register(
+ pp::proxy::PluginDispatcher* plugin_dispatcher) OVERRIDE;
+ virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
// Message handlers.
void OnMsgLoadPlugin(const FilePath& path);
void OnMsgCreateChannel(base::ProcessHandle host_process_handle,
int renderer_id);
+ void OnPluginDispatcherMessageReceived(const IPC::Message& msg);
// Sets up the channel to the given renderer. On success, returns true and
// fills the given ChannelHandle with the information from the new channel.
@@ -81,6 +87,10 @@ class PpapiThread : public ChildThread,
scoped_ptr<PpapiWebKitThread> webkit_thread_;
+ // The PluginDispatcher instances contained in the map are not owned by it.
+ std::map<uint32, pp::proxy::PluginDispatcher*> plugin_dispatchers_;
+ uint32 next_plugin_dispatcher_id_;
+
DISALLOW_IMPLICIT_CONSTRUCTORS(PpapiThread);
};
diff --git a/ppapi/c/private/ppb_flash_net_connector.h b/ppapi/c/private/ppb_flash_net_connector.h
index 2cc25bb..188dbef 100644
--- a/ppapi/c/private/ppb_flash_net_connector.h
+++ b/ppapi/c/private/ppb_flash_net_connector.h
@@ -9,15 +9,10 @@
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/private/ppb_flash_file.h" // For |PP_FileHandle|.
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#define PPB_FLASH_NETCONNECTOR_INTERFACE "PPB_Flash_NetConnector;0.1"
-// This is an opaque type holding a network address.
-struct PP_Flash_NetAddress {
- uint32_t size;
- char data[128];
-};
-
struct PPB_Flash_NetConnector {
PP_Resource (*Create)(PP_Instance instance_id);
PP_Bool (*IsFlashNetConnector)(PP_Resource resource_id);
diff --git a/ppapi/c/private/ppb_flash_tcp_socket.h b/ppapi/c/private/ppb_flash_tcp_socket.h
new file mode 100644
index 0000000..6577631
--- /dev/null
+++ b/ppapi/c/private/ppb_flash_tcp_socket.h
@@ -0,0 +1,88 @@
+// Copyright (c) 2011 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_C_PRIVATE_PPB_FLASH_TCP_SOCKET_H_
+#define PPAPI_C_PRIVATE_PPB_FLASH_TCP_SOCKET_H_
+
+#include "ppapi/c/pp_bool.h"
+#include "ppapi/c/pp_completion_callback.h"
+#include "ppapi/c/pp_instance.h"
+#include "ppapi/c/pp_resource.h"
+#include "ppapi/c/pp_stdint.h"
+
+// This is an opaque type holding a network address.
+struct PP_Flash_NetAddress {
+ uint32_t size;
+ char data[128];
+};
+
+#define PPB_FLASH_TCPSOCKET_INTERFACE "PPB_Flash_TCPSocket;0.1"
+
+struct PPB_Flash_TCPSocket {
+ PP_Resource (*Create)(PP_Instance instance);
+
+ PP_Bool (*IsFlashTCPSocket)(PP_Resource resource);
+
+ // Connects to a TCP port given as a host-port pair.
+ int32_t (*Connect)(PP_Resource tcp_socket,
+ const char* host,
+ uint16_t port,
+ struct PP_CompletionCallback callback);
+
+ // Same as Connect(), but connecting to the address given by |addr|. A typical
+ // use-case would be for reconnections.
+ int32_t (*ConnectWithNetAddress)(PP_Resource tcp_socket,
+ const struct PP_Flash_NetAddress* addr,
+ struct PP_CompletionCallback callback);
+
+ // Gets the local address of the socket, if it has been connected.
+ // Returns PP_TRUE on success.
+ PP_Bool (*GetLocalAddress)(PP_Resource tcp_socket,
+ struct PP_Flash_NetAddress* local_addr);
+
+ // Gets the remote address of the socket, if it has been connected.
+ // Returns PP_TRUE on success.
+ PP_Bool (*GetRemoteAddress)(PP_Resource tcp_socket,
+ struct PP_Flash_NetAddress* remote_addr);
+
+ // Does SSL handshake and moves to sending and receiving encrypted data. The
+ // socket must have been successfully connected. |server_name| will be
+ // compared with the name(s) in the server's certificate during the SSL
+ // handshake.
+ int32_t (*InitiateSSL)(PP_Resource tcp_socket,
+ const char* server_name,
+ struct PP_CompletionCallback callback);
+
+ // Reads data from the socket. The size of |buffer| must be at least as large
+ // as |bytes_to_read|. May perform a partial read. Returns the number of bytes
+ // read or an error code. If the return value is 0, then it indicates that
+ // end-of-file was reached.
+ // This method won't return more than 1 megabyte, so if |bytes_to_read|
+ // exceeds 1 megabyte, it will always perform a partial read.
+ // Multiple outstanding read requests are not supported.
+ int32_t (*Read)(PP_Resource tcp_socket,
+ char* buffer,
+ int32_t bytes_to_read,
+ struct PP_CompletionCallback callback);
+
+ // Writes data to the socket. May perform a partial write. Returns the number
+ // of bytes written or an error code.
+ // This method won't write more than 1 megabyte, so if |bytes_to_write|
+ // exceeds 1 megabyte, it will always perform a partial write.
+ // Multiple outstanding write requests are not supported.
+ int32_t (*Write)(PP_Resource tcp_socket,
+ const char* buffer,
+ int32_t bytes_to_write,
+ struct PP_CompletionCallback callback);
+
+ // Cancels any IO that may be pending, and disconnects the socket. Any pending
+ // callbacks will still run, reporting PP_Error_Aborted if pending IO was
+ // interrupted. It is NOT valid to call Connect() again after a call to this
+ // method. Note: If the socket is destroyed when it is still connected, then
+ // it will be implicitly disconnected, so you are not required to call this
+ // method.
+ void (*Disconnect)(PP_Resource tcp_socket);
+};
+
+#endif // PPAPI_C_PRIVATE_PPB_FLASH_TCP_SOCKET_H_
diff --git a/ppapi/cpp/private/flash_tcp_socket.cc b/ppapi/cpp/private/flash_tcp_socket.cc
new file mode 100644
index 0000000..f5b4633
--- /dev/null
+++ b/ppapi/cpp/private/flash_tcp_socket.cc
@@ -0,0 +1,102 @@
+// Copyright (c) 2011 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.
+
+// TODO(yzshen): See the comment in corresponding .h file.
+
+#include "ppapi/cpp/private/flash_tcp_socket.h"
+
+#include "ppapi/c/pp_bool.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/cpp/module_impl.h"
+
+namespace pp {
+
+namespace {
+
+template <> const char* interface_name<PPB_Flash_TCPSocket>() {
+ return PPB_FLASH_TCPSOCKET_INTERFACE;
+}
+
+} // namespace
+
+namespace flash {
+
+TCPSocket::TCPSocket(Instance* instance) {
+ if (has_interface<PPB_Flash_TCPSocket>() && instance) {
+ PassRefFromConstructor(get_interface<PPB_Flash_TCPSocket>()->Create(
+ instance->pp_instance()));
+ }
+}
+
+int32_t TCPSocket::Connect(const char* host,
+ uint16_t port,
+ const CompletionCallback& callback) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return PP_ERROR_NOINTERFACE;
+ return get_interface<PPB_Flash_TCPSocket>()->Connect(
+ pp_resource(), host, port, callback.pp_completion_callback());
+}
+
+int32_t TCPSocket::ConnectWithNetAddress(const PP_Flash_NetAddress* addr,
+ const CompletionCallback& callback) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return PP_ERROR_NOINTERFACE;
+ return get_interface<PPB_Flash_TCPSocket>()->ConnectWithNetAddress(
+ pp_resource(), addr, callback.pp_completion_callback());
+}
+
+bool TCPSocket::GetLocalAddress(PP_Flash_NetAddress* local_addr) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return false;
+
+ PP_Bool result = get_interface<PPB_Flash_TCPSocket>()->GetLocalAddress(
+ pp_resource(), local_addr);
+ return PP_ToBool(result);
+}
+
+bool TCPSocket::GetRemoteAddress(PP_Flash_NetAddress* remote_addr) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return false;
+ PP_Bool result = get_interface<PPB_Flash_TCPSocket>()->GetRemoteAddress(
+ pp_resource(), remote_addr);
+ return PP_ToBool(result);
+}
+
+int32_t TCPSocket::InitiateSSL(const char* server_name,
+ const CompletionCallback& callback) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return PP_ERROR_NOINTERFACE;
+ return get_interface<PPB_Flash_TCPSocket>()->InitiateSSL(
+ pp_resource(), server_name, callback.pp_completion_callback());
+}
+
+int32_t TCPSocket::Read(char* buffer,
+ int32_t bytes_to_read,
+ const CompletionCallback& callback) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return PP_ERROR_NOINTERFACE;
+ return get_interface<PPB_Flash_TCPSocket>()->Read(
+ pp_resource(), buffer, bytes_to_read, callback.pp_completion_callback());
+}
+
+int32_t TCPSocket::Write(const char* buffer,
+ int32_t bytes_to_write,
+ const CompletionCallback& callback) {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return PP_ERROR_NOINTERFACE;
+ return get_interface<PPB_Flash_TCPSocket>()->Write(
+ pp_resource(), buffer, bytes_to_write, callback.pp_completion_callback());
+}
+
+void TCPSocket::Disconnect() {
+ if (!has_interface<PPB_Flash_TCPSocket>())
+ return;
+ return get_interface<PPB_Flash_TCPSocket>()->Disconnect(pp_resource());
+}
+
+} // namespace flash
+} // namespace pp
diff --git a/ppapi/cpp/private/flash_tcp_socket.h b/ppapi/cpp/private/flash_tcp_socket.h
new file mode 100644
index 0000000..8998728
--- /dev/null
+++ b/ppapi/cpp/private/flash_tcp_socket.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2011 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.
+
+// TODO(yzshen): This (and the .cc file) contain C++ wrappers for things
+// in ppapi/c/private/ppb_flash_tcp_socket.h. This is currently not used in
+// (or even compiled with) Chromium.
+
+#ifndef PPAPI_CPP_PRIVATE_FLASH_TCP_SOCKET_H_
+#define PPAPI_CPP_PRIVATE_FLASH_TCP_SOCKET_H_
+
+#include "ppapi/c/pp_stdint.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
+#include "ppapi/cpp/resource.h"
+
+namespace pp {
+
+class CompletionCallback;
+class Instance;
+
+namespace flash {
+
+class TCPSocket : public Resource {
+ public:
+ explicit TCPSocket(Instance* instance);
+
+ int32_t Connect(const char* host,
+ uint16_t port,
+ const CompletionCallback& callback);
+ int32_t ConnectWithNetAddress(const PP_Flash_NetAddress* addr,
+ const CompletionCallback& callback);
+ bool GetLocalAddress(PP_Flash_NetAddress* local_addr);
+ bool GetRemoteAddress(PP_Flash_NetAddress* remote_addr);
+ int32_t InitiateSSL(const char* server_name,
+ const CompletionCallback& callback);
+ int32_t Read(char* buffer,
+ int32_t bytes_to_read,
+ const CompletionCallback& callback);
+ int32_t Write(const char* buffer,
+ int32_t bytes_to_write,
+ const CompletionCallback& callback);
+ void Disconnect();
+};
+
+} // namespace flash
+} // namespace pp
+
+#endif // PPAPI_CPP_PRIVATE_FLASH_TCP_SOCKET_H_
diff --git a/ppapi/ppapi_cpp.gypi b/ppapi/ppapi_cpp.gypi
index 045af8b..315e71c 100644
--- a/ppapi/ppapi_cpp.gypi
+++ b/ppapi/ppapi_cpp.gypi
@@ -92,6 +92,7 @@
'c/private/ppb_flash_file.h',
'c/private/ppb_flash_menu.h',
'c/private/ppb_flash_net_connector.h',
+ 'c/private/ppb_flash_tcp_socket.h',
'c/private/ppb_instance_private.h',
'c/private/ppb_nacl_private.h',
'c/private/ppb_pdf.h',
diff --git a/ppapi/ppapi_proxy.gypi b/ppapi/ppapi_proxy.gypi
index 1d4ba98..5af875d 100644
--- a/ppapi/ppapi_proxy.gypi
+++ b/ppapi/ppapi_proxy.gypi
@@ -90,6 +90,8 @@
'proxy/ppb_flash_menu_proxy.h',
'proxy/ppb_flash_net_connector_proxy.cc',
'proxy/ppb_flash_net_connector_proxy.h',
+ 'proxy/ppb_flash_tcp_socket_proxy.cc',
+ 'proxy/ppb_flash_tcp_socket_proxy.h',
'proxy/ppb_font_proxy.cc',
'proxy/ppb_font_proxy.h',
'proxy/ppb_graphics_2d_proxy.cc',
diff --git a/ppapi/ppapi_shared.gypi b/ppapi/ppapi_shared.gypi
index 11440a1..9d0d734 100644
--- a/ppapi/ppapi_shared.gypi
+++ b/ppapi/ppapi_shared.gypi
@@ -87,6 +87,8 @@
'thunk/ppb_flash_menu_thunk.cc',
'thunk/ppb_flash_net_connector_api.h',
'thunk/ppb_flash_net_connector_thunk.cc',
+ 'thunk/ppb_flash_tcp_socket_api.h',
+ 'thunk/ppb_flash_tcp_socket_thunk.cc',
'thunk/ppb_font_api.h',
'thunk/ppb_font_thunk.cc',
'thunk/ppb_fullscreen_thunk.cc',
diff --git a/ppapi/proxy/dispatcher.cc b/ppapi/proxy/dispatcher.cc
index 3520752..b8db635 100644
--- a/ppapi/proxy/dispatcher.cc
+++ b/ppapi/proxy/dispatcher.cc
@@ -40,6 +40,7 @@
#include "ppapi/c/private/ppb_flash_file.h"
#include "ppapi/c/private/ppb_flash_menu.h"
#include "ppapi/c/private/ppb_flash_net_connector.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#include "ppapi/c/private/ppb_pdf.h"
#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
#include "ppapi/proxy/ppapi_messages.h"
@@ -61,6 +62,7 @@
#include "ppapi/proxy/ppb_flash_proxy.h"
#include "ppapi/proxy/ppb_flash_menu_proxy.h"
#include "ppapi/proxy/ppb_flash_net_connector_proxy.h"
+#include "ppapi/proxy/ppb_flash_tcp_socket_proxy.h"
#include "ppapi/proxy/ppb_font_proxy.h"
#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
#include "ppapi/proxy/ppb_image_data_proxy.h"
@@ -129,6 +131,7 @@ InterfaceList::InterfaceList() {
AddPPB(PPB_Flash_File_ModuleLocal_Proxy::GetInfo());
AddPPB(PPB_Flash_Proxy::GetInfo());
AddPPB(PPB_Flash_Menu_Proxy::GetInfo());
+ AddPPB(PPB_Flash_TCPSocket_Proxy::GetInfo());
AddPPB(PPB_Font_Proxy::GetInfo());
AddPPB(PPB_Graphics2D_Proxy::GetInfo());
AddPPB(PPB_ImageData_Proxy::GetInfo());
diff --git a/ppapi/proxy/interface_id.h b/ppapi/proxy/interface_id.h
index 40554f5..8fccd1d 100644
--- a/ppapi/proxy/interface_id.h
+++ b/ppapi/proxy/interface_id.h
@@ -33,6 +33,7 @@ enum InterfaceID {
INTERFACE_ID_PPB_FLASH_FILE_MODULELOCAL,
INTERFACE_ID_PPB_FLASH_MENU,
INTERFACE_ID_PPB_FLASH_NETCONNECTOR,
+ INTERFACE_ID_PPB_FLASH_TCPSOCKET,
INTERFACE_ID_PPB_FONT,
INTERFACE_ID_PPB_GRAPHICS_2D,
INTERFACE_ID_PPB_IMAGE_DATA,
diff --git a/ppapi/proxy/plugin_dispatcher.cc b/ppapi/proxy/plugin_dispatcher.cc
index ac0a072..6e13b8f 100644
--- a/ppapi/proxy/plugin_dispatcher.cc
+++ b/ppapi/proxy/plugin_dispatcher.cc
@@ -44,7 +44,8 @@ PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
GetInterfaceFunc get_interface)
: Dispatcher(remote_process_handle, get_interface),
plugin_delegate_(NULL),
- received_preferences_(false) {
+ received_preferences_(false),
+ plugin_dispatcher_id_(0) {
SetSerializationRules(new PluginVarSerializationRules);
// As a plugin, we always support the PPP_Class interface. There's no
@@ -56,6 +57,8 @@ PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
}
PluginDispatcher::~PluginDispatcher() {
+ if (plugin_delegate_)
+ plugin_delegate_->Unregister(plugin_dispatcher_id_);
}
// static
@@ -86,6 +89,7 @@ bool PluginDispatcher::InitPluginWithChannel(
if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client))
return false;
plugin_delegate_ = delegate;
+ plugin_dispatcher_id_ = plugin_delegate_->Register(this);
// The message filter will intercept and process certain messages directly
// on the I/O thread.
diff --git a/ppapi/proxy/plugin_dispatcher.h b/ppapi/proxy/plugin_dispatcher.h
index 1964d94..ae6da60 100644
--- a/ppapi/proxy/plugin_dispatcher.h
+++ b/ppapi/proxy/plugin_dispatcher.h
@@ -7,6 +7,7 @@
#include <string>
+#include "base/basictypes.h"
#include "base/hash_tables.h"
#include "base/memory/scoped_ptr.h"
#include "base/process.h"
@@ -60,6 +61,12 @@ class PluginDispatcher : public Dispatcher {
// Sends the given message to the browser. Identical semantics to
// IPC::Message::Sender interface.
virtual bool SendToBrowser(IPC::Message* msg) = 0;
+
+ // Registers the plugin dispatcher and returns an ID.
+ // Plugin dispatcher IDs will be used to dispatch messages from the browser.
+ // Each call to Register() has to be matched with a call to Unregister().
+ virtual uint32 Register(PluginDispatcher* plugin_dispatcher) = 0;
+ virtual void Unregister(uint32 plugin_dispatcher_id) = 0;
};
// Constructor for the plugin side. The init and shutdown functions will be
@@ -123,6 +130,8 @@ class PluginDispatcher : public Dispatcher {
ppapi::FunctionGroupBase* GetFunctionAPI(
pp::proxy::InterfaceID id);
+ uint32 plugin_dispatcher_id() const { return plugin_dispatcher_id_; }
+
private:
friend class PluginDispatcherTest;
@@ -154,6 +163,8 @@ class PluginDispatcher : public Dispatcher {
bool received_preferences_;
ppapi::Preferences preferences_;
+ uint32 plugin_dispatcher_id_;
+
DISALLOW_COPY_AND_ASSIGN(PluginDispatcher);
};
diff --git a/ppapi/proxy/ppapi_messages.h b/ppapi/proxy/ppapi_messages.h
index 9b29993..75f79ac 100644
--- a/ppapi/proxy/ppapi_messages.h
+++ b/ppapi/proxy/ppapi_messages.h
@@ -26,6 +26,7 @@
#include "ppapi/c/pp_rect.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_size.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#include "ppapi/proxy/ppapi_param_traits.h"
#include "ppapi/proxy/serialized_flash_menu.h"
#include "ppapi/proxy/serialized_structs.h"
@@ -149,6 +150,24 @@ IPC_MESSAGE_ROUTED5(PpapiMsg_PPBFlashNetConnector_ConnectACK,
std::string /* local_addr_as_string */,
std::string /* remote_addr_as_string */)
+// PPB_Flash_TCPSocket.
+IPC_MESSAGE_ROUTED5(PpapiMsg_PPBFlashTCPSocket_ConnectACK,
+ uint32 /* plugin_dispatcher_id */,
+ uint32 /* socket_id */,
+ bool /* succeeded */,
+ PP_Flash_NetAddress /* local_addr */,
+ PP_Flash_NetAddress /* remote_addr */)
+IPC_MESSAGE_ROUTED4(PpapiMsg_PPBFlashTCPSocket_ReadACK,
+ uint32 /* plugin_dispatcher_id */,
+ uint32 /* socket_id */,
+ bool /* succeeded */,
+ std::string /* data */)
+IPC_MESSAGE_ROUTED4(PpapiMsg_PPBFlashTCPSocket_WriteACK,
+ uint32 /* plugin_dispatcher_id */,
+ uint32 /* socket_id */,
+ bool /* succeeded */,
+ int32_t /* bytes_written */)
+
// PPB_Graphics2D.
IPC_MESSAGE_ROUTED2(PpapiMsg_PPBGraphics2D_FlushACK,
pp::proxy::HostResource /* graphics_2d */,
@@ -536,6 +555,27 @@ IPC_MESSAGE_ROUTED2(PpapiHostMsg_PPBFlashNetConnector_ConnectTcpAddress,
pp::proxy::HostResource /* connector */,
std::string /* net_address_as_string */)
+// PPB_Flash_TCPSocket.
+IPC_SYNC_MESSAGE_CONTROL2_1(PpapiHostMsg_PPBFlashTCPSocket_Create,
+ int32 /* routing_id */,
+ uint32 /* plugin_dispatcher_id */,
+ uint32 /* socket_id */)
+IPC_MESSAGE_CONTROL3(PpapiHostMsg_PPBFlashTCPSocket_Connect,
+ uint32 /* socket_id */,
+ std::string /* host */,
+ uint16_t /* port */)
+IPC_MESSAGE_CONTROL2(PpapiHostMsg_PPBFlashTCPSocket_ConnectWithNetAddress,
+ uint32 /* socket_id */,
+ PP_Flash_NetAddress /* net_addr */)
+IPC_MESSAGE_CONTROL2(PpapiHostMsg_PPBFlashTCPSocket_Read,
+ uint32 /* socket_id */,
+ int32_t /* bytes_to_read */)
+IPC_MESSAGE_CONTROL2(PpapiHostMsg_PPBFlashTCPSocket_Write,
+ uint32 /* socket_id */,
+ std::string /* data */)
+IPC_MESSAGE_CONTROL1(PpapiHostMsg_PPBFlashTCPSocket_Disconnect,
+ uint32 /* socket_id */)
+
// PPB_Font.
IPC_SYNC_MESSAGE_CONTROL0_1(PpapiHostMsg_PPBFont_GetFontFamilies,
std::string /* result */)
diff --git a/ppapi/proxy/ppapi_param_traits.cc b/ppapi/proxy/ppapi_param_traits.cc
index 20a00c8..aa21dc1 100644
--- a/ppapi/proxy/ppapi_param_traits.cc
+++ b/ppapi/proxy/ppapi_param_traits.cc
@@ -8,6 +8,7 @@
#include "ppapi/c/dev/pp_file_info_dev.h"
#include "ppapi/c/pp_resource.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#include "ppapi/proxy/host_resource.h"
#include "ppapi/proxy/interface_proxy.h"
#include "ppapi/proxy/ppapi_messages.h"
@@ -136,6 +137,40 @@ bool ParamTraits<PP_FileInfo_Dev>::Read(const Message* m, void** iter,
void ParamTraits<PP_FileInfo_Dev>::Log(const param_type& p, std::string* l) {
}
+// PP_Flash_NetAddress ---------------------------------------------------------
+
+// static
+void ParamTraits<PP_Flash_NetAddress>::Write(Message* m, const param_type& p) {
+ WriteParam(m, p.size);
+ m->WriteBytes(p.data, static_cast<int>(p.size));
+}
+
+// static
+bool ParamTraits<PP_Flash_NetAddress>::Read(const Message* m,
+ void** iter,
+ param_type* p) {
+ uint16 size;
+ if (!ReadParam(m, iter, &size))
+ return false;
+ if (size > sizeof(p->data))
+ return false;
+ p->size = size;
+
+ const char* data;
+ if (!m->ReadBytes(iter, &data, size))
+ return false;
+ memcpy(p->data, data, size);
+ return true;
+}
+
+// static
+void ParamTraits<PP_Flash_NetAddress>::Log(const param_type& p,
+ std::string* l) {
+ l->append("<PP_Flash_NetAddress (");
+ LogParam(p.size, l);
+ l->append(" bytes)>");
+}
+
// PP_InputEvent ---------------------------------------------------------------
// static
diff --git a/ppapi/proxy/ppapi_param_traits.h b/ppapi/proxy/ppapi_param_traits.h
index 3d6d57f..62f807b 100644
--- a/ppapi/proxy/ppapi_param_traits.h
+++ b/ppapi/proxy/ppapi_param_traits.h
@@ -15,6 +15,7 @@
struct PP_FileInfo_Dev;
struct PP_ObjectProperty;
struct PP_Flash_Menu;
+struct PP_Flash_NetAddress;
namespace pp {
namespace proxy {
@@ -49,6 +50,14 @@ struct ParamTraits<PP_FileInfo_Dev> {
static void Log(const param_type& p, std::string* l);
};
+template <>
+struct ParamTraits<PP_Flash_NetAddress> {
+ typedef PP_Flash_NetAddress param_type;
+ static void Write(Message* m, const param_type& p);
+ static bool Read(const Message* m, void** iter, param_type* p);
+ static void Log(const param_type& p, std::string* l);
+};
+
template<>
struct ParamTraits<PP_InputEvent> {
typedef PP_InputEvent param_type;
diff --git a/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc b/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc
new file mode 100644
index 0000000..d8968d8
--- /dev/null
+++ b/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc
@@ -0,0 +1,438 @@
+// Copyright (c) 2011 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/proxy/ppb_flash_tcp_socket_proxy.h"
+
+#include <algorithm>
+#include <cstring>
+#include <map>
+
+#include "base/logging.h"
+#include "base/memory/linked_ptr.h"
+#include "base/message_loop.h"
+#include "base/scoped_ptr.h"
+#include "base/task.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/proxy/plugin_dispatcher.h"
+#include "ppapi/proxy/plugin_resource.h"
+#include "ppapi/proxy/plugin_resource_tracker.h"
+#include "ppapi/proxy/ppapi_messages.h"
+#include "ppapi/thunk/ppb_flash_tcp_socket_api.h"
+#include "ppapi/thunk/thunk.h"
+
+using ppapi::thunk::PPB_Flash_TCPSocket_API;
+
+namespace pp {
+namespace proxy {
+
+const int32_t kFlashTCPSocketMaxReadSize = 1024 * 1024;
+const int32_t kFlashTCPSocketMaxWriteSize = 1024 * 1024;
+
+class FlashTCPSocket;
+
+namespace {
+
+typedef std::map<uint32, FlashTCPSocket*> IDToSocketMap;
+IDToSocketMap* g_id_to_socket = NULL;
+
+class AbortCallbackTask : public Task {
+ public:
+ explicit AbortCallbackTask(PP_CompletionCallback callback)
+ : callback_(callback) {}
+ virtual ~AbortCallbackTask() {}
+ virtual void Run() {
+ if (callback_.func)
+ PP_RunCompletionCallback(&callback_, PP_ERROR_ABORTED);
+ }
+
+ private:
+ PP_CompletionCallback callback_;
+};
+
+InterfaceProxy* CreateFlashTCPSocketProxy(Dispatcher* dispatcher,
+ const void* target_interface) {
+ return new PPB_Flash_TCPSocket_Proxy(dispatcher, target_interface);
+}
+
+} // namespace
+
+class FlashTCPSocket : public PPB_Flash_TCPSocket_API,
+ public PluginResource {
+ public:
+ FlashTCPSocket(const HostResource& resource, uint32 socket_id);
+ virtual ~FlashTCPSocket();
+
+ // ResourceObjectBase overrides.
+ virtual PPB_Flash_TCPSocket_API* AsPPB_Flash_TCPSocket_API() OVERRIDE;
+
+ // PPB_Flash_TCPSocket_API implementation.
+ virtual int32_t Connect(const char* host,
+ uint16_t port,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual int32_t ConnectWithNetAddress(
+ const PP_Flash_NetAddress* addr,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual PP_Bool GetLocalAddress(PP_Flash_NetAddress* local_addr) OVERRIDE;
+ virtual PP_Bool GetRemoteAddress(PP_Flash_NetAddress* remote_addr) OVERRIDE;
+ virtual int32_t InitiateSSL(const char* server_name,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual int32_t Read(char* buffer,
+ int32_t bytes_to_read,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual int32_t Write(const char* buffer,
+ int32_t bytes_to_write,
+ PP_CompletionCallback callback) OVERRIDE;
+ virtual void Disconnect() OVERRIDE;
+
+ // Notifications from the proxy.
+ void OnConnectCompleted(bool succeeded,
+ const PP_Flash_NetAddress& local_addr,
+ const PP_Flash_NetAddress& remote_addr);
+ void OnReadCompleted(bool succeeded, const std::string& data);
+ void OnWriteCompleted(bool succeeded, int32_t bytes_written);
+
+ private:
+ enum ConnectionState {
+ // Before a connection is successfully established (including a connect
+ // request is pending or a previous connect request failed).
+ BEFORE_CONNECT,
+ CONNECTED,
+ DISCONNECTED
+ };
+
+ // Backend for both Connect() and ConnectWithNetAddress(). To keep things
+ // generic, the message is passed in (on error, it's deleted).
+ int32_t ConnectWithMessage(IPC::Message* msg,
+ PP_CompletionCallback callback);
+
+ void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback);
+
+ uint32 socket_id_;
+ ConnectionState connection_state_;
+
+ PP_CompletionCallback connect_callback_;
+ PP_CompletionCallback read_callback_;
+ PP_CompletionCallback write_callback_;
+
+ char* read_buffer_;
+ int32_t bytes_to_read_;
+
+ PP_Flash_NetAddress local_addr_;
+ PP_Flash_NetAddress remote_addr_;
+
+ DISALLOW_COPY_AND_ASSIGN(FlashTCPSocket);
+};
+
+FlashTCPSocket::FlashTCPSocket(const HostResource& resource, uint32 socket_id)
+ : PluginResource(resource),
+ socket_id_(socket_id),
+ connection_state_(BEFORE_CONNECT),
+ connect_callback_(PP_BlockUntilComplete()),
+ read_callback_(PP_BlockUntilComplete()),
+ write_callback_(PP_BlockUntilComplete()),
+ read_buffer_(NULL),
+ bytes_to_read_(-1) {
+ DCHECK(socket_id != 0);
+
+ local_addr_.size = 0;
+ memset(local_addr_.data, 0, sizeof(local_addr_.data));
+ remote_addr_.size = 0;
+ memset(remote_addr_.data, 0, sizeof(remote_addr_.data));
+
+ if (!g_id_to_socket)
+ g_id_to_socket = new IDToSocketMap();
+ DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
+ (*g_id_to_socket)[socket_id] = this;
+}
+
+FlashTCPSocket::~FlashTCPSocket() {
+ Disconnect();
+}
+
+PPB_Flash_TCPSocket_API* FlashTCPSocket::AsPPB_Flash_TCPSocket_API() {
+ return this;
+}
+
+int32_t FlashTCPSocket::Connect(const char* host,
+ uint16_t port,
+ PP_CompletionCallback callback) {
+ if (!host)
+ return PP_ERROR_BADARGUMENT;
+
+ return ConnectWithMessage(
+ new PpapiHostMsg_PPBFlashTCPSocket_Connect(socket_id_, host, port),
+ callback);
+}
+
+int32_t FlashTCPSocket::ConnectWithNetAddress(
+ const PP_Flash_NetAddress* addr,
+ PP_CompletionCallback callback) {
+ if (!addr)
+ return PP_ERROR_BADARGUMENT;
+
+ return ConnectWithMessage(
+ new PpapiHostMsg_PPBFlashTCPSocket_ConnectWithNetAddress(
+ socket_id_, *addr),
+ callback);
+}
+
+PP_Bool FlashTCPSocket::GetLocalAddress(PP_Flash_NetAddress* local_addr) {
+ if (connection_state_ != CONNECTED || !local_addr)
+ return PP_FALSE;
+
+ *local_addr = local_addr_;
+ return PP_TRUE;
+}
+
+PP_Bool FlashTCPSocket::GetRemoteAddress(PP_Flash_NetAddress* remote_addr) {
+ if (connection_state_ != CONNECTED || !remote_addr)
+ return PP_FALSE;
+
+ *remote_addr = remote_addr_;
+ return PP_TRUE;
+}
+
+int32_t FlashTCPSocket::InitiateSSL(const char* server_name,
+ PP_CompletionCallback callback) {
+ // TODO(yzshen): add it.
+ return PP_ERROR_FAILED;
+}
+
+int32_t FlashTCPSocket::Read(char* buffer,
+ int32_t bytes_to_read,
+ PP_CompletionCallback callback) {
+ if (!buffer || bytes_to_read <= 0 || !callback.func)
+ return PP_ERROR_BADARGUMENT;
+
+ if (connection_state_ != CONNECTED)
+ return PP_ERROR_FAILED;
+ if (read_callback_.func)
+ return PP_ERROR_INPROGRESS;
+
+ read_buffer_ = buffer;
+ bytes_to_read_ = std::min(bytes_to_read, kFlashTCPSocketMaxReadSize);
+ read_callback_ = callback;
+
+ // Send the request, the browser will call us back via ReadACK.
+ GetDispatcher()->SendToBrowser(
+ new PpapiHostMsg_PPBFlashTCPSocket_Read(socket_id_, bytes_to_read_));
+ return PP_OK_COMPLETIONPENDING;
+}
+
+int32_t FlashTCPSocket::Write(const char* buffer,
+ int32_t bytes_to_write,
+ PP_CompletionCallback callback) {
+ if (!buffer || bytes_to_write <= 0 || !callback.func)
+ return PP_ERROR_BADARGUMENT;
+
+ if (connection_state_ != CONNECTED)
+ return PP_ERROR_FAILED;
+ if (write_callback_.func)
+ return PP_ERROR_INPROGRESS;
+
+ if (bytes_to_write > kFlashTCPSocketMaxWriteSize)
+ bytes_to_write = kFlashTCPSocketMaxWriteSize;
+
+ write_callback_ = callback;
+
+ // Send the request, the browser will call us back via WriteACK.
+ GetDispatcher()->SendToBrowser(
+ new PpapiHostMsg_PPBFlashTCPSocket_Write(
+ socket_id_, std::string(buffer, bytes_to_write)));
+ return PP_OK_COMPLETIONPENDING;
+}
+
+void FlashTCPSocket::Disconnect() {
+ if (connection_state_ == DISCONNECTED)
+ return;
+
+ connection_state_ = DISCONNECTED;
+ // After removed from the mapping, this object won't receive any notfications
+ // from the proxy.
+ DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
+ g_id_to_socket->erase(socket_id_);
+
+ GetDispatcher()->SendToBrowser(
+ new PpapiHostMsg_PPBFlashTCPSocket_Disconnect(socket_id_));
+ socket_id_ = 0;
+
+ PostAbortAndClearIfNecessary(&connect_callback_);
+ PostAbortAndClearIfNecessary(&read_callback_);
+ PostAbortAndClearIfNecessary(&write_callback_);
+ read_buffer_ = NULL;
+ bytes_to_read_ = -1;
+}
+
+void FlashTCPSocket::OnConnectCompleted(
+ bool succeeded,
+ const PP_Flash_NetAddress& local_addr,
+ const PP_Flash_NetAddress& remote_addr) {
+ if (connection_state_ != BEFORE_CONNECT || !connect_callback_.func) {
+ NOTREACHED();
+ return;
+ }
+
+ if (succeeded) {
+ local_addr_ = local_addr;
+ remote_addr_ = remote_addr;
+ connection_state_ = CONNECTED;
+ }
+ PP_RunAndClearCompletionCallback(&connect_callback_,
+ succeeded ? PP_OK : PP_ERROR_FAILED);
+}
+
+void FlashTCPSocket::OnReadCompleted(bool succeeded, const std::string& data) {
+ if (!read_callback_.func || !read_buffer_) {
+ NOTREACHED();
+ return;
+ }
+
+ if (succeeded) {
+ CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
+ if (!data.empty())
+ memcpy(read_buffer_, data.c_str(), data.size());
+ }
+ read_buffer_ = NULL;
+ bytes_to_read_ = -1;
+
+ PP_RunAndClearCompletionCallback(
+ &read_callback_,
+ succeeded ? data.size() : static_cast<int32_t>(PP_ERROR_FAILED));
+}
+
+void FlashTCPSocket::OnWriteCompleted(bool succeeded, int32_t bytes_written) {
+ if (!write_callback_.func || (succeeded && bytes_written < 0)) {
+ NOTREACHED();
+ return;
+ }
+
+ PP_RunAndClearCompletionCallback(
+ &write_callback_,
+ succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
+}
+
+int32_t FlashTCPSocket::ConnectWithMessage(IPC::Message* msg,
+ PP_CompletionCallback callback) {
+ scoped_ptr<IPC::Message> msg_deletor(msg);
+ if (!callback.func)
+ return PP_ERROR_BADARGUMENT;
+ if (connection_state_ != BEFORE_CONNECT)
+ return PP_ERROR_FAILED;
+ if (connect_callback_.func)
+ return PP_ERROR_INPROGRESS; // Can only have one pending request.
+
+ connect_callback_ = callback;
+ // Send the request, the browser will call us back via ConnectACK.
+ GetDispatcher()->SendToBrowser(msg_deletor.release());
+ return PP_OK_COMPLETIONPENDING;
+}
+
+void FlashTCPSocket::PostAbortAndClearIfNecessary(
+ PP_CompletionCallback* callback) {
+ DCHECK(callback);
+
+ if (callback->func) {
+ MessageLoop::current()->PostTask(FROM_HERE,
+ new AbortCallbackTask(*callback));
+ *callback = PP_BlockUntilComplete();
+ }
+}
+
+PPB_Flash_TCPSocket_Proxy::PPB_Flash_TCPSocket_Proxy(
+ Dispatcher* dispatcher,
+ const void* target_interface)
+ : InterfaceProxy(dispatcher, target_interface) {
+}
+
+PPB_Flash_TCPSocket_Proxy::~PPB_Flash_TCPSocket_Proxy() {
+}
+
+// static
+const InterfaceProxy::Info* PPB_Flash_TCPSocket_Proxy::GetInfo() {
+ static const Info info = {
+ ::ppapi::thunk::GetPPB_Flash_TCPSocket_Thunk(),
+ PPB_FLASH_TCPSOCKET_INTERFACE,
+ INTERFACE_ID_PPB_FLASH_TCPSOCKET,
+ false,
+ &CreateFlashTCPSocketProxy,
+ };
+ return &info;
+}
+
+// static
+PP_Resource PPB_Flash_TCPSocket_Proxy::CreateProxyResource(
+ PP_Instance instance) {
+ PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
+ if (!dispatcher)
+ return 0;
+
+ uint32 socket_id = 0;
+ dispatcher->SendToBrowser(new PpapiHostMsg_PPBFlashTCPSocket_Create(
+ INTERFACE_ID_PPB_FLASH_TCPSOCKET, dispatcher->plugin_dispatcher_id(),
+ &socket_id));
+ if (socket_id == 0)
+ return 0;
+
+ linked_ptr<FlashTCPSocket> object(new FlashTCPSocket(
+ HostResource::MakeInstanceOnly(instance), socket_id));
+ return PluginResourceTracker::GetInstance()->AddResource(object);
+}
+
+bool PPB_Flash_TCPSocket_Proxy::OnMessageReceived(const IPC::Message& msg) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(PPB_Flash_TCPSocket_Proxy, msg)
+ IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashTCPSocket_ConnectACK, OnMsgConnectACK)
+ IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashTCPSocket_ReadACK, OnMsgReadACK)
+ IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashTCPSocket_WriteACK, OnMsgWriteACK)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+void PPB_Flash_TCPSocket_Proxy::OnMsgConnectACK(
+ uint32 /* plugin_dispatcher_id */,
+ uint32 socket_id,
+ bool succeeded,
+ const PP_Flash_NetAddress& local_addr,
+ const PP_Flash_NetAddress& remote_addr) {
+ if (!g_id_to_socket) {
+ NOTREACHED();
+ return;
+ }
+ IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
+ if (iter == g_id_to_socket->end())
+ return;
+ iter->second->OnConnectCompleted(succeeded, local_addr, remote_addr);
+}
+
+void PPB_Flash_TCPSocket_Proxy::OnMsgReadACK(uint32 /* plugin_dispatcher_id */,
+ uint32 socket_id,
+ bool succeeded,
+ const std::string& data) {
+ if (!g_id_to_socket) {
+ NOTREACHED();
+ return;
+ }
+ IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
+ if (iter == g_id_to_socket->end())
+ return;
+ iter->second->OnReadCompleted(succeeded, data);
+}
+
+void PPB_Flash_TCPSocket_Proxy::OnMsgWriteACK(uint32 /* plugin_dispatcher_id */,
+ uint32 socket_id,
+ bool succeeded,
+ int32_t bytes_written) {
+ if (!g_id_to_socket) {
+ NOTREACHED();
+ return;
+ }
+ IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
+ if (iter == g_id_to_socket->end())
+ return;
+ iter->second->OnWriteCompleted(succeeded, bytes_written);
+}
+
+} // namespace proxy
+} // namespace pp
diff --git a/ppapi/proxy/ppb_flash_tcp_socket_proxy.h b/ppapi/proxy/ppb_flash_tcp_socket_proxy.h
new file mode 100644
index 0000000..aac9bbd
--- /dev/null
+++ b/ppapi/proxy/ppb_flash_tcp_socket_proxy.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2011 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_PROXY_PPB_FLASH_TCP_SOCKET_PROXY_H_
+#define PPAPI_PROXY_PPB_FLASH_TCP_SOCKET_PROXY_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "ppapi/c/pp_instance.h"
+#include "ppapi/c/pp_resource.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
+#include "ppapi/proxy/interface_proxy.h"
+
+namespace pp {
+namespace proxy {
+
+// The maximum number of bytes that each PpapiHostMsg_PPBFlashTCPSocket_Read
+// message is allowed to request.
+extern const int32_t kFlashTCPSocketMaxReadSize;
+// The maximum number of bytes that each PpapiHostMsg_PPBFlashTCPSocket_Write
+// message is allowed to carry.
+extern const int32_t kFlashTCPSocketMaxWriteSize;
+
+class PPB_Flash_TCPSocket_Proxy : public InterfaceProxy {
+ public:
+ PPB_Flash_TCPSocket_Proxy(Dispatcher* dispatcher,
+ const void* target_interface);
+ virtual ~PPB_Flash_TCPSocket_Proxy();
+
+ static const Info* GetInfo();
+
+ static PP_Resource CreateProxyResource(PP_Instance instance);
+
+ // InterfaceProxy implementation.
+ virtual bool OnMessageReceived(const IPC::Message& msg);
+
+ private:
+ // Browser->plugin message handlers.
+ void OnMsgConnectACK(uint32 plugin_dispatcher_id,
+ uint32 socket_id,
+ bool succeeded,
+ const PP_Flash_NetAddress& local_addr,
+ const PP_Flash_NetAddress& remote_addr);
+ void OnMsgReadACK(uint32 plugin_dispatcher_id,
+ uint32 socket_id,
+ bool succeeded,
+ const std::string& data);
+ void OnMsgWriteACK(uint32 plugin_dispatcher_id,
+ uint32 socket_id,
+ bool succeeded,
+ int32_t bytes_written);
+
+ DISALLOW_COPY_AND_ASSIGN(PPB_Flash_TCPSocket_Proxy);
+};
+
+} // namespace proxy
+} // namespace pp
+
+#endif // PPAPI_PROXY_PPB_FLASH_TCP_SOCKET_PROXY_H_
diff --git a/ppapi/proxy/resource_creation_proxy.cc b/ppapi/proxy/resource_creation_proxy.cc
index 095efb0..07d6248 100644
--- a/ppapi/proxy/resource_creation_proxy.cc
+++ b/ppapi/proxy/resource_creation_proxy.cc
@@ -22,6 +22,7 @@
#include "ppapi/proxy/ppb_file_system_proxy.h"
#include "ppapi/proxy/ppb_flash_menu_proxy.h"
#include "ppapi/proxy/ppb_flash_net_connector_proxy.h"
+#include "ppapi/proxy/ppb_flash_tcp_socket_proxy.h"
#include "ppapi/proxy/ppb_font_proxy.h"
#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
#include "ppapi/proxy/ppb_image_data_proxy.h"
@@ -138,6 +139,11 @@ PP_Resource ResourceCreationProxy::CreateFlashNetConnector(
return PPB_Flash_NetConnector_Proxy::CreateProxyResource(instance);
}
+PP_Resource ResourceCreationProxy::CreateFlashTCPSocket(
+ PP_Instance instance) {
+ return PPB_Flash_TCPSocket_Proxy::CreateProxyResource(instance);
+}
+
PP_Resource ResourceCreationProxy::CreateFontObject(
PP_Instance instance,
const PP_FontDescription_Dev* description) {
diff --git a/ppapi/proxy/resource_creation_proxy.h b/ppapi/proxy/resource_creation_proxy.h
index 107ac3c..218607b 100644
--- a/ppapi/proxy/resource_creation_proxy.h
+++ b/ppapi/proxy/resource_creation_proxy.h
@@ -63,6 +63,7 @@ class ResourceCreationProxy : public ::ppapi::FunctionGroupBase,
virtual PP_Resource CreateFlashMenu(PP_Instance instance,
const PP_Flash_Menu* menu_data) OVERRIDE;
virtual PP_Resource CreateFlashNetConnector(PP_Instance instance) OVERRIDE;
+ virtual PP_Resource CreateFlashTCPSocket(PP_Instance instance) OVERRIDE;
virtual PP_Resource CreateFontObject(
PP_Instance instance,
const PP_FontDescription_Dev* description) OVERRIDE;
diff --git a/ppapi/shared_impl/resource_object_base.h b/ppapi/shared_impl/resource_object_base.h
index d61e55b..77f6dd16 100644
--- a/ppapi/shared_impl/resource_object_base.h
+++ b/ppapi/shared_impl/resource_object_base.h
@@ -23,6 +23,7 @@
F(PPB_Find_API) \
F(PPB_Flash_Menu_API) \
F(PPB_Flash_NetConnector_API) \
+ F(PPB_Flash_TCPSocket_API) \
F(PPB_Font_API) \
F(PPB_Graphics2D_API) \
F(PPB_Graphics3D_API) \
diff --git a/ppapi/tests/all_c_includes.h b/ppapi/tests/all_c_includes.h
index 7dd0960..d4f510d 100644
--- a/ppapi/tests/all_c_includes.h
+++ b/ppapi/tests/all_c_includes.h
@@ -82,6 +82,7 @@
#include "ppapi/c/ppp_messaging.h"
#include "ppapi/c/private/ppb_flash.h"
#include "ppapi/c/private/ppb_flash_menu.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#include "ppapi/c/private/ppb_instance_private.h"
#include "ppapi/c/private/ppb_nacl_private.h"
#include "ppapi/c/private/ppb_pdf.h"
diff --git a/ppapi/thunk/ppb_flash_tcp_socket_api.h b/ppapi/thunk/ppb_flash_tcp_socket_api.h
new file mode 100644
index 0000000..d18eb6e
--- /dev/null
+++ b/ppapi/thunk/ppb_flash_tcp_socket_api.h
@@ -0,0 +1,38 @@
+// Copyright (c) 2011 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_THUNK_PPB_FLASH_TCP_SOCKET_API_H_
+#define PPAPI_THUNK_PPB_FLASH_TCP_SOCKET_API_H_
+
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
+
+namespace ppapi {
+namespace thunk {
+
+class PPB_Flash_TCPSocket_API {
+ public:
+ virtual ~PPB_Flash_TCPSocket_API() {}
+
+ virtual int32_t Connect(const char* host,
+ uint16_t port,
+ PP_CompletionCallback callback) = 0;
+ virtual int32_t ConnectWithNetAddress(const PP_Flash_NetAddress* addr,
+ PP_CompletionCallback callback) = 0;
+ virtual PP_Bool GetLocalAddress(PP_Flash_NetAddress* local_addr) = 0;
+ virtual PP_Bool GetRemoteAddress(PP_Flash_NetAddress* remote_addr) = 0;
+ virtual int32_t InitiateSSL(const char* server_name,
+ PP_CompletionCallback callback) = 0;
+ virtual int32_t Read(char* buffer,
+ int32_t bytes_to_read,
+ PP_CompletionCallback callback) = 0;
+ virtual int32_t Write(const char* buffer,
+ int32_t bytes_to_write,
+ PP_CompletionCallback callback) = 0;
+ virtual void Disconnect() = 0;
+};
+
+} // namespace thunk
+} // namespace ppapi
+
+#endif // PPAPI_THUNK_PPB_FLASH_TCP_SOCKET_API_H_
diff --git a/ppapi/thunk/ppb_flash_tcp_socket_thunk.cc b/ppapi/thunk/ppb_flash_tcp_socket_thunk.cc
new file mode 100644
index 0000000..815dd53
--- /dev/null
+++ b/ppapi/thunk/ppb_flash_tcp_socket_thunk.cc
@@ -0,0 +1,121 @@
+// Copyright (c) 2011 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/c/pp_completion_callback.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
+#include "ppapi/thunk/thunk.h"
+#include "ppapi/thunk/enter.h"
+#include "ppapi/thunk/ppb_flash_tcp_socket_api.h"
+#include "ppapi/thunk/resource_creation_api.h"
+
+namespace ppapi {
+namespace thunk {
+
+namespace {
+
+PP_Resource Create(PP_Instance instance) {
+ EnterFunction<ResourceCreationAPI> enter(instance, true);
+ if (enter.failed())
+ return 0;
+ return enter.functions()->CreateFlashTCPSocket(instance);
+}
+
+PP_Bool IsFlashTCPSocket(PP_Resource resource) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(resource, false);
+ return PP_FromBool(enter.succeeded());
+}
+
+int32_t Connect(PP_Resource tcp_socket,
+ const char* host,
+ uint16_t port,
+ PP_CompletionCallback callback) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_ERROR_BADRESOURCE;
+ return enter.object()->Connect(host, port, callback);
+}
+
+int32_t ConnectWithNetAddress(PP_Resource tcp_socket,
+ const PP_Flash_NetAddress* addr,
+ PP_CompletionCallback callback) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_ERROR_BADRESOURCE;
+ return enter.object()->ConnectWithNetAddress(addr, callback);
+}
+
+PP_Bool GetLocalAddress(PP_Resource tcp_socket,
+ PP_Flash_NetAddress* local_addr) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_FALSE;
+ return enter.object()->GetLocalAddress(local_addr);
+}
+
+PP_Bool GetRemoteAddress(PP_Resource tcp_socket,
+ PP_Flash_NetAddress* remote_addr) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_FALSE;
+ return enter.object()->GetRemoteAddress(remote_addr);
+}
+
+int32_t InitiateSSL(PP_Resource tcp_socket,
+ const char* server_name,
+ PP_CompletionCallback callback) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_ERROR_BADRESOURCE;
+ return enter.object()->InitiateSSL(server_name, callback);
+}
+
+int32_t Read(PP_Resource tcp_socket,
+ char* buffer,
+ int32_t bytes_to_read,
+ PP_CompletionCallback callback) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_ERROR_BADRESOURCE;
+ return enter.object()->Read(buffer, bytes_to_read, callback);
+}
+
+int32_t Write(PP_Resource tcp_socket,
+ const char* buffer,
+ int32_t bytes_to_write,
+ PP_CompletionCallback callback) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.failed())
+ return PP_ERROR_BADRESOURCE;
+ return enter.object()->Write(buffer, bytes_to_write, callback);
+}
+
+void Disconnect(PP_Resource tcp_socket) {
+ EnterResource<PPB_Flash_TCPSocket_API> enter(tcp_socket, true);
+ if (enter.succeeded())
+ enter.object()->Disconnect();
+}
+
+const PPB_Flash_TCPSocket g_ppb_flash_tcp_socket_thunk = {
+ &Create,
+ &IsFlashTCPSocket,
+ &Connect,
+ &ConnectWithNetAddress,
+ &GetLocalAddress,
+ &GetRemoteAddress,
+ &InitiateSSL,
+ &Read,
+ &Write,
+ &Disconnect
+};
+
+} // namespace
+
+const PPB_Flash_TCPSocket* GetPPB_Flash_TCPSocket_Thunk() {
+ return &g_ppb_flash_tcp_socket_thunk;
+}
+
+} // namespace thunk
+} // namespace ppapi
+
diff --git a/ppapi/thunk/resource_creation_api.h b/ppapi/thunk/resource_creation_api.h
index c998b56..e2e5ba1 100644
--- a/ppapi/thunk/resource_creation_api.h
+++ b/ppapi/thunk/resource_creation_api.h
@@ -63,6 +63,7 @@ class ResourceCreationAPI {
virtual PP_Resource CreateFlashMenu(PP_Instance instance,
const PP_Flash_Menu* menu_data) = 0;
virtual PP_Resource CreateFlashNetConnector(PP_Instance instance) = 0;
+ virtual PP_Resource CreateFlashTCPSocket(PP_Instance instace) = 0;
// Note: can't be called CreateFont due to Windows #defines.
virtual PP_Resource CreateFontObject(
PP_Instance instance,
diff --git a/ppapi/thunk/thunk.h b/ppapi/thunk/thunk.h
index 645c5c6..9fb84b3 100644
--- a/ppapi/thunk/thunk.h
+++ b/ppapi/thunk/thunk.h
@@ -24,6 +24,7 @@ struct PPB_FileSystem_Dev;
struct PPB_Find_Dev;
struct PPB_Flash_Menu;
struct PPB_Flash_NetConnector;
+struct PPB_Flash_TCPSocket;
struct PPB_Font_Dev;
struct PPB_Fullscreen_Dev;
struct PPB_GLESChromiumTextureMapping_Dev;
@@ -75,6 +76,7 @@ const PPB_FileSystem_Dev* GetPPB_FileSystem_Thunk();
const PPB_Find_Dev* GetPPB_Find_Thunk();
const PPB_Flash_Menu* GetPPB_Flash_Menu_Thunk();
const PPB_Flash_NetConnector* GetPPB_Flash_NetConnector_Thunk();
+const PPB_Flash_TCPSocket* GetPPB_Flash_TCPSocket_Thunk();
const PPB_Font_Dev* GetPPB_Font_Thunk();
const PPB_Fullscreen_Dev* GetPPB_Fullscreen_Thunk();
const PPB_GLESChromiumTextureMapping_Dev*
diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc
index 340d0e44..bd7bf11 100644
--- a/webkit/plugins/ppapi/plugin_module.cc
+++ b/webkit/plugins/ppapi/plugin_module.cc
@@ -61,6 +61,7 @@
#include "ppapi/c/private/ppb_flash.h"
#include "ppapi/c/private/ppb_flash_clipboard.h"
#include "ppapi/c/private/ppb_flash_file.h"
+#include "ppapi/c/private/ppb_flash_tcp_socket.h"
#include "ppapi/c/private/ppb_instance_private.h"
#include "ppapi/c/private/ppb_pdf.h"
#include "ppapi/c/private/ppb_proxy_private.h"
@@ -273,6 +274,8 @@ const void* GetInterface(const char* name) {
return PPB_Flash_File_ModuleLocal_Impl::GetInterface();
if (strcmp(name, PPB_FLASH_MENU_INTERFACE) == 0)
return ::ppapi::thunk::GetPPB_Flash_Menu_Thunk();
+ if (strcmp(name, PPB_FLASH_TCPSOCKET_INTERFACE) == 0)
+ return ::ppapi::thunk::GetPPB_Flash_TCPSocket_Thunk();
if (strcmp(name, PPB_FONT_DEV_INTERFACE) == 0)
return ::ppapi::thunk::GetPPB_Font_Thunk();
if (strcmp(name, PPB_FULLSCREEN_DEV_INTERFACE) == 0)
diff --git a/webkit/plugins/ppapi/resource_creation_impl.cc b/webkit/plugins/ppapi/resource_creation_impl.cc
index 9dc2800..ef2830f 100644
--- a/webkit/plugins/ppapi/resource_creation_impl.cc
+++ b/webkit/plugins/ppapi/resource_creation_impl.cc
@@ -154,6 +154,12 @@ PP_Resource ResourceCreationImpl::CreateFlashNetConnector(
return ReturnResource(new PPB_Flash_NetConnector_Impl(instance_));
}
+PP_Resource ResourceCreationImpl::CreateFlashTCPSocket(
+ PP_Instance instance) {
+ // Creating TCP socket resource at the renderer side is not supported.
+ return 0;
+}
+
PP_Resource ResourceCreationImpl::CreateFontObject(
PP_Instance pp_instance,
const PP_FontDescription_Dev* description) {
diff --git a/webkit/plugins/ppapi/resource_creation_impl.h b/webkit/plugins/ppapi/resource_creation_impl.h
index e970d57..b20df64 100644
--- a/webkit/plugins/ppapi/resource_creation_impl.h
+++ b/webkit/plugins/ppapi/resource_creation_impl.h
@@ -56,6 +56,7 @@ class ResourceCreationImpl : public ::ppapi::FunctionGroupBase,
virtual PP_Resource CreateFlashMenu(PP_Instance instance,
const PP_Flash_Menu* menu_data) OVERRIDE;
virtual PP_Resource CreateFlashNetConnector(PP_Instance instance) OVERRIDE;
+ virtual PP_Resource CreateFlashTCPSocket(PP_Instance instance) OVERRIDE;
virtual PP_Resource CreateFontObject(
PP_Instance instance,
const PP_FontDescription_Dev* description) OVERRIDE;