OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdio.h> | 5 #include <stdio.h> |
6 #include <string.h> | 6 #include <string.h> |
| 7 #include <set> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "dart/runtime/include/dart_api.h" | 13 #include "dart/runtime/include/dart_api.h" |
13 #include "mojo/dart/embedder/builtin.h" | 14 #include "mojo/dart/embedder/builtin.h" |
| 15 #include "mojo/dart/embedder/isolate_data.h" |
14 #include "mojo/public/c/system/core.h" | 16 #include "mojo/public/c/system/core.h" |
15 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
16 | 18 |
17 namespace mojo { | 19 namespace mojo { |
18 namespace dart { | 20 namespace dart { |
19 | 21 |
20 #define MOJO_NATIVE_LIST(V) \ | 22 #define MOJO_NATIVE_LIST(V) \ |
21 V(MojoSharedBuffer_Create, 2) \ | 23 V(MojoSharedBuffer_Create, 2) \ |
22 V(MojoSharedBuffer_Duplicate, 2) \ | 24 V(MojoSharedBuffer_Duplicate, 2) \ |
23 V(MojoSharedBuffer_Map, 5) \ | 25 V(MojoSharedBuffer_Map, 5) \ |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 struct CloserCallbackPeer { | 117 struct CloserCallbackPeer { |
116 MojoHandle handle; | 118 MojoHandle handle; |
117 }; | 119 }; |
118 | 120 |
119 static void MojoHandleCloserCallback(void* isolate_data, | 121 static void MojoHandleCloserCallback(void* isolate_data, |
120 Dart_WeakPersistentHandle handle, | 122 Dart_WeakPersistentHandle handle, |
121 void* peer) { | 123 void* peer) { |
122 CloserCallbackPeer* callback_peer = | 124 CloserCallbackPeer* callback_peer = |
123 reinterpret_cast<CloserCallbackPeer*>(peer); | 125 reinterpret_cast<CloserCallbackPeer*>(peer); |
124 if (callback_peer->handle != MOJO_HANDLE_INVALID) { | 126 if (callback_peer->handle != MOJO_HANDLE_INVALID) { |
125 MojoClose(callback_peer->handle); | 127 MojoResult res = MojoClose(callback_peer->handle); |
| 128 if (res == MOJO_RESULT_OK) { |
| 129 // If this finalizer callback successfully closes a handle, it means that |
| 130 // the handle has leaked from the Dart code, which is an error. |
| 131 LOG(ERROR) << "Handle Finalizer closing handle:\n\tisolate: " |
| 132 << "\n\thandle: " << callback_peer->handle; |
| 133 } |
126 } | 134 } |
127 delete callback_peer; | 135 delete callback_peer; |
128 } | 136 } |
129 | 137 |
130 // Setup a weak persistent handle for a Dart MojoHandle that calls MojoClose | 138 // Setup a weak persistent handle for a Dart MojoHandle that calls MojoClose |
131 // on the handle when the MojoHandle is GC'd or the VM is going down. | 139 // on the handle when the MojoHandle is GC'd or the VM is going down. |
132 void MojoHandle_Register(Dart_NativeArguments arguments) { | 140 void MojoHandle_Register(Dart_NativeArguments arguments) { |
133 // An instance of Dart class MojoHandle. | 141 // An instance of Dart class MojoHandle. |
134 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | 142 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); |
135 if (!Dart_IsInstance(mojo_handle_instance)) { | 143 if (!Dart_IsInstance(mojo_handle_instance)) { |
(...skipping 24 matching lines...) Expand all Loading... |
160 if (Dart_IsError(result)) { | 168 if (Dart_IsError(result)) { |
161 SetInvalidArgumentReturn(arguments); | 169 SetInvalidArgumentReturn(arguments); |
162 return; | 170 return; |
163 } | 171 } |
164 | 172 |
165 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | 173 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { |
166 SetInvalidArgumentReturn(arguments); | 174 SetInvalidArgumentReturn(arguments); |
167 return; | 175 return; |
168 } | 176 } |
169 | 177 |
| 178 // Add the handle to this isolate's set. |
| 179 MojoHandle handle = static_cast<MojoHandle>(raw_handle); |
| 180 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 181 void* data = Dart_IsolateData(isolate); |
| 182 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); |
| 183 isolate_data->unclosed_handles.insert(handle); |
| 184 |
| 185 // Set up a finalizer. |
170 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); | 186 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); |
171 callback_peer->handle = static_cast<MojoHandle>(raw_handle); | 187 callback_peer->handle = handle; |
172 Dart_NewWeakPersistentHandle(mojo_handle_instance, | 188 Dart_NewWeakPersistentHandle(mojo_handle_instance, |
173 reinterpret_cast<void*>(callback_peer), | 189 reinterpret_cast<void*>(callback_peer), |
174 sizeof(CloserCallbackPeer), | 190 sizeof(CloserCallbackPeer), |
175 MojoHandleCloserCallback); | 191 MojoHandleCloserCallback); |
176 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 192 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
177 } | 193 } |
178 | 194 |
179 void MojoHandle_Close(Dart_NativeArguments arguments) { | 195 void MojoHandle_Close(Dart_NativeArguments arguments) { |
180 int64_t handle; | 196 int64_t raw_handle; |
181 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 197 CHECK_INTEGER_ARGUMENT(arguments, 0, &raw_handle, InvalidArgument); |
182 | 198 |
183 MojoResult res = MojoClose(static_cast<MojoHandle>(handle)); | 199 // Remove the handle from this isolate's set. |
| 200 MojoHandle handle = static_cast<MojoHandle>(raw_handle); |
| 201 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 202 void* data = Dart_IsolateData(isolate); |
| 203 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); |
| 204 isolate_data->unclosed_handles.erase(handle); |
| 205 |
| 206 MojoResult res = MojoClose(handle); |
184 | 207 |
185 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | 208 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
186 } | 209 } |
187 | 210 |
188 void MojoHandle_Wait(Dart_NativeArguments arguments) { | 211 void MojoHandle_Wait(Dart_NativeArguments arguments) { |
189 int64_t handle = 0; | 212 int64_t handle = 0; |
190 int64_t signals = 0; | 213 int64_t signals = 0; |
191 int64_t deadline = 0; | 214 int64_t deadline = 0; |
192 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 215 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
193 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | 216 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 mojo_control_handle = control_handle; | 816 mojo_control_handle = control_handle; |
794 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 817 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
795 } | 818 } |
796 | 819 |
797 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 820 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
798 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 821 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
799 } | 822 } |
800 | 823 |
801 } // namespace dart | 824 } // namespace dart |
802 } // namespace mojo | 825 } // namespace mojo |
OLD | NEW |