| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 extern const uint8_t* isolate_snapshot_buffer; | 38 extern const uint8_t* isolate_snapshot_buffer; |
| 39 | 39 |
| 40 static const char* kAsyncLibURL = "dart:async"; | 40 static const char* kAsyncLibURL = "dart:async"; |
| 41 static const char* kInternalLibURL = "dart:_internal"; | 41 static const char* kInternalLibURL = "dart:_internal"; |
| 42 static const char* kIsolateLibURL = "dart:isolate"; | 42 static const char* kIsolateLibURL = "dart:isolate"; |
| 43 static const char* kCoreLibURL = "dart:core"; | 43 static const char* kCoreLibURL = "dart:core"; |
| 44 | 44 |
| 45 static uint8_t snapshot_magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; | 45 static uint8_t snapshot_magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; |
| 46 | 46 |
| 47 static Dart_Handle PrepareBuiltinLibraries(const std::string& package_root, | 47 static Dart_Handle PrepareBuiltinLibraries(const std::string& package_root, |
| 48 const std::string& base_uri) { | 48 const std::string& base_uri, |
| 49 const std::string& script_uri) { |
| 49 // First ensure all required libraries are available. | 50 // First ensure all required libraries are available. |
| 50 Dart_Handle builtin_lib = Builtin::PrepareLibrary(Builtin::kBuiltinLibrary); | 51 Dart_Handle builtin_lib = Builtin::PrepareLibrary(Builtin::kBuiltinLibrary); |
| 51 Builtin::PrepareLibrary(Builtin::kMojoInternalLibrary); | 52 Builtin::PrepareLibrary(Builtin::kMojoInternalLibrary); |
| 52 Builtin::PrepareLibrary(Builtin::kDartMojoIoLibrary); | 53 Builtin::PrepareLibrary(Builtin::kDartMojoIoLibrary); |
| 53 Dart_Handle url = Dart_NewStringFromCString(kInternalLibURL); | 54 Dart_Handle url = Dart_NewStringFromCString(kInternalLibURL); |
| 54 DART_CHECK_VALID(url); | 55 DART_CHECK_VALID(url); |
| 55 Dart_Handle internal_lib = Dart_LookupLibrary(url); | 56 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
| 56 DART_CHECK_VALID(internal_lib); | 57 DART_CHECK_VALID(internal_lib); |
| 57 url = Dart_NewStringFromCString(kCoreLibURL); | 58 url = Dart_NewStringFromCString(kCoreLibURL); |
| 58 DART_CHECK_VALID(url); | 59 DART_CHECK_VALID(url); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 83 Dart_Handle print = Dart_Invoke(builtin_lib, | 84 Dart_Handle print = Dart_Invoke(builtin_lib, |
| 84 Dart_NewStringFromCString("_getPrintClosure"), | 85 Dart_NewStringFromCString("_getPrintClosure"), |
| 85 0, | 86 0, |
| 86 nullptr); | 87 nullptr); |
| 87 DART_CHECK_VALID(print); | 88 DART_CHECK_VALID(print); |
| 88 result = Dart_SetField(internal_lib, | 89 result = Dart_SetField(internal_lib, |
| 89 Dart_NewStringFromCString("_printClosure"), | 90 Dart_NewStringFromCString("_printClosure"), |
| 90 print); | 91 print); |
| 91 DART_CHECK_VALID(result); | 92 DART_CHECK_VALID(result); |
| 92 | 93 |
| 93 DART_CHECK_VALID(Dart_Invoke( | |
| 94 builtin_lib, Dart_NewStringFromCString("_setupHooks"), 0, nullptr)); | |
| 95 DART_CHECK_VALID(Dart_Invoke( | |
| 96 isolate_lib, Dart_NewStringFromCString("_setupHooks"), 0, nullptr)); | |
| 97 | |
| 98 // Setup the 'scheduleImmediate' closure. | 94 // Setup the 'scheduleImmediate' closure. |
| 99 Dart_Handle schedule_immediate_closure = Dart_Invoke( | 95 Dart_Handle schedule_immediate_closure = Dart_Invoke( |
| 100 isolate_lib, | 96 isolate_lib, |
| 101 Dart_NewStringFromCString("_getIsolateScheduleImmediateClosure"), | 97 Dart_NewStringFromCString("_getIsolateScheduleImmediateClosure"), |
| 102 0, | 98 0, |
| 103 nullptr); | 99 nullptr); |
| 104 Dart_Handle schedule_args[1]; | 100 Dart_Handle schedule_args[1]; |
| 105 schedule_args[0] = schedule_immediate_closure; | 101 schedule_args[0] = schedule_immediate_closure; |
| 106 result = Dart_Invoke( | 102 result = Dart_Invoke( |
| 107 async_lib, | 103 async_lib, |
| 108 Dart_NewStringFromCString("_setScheduleImmediateClosure"), | 104 Dart_NewStringFromCString("_setScheduleImmediateClosure"), |
| 109 1, | 105 1, |
| 110 schedule_args); | 106 schedule_args); |
| 111 DART_CHECK_VALID(result); | 107 DART_CHECK_VALID(result); |
| 112 | 108 |
| 109 // Set the script location. |
| 110 Dart_Handle uri = Dart_NewStringFromUTF8( |
| 111 reinterpret_cast<const uint8_t*>(script_uri.c_str()), |
| 112 script_uri.length()); |
| 113 DART_CHECK_VALID(uri); |
| 114 result = Dart_SetField(builtin_lib, |
| 115 Dart_NewStringFromCString("_rawScript"), |
| 116 uri); |
| 117 DART_CHECK_VALID(result); |
| 118 |
| 113 // Set the base URI. | 119 // Set the base URI. |
| 114 Dart_Handle uri = Dart_NewStringFromUTF8( | 120 uri = Dart_NewStringFromUTF8( |
| 115 reinterpret_cast<const uint8_t*>(base_uri.c_str()), | 121 reinterpret_cast<const uint8_t*>(base_uri.c_str()), |
| 116 base_uri.length()); | 122 base_uri.length()); |
| 117 DART_CHECK_VALID(uri); | 123 DART_CHECK_VALID(uri); |
| 118 result = Dart_SetField(builtin_lib, | 124 result = Dart_SetField(builtin_lib, |
| 119 Dart_NewStringFromCString("_rawUriBase"), | 125 Dart_NewStringFromCString("_rawUriBase"), |
| 120 uri); | 126 uri); |
| 121 DART_CHECK_VALID(result); | 127 DART_CHECK_VALID(result); |
| 122 | 128 |
| 123 // Setup the uriBase with the base uri of the mojo app. | 129 // Setup the uriBase with the base uri of the mojo app. |
| 124 Dart_Handle uri_base = Dart_Invoke( | 130 Dart_Handle uri_base = Dart_Invoke( |
| 125 builtin_lib, | 131 builtin_lib, |
| 126 Dart_NewStringFromCString("_getUriBaseClosure"), | 132 Dart_NewStringFromCString("_getUriBaseClosure"), |
| 127 0, | 133 0, |
| 128 nullptr); | 134 nullptr); |
| 129 DART_CHECK_VALID(uri_base); | 135 DART_CHECK_VALID(uri_base); |
| 130 result = Dart_SetField(core_lib, | 136 result = Dart_SetField(core_lib, |
| 131 Dart_NewStringFromCString("_uriBaseClosure"), | 137 Dart_NewStringFromCString("_uriBaseClosure"), |
| 132 uri_base); | 138 uri_base); |
| 133 DART_CHECK_VALID(result); | 139 DART_CHECK_VALID(result); |
| 140 |
| 141 DART_CHECK_VALID(Dart_Invoke( |
| 142 builtin_lib, Dart_NewStringFromCString("_setupHooks"), 0, nullptr)); |
| 143 DART_CHECK_VALID(Dart_Invoke( |
| 144 isolate_lib, Dart_NewStringFromCString("_setupHooks"), 0, nullptr)); |
| 145 |
| 134 return result; | 146 return result; |
| 135 } | 147 } |
| 136 | 148 |
| 137 static const intptr_t kStartIsolateArgumentsLength = 2; | 149 static const intptr_t kStartIsolateArgumentsLength = 2; |
| 138 | 150 |
| 139 static void SetupStartIsolateArguments( | 151 static void SetupStartIsolateArguments( |
| 140 const DartControllerConfig& config, | 152 const DartControllerConfig& config, |
| 141 Dart_Handle main_closure, | 153 Dart_Handle main_closure, |
| 142 Dart_Handle* start_isolate_args) { | 154 Dart_Handle* start_isolate_args) { |
| 143 // start_isolate_args: | 155 // start_isolate_args: |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 isolate_data->set_library_provider( | 323 isolate_data->set_library_provider( |
| 312 new tonic::DartLibraryProviderNetwork(network_service)); | 324 new tonic::DartLibraryProviderNetwork(network_service)); |
| 313 } else { | 325 } else { |
| 314 isolate_data->set_library_provider( | 326 isolate_data->set_library_provider( |
| 315 new tonic::DartLibraryProviderFiles( | 327 new tonic::DartLibraryProviderFiles( |
| 316 base::FilePath(package_root_str))); | 328 base::FilePath(package_root_str))); |
| 317 } | 329 } |
| 318 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); | 330 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); |
| 319 DART_CHECK_VALID(result); | 331 DART_CHECK_VALID(result); |
| 320 // Prepare builtin and its dependent libraries. | 332 // Prepare builtin and its dependent libraries. |
| 321 result = PrepareBuiltinLibraries(package_root, base_uri); | 333 result = PrepareBuiltinLibraries(package_root, base_uri, script_uri); |
| 322 DART_CHECK_VALID(result); | 334 DART_CHECK_VALID(result); |
| 323 | 335 |
| 324 // Set the handle watcher's control handle in the spawning isolate. | 336 // Set the handle watcher's control handle in the spawning isolate. |
| 325 result = SetHandleWatcherControlHandle(); | 337 result = SetHandleWatcherControlHandle(); |
| 326 DART_CHECK_VALID(result); | 338 DART_CHECK_VALID(result); |
| 327 | 339 |
| 328 if (!use_dart_run_loop) { | 340 if (!use_dart_run_loop) { |
| 329 // Verify that we are being created on a thread with a message loop. | 341 // Verify that we are being created on a thread with a message loop. |
| 330 DCHECK(base::MessageLoop::current()); | 342 DCHECK(base::MessageLoop::current()); |
| 331 // Set the task runner. | 343 // Set the task runner. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 Dart_Handle control_port_value = | 432 Dart_Handle control_port_value = |
| 421 Dart_NewInteger(handle_watcher_producer_handle_); | 433 Dart_NewInteger(handle_watcher_producer_handle_); |
| 422 Dart_Handle result = | 434 Dart_Handle result = |
| 423 Dart_SetField(handle_watcher_type, field_name, control_port_value); | 435 Dart_SetField(handle_watcher_type, field_name, control_port_value); |
| 424 return result; | 436 return result; |
| 425 } | 437 } |
| 426 | 438 |
| 427 Dart_Isolate DartController::IsolateCreateCallback(const char* script_uri, | 439 Dart_Isolate DartController::IsolateCreateCallback(const char* script_uri, |
| 428 const char* main, | 440 const char* main, |
| 429 const char* package_root, | 441 const char* package_root, |
| 430 const char** package_map, | 442 const char* package_config, |
| 431 Dart_IsolateFlags* flags, | 443 Dart_IsolateFlags* flags, |
| 432 void* callback_data, | 444 void* callback_data, |
| 433 char** error) { | 445 char** error) { |
| 446 DCHECK(script_uri != nullptr); |
| 434 auto parent_isolate_data = MojoDartState::Cast(callback_data); | 447 auto parent_isolate_data = MojoDartState::Cast(callback_data); |
| 435 std::string script_uri_string; | 448 std::string script_uri_string(script_uri); |
| 436 std::string package_root_string; | 449 std::string package_root_string; |
| 437 std::string base_uri_string; | 450 std::string base_uri_string; |
| 438 | 451 |
| 439 if (script_uri == nullptr) { | 452 // If it's a file URI, strip the scheme. |
| 453 const char* file_scheme = "file://"; |
| 454 if (StartsWithASCII(script_uri_string, file_scheme, true)) { |
| 455 script_uri_string = script_uri_string.substr(strlen(file_scheme)); |
| 456 } |
| 457 |
| 458 if ((parent_isolate_data != nullptr) && |
| 459 (parent_isolate_data->script_uri() == script_uri_string)) { |
| 440 // We are spawning a function, use the parent isolate's base URI. | 460 // We are spawning a function, use the parent isolate's base URI. |
| 441 if (callback_data == nullptr) { | |
| 442 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | |
| 443 return nullptr; | |
| 444 } | |
| 445 script_uri_string = parent_isolate_data->script_uri(); | |
| 446 base_uri_string = parent_isolate_data->base_uri(); | 461 base_uri_string = parent_isolate_data->base_uri(); |
| 447 } else { | 462 } else { |
| 448 // If we are spawning a URI directly, use the URI as the base URI. | 463 // If we are spawning a URI directly, use the URI as the base URI. |
| 449 script_uri_string = std::string(script_uri); | 464 base_uri_string = std::string(script_uri); |
| 450 base_uri_string = script_uri_string; | |
| 451 } | 465 } |
| 452 | 466 |
| 453 if (package_root == nullptr) { | 467 if (package_root == nullptr) { |
| 454 if (parent_isolate_data != nullptr) { | 468 if (parent_isolate_data != nullptr) { |
| 455 package_root_string = parent_isolate_data->package_root(); | 469 package_root_string = parent_isolate_data->package_root(); |
| 456 } | 470 } |
| 457 } else { | 471 } else { |
| 458 package_root_string = std::string(package_root); | 472 package_root_string = std::string(package_root); |
| 459 } | 473 } |
| 460 // Inherit parameters from parent isolate (if any). | 474 // Inherit parameters from parent isolate (if any). |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 } | 748 } |
| 735 BlockForServiceIsolateLocked(); | 749 BlockForServiceIsolateLocked(); |
| 736 HandleWatcher::StopAll(); | 750 HandleWatcher::StopAll(); |
| 737 Dart_Cleanup(); | 751 Dart_Cleanup(); |
| 738 service_isolate_running_ = false; | 752 service_isolate_running_ = false; |
| 739 initialized_ = false; | 753 initialized_ = false; |
| 740 } | 754 } |
| 741 | 755 |
| 742 } // namespace apps | 756 } // namespace apps |
| 743 } // namespace mojo | 757 } // namespace mojo |
| OLD | NEW |