| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/plugins/ppapi/ppb_transport_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_transport_impl.h" |
| 6 | 6 |
| 7 #include "base/singleton.h" |
| 8 #include "base/threading/thread_local.h" |
| 7 #include "ppapi/c/dev/ppb_transport_dev.h" | 9 #include "ppapi/c/dev/ppb_transport_dev.h" |
| 8 #include "ppapi/c/pp_completion_callback.h" | |
| 9 #include "ppapi/c/pp_errors.h" | |
| 10 #include "third_party/libjingle/source/talk/p2p/base/p2ptransportchannel.h" | |
| 11 #include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h" | |
| 12 #include "webkit/plugins/ppapi/common.h" | 10 #include "webkit/plugins/ppapi/common.h" |
| 13 #include "webkit/plugins/ppapi/plugin_module.h" | 11 #include "webkit/plugins/ppapi/plugin_module.h" |
| 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 12 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 15 #include "webkit/plugins/ppapi/var.h" | |
| 16 | 13 |
| 17 namespace webkit { | 14 namespace webkit { |
| 18 namespace ppapi { | 15 namespace ppapi { |
| 19 | 16 |
| 20 namespace { | 17 namespace { |
| 21 | 18 |
| 22 PP_Resource CreateTransport(PP_Instance instance_id, const char* name, | 19 // Creates a new transport object with the specified name |
| 20 // using the specified protocol. |
| 21 PP_Resource CreateTransport(PP_Instance instance, |
| 22 const char* name, |
| 23 const char* proto) { | 23 const char* proto) { |
| 24 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 24 // TODO(juberti): implement me |
| 25 if (!instance) | 25 PP_Resource p(0); |
| 26 return 0; | 26 return p; |
| 27 | |
| 28 scoped_refptr<PPB_Transport_Impl> t(new PPB_Transport_Impl(instance)); | |
| 29 if (!t->Init(name, proto)) | |
| 30 return 0; | |
| 31 | |
| 32 return t->GetReference(); | |
| 33 } | 27 } |
| 34 | 28 |
| 29 // Returns whether or not resource is PPB_Transport_Impl |
| 35 PP_Bool IsTransport(PP_Resource resource) { | 30 PP_Bool IsTransport(PP_Resource resource) { |
| 36 return BoolToPPBool(Resource::GetAs<PPB_Transport_Impl>(resource) != NULL); | 31 return BoolToPPBool(!!Resource::GetAs<PPB_Transport_Impl>(resource)); |
| 37 } | 32 } |
| 38 | 33 |
| 39 PP_Bool IsWritable(PP_Resource resource) { | 34 // Returns whether the transport is currently writable |
| 40 scoped_refptr<PPB_Transport_Impl> t( | 35 // (i.e. can send data to the remote peer) |
| 41 Resource::GetAs<PPB_Transport_Impl>(resource)); | 36 PP_Bool IsWritable(PP_Resource transport) { |
| 42 return BoolToPPBool((t.get()) ? t->IsWritable() : false); | 37 // TODO(juberti): impelement me |
| 38 return PP_FALSE; |
| 43 } | 39 } |
| 44 | 40 |
| 45 int32_t Connect(PP_Resource resource, PP_CompletionCallback callback) { | 41 |
| 46 scoped_refptr<PPB_Transport_Impl> t( | 42 // TODO(juberti): other getters/setters |
| 47 Resource::GetAs<PPB_Transport_Impl>(resource)); | 43 // connect state |
| 48 return (t.get()) ? t->Connect(callback) : PP_ERROR_BADRESOURCE; | 44 // connect type, protocol |
| 45 // RTT |
| 46 |
| 47 |
| 48 // Establishes a connection to the remote peer. |
| 49 // Returns PP_ERROR_WOULDBLOCK and notifies on |cb| |
| 50 // when connectivity is established (or timeout occurs). |
| 51 int32_t Connect(PP_Resource transport, |
| 52 PP_CompletionCallback cb) { |
| 53 // TODO(juberti): impelement me |
| 54 return 0; |
| 49 } | 55 } |
| 50 | 56 |
| 51 int32_t GetNextAddress(PP_Resource resource, PP_Var* address, | 57 |
| 52 PP_CompletionCallback callback) { | 58 // Obtains another ICE candidate address to be provided |
| 53 scoped_refptr<PPB_Transport_Impl> t( | 59 // to the remote peer. Returns PP_ERROR_WOULDBLOCK |
| 54 Resource::GetAs<PPB_Transport_Impl>(resource)); | 60 // if there are no more addresses to be sent. |
| 55 return (t.get())? t->GetNextAddress(address, callback) : PP_ERROR_BADRESOURCE; | 61 int32_t GetNextAddress(PP_Resource transport, |
| 62 PP_Var* address, |
| 63 PP_CompletionCallback cb) { |
| 64 // TODO(juberti): implement me |
| 65 return 0; |
| 56 } | 66 } |
| 57 | 67 |
| 58 int32_t ReceiveRemoteAddress(PP_Resource resource, PP_Var address) { | 68 |
| 59 scoped_refptr<PPB_Transport_Impl> t( | 69 // Provides an ICE candidate address that was received |
| 60 Resource::GetAs<PPB_Transport_Impl>(resource)); | 70 // from the remote peer. |
| 61 return (t.get())? t->ReceiveRemoteAddress(address) : PP_ERROR_BADRESOURCE; | 71 int32_t ReceiveRemoteAddress(PP_Resource transport, |
| 72 PP_Var address) { |
| 73 // TODO(juberti): implement me |
| 74 return 0; |
| 62 } | 75 } |
| 63 | 76 |
| 64 int32_t Recv(PP_Resource resource, void* data, uint32_t len, | 77 |
| 65 PP_CompletionCallback callback) { | 78 // Like recv(), receives data. Returns PP_ERROR_WOULDBLOCK |
| 66 scoped_refptr<PPB_Transport_Impl> t( | 79 // if there is currently no data to receive. |
| 67 Resource::GetAs<PPB_Transport_Impl>(resource)); | 80 int32_t Recv(PP_Resource transport, |
| 68 return (t.get())? t->Recv(data, len, callback) : PP_ERROR_BADRESOURCE; | 81 void* data, |
| 82 uint32_t len, |
| 83 PP_CompletionCallback cb) { |
| 84 // TODO(juberti): implement me |
| 85 return 0; |
| 69 } | 86 } |
| 70 | 87 |
| 71 int32_t Send(PP_Resource resource, const void* data, uint32_t len, | 88 |
| 72 PP_CompletionCallback callback) { | 89 // Like send(), sends data. Returns PP_ERROR_WOULDBLOCK |
| 73 scoped_refptr<PPB_Transport_Impl> t( | 90 // if the socket is currently flow-controlled. |
| 74 Resource::GetAs<PPB_Transport_Impl>(resource)); | 91 int32_t Send(PP_Resource transport, |
| 75 return (t.get())? t->Send(data, len, callback) : PP_ERROR_BADRESOURCE; | 92 const void* data, |
| 93 uint32_t len, |
| 94 PP_CompletionCallback cb) { |
| 95 // TODO(juberti): implement me |
| 96 return 0; |
| 76 } | 97 } |
| 77 | 98 |
| 99 |
| 78 // Disconnects from the remote peer. | 100 // Disconnects from the remote peer. |
| 79 int32_t Close(PP_Resource resource) { | 101 int32_t Close(PP_Resource transport) { |
| 80 scoped_refptr<PPB_Transport_Impl> t( | 102 // TODO(juberti): implement me |
| 81 Resource::GetAs<PPB_Transport_Impl>(resource)); | 103 return 0; |
| 82 return (t.get())? t->Close() : PP_ERROR_BADRESOURCE; | |
| 83 } | 104 } |
| 84 | 105 |
| 106 |
| 85 const PPB_Transport_Dev ppb_transport = { | 107 const PPB_Transport_Dev ppb_transport = { |
| 86 &CreateTransport, | 108 &CreateTransport, |
| 87 &IsTransport, | 109 &IsTransport, |
| 88 &IsWritable, | 110 &IsWritable, |
| 89 &Connect, | 111 &Connect, |
| 90 &GetNextAddress, | 112 &GetNextAddress, |
| 91 &ReceiveRemoteAddress, | 113 &ReceiveRemoteAddress, |
| 92 &Recv, | 114 &Recv, |
| 93 &Send, | 115 &Send, |
| 94 &Close, | 116 &Close, |
| 95 }; | 117 }; |
| 96 | 118 |
| 97 } // namespace | 119 } // namespace |
| 98 | 120 |
| 99 PPB_Transport_Impl::PPB_Transport_Impl(PluginInstance* instance) | 121 PPB_Transport_Impl::PPB_Transport_Impl(PluginInstance* instance) |
| 100 : Resource(instance), | 122 : Resource(instance) { |
| 101 network_manager_(new talk_base::NetworkManager()), | 123 // TODO(juberti): impl |
| 102 allocator_(new cricket::HttpPortAllocator(network_manager_.get(), "")) { | |
| 103 std::vector<talk_base::SocketAddress> stun_hosts; | |
| 104 stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); | |
| 105 allocator_->SetStunHosts(stun_hosts); | |
| 106 // TODO(sergeyu): Use port allocator that works inside sandbox. | |
| 107 } | |
| 108 | |
| 109 PPB_Transport_Impl::~PPB_Transport_Impl() { | |
| 110 } | 124 } |
| 111 | 125 |
| 112 const PPB_Transport_Dev* PPB_Transport_Impl::GetInterface() { | 126 const PPB_Transport_Dev* PPB_Transport_Impl::GetInterface() { |
| 113 return &ppb_transport; | 127 return &ppb_transport; |
| 114 } | 128 } |
| 115 | 129 |
| 130 PPB_Transport_Impl::~PPB_Transport_Impl() { |
| 131 // TODO(juberti): teardown |
| 132 } |
| 133 |
| 116 PPB_Transport_Impl* PPB_Transport_Impl::AsPPB_Transport_Impl() { | 134 PPB_Transport_Impl* PPB_Transport_Impl::AsPPB_Transport_Impl() { |
| 117 return this; | 135 return this; |
| 118 } | 136 } |
| 119 | 137 |
| 120 bool PPB_Transport_Impl::Init(const char* name, const char* proto) { | 138 bool PPB_Transport_Impl::Init(const char* name, const char* proto) { |
| 121 // For now, always create http://www.google.com/transport/p2p . | 139 // TODO(juberti): impl |
| 122 channel_.reset(new cricket::P2PTransportChannel( | |
| 123 name, "", NULL, allocator_.get())); | |
| 124 channel_->SignalRequestSignaling.connect( | |
| 125 this, &PPB_Transport_Impl::OnRequestSignaling); | |
| 126 channel_->SignalWritableState.connect( | |
| 127 this, &PPB_Transport_Impl::OnWriteableState); | |
| 128 channel_->SignalCandidateReady.connect( | |
| 129 this, &PPB_Transport_Impl::OnCandidateReady); | |
| 130 channel_->SignalReadPacket.connect( | |
| 131 this, &PPB_Transport_Impl::OnReadPacket); | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 bool PPB_Transport_Impl::IsWritable() const { | |
| 136 return channel_->writable(); | |
| 137 } | |
| 138 | |
| 139 int32_t PPB_Transport_Impl::Connect(PP_CompletionCallback callback) { | |
| 140 // TODO(juberti): Fail if we're already connected. | |
| 141 if (connect_callback_.get() && !connect_callback_->completed()) | |
| 142 return PP_ERROR_INPROGRESS; | |
| 143 | |
| 144 channel_->Connect(); | |
| 145 | |
| 146 PP_Resource resource_id = GetReferenceNoAddRef(); | |
| 147 CHECK(resource_id); | |
| 148 connect_callback_ = new TrackedCompletionCallback( | |
| 149 instance()->module()->GetCallbackTracker(), resource_id, callback); | |
| 150 return PP_ERROR_WOULDBLOCK; | |
| 151 } | |
| 152 | |
| 153 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, | |
| 154 PP_CompletionCallback callback) { | |
| 155 if (next_address_callback_.get() && !next_address_callback_->completed()) | |
| 156 return PP_ERROR_INPROGRESS; | |
| 157 | |
| 158 if (!local_candidates_.empty()) { | |
| 159 Serialize(local_candidates_.front(), address); | |
| 160 local_candidates_.pop_front(); | |
| 161 return PP_OK; | |
| 162 } | |
| 163 | |
| 164 PP_Resource resource_id = GetReferenceNoAddRef(); | |
| 165 CHECK(resource_id); | |
| 166 next_address_callback_ = new TrackedCompletionCallback( | |
| 167 instance()->module()->GetCallbackTracker(), resource_id, callback); | |
| 168 return PP_ERROR_WOULDBLOCK; | |
| 169 } | |
| 170 | |
| 171 int32_t PPB_Transport_Impl::ReceiveRemoteAddress(PP_Var address) { | |
| 172 cricket::Candidate candidate; | |
| 173 if (!Deserialize(address, &candidate)) { | |
| 174 return PP_ERROR_FAILED; | |
| 175 } | |
| 176 | |
| 177 channel_->OnCandidate(candidate); | |
| 178 return PP_OK; | |
| 179 } | |
| 180 | |
| 181 int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len, | |
| 182 PP_CompletionCallback callback) { | |
| 183 if (recv_callback_.get() && !recv_callback_->completed()) | |
| 184 return PP_ERROR_INPROGRESS; | |
| 185 | |
| 186 // TODO(juberti): Should we store packets that are received when | |
| 187 // no callback is installed? | |
| 188 | |
| 189 recv_buffer_ = data; | |
| 190 recv_buffer_size_ = len; | |
| 191 | |
| 192 PP_Resource resource_id = GetReferenceNoAddRef(); | |
| 193 CHECK(resource_id); | |
| 194 recv_callback_ = new TrackedCompletionCallback( | |
| 195 instance()->module()->GetCallbackTracker(), resource_id, callback); | |
| 196 return PP_ERROR_WOULDBLOCK; | |
| 197 } | |
| 198 | |
| 199 int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len, | |
| 200 PP_CompletionCallback callback) { | |
| 201 return channel_->SendPacket(static_cast<const char*>(data), len); | |
| 202 } | |
| 203 | |
| 204 int32_t PPB_Transport_Impl::Close() { | |
| 205 channel_->Reset(); | |
| 206 instance()->module()->GetCallbackTracker()->AbortAll(); | |
| 207 return PP_OK; | |
| 208 } | |
| 209 | |
| 210 void PPB_Transport_Impl::OnRequestSignaling() { | |
| 211 channel_->OnSignalingReady(); | |
| 212 } | |
| 213 | |
| 214 void PPB_Transport_Impl::OnCandidateReady( | |
| 215 cricket::TransportChannelImpl* channel, | |
| 216 const cricket::Candidate& candidate) { | |
| 217 // Store the candidate first before calling the callback. | |
| 218 local_candidates_.push_back(candidate); | |
| 219 | |
| 220 if (next_address_callback_.get() && next_address_callback_->completed()) { | |
| 221 scoped_refptr<TrackedCompletionCallback> callback; | |
| 222 callback.swap(next_address_callback_); | |
| 223 callback->Run(PP_OK); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void PPB_Transport_Impl::OnWriteableState(cricket::TransportChannel* channel) { | |
| 228 if (connect_callback_.get() && connect_callback_->completed()) { | |
| 229 scoped_refptr<TrackedCompletionCallback> callback; | |
| 230 callback.swap(connect_callback_); | |
| 231 callback->Run(PP_OK); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void PPB_Transport_Impl::OnReadPacket(cricket::TransportChannel* channel, | |
| 236 const char* data, size_t len) { | |
| 237 if (recv_callback_.get() && recv_callback_->completed()) { | |
| 238 scoped_refptr<TrackedCompletionCallback> callback; | |
| 239 callback.swap(recv_callback_); | |
| 240 | |
| 241 if (len <= recv_buffer_size_) { | |
| 242 memcpy(recv_buffer_, data, len); | |
| 243 callback->Run(PP_OK); | |
| 244 } else { | |
| 245 callback->Run(PP_ERROR_FAILED); | |
| 246 } | |
| 247 } | |
| 248 // TODO(sergeyu): Buffer incoming packet if there is no pending read. | |
| 249 } | |
| 250 | |
| 251 bool PPB_Transport_Impl::Serialize(const cricket::Candidate& candidate, | |
| 252 PP_Var* address) { | |
| 253 // TODO(juberti): Come up with a real wire format! | |
| 254 std::string blob = candidate.ToString(); | |
| 255 Var::PluginReleasePPVar(*address); | |
| 256 *address = StringVar::StringToPPVar(instance()->module(), blob); | |
| 257 return true; | |
| 258 } | |
| 259 | |
| 260 bool PPB_Transport_Impl::Deserialize(PP_Var address, | |
| 261 cricket::Candidate* candidate) { | |
| 262 // TODO(juberti): Implement this. | |
| 263 return false; | 140 return false; |
| 264 } | 141 } |
| 265 | 142 |
| 266 } // namespace ppapi | 143 } // namespace ppapi |
| 267 } // namespace webkit | 144 } // namespace webkit |
| 145 |
| OLD | NEW |