Index: webkit/glue/plugins/pepper_transport.cc |
=================================================================== |
--- webkit/glue/plugins/pepper_transport.cc (revision 68979) |
+++ webkit/glue/plugins/pepper_transport.cc (working copy) |
@@ -4,104 +4,76 @@ |
#include "webkit/glue/plugins/pepper_transport.h" |
-#include "base/singleton.h" |
-#include "base/thread_local.h" |
-#include "ppapi/c/dev/ppb_transport_dev.h" |
+#include "ppapi/c/pp_completion_callback.h" |
+#include "ppapi/c/pp_errors.h" |
+#include "third_party/libjingle/source/talk/p2p/base/p2ptransportchannel.h" |
+#include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h" |
+#include "third_party/ppapi/c/dev/ppb_transport_dev.h" |
#include "webkit/glue/plugins/pepper_common.h" |
-#include "webkit/glue/plugins/pepper_plugin_instance.h" |
#include "webkit/glue/plugins/pepper_plugin_module.h" |
+#include "webkit/glue/plugins/pepper_var.h" |
namespace pepper { |
namespace { |
-// Creates a new transport object with the specified name |
-// using the specified protocol. |
-PP_Resource CreateTransport(PP_Module module, |
- const char* name, |
+PP_Resource CreateTransport(PP_Module module_id, const char* name, |
const char* proto) { |
- // TODO(juberti): implement me |
- PP_Resource p(0); |
- return p; |
+ PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
+ if (!module) |
+ return 0; |
+ |
+ scoped_refptr<Transport> t(new Transport(module)); |
+ if (!t->Init(name, proto)) { |
+ return 0; |
+ } |
+ |
+ return t->GetReference(); |
} |
-// Returns whether or not resource is Transport |
PP_Bool IsTransport(PP_Resource resource) { |
- return BoolToPPBool(!!Resource::GetAs<Transport>(resource)); |
+ return BoolToPPBool(Resource::GetAs<Transport>(resource) != NULL); |
} |
-// Returns whether the transport is currently writable |
-// (i.e. can send data to the remote peer) |
-PP_Bool IsWritable(PP_Resource transport) { |
- // TODO(juberti): impelement me |
- return PP_FALSE; |
+PP_Bool IsWritable(PP_Resource resource) { |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return BoolToPPBool((t.get()) ? t->IsWritable() : false); |
} |
- |
-// TODO(juberti): other getters/setters |
-// connect state |
-// connect type, protocol |
-// RTT |
- |
- |
-// Establishes a connection to the remote peer. |
-// Returns PP_ERROR_WOULDBLOCK and notifies on |cb| |
-// when connectivity is established (or timeout occurs). |
-int32_t Connect(PP_Resource transport, |
- PP_CompletionCallback cb) { |
- // TODO(juberti): impelement me |
- return 0; |
+int32_t Connect(PP_Resource resource, PP_CompletionCallback cb) { |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get()) ? t->Connect(cb) : PP_ERROR_BADRESOURCE; |
} |
- |
-// Obtains another ICE candidate address to be provided |
-// to the remote peer. Returns PP_ERROR_WOULDBLOCK |
-// if there are no more addresses to be sent. |
-int32_t GetNextAddress(PP_Resource transport, |
- PP_Var* address, |
+int32_t GetNextAddress(PP_Resource resource, PP_Var* address, |
PP_CompletionCallback cb) { |
- // TODO(juberti): implement me |
- return 0; |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get())? t->GetNextAddress(address, cb) : PP_ERROR_BADRESOURCE; |
} |
- |
-// Provides an ICE candidate address that was received |
-// from the remote peer. |
-int32_t ReceiveRemoteAddress(PP_Resource transport, |
- PP_Var address) { |
- // TODO(juberti): implement me |
- return 0; |
+int32_t ReceiveRemoteAddress(PP_Resource resource, PP_Var address) { |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get())? t->ReceiveRemoteAddress(address) : PP_ERROR_BADRESOURCE; |
} |
- |
-// Like recv(), receives data. Returns PP_ERROR_WOULDBLOCK |
-// if there is currently no data to receive. |
-int32_t Recv(PP_Resource transport, |
- void* data, |
- uint32_t len, |
+int32_t Recv(PP_Resource resource, void* data, uint32_t len, |
PP_CompletionCallback cb) { |
- // TODO(juberti): implement me |
- return 0; |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get())? t->Recv(data, len, cb) : PP_ERROR_BADRESOURCE; |
} |
- |
-// Like send(), sends data. Returns PP_ERROR_WOULDBLOCK |
-// if the socket is currently flow-controlled. |
-int32_t Send(PP_Resource transport, |
- const void* data, |
- uint32_t len, |
- PP_CompletionCallback cb) { |
- // TODO(juberti): implement me |
- return 0; |
+int32_t Send(PP_Resource resource, const void* data, |
+ uint32_t len, PP_CompletionCallback cb) { |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get())? t->Send(data, len, cb) : PP_ERROR_BADRESOURCE; |
} |
- |
-// Disconnects from the remote peer. |
-int32_t Close(PP_Resource transport) { |
- // TODO(juberti): implement me |
- return 0; |
+int32_t Close(PP_Resource resource) { |
+ scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
+ return (t.get())? t->Close() : PP_ERROR_BADRESOURCE; |
} |
+} // namespace |
const PPB_Transport_Dev ppb_transport = { |
&CreateTransport, |
@@ -112,29 +84,154 @@ |
&ReceiveRemoteAddress, |
&Recv, |
&Send, |
- &Close, |
+ &Close |
}; |
-} // namespace |
+const PPB_Transport_Dev* Transport::GetInterface() { |
+ return &ppb_transport; |
+} |
Transport::Transport(PluginModule* module) |
- : Resource(module) { |
- // TODO(juberti): impl |
+ : Resource(module), |
+ network_manager_(new talk_base::NetworkManager()), |
+ allocator_(new cricket::HttpPortAllocator(network_manager_.get(), "")) { |
+ std::vector<talk_base::SocketAddress> stun_hosts; |
+ stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); |
+ allocator_->SetStunHosts(stun_hosts); |
+ // TODO(juberti): Run timer, then move to use ChromeSocketServer |
} |
-const PPB_Transport_Dev* Transport::GetInterface() { |
- return &ppb_transport; |
+bool Transport::Init(const char* name, const char* protocol) { |
+ // for now, always create http://www.google.com/transport/p2p |
brettw
2010/12/21 21:55:50
Be sure comments are complete sentences with capit
|
+ channel_.reset(new cricket::P2PTransportChannel( |
+ name, "", NULL, allocator_.get())); |
+ channel_->SignalRequestSignaling.connect(this, |
+ &Transport::OnRequestSignaling); |
+ channel_->SignalWritableState.connect(this, |
+ &Transport::OnWriteableState); |
+ channel_->SignalCandidateReady.connect(this, |
+ &Transport::OnCandidateReady); |
+ channel_->SignalReadPacket.connect(this, |
+ &Transport::OnReadPacket); |
+ return true; |
} |
Transport::~Transport() { |
- // TODO(juberti): teardown |
} |
-bool Transport::Init(const char* name, |
- const char* proto) { |
- // TODO(juberti): impl |
+bool Transport::IsWritable() const { |
+ return channel_->writable(); |
+} |
+ |
+int32_t Transport::Connect(PP_CompletionCallback cb) { |
+ // TODO(juberti): fail if we're already connected. |
+ if (connect_cx_.get()) { |
+ return PP_ERROR_INPROGRESS; |
+ } |
+ channel_->Connect(); |
+ connect_cx_.reset(new ConnectContext(cb)); |
+ return PP_ERROR_WOULDBLOCK; |
+} |
+ |
+int32_t Transport::GetNextAddress(PP_Var* address, PP_CompletionCallback cb) { |
+ if (next_address_cx_.get()) { |
+ return PP_ERROR_INPROGRESS; |
+ } |
+ |
+ if (!local_candidates_.empty()) { |
+ Serialize(local_candidates_.front(), address); |
+ local_candidates_.pop_front(); |
+ return PP_OK; |
+ } |
+ |
+ next_address_cx_.reset(new GetNextAddressContext(address, cb)); |
+ return PP_ERROR_WOULDBLOCK; |
+} |
+ |
+int32_t Transport::ReceiveRemoteAddress(PP_Var address) { |
+ cricket::Candidate candidate; |
+ if (!Deserialize(address, &candidate)) { |
+ return PP_ERROR_FAILED; |
+ } |
+ |
+ channel_->OnCandidate(candidate); |
+ return PP_OK; |
+} |
+ |
+int32_t Transport::Recv(void* data, uint32_t len, |
+ PP_CompletionCallback cb) { |
+ if (recv_cx_.get()) { |
+ return PP_ERROR_INPROGRESS; |
+ } |
+ |
+ // TODO(juberti): Should we store packets that are received when |
+ // no callback is installed? |
+ recv_cx_.reset(new RecvContext(data, len, cb)); |
+ return PP_ERROR_WOULDBLOCK; |
+} |
+ |
+int32_t Transport::Send(const void* data, uint32_t len, |
+ PP_CompletionCallback cb) { |
+ return channel_->SendPacket(static_cast<const char*>(data), len); |
+} |
+ |
+int32_t Transport::Close() { |
+ channel_->Reset(); |
+ return PP_OK; |
+} |
+ |
+void Transport::OnRequestSignaling() { |
+ channel_->OnSignalingReady(); |
+} |
+ |
+void Transport::OnCandidateReady(cricket::TransportChannelImpl* channel, |
+ const cricket::Candidate& candidate) { |
+ if (next_address_cx_.get()) { |
+ scoped_ptr<GetNextAddressContext> cx; |
+ cx.swap(next_address_cx_); |
+ Serialize(candidate, cx->address); |
+ cx->Callback(PP_OK); |
+ } else { |
+ local_candidates_.push_back(candidate); |
+ } |
+} |
+ |
+void Transport::OnWriteableState(cricket::TransportChannel* channel) { |
+ if (connect_cx_.get()) { |
+ scoped_ptr<ConnectContext> cx; |
+ cx.swap(connect_cx_); |
+ cx->Callback(PP_OK); |
+ } |
+} |
+ |
+void Transport::OnReadPacket(cricket::TransportChannel* channel, |
+ const char* data, size_t len) { |
+ if (recv_cx_.get()) { |
+ scoped_ptr<RecvContext> cx; |
+ int32_t result; |
+ cx.swap(recv_cx_); |
+ if (len <= cx->len) { |
+ memcpy(cx->data, data, len); |
+ result = PP_OK; |
+ } else { |
+ result = PP_ERROR_FAILED; |
+ } |
+ cx->Callback(result); |
+ } |
+ // Otherwise we eat the packet. Should we try harder? |
+} |
+ |
+bool Transport::Serialize(const cricket::Candidate& candidate, |
+ PP_Var* address) { |
+ // TODO(juberti): come up with a a real wire format! |
nfullagar
2011/01/04 22:07:17
comment above "...with a real..."
|
+ std::string blob = candidate.ToString(); |
+ *address = StringVar::StringToPPVar(module(), blob); |
+ return true; |
+} |
+ |
+bool Transport::Deserialize(PP_Var address, cricket::Candidate* candidate) { |
+ // TODO(juberti): Implement this. |
return false; |
} |
} // namespace pepper |
- |