OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/directory.h" | 5 #include "bin/directory.h" |
6 | 6 |
7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
11 | 11 |
12 dart::Mutex Directory::mutex_; | 12 |
13 int Directory::service_ports_size_ = 0; | 13 // Forward declaration. |
14 Dart_Port* Directory::service_ports_ = NULL; | 14 static void DirectoryService(Dart_Port, Dart_Port, Dart_CObject*); |
15 int Directory::service_ports_index_ = 0; | 15 |
| 16 NativeService Directory::directory_service_("DirectoryService", |
| 17 DirectoryService, |
| 18 16); |
| 19 |
16 | 20 |
17 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { | 21 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { |
18 Dart_EnterScope(); | 22 Dart_EnterScope(); |
19 char* current = Directory::Current(); | 23 char* current = Directory::Current(); |
20 if (current != NULL) { | 24 if (current != NULL) { |
21 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 25 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
22 free(current); | 26 free(current); |
23 } | 27 } |
24 Dart_ExitScope(); | 28 Dart_ExitScope(); |
25 } | 29 } |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 CObjectString path(request[1]); | 233 CObjectString path(request[1]); |
230 CObjectString new_path(request[2]); | 234 CObjectString new_path(request[2]); |
231 bool completed = Directory::Rename(path.CString(), new_path.CString()); | 235 bool completed = Directory::Rename(path.CString(), new_path.CString()); |
232 if (completed) return CObject::True(); | 236 if (completed) return CObject::True(); |
233 return CObject::NewOSError(); | 237 return CObject::NewOSError(); |
234 } | 238 } |
235 return CObject::IllegalArgumentError(); | 239 return CObject::IllegalArgumentError(); |
236 } | 240 } |
237 | 241 |
238 | 242 |
239 void DirectoryService(Dart_Port dest_port_id, | 243 static void DirectoryService(Dart_Port dest_port_id, |
240 Dart_Port reply_port_id, | 244 Dart_Port reply_port_id, |
241 Dart_CObject* message) { | 245 Dart_CObject* message) { |
242 CObject* response = CObject::False(); | 246 CObject* response = CObject::IllegalArgumentError(); |
243 CObjectArray request(message); | 247 CObjectArray request(message); |
244 if (message->type == Dart_CObject::kArray) { | 248 if (message->type == Dart_CObject::kArray) { |
245 if (request.Length() > 1 && request[0]->IsInt32()) { | 249 if (request.Length() > 1 && request[0]->IsInt32()) { |
246 CObjectInt32 requestType(request[0]); | 250 CObjectInt32 requestType(request[0]); |
247 switch (requestType.Value()) { | 251 switch (requestType.Value()) { |
248 case Directory::kCreateRequest: | 252 case Directory::kCreateRequest: |
249 response = DirectoryCreateRequest(request); | 253 response = DirectoryCreateRequest(request); |
250 break; | 254 break; |
251 case Directory::kDeleteRequest: | 255 case Directory::kDeleteRequest: |
252 response = DirectoryDeleteRequest(request); | 256 response = DirectoryDeleteRequest(request); |
(...skipping 14 matching lines...) Expand all Loading... |
267 UNREACHABLE(); | 271 UNREACHABLE(); |
268 } | 272 } |
269 } | 273 } |
270 } | 274 } |
271 | 275 |
272 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | 276 Dart_PostCObject(reply_port_id, response->AsApiCObject()); |
273 } | 277 } |
274 | 278 |
275 | 279 |
276 Dart_Port Directory::GetServicePort() { | 280 Dart_Port Directory::GetServicePort() { |
277 MutexLocker lock(&mutex_); | 281 return directory_service_.GetServicePort(); |
278 if (service_ports_size_ == 0) { | |
279 ASSERT(service_ports_ == NULL); | |
280 service_ports_size_ = 16; | |
281 service_ports_ = new Dart_Port[service_ports_size_]; | |
282 service_ports_index_ = 0; | |
283 for (int i = 0; i < service_ports_size_; i++) { | |
284 service_ports_[i] = ILLEGAL_PORT; | |
285 } | |
286 } | |
287 | |
288 Dart_Port result = service_ports_[service_ports_index_]; | |
289 if (result == ILLEGAL_PORT) { | |
290 result = Dart_NewNativePort("DirectoryService", | |
291 DirectoryService, | |
292 true); | |
293 ASSERT(result != ILLEGAL_PORT); | |
294 service_ports_[service_ports_index_] = result; | |
295 } | |
296 service_ports_index_ = (service_ports_index_ + 1) % service_ports_size_; | |
297 return result; | |
298 } | 282 } |
299 | 283 |
300 | 284 |
301 void FUNCTION_NAME(Directory_NewServicePort)(Dart_NativeArguments args) { | 285 void FUNCTION_NAME(Directory_NewServicePort)(Dart_NativeArguments args) { |
302 Dart_EnterScope(); | 286 Dart_EnterScope(); |
303 Dart_SetReturnValue(args, Dart_Null()); | 287 Dart_SetReturnValue(args, Dart_Null()); |
304 Dart_Port service_port = Directory::GetServicePort(); | 288 Dart_Port service_port = Directory::GetServicePort(); |
305 if (service_port != ILLEGAL_PORT) { | 289 if (service_port != ILLEGAL_PORT) { |
306 // Return a send port for the service port. | 290 // Return a send port for the service port. |
307 Dart_Handle send_port = Dart_NewSendPort(service_port); | 291 Dart_Handle send_port = Dart_NewSendPort(service_port); |
(...skipping 27 matching lines...) Expand all Loading... |
335 CObject* err = CObject::NewOSError(); | 319 CObject* err = CObject::NewOSError(); |
336 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); | 320 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); |
337 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); | 321 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); |
338 response->SetAt(1, new CObjectString(CObject::NewString(dir_name))); | 322 response->SetAt(1, new CObjectString(CObject::NewString(dir_name))); |
339 response->SetAt(2, err); | 323 response->SetAt(2, err); |
340 return Dart_PostCObject(response_port_, response->AsApiCObject()); | 324 return Dart_PostCObject(response_port_, response->AsApiCObject()); |
341 } | 325 } |
342 | 326 |
343 bool SyncDirectoryListing::HandleDirectory(char* dir_name) { | 327 bool SyncDirectoryListing::HandleDirectory(char* dir_name) { |
344 Dart_Handle dir_name_dart = DartUtils::NewString(dir_name); | 328 Dart_Handle dir_name_dart = DartUtils::NewString(dir_name); |
345 Dart_Handle dir = Dart_New(directory_class_, Dart_Null(), 1, &dir_name_dart); | 329 Dart_Handle dir = |
| 330 Dart_New(directory_class_, Dart_Null(), 1, &dir_name_dart); |
346 Dart_Invoke(results_, add_string_, 1, &dir); | 331 Dart_Invoke(results_, add_string_, 1, &dir); |
347 return true; | 332 return true; |
348 } | 333 } |
349 | 334 |
350 bool SyncDirectoryListing::HandleFile(char* file_name) { | 335 bool SyncDirectoryListing::HandleFile(char* file_name) { |
351 Dart_Handle file_name_dart = DartUtils::NewString(file_name); | 336 Dart_Handle file_name_dart = DartUtils::NewString(file_name); |
352 Dart_Handle file = Dart_New(file_class_, Dart_Null(), 1, &file_name_dart); | 337 Dart_Handle file = |
| 338 Dart_New(file_class_, Dart_Null(), 1, &file_name_dart); |
353 Dart_Invoke(results_, add_string_, 1, &file); | 339 Dart_Invoke(results_, add_string_, 1, &file); |
354 return true; | 340 return true; |
355 } | 341 } |
356 | 342 |
357 bool SyncDirectoryListing::HandleError(const char* dir_name) { | 343 bool SyncDirectoryListing::HandleError(const char* dir_name) { |
358 Dart_Handle dart_os_error = DartUtils::NewDartOSError(); | 344 Dart_Handle dart_os_error = DartUtils::NewDartOSError(); |
359 Dart_Handle args[3]; | 345 Dart_Handle args[3]; |
360 args[0] = DartUtils::NewString("Directory listing failed"); | 346 args[0] = DartUtils::NewString("Directory listing failed"); |
361 args[1] = DartUtils::NewString(dir_name); | 347 args[1] = DartUtils::NewString(dir_name); |
362 args[2] = dart_os_error; | 348 args[2] = dart_os_error; |
363 Dart_ThrowException(Dart_New( | 349 Dart_ThrowException(Dart_New( |
364 DartUtils::GetDartClass(DartUtils::kIOLibURL, "DirectoryIOException"), | 350 DartUtils::GetDartClass(DartUtils::kIOLibURL, "DirectoryIOException"), |
365 Dart_Null(), | 351 Dart_Null(), |
366 3, | 352 3, |
367 args)); | 353 args)); |
368 return true; | 354 return true; |
369 } | 355 } |
OLD | NEW |