Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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/glue/plugins/pepper_transport.h" | 5 #include "webkit/glue/plugins/pepper_transport.h" |
| 6 | 6 |
| 7 #include "base/singleton.h" | 7 #include "ppapi/c/pp_completion_callback.h" |
| 8 #include "base/thread_local.h" | 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/c/dev/ppb_transport_dev.h" | 9 #include "third_party/libjingle/source/talk/p2p/base/p2ptransportchannel.h" |
| 10 #include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h" | |
| 11 #include "third_party/ppapi/c/dev/ppb_transport_dev.h" | |
| 10 #include "webkit/glue/plugins/pepper_common.h" | 12 #include "webkit/glue/plugins/pepper_common.h" |
| 11 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
| 12 #include "webkit/glue/plugins/pepper_plugin_module.h" | 13 #include "webkit/glue/plugins/pepper_plugin_module.h" |
| 14 #include "webkit/glue/plugins/pepper_var.h" | |
| 13 | 15 |
| 14 namespace pepper { | 16 namespace pepper { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // Creates a new transport object with the specified name | 20 PP_Resource CreateTransport(PP_Module module_id, const char* name, |
| 19 // using the specified protocol. | |
| 20 PP_Resource CreateTransport(PP_Module module, | |
| 21 const char* name, | |
| 22 const char* proto) { | 21 const char* proto) { |
| 23 // TODO(juberti): implement me | 22 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
| 24 PP_Resource p(0); | 23 if (!module) |
| 25 return p; | 24 return 0; |
| 25 | |
| 26 scoped_refptr<Transport> t(new Transport(module)); | |
| 27 if (!t->Init(name, proto)) { | |
| 28 return 0; | |
| 29 } | |
| 30 | |
| 31 return t->GetReference(); | |
| 26 } | 32 } |
| 27 | 33 |
| 28 // Returns whether or not resource is Transport | |
| 29 PP_Bool IsTransport(PP_Resource resource) { | 34 PP_Bool IsTransport(PP_Resource resource) { |
| 30 return BoolToPPBool(!!Resource::GetAs<Transport>(resource)); | 35 return BoolToPPBool(Resource::GetAs<Transport>(resource) != NULL); |
| 31 } | 36 } |
| 32 | 37 |
| 33 // Returns whether the transport is currently writable | 38 PP_Bool IsWritable(PP_Resource resource) { |
| 34 // (i.e. can send data to the remote peer) | 39 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 35 PP_Bool IsWritable(PP_Resource transport) { | 40 return BoolToPPBool((t.get()) ? t->IsWritable() : false); |
| 36 // TODO(juberti): impelement me | |
| 37 return PP_FALSE; | |
| 38 } | 41 } |
| 39 | 42 |
| 40 | 43 int32_t Connect(PP_Resource resource, PP_CompletionCallback cb) { |
| 41 // TODO(juberti): other getters/setters | 44 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 42 // connect state | 45 return (t.get()) ? t->Connect(cb) : PP_ERROR_BADRESOURCE; |
| 43 // connect type, protocol | |
| 44 // RTT | |
| 45 | |
| 46 | |
| 47 // Establishes a connection to the remote peer. | |
| 48 // Returns PP_ERROR_WOULDBLOCK and notifies on |cb| | |
| 49 // when connectivity is established (or timeout occurs). | |
| 50 int32_t Connect(PP_Resource transport, | |
| 51 PP_CompletionCallback cb) { | |
| 52 // TODO(juberti): impelement me | |
| 53 return 0; | |
| 54 } | 46 } |
| 55 | 47 |
| 56 | 48 int32_t GetNextAddress(PP_Resource resource, PP_Var* address, |
| 57 // Obtains another ICE candidate address to be provided | |
| 58 // to the remote peer. Returns PP_ERROR_WOULDBLOCK | |
| 59 // if there are no more addresses to be sent. | |
| 60 int32_t GetNextAddress(PP_Resource transport, | |
| 61 PP_Var* address, | |
| 62 PP_CompletionCallback cb) { | 49 PP_CompletionCallback cb) { |
| 63 // TODO(juberti): implement me | 50 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 64 return 0; | 51 return (t.get())? t->GetNextAddress(address, cb) : PP_ERROR_BADRESOURCE; |
| 65 } | 52 } |
| 66 | 53 |
| 67 | 54 int32_t ReceiveRemoteAddress(PP_Resource resource, PP_Var address) { |
| 68 // Provides an ICE candidate address that was received | 55 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 69 // from the remote peer. | 56 return (t.get())? t->ReceiveRemoteAddress(address) : PP_ERROR_BADRESOURCE; |
| 70 int32_t ReceiveRemoteAddress(PP_Resource transport, | |
| 71 PP_Var address) { | |
| 72 // TODO(juberti): implement me | |
| 73 return 0; | |
| 74 } | 57 } |
| 75 | 58 |
| 76 | 59 int32_t Recv(PP_Resource resource, void* data, uint32_t len, |
| 77 // Like recv(), receives data. Returns PP_ERROR_WOULDBLOCK | |
| 78 // if there is currently no data to receive. | |
| 79 int32_t Recv(PP_Resource transport, | |
| 80 void* data, | |
| 81 uint32_t len, | |
| 82 PP_CompletionCallback cb) { | 60 PP_CompletionCallback cb) { |
| 83 // TODO(juberti): implement me | 61 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 84 return 0; | 62 return (t.get())? t->Recv(data, len, cb) : PP_ERROR_BADRESOURCE; |
| 85 } | 63 } |
| 86 | 64 |
| 87 | 65 int32_t Send(PP_Resource resource, const void* data, |
| 88 // Like send(), sends data. Returns PP_ERROR_WOULDBLOCK | 66 uint32_t len, PP_CompletionCallback cb) { |
| 89 // if the socket is currently flow-controlled. | 67 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 90 int32_t Send(PP_Resource transport, | 68 return (t.get())? t->Send(data, len, cb) : PP_ERROR_BADRESOURCE; |
| 91 const void* data, | |
| 92 uint32_t len, | |
| 93 PP_CompletionCallback cb) { | |
| 94 // TODO(juberti): implement me | |
| 95 return 0; | |
| 96 } | 69 } |
| 97 | 70 |
| 98 | 71 int32_t Close(PP_Resource resource) { |
| 99 // Disconnects from the remote peer. | 72 scoped_refptr<Transport> t(Resource::GetAs<Transport>(resource)); |
| 100 int32_t Close(PP_Resource transport) { | 73 return (t.get())? t->Close() : PP_ERROR_BADRESOURCE; |
| 101 // TODO(juberti): implement me | |
| 102 return 0; | |
| 103 } | 74 } |
| 104 | 75 |
| 76 } // namespace | |
| 105 | 77 |
| 106 const PPB_Transport_Dev ppb_transport = { | 78 const PPB_Transport_Dev ppb_transport = { |
| 107 &CreateTransport, | 79 &CreateTransport, |
| 108 &IsTransport, | 80 &IsTransport, |
| 109 &IsWritable, | 81 &IsWritable, |
| 110 &Connect, | 82 &Connect, |
| 111 &GetNextAddress, | 83 &GetNextAddress, |
| 112 &ReceiveRemoteAddress, | 84 &ReceiveRemoteAddress, |
| 113 &Recv, | 85 &Recv, |
| 114 &Send, | 86 &Send, |
| 115 &Close, | 87 &Close |
| 116 }; | 88 }; |
| 117 | 89 |
| 118 } // namespace | |
| 119 | |
| 120 Transport::Transport(PluginModule* module) | |
| 121 : Resource(module) { | |
| 122 // TODO(juberti): impl | |
| 123 } | |
| 124 | |
| 125 const PPB_Transport_Dev* Transport::GetInterface() { | 90 const PPB_Transport_Dev* Transport::GetInterface() { |
| 126 return &ppb_transport; | 91 return &ppb_transport; |
| 127 } | 92 } |
| 128 | 93 |
| 129 Transport::~Transport() { | 94 Transport::Transport(PluginModule* module) |
| 130 // TODO(juberti): teardown | 95 : Resource(module), |
| 96 network_manager_(new talk_base::NetworkManager()), | |
| 97 allocator_(new cricket::HttpPortAllocator(network_manager_.get(), "")) { | |
| 98 std::vector<talk_base::SocketAddress> stun_hosts; | |
| 99 stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); | |
| 100 allocator_->SetStunHosts(stun_hosts); | |
| 101 // TODO(juberti): Run timer, then move to use ChromeSocketServer | |
| 131 } | 102 } |
| 132 | 103 |
| 133 bool Transport::Init(const char* name, | 104 bool Transport::Init(const char* name, const char* protocol) { |
| 134 const char* proto) { | 105 // 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
| |
| 135 // TODO(juberti): impl | 106 channel_.reset(new cricket::P2PTransportChannel( |
| 107 name, "", NULL, allocator_.get())); | |
| 108 channel_->SignalRequestSignaling.connect(this, | |
| 109 &Transport::OnRequestSignaling); | |
| 110 channel_->SignalWritableState.connect(this, | |
| 111 &Transport::OnWriteableState); | |
| 112 channel_->SignalCandidateReady.connect(this, | |
| 113 &Transport::OnCandidateReady); | |
| 114 channel_->SignalReadPacket.connect(this, | |
| 115 &Transport::OnReadPacket); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 Transport::~Transport() { | |
| 120 } | |
| 121 | |
| 122 bool Transport::IsWritable() const { | |
| 123 return channel_->writable(); | |
| 124 } | |
| 125 | |
| 126 int32_t Transport::Connect(PP_CompletionCallback cb) { | |
| 127 // TODO(juberti): fail if we're already connected. | |
| 128 if (connect_cx_.get()) { | |
| 129 return PP_ERROR_INPROGRESS; | |
| 130 } | |
| 131 channel_->Connect(); | |
| 132 connect_cx_.reset(new ConnectContext(cb)); | |
| 133 return PP_ERROR_WOULDBLOCK; | |
| 134 } | |
| 135 | |
| 136 int32_t Transport::GetNextAddress(PP_Var* address, PP_CompletionCallback cb) { | |
| 137 if (next_address_cx_.get()) { | |
| 138 return PP_ERROR_INPROGRESS; | |
| 139 } | |
| 140 | |
| 141 if (!local_candidates_.empty()) { | |
| 142 Serialize(local_candidates_.front(), address); | |
| 143 local_candidates_.pop_front(); | |
| 144 return PP_OK; | |
| 145 } | |
| 146 | |
| 147 next_address_cx_.reset(new GetNextAddressContext(address, cb)); | |
| 148 return PP_ERROR_WOULDBLOCK; | |
| 149 } | |
| 150 | |
| 151 int32_t Transport::ReceiveRemoteAddress(PP_Var address) { | |
| 152 cricket::Candidate candidate; | |
| 153 if (!Deserialize(address, &candidate)) { | |
| 154 return PP_ERROR_FAILED; | |
| 155 } | |
| 156 | |
| 157 channel_->OnCandidate(candidate); | |
| 158 return PP_OK; | |
| 159 } | |
| 160 | |
| 161 int32_t Transport::Recv(void* data, uint32_t len, | |
| 162 PP_CompletionCallback cb) { | |
| 163 if (recv_cx_.get()) { | |
| 164 return PP_ERROR_INPROGRESS; | |
| 165 } | |
| 166 | |
| 167 // TODO(juberti): Should we store packets that are received when | |
| 168 // no callback is installed? | |
| 169 recv_cx_.reset(new RecvContext(data, len, cb)); | |
| 170 return PP_ERROR_WOULDBLOCK; | |
| 171 } | |
| 172 | |
| 173 int32_t Transport::Send(const void* data, uint32_t len, | |
| 174 PP_CompletionCallback cb) { | |
| 175 return channel_->SendPacket(static_cast<const char*>(data), len); | |
| 176 } | |
| 177 | |
| 178 int32_t Transport::Close() { | |
| 179 channel_->Reset(); | |
| 180 return PP_OK; | |
| 181 } | |
| 182 | |
| 183 void Transport::OnRequestSignaling() { | |
| 184 channel_->OnSignalingReady(); | |
| 185 } | |
| 186 | |
| 187 void Transport::OnCandidateReady(cricket::TransportChannelImpl* channel, | |
| 188 const cricket::Candidate& candidate) { | |
| 189 if (next_address_cx_.get()) { | |
| 190 scoped_ptr<GetNextAddressContext> cx; | |
| 191 cx.swap(next_address_cx_); | |
| 192 Serialize(candidate, cx->address); | |
| 193 cx->Callback(PP_OK); | |
| 194 } else { | |
| 195 local_candidates_.push_back(candidate); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void Transport::OnWriteableState(cricket::TransportChannel* channel) { | |
| 200 if (connect_cx_.get()) { | |
| 201 scoped_ptr<ConnectContext> cx; | |
| 202 cx.swap(connect_cx_); | |
| 203 cx->Callback(PP_OK); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void Transport::OnReadPacket(cricket::TransportChannel* channel, | |
| 208 const char* data, size_t len) { | |
| 209 if (recv_cx_.get()) { | |
| 210 scoped_ptr<RecvContext> cx; | |
| 211 int32_t result; | |
| 212 cx.swap(recv_cx_); | |
| 213 if (len <= cx->len) { | |
| 214 memcpy(cx->data, data, len); | |
| 215 result = PP_OK; | |
| 216 } else { | |
| 217 result = PP_ERROR_FAILED; | |
| 218 } | |
| 219 cx->Callback(result); | |
| 220 } | |
| 221 // Otherwise we eat the packet. Should we try harder? | |
| 222 } | |
| 223 | |
| 224 bool Transport::Serialize(const cricket::Candidate& candidate, | |
| 225 PP_Var* address) { | |
| 226 // TODO(juberti): come up with a a real wire format! | |
|
nfullagar
2011/01/04 22:07:17
comment above "...with a real..."
| |
| 227 std::string blob = candidate.ToString(); | |
| 228 *address = StringVar::StringToPPVar(module(), blob); | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 bool Transport::Deserialize(PP_Var address, cricket::Candidate* candidate) { | |
| 233 // TODO(juberti): Implement this. | |
| 136 return false; | 234 return false; |
| 137 } | 235 } |
| 138 | 236 |
| 139 } // namespace pepper | 237 } // namespace pepper |
| 140 | |
| OLD | NEW |