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/callback.h" | 5 #include "base/callback.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
10 #include "crypto/random.h" | 10 #include "crypto/random.h" |
11 #include "dart/runtime/include/dart_api.h" | 11 #include "dart/runtime/include/dart_api.h" |
12 #include "dart/runtime/include/dart_native_api.h" | 12 #include "dart/runtime/include/dart_native_api.h" |
13 #include "mojo/dart/embedder/builtin.h" | 13 #include "mojo/dart/embedder/builtin.h" |
14 #include "mojo/dart/embedder/dart_controller.h" | 14 #include "mojo/dart/embedder/dart_controller.h" |
15 #include "mojo/dart/embedder/isolate_data.h" | 15 #include "mojo/dart/embedder/isolate_data.h" |
16 #include "mojo/public/c/system/core.h" | 16 #include "mojo/public/c/system/core.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace dart { | 19 namespace dart { |
20 | 20 |
21 extern const uint8_t* snapshot_buffer; | 21 extern const uint8_t* snapshot_buffer; |
22 | 22 |
23 const char* kDartScheme = "dart:"; | 23 const char* kDartScheme = "dart:"; |
24 const char* kMojoScheme = "mojo:"; | |
25 const char* kAsyncLibURL = "dart:async"; | 24 const char* kAsyncLibURL = "dart:async"; |
26 const char* kInternalLibURL = "dart:_internal"; | 25 const char* kInternalLibURL = "dart:_internal"; |
27 const char* kIsolateLibURL = "dart:isolate"; | 26 const char* kIsolateLibURL = "dart:isolate"; |
28 const char* kIOLibURL = "dart:io"; | 27 const char* kIOLibURL = "dart:io"; |
29 const char* kCoreLibURL = "dart:core"; | 28 const char* kCoreLibURL = "dart:core"; |
30 | 29 |
31 static bool IsDartSchemeURL(const char* url_name) { | 30 static bool IsDartSchemeURL(const char* url_name) { |
32 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 31 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
33 // If the URL starts with "dart:" then it is considered as a special | 32 // If the URL starts with "dart:" then it is considered as a special |
34 // library URL which is handled differently from other URLs. | 33 // library URL which is handled differently from other URLs. |
35 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); | 34 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); |
36 } | 35 } |
37 | 36 |
38 static bool IsMojoSchemeURL(const char* url_name) { | |
39 static const intptr_t kMojoSchemeLen = strlen(kMojoScheme); | |
40 // If the URL starts with "mojo:" then it is considered as a special | |
41 // library URL which is handled differently from other URLs. | |
42 return (strncmp(url_name, kMojoScheme, kMojoSchemeLen) == 0); | |
43 } | |
44 | |
45 static bool IsServiceIsolateURL(const char* url_name) { | 37 static bool IsServiceIsolateURL(const char* url_name) { |
46 if (url_name == nullptr) { | 38 if (url_name == nullptr) { |
47 return false; | 39 return false; |
48 } | 40 } |
49 static const intptr_t kServiceIsolateNameLen = | 41 static const intptr_t kServiceIsolateNameLen = |
50 strlen(DART_VM_SERVICE_ISOLATE_NAME); | 42 strlen(DART_VM_SERVICE_ISOLATE_NAME); |
51 return (strncmp(url_name, | 43 return (strncmp(url_name, |
52 DART_VM_SERVICE_ISOLATE_NAME, | 44 DART_VM_SERVICE_ISOLATE_NAME, |
53 kServiceIsolateNameLen) == 0); | 45 kServiceIsolateNameLen) == 0); |
54 } | 46 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 library_url_string); | 112 library_url_string); |
121 } | 113 } |
122 | 114 |
123 // Handle URI canonicalization requests. | 115 // Handle URI canonicalization requests. |
124 const char* url_string = nullptr; | 116 const char* url_string = nullptr; |
125 result = Dart_StringToCString(url, &url_string); | 117 result = Dart_StringToCString(url, &url_string); |
126 if (tag == Dart_kCanonicalizeUrl) { | 118 if (tag == Dart_kCanonicalizeUrl) { |
127 if (Dart_IsError(result)) { | 119 if (Dart_IsError(result)) { |
128 return result; | 120 return result; |
129 } | 121 } |
130 const bool is_internal_scheme_url = | 122 const bool is_internal_scheme_url = IsDartSchemeURL(url_string); |
131 IsDartSchemeURL(url_string) || IsMojoSchemeURL(url_string); | |
132 // If this is a Dart Scheme URL, or a Mojo Scheme URL, then it is not | 123 // If this is a Dart Scheme URL, or a Mojo Scheme URL, then it is not |
133 // modified as it will be handled internally. | 124 // modified as it will be handled internally. |
134 if (is_internal_scheme_url) { | 125 if (is_internal_scheme_url) { |
135 return url; | 126 return url; |
136 } | 127 } |
137 // Resolve the url within the context of the library's URL. | 128 // Resolve the url within the context of the library's URL. |
138 Dart_Handle builtin_lib = | 129 Dart_Handle builtin_lib = |
139 Builtin::GetLibrary(Builtin::kBuiltinLibrary); | 130 Builtin::GetLibrary(Builtin::kBuiltinLibrary); |
140 return ResolveUri(library_url, url, builtin_lib); | 131 return ResolveUri(library_url, url, builtin_lib); |
141 } | 132 } |
142 | 133 |
143 if (tag == Dart_kImportTag) { | |
144 if (IsMojoSchemeURL(url_string)) { | |
145 Dart_Handle library = Dart_LookupLibrary(url); | |
146 DART_CHECK_VALID(library); | |
147 return library; | |
148 } | |
149 } | |
150 | |
151 Dart_Handle builtin_lib = | 134 Dart_Handle builtin_lib = |
152 Builtin::GetLibrary(Builtin::kBuiltinLibrary); | 135 Builtin::GetLibrary(Builtin::kBuiltinLibrary); |
153 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | 136 // Handle 'import' or 'part' requests for all other URIs. Call dart code to |
154 // read the source code asynchronously. | 137 // read the source code asynchronously. |
155 return LoadDataAsync_Invoke( | 138 return LoadDataAsync_Invoke( |
156 Dart_NewInteger(tag), url, library_url, builtin_lib, Dart_Null()); | 139 Dart_NewInteger(tag), url, library_url, builtin_lib, Dart_Null()); |
157 } | 140 } |
158 | 141 |
159 static Dart_Handle SetWorkingDirectory(Dart_Handle builtin_lib) { | 142 static Dart_Handle SetWorkingDirectory(Dart_Handle builtin_lib) { |
160 base::FilePath current_dir; | 143 base::FilePath current_dir; |
(...skipping 23 matching lines...) Expand all Loading... |
184 Dart_Handle core_lib = Dart_LookupLibrary(url); | 167 Dart_Handle core_lib = Dart_LookupLibrary(url); |
185 DART_CHECK_VALID(internal_lib); | 168 DART_CHECK_VALID(internal_lib); |
186 url = Dart_NewStringFromCString(kAsyncLibURL); | 169 url = Dart_NewStringFromCString(kAsyncLibURL); |
187 DART_CHECK_VALID(url); | 170 DART_CHECK_VALID(url); |
188 Dart_Handle async_lib = Dart_LookupLibrary(url); | 171 Dart_Handle async_lib = Dart_LookupLibrary(url); |
189 DART_CHECK_VALID(async_lib); | 172 DART_CHECK_VALID(async_lib); |
190 url = Dart_NewStringFromCString(kIsolateLibURL); | 173 url = Dart_NewStringFromCString(kIsolateLibURL); |
191 DART_CHECK_VALID(url); | 174 DART_CHECK_VALID(url); |
192 Dart_Handle isolate_lib = Dart_LookupLibrary(url); | 175 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
193 DART_CHECK_VALID(isolate_lib); | 176 DART_CHECK_VALID(isolate_lib); |
194 Dart_Handle mojo_core_lib = | 177 Dart_Handle mojo_internal_lib = |
195 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); | 178 Builtin::GetLibrary(Builtin::kMojoInternalLibrary); |
196 DART_CHECK_VALID(mojo_core_lib); | 179 DART_CHECK_VALID(mojo_internal_lib); |
197 | 180 |
198 // We need to ensure that all the scripts loaded so far are finalized | 181 // We need to ensure that all the scripts loaded so far are finalized |
199 // as we are about to invoke some Dart code below to setup closures. | 182 // as we are about to invoke some Dart code below to setup closures. |
200 Dart_Handle result = Dart_FinalizeLoading(false); | 183 Dart_Handle result = Dart_FinalizeLoading(false); |
201 DART_CHECK_VALID(result); | 184 DART_CHECK_VALID(result); |
202 | 185 |
203 // Import dart:_internal into dart:mojo.builtin for setting up hooks. | 186 // Import dart:_internal into dart:mojo.builtin for setting up hooks. |
204 result = Dart_LibraryImportLibrary(builtin_lib, internal_lib, Dart_Null()); | 187 result = Dart_LibraryImportLibrary(builtin_lib, internal_lib, Dart_Null()); |
205 DART_CHECK_VALID(result); | 188 DART_CHECK_VALID(result); |
206 | 189 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 | 296 |
314 DPCHECK(!Dart_IsServiceIsolate(isolate)); | 297 DPCHECK(!Dart_IsServiceIsolate(isolate)); |
315 Dart_EnterScope(); | 298 Dart_EnterScope(); |
316 | 299 |
317 Dart_IsolateSetStrictCompilation(strict_compilation); | 300 Dart_IsolateSetStrictCompilation(strict_compilation); |
318 | 301 |
319 // Setup the native resolvers for the builtin libraries as they are not set | 302 // Setup the native resolvers for the builtin libraries as they are not set |
320 // up when the snapshot is read. | 303 // up when the snapshot is read. |
321 CHECK(snapshot_buffer != nullptr); | 304 CHECK(snapshot_buffer != nullptr); |
322 Builtin::PrepareLibrary(Builtin::kBuiltinLibrary); | 305 Builtin::PrepareLibrary(Builtin::kBuiltinLibrary); |
323 Builtin::PrepareLibrary(Builtin::kMojoCoreLibrary); | 306 Builtin::PrepareLibrary(Builtin::kMojoInternalLibrary); |
324 | 307 |
325 if (!callbacks.create.is_null()) { | 308 if (!callbacks.create.is_null()) { |
326 callbacks.create.Run(script_uri.c_str(), | 309 callbacks.create.Run(script_uri.c_str(), |
327 "main", | 310 "main", |
328 package_root.c_str(), | 311 package_root.c_str(), |
329 isolate_data, | 312 isolate_data, |
330 error); | 313 error); |
331 } | 314 } |
332 | 315 |
333 // Set up the library tag handler for this isolate. | 316 // Set up the library tag handler for this isolate. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 config.error); | 479 config.error); |
497 if (isolate == nullptr) { | 480 if (isolate == nullptr) { |
498 return false; | 481 return false; |
499 } | 482 } |
500 | 483 |
501 Dart_EnterIsolate(isolate); | 484 Dart_EnterIsolate(isolate); |
502 Dart_Handle result; | 485 Dart_Handle result; |
503 Dart_EnterScope(); | 486 Dart_EnterScope(); |
504 | 487 |
505 // Start the MojoHandleWatcher. | 488 // Start the MojoHandleWatcher. |
506 Dart_Handle mojo_core_lib = | 489 Dart_Handle mojo_internal_lib = |
507 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); | 490 Builtin::GetLibrary(Builtin::kMojoInternalLibrary); |
508 DART_CHECK_VALID(mojo_core_lib); | 491 DART_CHECK_VALID(mojo_internal_lib); |
509 Dart_Handle handle_watcher_type = Dart_GetType( | 492 Dart_Handle handle_watcher_type = |
510 mojo_core_lib, | 493 Dart_GetType(mojo_internal_lib, |
511 Dart_NewStringFromCString("MojoHandleWatcher"), | 494 Dart_NewStringFromCString("MojoHandleWatcher"), 0, nullptr); |
512 0, | |
513 nullptr); | |
514 DART_CHECK_VALID(handle_watcher_type); | 495 DART_CHECK_VALID(handle_watcher_type); |
515 result = Dart_Invoke( | 496 result = Dart_Invoke( |
516 handle_watcher_type, | 497 handle_watcher_type, |
517 Dart_NewStringFromCString("_start"), | 498 Dart_NewStringFromCString("_start"), |
518 0, | 499 0, |
519 nullptr); | 500 nullptr); |
520 DART_CHECK_VALID(result); | 501 DART_CHECK_VALID(result); |
521 | 502 |
522 // RunLoop until the handle watcher isolate is spun-up. | 503 // RunLoop until the handle watcher isolate is spun-up. |
523 result = Dart_RunLoop(); | 504 result = Dart_RunLoop(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 Dart_Cleanup(); | 588 Dart_Cleanup(); |
608 initialized_ = false; | 589 initialized_ = false; |
609 return false; | 590 return false; |
610 } | 591 } |
611 | 592 |
612 Dart_EnterIsolate(root_isolate_); | 593 Dart_EnterIsolate(root_isolate_); |
613 Dart_Handle result; | 594 Dart_Handle result; |
614 Dart_EnterScope(); | 595 Dart_EnterScope(); |
615 | 596 |
616 // Start the MojoHandleWatcher. | 597 // Start the MojoHandleWatcher. |
617 Dart_Handle mojo_core_lib = | 598 Dart_Handle mojo_internal_lib = |
618 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); | 599 Builtin::GetLibrary(Builtin::kMojoInternalLibrary); |
619 DART_CHECK_VALID(mojo_core_lib); | 600 DART_CHECK_VALID(mojo_internal_lib); |
620 Dart_Handle handle_watcher_type = Dart_GetType( | 601 Dart_Handle handle_watcher_type = |
621 mojo_core_lib, | 602 Dart_GetType(mojo_internal_lib, |
622 Dart_NewStringFromCString("MojoHandleWatcher"), | 603 Dart_NewStringFromCString("MojoHandleWatcher"), 0, nullptr); |
623 0, | |
624 nullptr); | |
625 DART_CHECK_VALID(handle_watcher_type); | 604 DART_CHECK_VALID(handle_watcher_type); |
626 result = Dart_Invoke( | 605 result = Dart_Invoke( |
627 handle_watcher_type, | 606 handle_watcher_type, |
628 Dart_NewStringFromCString("_start"), | 607 Dart_NewStringFromCString("_start"), |
629 0, | 608 0, |
630 nullptr); | 609 nullptr); |
631 DART_CHECK_VALID(result); | 610 DART_CHECK_VALID(result); |
632 | 611 |
633 // RunLoop until the handle watcher isolate is spun-up. | 612 // RunLoop until the handle watcher isolate is spun-up. |
634 result = Dart_RunLoop(); | 613 result = Dart_RunLoop(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 return true; | 686 return true; |
708 } | 687 } |
709 | 688 |
710 void DartController::Shutdown() { | 689 void DartController::Shutdown() { |
711 CHECK(root_isolate_ != nullptr); | 690 CHECK(root_isolate_ != nullptr); |
712 Dart_EnterIsolate(root_isolate_); | 691 Dart_EnterIsolate(root_isolate_); |
713 Dart_Handle result; | 692 Dart_Handle result; |
714 Dart_EnterScope(); | 693 Dart_EnterScope(); |
715 | 694 |
716 // Stop the MojoHandleWatcher. | 695 // Stop the MojoHandleWatcher. |
717 Dart_Handle mojo_core_lib = | 696 Dart_Handle mojo_internal_lib = |
718 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); | 697 Builtin::GetLibrary(Builtin::kMojoInternalLibrary); |
719 DART_CHECK_VALID(mojo_core_lib); | 698 DART_CHECK_VALID(mojo_internal_lib); |
720 Dart_Handle handle_watcher_type = Dart_GetType( | 699 Dart_Handle handle_watcher_type = |
721 mojo_core_lib, | 700 Dart_GetType(mojo_internal_lib, |
722 Dart_NewStringFromCString("MojoHandleWatcher"), | 701 Dart_NewStringFromCString("MojoHandleWatcher"), 0, nullptr); |
723 0, | |
724 nullptr); | |
725 DART_CHECK_VALID(handle_watcher_type); | 702 DART_CHECK_VALID(handle_watcher_type); |
726 result = Dart_Invoke( | 703 result = Dart_Invoke( |
727 handle_watcher_type, | 704 handle_watcher_type, |
728 Dart_NewStringFromCString("_stop"), | 705 Dart_NewStringFromCString("_stop"), |
729 0, | 706 0, |
730 nullptr); | 707 nullptr); |
731 DART_CHECK_VALID(result); | 708 DART_CHECK_VALID(result); |
732 | 709 |
733 result = Dart_RunLoop(); | 710 result = Dart_RunLoop(); |
734 DART_CHECK_VALID(result); | 711 DART_CHECK_VALID(result); |
735 | 712 |
736 Dart_ExitScope(); | 713 Dart_ExitScope(); |
737 Dart_ShutdownIsolate(); | 714 Dart_ShutdownIsolate(); |
738 Dart_Cleanup(); | 715 Dart_Cleanup(); |
739 root_isolate_ = nullptr; | 716 root_isolate_ = nullptr; |
740 initialized_ = false; | 717 initialized_ = false; |
741 } | 718 } |
742 | 719 |
743 } // namespace apps | 720 } // namespace apps |
744 } // namespace mojo | 721 } // namespace mojo |
OLD | NEW |