| 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 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace dart { | 18 namespace dart { |
| 19 | 19 |
| 20 extern const uint8_t* snapshot_buffer; | 20 extern const uint8_t* snapshot_buffer; |
| 21 | 21 |
| 22 const char* kDartScheme = "dart:"; | 22 const char* kDartScheme = "dart:"; |
| 23 const char* kMojoScheme = "mojo:"; |
| 23 const char* kAsyncLibURL = "dart:async"; | 24 const char* kAsyncLibURL = "dart:async"; |
| 24 const char* kInternalLibURL = "dart:_internal"; | 25 const char* kInternalLibURL = "dart:_internal"; |
| 25 const char* kIsolateLibURL = "dart:isolate"; | 26 const char* kIsolateLibURL = "dart:isolate"; |
| 26 const char* kIOLibURL = "dart:io"; | 27 const char* kIOLibURL = "dart:io"; |
| 27 | 28 |
| 28 static bool IsDartSchemeURL(const char* url_name) { | 29 static bool IsDartSchemeURL(const char* url_name) { |
| 29 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 30 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| 30 // If the URL starts with "dart:" then it is considered as a special | 31 // If the URL starts with "dart:" then it is considered as a special |
| 31 // library URL which is handled differently from other URLs. | 32 // library URL which is handled differently from other URLs. |
| 32 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); | 33 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); |
| 33 } | 34 } |
| 34 | 35 |
| 36 static bool IsMojoSchemeURL(const char* url_name) { |
| 37 static const intptr_t kMojoSchemeLen = strlen(kMojoScheme); |
| 38 // If the URL starts with "mojo:" then it is considered as a special |
| 39 // library URL which is handled differently from other URLs. |
| 40 return (strncmp(url_name, kMojoScheme, kMojoSchemeLen) == 0); |
| 41 } |
| 42 |
| 35 static bool IsServiceIsolateURL(const char* url_name) { | 43 static bool IsServiceIsolateURL(const char* url_name) { |
| 36 if (url_name == nullptr) { | 44 if (url_name == nullptr) { |
| 37 return false; | 45 return false; |
| 38 } | 46 } |
| 39 static const intptr_t kServiceIsolateNameLen = | 47 static const intptr_t kServiceIsolateNameLen = |
| 40 strlen(DART_VM_SERVICE_ISOLATE_NAME); | 48 strlen(DART_VM_SERVICE_ISOLATE_NAME); |
| 41 return (strncmp(url_name, | 49 return (strncmp(url_name, |
| 42 DART_VM_SERVICE_ISOLATE_NAME, | 50 DART_VM_SERVICE_ISOLATE_NAME, |
| 43 kServiceIsolateNameLen) == 0); | 51 kServiceIsolateNameLen) == 0); |
| 44 } | 52 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 bool is_io_library = IsDartIOLibURL(library_url_string); | 112 bool is_io_library = IsDartIOLibURL(library_url_string); |
| 105 | 113 |
| 106 // Handle IO library. | 114 // Handle IO library. |
| 107 if (is_io_library) { | 115 if (is_io_library) { |
| 108 return Builtin::NewError( | 116 return Builtin::NewError( |
| 109 "The built-in io library is not available in this embedding: %s.\n", | 117 "The built-in io library is not available in this embedding: %s.\n", |
| 110 library_url_string); | 118 library_url_string); |
| 111 } | 119 } |
| 112 | 120 |
| 113 // Handle URI canonicalization requests. | 121 // Handle URI canonicalization requests. |
| 122 const char* url_string = nullptr; |
| 123 result = Dart_StringToCString(url, &url_string); |
| 114 if (tag == Dart_kCanonicalizeUrl) { | 124 if (tag == Dart_kCanonicalizeUrl) { |
| 115 const char* url_string = nullptr; | |
| 116 result = Dart_StringToCString(url, &url_string); | |
| 117 if (Dart_IsError(result)) { | 125 if (Dart_IsError(result)) { |
| 118 return result; | 126 return result; |
| 119 } | 127 } |
| 120 bool is_dart_scheme_url = IsDartSchemeURL(url_string); | 128 const bool is_internal_scheme_url = |
| 121 // If this is a Dart Scheme URL then it is not modified as it will be | 129 IsDartSchemeURL(url_string) || IsMojoSchemeURL(url_string); |
| 122 // handled internally. | 130 // If this is a Dart Scheme URL, or a Mojo Scheme URL, then it is not |
| 123 if (is_dart_scheme_url) { | 131 // modified as it will be handled internally. |
| 132 if (is_internal_scheme_url) { |
| 124 return url; | 133 return url; |
| 125 } | 134 } |
| 126 // Resolve the url within the context of the library's URL. | 135 // Resolve the url within the context of the library's URL. |
| 127 Dart_Handle builtin_lib = | 136 Dart_Handle builtin_lib = |
| 128 Builtin::GetLibrary(Builtin::kBuiltinLibrary); | 137 Builtin::GetLibrary(Builtin::kBuiltinLibrary); |
| 129 return ResolveUri(library_url, url, builtin_lib); | 138 return ResolveUri(library_url, url, builtin_lib); |
| 130 } | 139 } |
| 131 | 140 |
| 141 if (tag == Dart_kImportTag) { |
| 142 if (IsMojoSchemeURL(url_string)) { |
| 143 Dart_Handle library = Dart_LookupLibrary(url); |
| 144 DART_CHECK_VALID(library); |
| 145 return library; |
| 146 } |
| 147 } |
| 148 |
| 132 Dart_Handle builtin_lib = | 149 Dart_Handle builtin_lib = |
| 133 Builtin::GetLibrary(Builtin::kBuiltinLibrary); | 150 Builtin::GetLibrary(Builtin::kBuiltinLibrary); |
| 134 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | 151 // Handle 'import' or 'part' requests for all other URIs. Call dart code to |
| 135 // read the source code asynchronously. | 152 // read the source code asynchronously. |
| 136 return LoadDataAsync_Invoke( | 153 return LoadDataAsync_Invoke( |
| 137 Dart_NewInteger(tag), url, library_url, builtin_lib, Dart_Null()); | 154 Dart_NewInteger(tag), url, library_url, builtin_lib, Dart_Null()); |
| 138 } | 155 } |
| 139 | 156 |
| 140 static Dart_Handle SetWorkingDirectory(Dart_Handle builtin_lib) { | 157 static Dart_Handle SetWorkingDirectory(Dart_Handle builtin_lib) { |
| 141 base::FilePath current_dir; | 158 base::FilePath current_dir; |
| 142 PathService::Get(base::DIR_CURRENT, ¤t_dir); | 159 PathService::Get(base::DIR_CURRENT, ¤t_dir); |
| 143 | 160 |
| 144 const int kNumArgs = 1; | 161 const int kNumArgs = 1; |
| 145 Dart_Handle dart_args[kNumArgs]; | 162 Dart_Handle dart_args[kNumArgs]; |
| 146 std::string current_dir_string = current_dir.AsUTF8Unsafe(); | 163 std::string current_dir_string = current_dir.AsUTF8Unsafe(); |
| 147 dart_args[0] = Dart_NewStringFromUTF8( | 164 dart_args[0] = Dart_NewStringFromUTF8( |
| 148 reinterpret_cast<const uint8_t*>(current_dir_string.data()), | 165 reinterpret_cast<const uint8_t*>(current_dir_string.data()), |
| 149 current_dir_string.length()); | 166 current_dir_string.length()); |
| 150 return Dart_Invoke(builtin_lib, | 167 return Dart_Invoke(builtin_lib, |
| 151 Dart_NewStringFromCString("_setWorkingDirectory"), | 168 Dart_NewStringFromCString("_setWorkingDirectory"), |
| 152 kNumArgs, | 169 kNumArgs, |
| 153 dart_args); | 170 dart_args); |
| 154 } | 171 } |
| 155 | 172 |
| 156 static Dart_Handle PrepareScriptForLoading(const std::string& package_root, | 173 static Dart_Handle PrepareScriptForLoading(const std::string& package_root, |
| 157 Dart_Handle builtin_lib) { | 174 Dart_Handle builtin_lib) { |
| 158 // First ensure all required libraries are available. | 175 // First ensure all required libraries are available. |
| 159 Dart_Handle url = Dart_NewStringFromCString(kAsyncLibURL); | 176 Dart_Handle url = Dart_NewStringFromCString(kInternalLibURL); |
| 177 DART_CHECK_VALID(url); |
| 178 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
| 179 DART_CHECK_VALID(internal_lib); |
| 180 url = Dart_NewStringFromCString(kAsyncLibURL); |
| 160 DART_CHECK_VALID(url); | 181 DART_CHECK_VALID(url); |
| 161 Dart_Handle async_lib = Dart_LookupLibrary(url); | 182 Dart_Handle async_lib = Dart_LookupLibrary(url); |
| 162 DART_CHECK_VALID(async_lib); | 183 DART_CHECK_VALID(async_lib); |
| 163 url = Dart_NewStringFromCString(kIsolateLibURL); | 184 url = Dart_NewStringFromCString(kIsolateLibURL); |
| 164 DART_CHECK_VALID(url); | 185 DART_CHECK_VALID(url); |
| 165 Dart_Handle isolate_lib = Dart_LookupLibrary(url); | 186 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
| 166 DART_CHECK_VALID(isolate_lib); | 187 DART_CHECK_VALID(isolate_lib); |
| 167 Dart_Handle mojo_core_lib = | 188 Dart_Handle mojo_core_lib = |
| 168 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); | 189 Builtin::GetLibrary(Builtin::kMojoCoreLibrary); |
| 169 DART_CHECK_VALID(mojo_core_lib); | 190 DART_CHECK_VALID(mojo_core_lib); |
| 170 | 191 |
| 171 // We need to ensure that all the scripts loaded so far are finalized | 192 // We need to ensure that all the scripts loaded so far are finalized |
| 172 // as we are about to invoke some Dart code below to setup closures. | 193 // as we are about to invoke some Dart code below to setup closures. |
| 173 Dart_Handle result = Dart_FinalizeLoading(false); | 194 Dart_Handle result = Dart_FinalizeLoading(false); |
| 174 DART_CHECK_VALID(result); | 195 DART_CHECK_VALID(result); |
| 175 | 196 |
| 197 // Import dart:_internal into mojo:builtin for setting up hooks. |
| 198 result = Dart_LibraryImportLibrary(builtin_lib, internal_lib, Dart_Null()); |
| 199 DART_CHECK_VALID(result); |
| 200 |
| 176 // Setup the internal library's 'internalPrint' function. | 201 // Setup the internal library's 'internalPrint' function. |
| 177 Dart_Handle print = Dart_Invoke(builtin_lib, | 202 Dart_Handle print = Dart_Invoke(builtin_lib, |
| 178 Dart_NewStringFromCString("_getPrintClosure"), | 203 Dart_NewStringFromCString("_getPrintClosure"), |
| 179 0, | 204 0, |
| 180 nullptr); | 205 nullptr); |
| 181 DART_CHECK_VALID(print); | 206 DART_CHECK_VALID(print); |
| 182 url = Dart_NewStringFromCString(kInternalLibURL); | |
| 183 DART_CHECK_VALID(url); | |
| 184 Dart_Handle internal_lib = Dart_LookupLibrary(url); | |
| 185 DART_CHECK_VALID(internal_lib); | |
| 186 result = Dart_SetField(internal_lib, | 207 result = Dart_SetField(internal_lib, |
| 187 Dart_NewStringFromCString("_printClosure"), | 208 Dart_NewStringFromCString("_printClosure"), |
| 188 print); | 209 print); |
| 189 DART_CHECK_VALID(result); | 210 DART_CHECK_VALID(result); |
| 190 | 211 |
| 191 DART_CHECK_VALID(Dart_Invoke( | 212 DART_CHECK_VALID(Dart_Invoke( |
| 192 builtin_lib, Dart_NewStringFromCString("_setupHooks"), 0, NULL)); | 213 builtin_lib, Dart_NewStringFromCString("_setupHooks"), 0, NULL)); |
| 193 DART_CHECK_VALID(Dart_Invoke( | 214 DART_CHECK_VALID(Dart_Invoke( |
| 194 isolate_lib, Dart_NewStringFromCString("_setupHooks"), 0, NULL)); | 215 isolate_lib, Dart_NewStringFromCString("_setupHooks"), 0, NULL)); |
| 195 | 216 |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 | 714 |
| 694 Dart_ExitScope(); | 715 Dart_ExitScope(); |
| 695 Dart_ShutdownIsolate(); | 716 Dart_ShutdownIsolate(); |
| 696 Dart_Cleanup(); | 717 Dart_Cleanup(); |
| 697 root_isolate_ = nullptr; | 718 root_isolate_ = nullptr; |
| 698 initialized_ = false; | 719 initialized_ = false; |
| 699 } | 720 } |
| 700 | 721 |
| 701 } // namespace apps | 722 } // namespace apps |
| 702 } // namespace mojo | 723 } // namespace mojo |
| OLD | NEW |