Chromium Code Reviews| Index: runtime/bin/tls_socket.cc |
| diff --git a/runtime/bin/tls_socket.cc b/runtime/bin/tls_socket.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4be76ca4dcac17b29d27f3a39bc3a63dd80a3e26 |
| --- /dev/null |
| +++ b/runtime/bin/tls_socket.cc |
| @@ -0,0 +1,164 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include "bin/tls_socket.h" |
| + |
| +#include "bin/builtin.h" |
| +#include "bin/dartutils.h" |
| +#include "bin/thread.h" |
| +#include "bin/utils.h" |
| + |
| +#include "include/dart_api.h" |
| + |
| +bool TlsFilter::library_initialized_ = false; |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* local_data = new TlsFilter; |
| + Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
| + if (Dart_IsError(dart_this)) { |
| + delete local_data; |
| + Dart_PropagateError(dart_this); |
| + } |
| + local_data->Init(dart_this); |
| + |
| + Dart_SetReturnValue(args, Dart_Null()); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* local_data; |
| + |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
|
Søren Gjesse
2012/08/09 10:22:52
Maybe add a function fir getting the local_data na
Bill Hesse
2012/08/13 11:20:03
Done.
|
| + ASSERT(Dart_IsInstance(dart_this)); |
| + int count; |
| + HandleError(Dart_GetNativeInstanceFieldCount(dart_this, &count)); |
| + ASSERT(count == 1); |
| + HandleError(Dart_GetNativeInstanceField(dart_this, 0, |
| + reinterpret_cast<intptr_t*>(&local_data))); |
| + |
| + local_data->Connect(); |
| + Dart_SetReturnValue(args, Dart_Null()); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* local_data; |
| + |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| + ASSERT(Dart_IsInstance(dart_this)); |
| + int count; |
| + HandleError(Dart_GetNativeInstanceFieldCount(dart_this, &count)); |
| + ASSERT(count == 1); |
| + HandleError(Dart_GetNativeInstanceField(dart_this, 0, |
| + reinterpret_cast<intptr_t*>(&local_data))); |
| + HandleError(Dart_SetNativeInstanceField(dart_this, 0, NULL)); |
| + |
| + local_data->Destroy(); |
| + delete local_data; |
| + Dart_SetReturnValue(args, Dart_Null()); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( |
| + Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* local_data; |
| + |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| + Dart_Handle handshake_start = HandleError(Dart_GetNativeArgument(args, 1)); |
| + Dart_Handle handshake_finish = HandleError(Dart_GetNativeArgument(args, 2)); |
| + if (!Dart_IsInstance(dart_this) || |
| + !Dart_IsClosure(handshake_start) || |
| + !Dart_IsClosure(handshake_finish)) { |
| + Dart_ThrowException(DartUtils::NewDartIllegalArgumentException( |
| + "Illegal argument to RegisterHandshakeCallbacks")); |
| + } |
| + HandleError(Dart_GetNativeInstanceField(dart_this, 0, |
| + reinterpret_cast<intptr_t*>(&local_data))); |
| + |
| + local_data->RegisterHandshakeCallbacks(handshake_start, handshake_finish); |
| + Dart_SetReturnValue(args, Dart_Null()); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* local_data; |
| + |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| + ASSERT(Dart_IsInstance(dart_this)); |
| + HandleError(Dart_GetNativeInstanceField(dart_this, 0, |
| + reinterpret_cast<intptr_t*>(&local_data))); |
| + Dart_Handle buffer_id_object = HandleError(Dart_GetNativeArgument(args, 1)); |
| + int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); |
| + if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { |
| + Dart_ThrowException(DartUtils::NewDartIllegalArgumentException( |
| + "Illegal argument to ProcessBuffer")); |
| + } |
| + |
| + intptr_t bytes_read = local_data->ProcessBuffer(static_cast<int>(buffer_id)); |
| + Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void TlsFilter::Init(Dart_Handle dart_this) { |
| + ASSERT(Dart_IsInstance(dart_this)); |
| + int count; |
| + HandleError(Dart_GetNativeInstanceFieldCount(dart_this, &count)); |
| + ASSERT(count == 1); |
| + HandleError(Dart_SetNativeInstanceField(dart_this, 0, |
| + reinterpret_cast<intptr_t>(this))); |
| + stringStart_ = HandleError( |
| + Dart_NewPersistentHandle(Dart_NewString("start"))); |
|
Søren Gjesse
2012/08/09 10:22:52
I don't see these persistent handles being deleted
Bill Hesse
2012/08/13 11:20:03
Fixed - they are now freed in destroy().
On 2012/
|
| + stringLength_ = HandleError( |
| + Dart_NewPersistentHandle(Dart_NewString("length"))); |
| + |
| + InitializeBuffers(dart_this); |
| + InitializePlatformData(); |
| +} |
| + |
| + |
| +void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { |
| + // Create TlsFilter buffers as ExternalUint8Array objects. |
| + Dart_Handle dart_buffers_object = HandleError( |
| + Dart_GetField(dart_this, Dart_NewString("buffers"))); |
| + Dart_Handle dart_buffer_object = HandleError( |
| + Dart_ListGetAt(dart_buffers_object, kReadPlaintext)); |
| + Dart_Handle tlsExternalBuffer_class = HandleError( |
| + Dart_InstanceGetClass(dart_buffer_object)); |
| + Dart_Handle dart_buffer_size = HandleError( |
| + Dart_GetField(tlsExternalBuffer_class, Dart_NewString("kSize"))); |
| + buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); |
| + if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { |
| + Dart_ThrowException( |
| + Dart_NewString("Invalid buffer size in _TlsExternalBuffer")); |
| + } |
| + |
| + for (int i = 0; i < kNumBuffers; ++i) { |
| + dart_buffer_objects_[i] = HandleError( |
| + Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); |
|
Søren Gjesse
2012/08/09 10:22:52
I don't see these persistent handles being deleted
Bill Hesse
2012/08/13 11:20:03
Right. Fixed.
|
| + buffers_[i] = new uint8_t[buffer_size_]; |
| + Dart_Handle data = HandleError( |
| + Dart_NewExternalByteArray(buffers_[i], |
| + buffer_size_, NULL, NULL)); |
| + HandleError( |
| + Dart_SetField(dart_buffer_objects_[i], Dart_NewString("data"), data)); |
| + } |
| +} |
| + |
| + |
| +void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, |
| + Dart_Handle finish) { |
| + handshake_start_ = HandleError(Dart_NewPersistentHandle(start)); |
| + handshake_finish_ = HandleError(Dart_NewPersistentHandle(finish)); |
| +} |