| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "bin/tls_socket.h" |
| 6 |
| 7 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" |
| 9 #include "bin/thread.h" |
| 10 #include "bin/utils.h" |
| 11 |
| 12 #include "include/dart_api.h" |
| 13 |
| 14 bool TlsFilter::library_initialized_ = false; |
| 15 |
| 16 static TlsFilter* NativePeer(Dart_NativeArguments args) { |
| 17 TlsFilter* native_peer; |
| 18 |
| 19 Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| 20 ASSERT(Dart_IsInstance(dart_this)); |
| 21 HandleError(Dart_GetNativeInstanceField(dart_this, 0, |
| 22 reinterpret_cast<intptr_t*>(&native_peer))); |
| 23 return native_peer; |
| 24 } |
| 25 |
| 26 |
| 27 static void SetNativePeer(Dart_NativeArguments args, TlsFilter* native_peer) { |
| 28 Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| 29 ASSERT(Dart_IsInstance(dart_this)); |
| 30 HandleError(Dart_SetNativeInstanceField(dart_this, 0, |
| 31 reinterpret_cast<intptr_t>(native_peer))); |
| 32 } |
| 33 |
| 34 |
| 35 void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { |
| 36 Dart_EnterScope(); |
| 37 TlsFilter* native_peer = new TlsFilter; |
| 38 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
| 39 if (Dart_IsError(dart_this)) { |
| 40 delete native_peer; |
| 41 Dart_PropagateError(dart_this); |
| 42 } |
| 43 SetNativePeer(args, native_peer); |
| 44 native_peer->Init(dart_this); |
| 45 |
| 46 |
| 47 Dart_SetReturnValue(args, Dart_Null()); |
| 48 Dart_ExitScope(); |
| 49 } |
| 50 |
| 51 |
| 52 void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { |
| 53 Dart_EnterScope(); |
| 54 NativePeer(args)->Connect(); |
| 55 Dart_SetReturnValue(args, Dart_Null()); |
| 56 Dart_ExitScope(); |
| 57 } |
| 58 |
| 59 |
| 60 void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { |
| 61 Dart_EnterScope(); |
| 62 TlsFilter* native_peer = NativePeer(args); |
| 63 SetNativePeer(args, NULL); |
| 64 native_peer->Destroy(); |
| 65 delete native_peer; |
| 66 Dart_SetReturnValue(args, Dart_Null()); |
| 67 Dart_ExitScope(); |
| 68 } |
| 69 |
| 70 |
| 71 void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( |
| 72 Dart_NativeArguments args) { |
| 73 Dart_EnterScope(); |
| 74 Dart_Handle handshake_start = HandleError(Dart_GetNativeArgument(args, 1)); |
| 75 Dart_Handle handshake_finish = HandleError(Dart_GetNativeArgument(args, 2)); |
| 76 if (!Dart_IsClosure(handshake_start) || |
| 77 !Dart_IsClosure(handshake_finish)) { |
| 78 Dart_ThrowException(DartUtils::NewDartIllegalArgumentException( |
| 79 "Illegal argument to RegisterHandshakeCallbacks")); |
| 80 } |
| 81 NativePeer(args)->RegisterHandshakeCallbacks(handshake_start, |
| 82 handshake_finish); |
| 83 Dart_SetReturnValue(args, Dart_Null()); |
| 84 Dart_ExitScope(); |
| 85 } |
| 86 |
| 87 |
| 88 void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { |
| 89 Dart_EnterScope(); |
| 90 Dart_Handle buffer_id_object = HandleError(Dart_GetNativeArgument(args, 1)); |
| 91 int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); |
| 92 if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { |
| 93 Dart_ThrowException(DartUtils::NewDartIllegalArgumentException( |
| 94 "Illegal argument to ProcessBuffer")); |
| 95 } |
| 96 |
| 97 intptr_t bytes_read = |
| 98 NativePeer(args)->ProcessBuffer(static_cast<int>(buffer_id)); |
| 99 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 100 Dart_ExitScope(); |
| 101 } |
| 102 |
| 103 |
| 104 void TlsFilter::Init(Dart_Handle dart_this) { |
| 105 stringStart_ = HandleError( |
| 106 Dart_NewPersistentHandle(Dart_NewString("start"))); |
| 107 stringLength_ = HandleError( |
| 108 Dart_NewPersistentHandle(Dart_NewString("length"))); |
| 109 |
| 110 InitializeBuffers(dart_this); |
| 111 InitializePlatformData(); |
| 112 } |
| 113 |
| 114 |
| 115 void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { |
| 116 // Create TlsFilter buffers as ExternalUint8Array objects. |
| 117 Dart_Handle dart_buffers_object = HandleError( |
| 118 Dart_GetField(dart_this, Dart_NewString("buffers"))); |
| 119 Dart_Handle dart_buffer_object = HandleError( |
| 120 Dart_ListGetAt(dart_buffers_object, kReadPlaintext)); |
| 121 Dart_Handle tlsExternalBuffer_class = HandleError( |
| 122 Dart_InstanceGetClass(dart_buffer_object)); |
| 123 Dart_Handle dart_buffer_size = HandleError( |
| 124 Dart_GetField(tlsExternalBuffer_class, Dart_NewString("kSize"))); |
| 125 buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); |
| 126 if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { |
| 127 Dart_ThrowException( |
| 128 Dart_NewString("Invalid buffer size in _TlsExternalBuffer")); |
| 129 } |
| 130 |
| 131 for (int i = 0; i < kNumBuffers; ++i) { |
| 132 dart_buffer_objects_[i] = HandleError( |
| 133 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); |
| 134 buffers_[i] = new uint8_t[buffer_size_]; |
| 135 Dart_Handle data = HandleError( |
| 136 Dart_NewExternalByteArray(buffers_[i], |
| 137 buffer_size_, NULL, NULL)); |
| 138 HandleError( |
| 139 Dart_SetField(dart_buffer_objects_[i], Dart_NewString("data"), data)); |
| 140 } |
| 141 } |
| 142 |
| 143 |
| 144 void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, |
| 145 Dart_Handle finish) { |
| 146 handshake_start_ = HandleError(Dart_NewPersistentHandle(start)); |
| 147 handshake_finish_ = HandleError(Dart_NewPersistentHandle(finish)); |
| 148 } |
| 149 |
| 150 |
| 151 void TlsFilter::DestroyPlatformIndependent() { |
| 152 for (int i = 0; i < kNumBuffers; ++i) { |
| 153 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); |
| 154 } |
| 155 Dart_DeletePersistentHandle(stringStart_); |
| 156 Dart_DeletePersistentHandle(stringLength_); |
| 157 } |
| OLD | NEW |