| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sky/engine/core/script/dart_controller.h" | 5 #include "sky/engine/core/script/dart_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "dart/runtime/bin/embedded_dart_io.h" | 11 #include "dart/runtime/bin/embedded_dart_io.h" |
| 12 #include "dart/runtime/include/dart_mirrors_api.h" | 12 #include "dart/runtime/include/dart_mirrors_api.h" |
| 13 #include "gen/sky/platform/RuntimeEnabledFeatures.h" | 13 #include "gen/sky/platform/RuntimeEnabledFeatures.h" |
| 14 #include "sky/engine/bindings/builtin.h" | 14 #include "sky/engine/bindings/builtin.h" |
| 15 #include "sky/engine/bindings/builtin_natives.h" | 15 #include "sky/engine/bindings/builtin_natives.h" |
| 16 #include "sky/engine/bindings/builtin_sky.h" | 16 #include "sky/engine/bindings/builtin_sky.h" |
| 17 #include "sky/engine/core/app/AbstractModule.h" | 17 #include "sky/engine/core/app/AbstractModule.h" |
| 18 #include "sky/engine/core/app/Module.h" | 18 #include "sky/engine/core/app/Module.h" |
| 19 #include "sky/engine/core/dom/Element.h" | 19 #include "sky/engine/core/dom/Element.h" |
| 20 #include "sky/engine/core/frame/LocalFrame.h" | 20 #include "sky/engine/core/frame/LocalFrame.h" |
| 21 #include "sky/engine/core/html/HTMLScriptElement.h" | |
| 22 #include "sky/engine/core/html/imports/HTMLImport.h" | |
| 23 #include "sky/engine/core/html/imports/HTMLImportChild.h" | |
| 24 #include "sky/engine/core/loader/FrameLoaderClient.h" | 21 #include "sky/engine/core/loader/FrameLoaderClient.h" |
| 25 #include "sky/engine/core/script/dart_debugger.h" | 22 #include "sky/engine/core/script/dart_debugger.h" |
| 26 #include "sky/engine/core/script/dart_library_provider_webview.h" | 23 #include "sky/engine/core/script/dart_library_provider_webview.h" |
| 27 #include "sky/engine/core/script/dart_service_isolate.h" | 24 #include "sky/engine/core/script/dart_service_isolate.h" |
| 28 #include "sky/engine/core/script/dom_dart_state.h" | 25 #include "sky/engine/core/script/dom_dart_state.h" |
| 29 #include "sky/engine/public/platform/Platform.h" | 26 #include "sky/engine/public/platform/Platform.h" |
| 30 #include "sky/engine/tonic/dart_api_scope.h" | 27 #include "sky/engine/tonic/dart_api_scope.h" |
| 31 #include "sky/engine/tonic/dart_class_library.h" | 28 #include "sky/engine/tonic/dart_class_library.h" |
| 32 #include "sky/engine/tonic/dart_dependency_catcher.h" | 29 #include "sky/engine/tonic/dart_dependency_catcher.h" |
| 33 #include "sky/engine/tonic/dart_error.h" | 30 #include "sky/engine/tonic/dart_error.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 62 |
| 66 extern const uint8_t* kDartVmIsolateSnapshotBuffer; | 63 extern const uint8_t* kDartVmIsolateSnapshotBuffer; |
| 67 extern const uint8_t* kDartIsolateSnapshotBuffer; | 64 extern const uint8_t* kDartIsolateSnapshotBuffer; |
| 68 | 65 |
| 69 DartController::DartController() : weak_factory_(this) { | 66 DartController::DartController() : weak_factory_(this) { |
| 70 } | 67 } |
| 71 | 68 |
| 72 DartController::~DartController() { | 69 DartController::~DartController() { |
| 73 } | 70 } |
| 74 | 71 |
| 75 bool DartController::ImportChildLibraries(AbstractModule* module, | |
| 76 Dart_Handle library) { | |
| 77 // If the document has never seen an <import> tag, it won't have an import | |
| 78 // controller, and thus will return null for its root HTMLImport. We could | |
| 79 // remove this null-check by always creating an ImportController. | |
| 80 HTMLImport* root = module->document()->import(); | |
| 81 if (!root) | |
| 82 return true; | |
| 83 | |
| 84 // TODO(abarth): Why doesn't HTMLImport do these casts for us? | |
| 85 for (HTMLImportChild* child = | |
| 86 static_cast<HTMLImportChild*>(root->firstChild()); | |
| 87 child; child = static_cast<HTMLImportChild*>(child->next())) { | |
| 88 if (Element* link = child->link()) { | |
| 89 String name = link->getAttribute(HTMLNames::asAttr); | |
| 90 | |
| 91 Module* child_module = child->module(); | |
| 92 if (!child_module) | |
| 93 continue; | |
| 94 for (const auto& entry : child_module->libraries()) { | |
| 95 if (entry.library()->is_empty()) | |
| 96 continue; | |
| 97 if (LogIfError(Dart_LibraryImportLibrary( | |
| 98 library, entry.library()->dart_value(), | |
| 99 StringToDart(dart_state(), name)))) | |
| 100 return false; | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 Dart_Handle DartController::CreateLibrary(AbstractModule* module, | |
| 108 const String& source, | |
| 109 const TextPosition& textPosition) { | |
| 110 Dart_Handle library = Dart_LoadLibrary( | |
| 111 StringToDart(dart_state(), module->UrlForLibraryAt(textPosition)), | |
| 112 StringToDart(dart_state(), source), textPosition.m_line.zeroBasedInt(), | |
| 113 textPosition.m_column.zeroBasedInt()); | |
| 114 | |
| 115 if (LogIfError(library)) | |
| 116 return nullptr; | |
| 117 | |
| 118 if (!ImportChildLibraries(module, library)) | |
| 119 return nullptr; | |
| 120 | |
| 121 return library; | |
| 122 } | |
| 123 | |
| 124 void DartController::DidLoadMainLibrary(String name) { | 72 void DartController::DidLoadMainLibrary(String name) { |
| 125 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); | 73 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); |
| 126 DartApiScope dart_api_scope; | 74 DartApiScope dart_api_scope; |
| 127 | 75 |
| 128 if (LogIfError(Dart_FinalizeLoading(true))) | 76 if (LogIfError(Dart_FinalizeLoading(true))) |
| 129 return; | 77 return; |
| 130 | 78 |
| 131 Dart_Handle library = Dart_LookupLibrary(StringToDart(dart_state(), name)); | 79 Dart_Handle library = Dart_LookupLibrary(StringToDart(dart_state(), name)); |
| 132 // TODO(eseidel): We need to load a 404 page instead! | 80 // TODO(eseidel): We need to load a 404 page instead! |
| 133 if (LogIfError(library)) | 81 if (LogIfError(library)) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 DartLibraryLoader& loader = dart_state()->library_loader(); | 113 DartLibraryLoader& loader = dart_state()->library_loader(); |
| 166 loader.set_library_provider(library_provider); | 114 loader.set_library_provider(library_provider); |
| 167 | 115 |
| 168 DartDependencyCatcher dependency_catcher(loader); | 116 DartDependencyCatcher dependency_catcher(loader); |
| 169 loader.LoadLibrary(name); | 117 loader.LoadLibrary(name); |
| 170 loader.WaitForDependencies(dependency_catcher.dependencies(), | 118 loader.WaitForDependencies(dependency_catcher.dependencies(), |
| 171 base::Bind(&DartController::DidLoadMainLibrary, | 119 base::Bind(&DartController::DidLoadMainLibrary, |
| 172 weak_factory_.GetWeakPtr(), name)); | 120 weak_factory_.GetWeakPtr(), name)); |
| 173 } | 121 } |
| 174 | 122 |
| 175 void DartController::LoadScriptInModule( | |
| 176 AbstractModule* module, | |
| 177 const String& source, | |
| 178 const TextPosition& position, | |
| 179 const LoadFinishedCallback& finished_callback) { | |
| 180 DartState::Scope scope(dart_state()); | |
| 181 CreateEmptyRootLibraryIfNeeded(); | |
| 182 | |
| 183 DartLibraryLoader& loader = dart_state()->library_loader(); | |
| 184 | |
| 185 if (!library_provider_) { | |
| 186 library_provider_ = adoptPtr(new DartLibraryProviderWebView()); | |
| 187 loader.set_library_provider(library_provider_.get()); | |
| 188 } | |
| 189 | |
| 190 DartDependencyCatcher dependency_catcher(loader); | |
| 191 Dart_Handle library_handle = CreateLibrary(module, source, position); | |
| 192 if (!library_handle) | |
| 193 return finished_callback.Run(nullptr, nullptr); | |
| 194 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); | |
| 195 module->AddLibrary(library, position); | |
| 196 | |
| 197 // TODO(eseidel): Better if the library/module retained its dependencies and | |
| 198 // dependency waiting could be separate from library creation. | |
| 199 dart_state()->library_loader().WaitForDependencies( | |
| 200 dependency_catcher.dependencies(), | |
| 201 base::Bind(finished_callback, module, library)); | |
| 202 } | |
| 203 | |
| 204 void DartController::ExecuteLibraryInModule(AbstractModule* module, | |
| 205 Dart_Handle library, | |
| 206 HTMLScriptElement* script) { | |
| 207 TRACE_EVENT1("sky", "DartController::ExecuteLibraryInModule", | |
| 208 "url", module->url().ascii().toStdString()); | |
| 209 ASSERT(library); | |
| 210 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); | |
| 211 DartApiScope dart_api_scope; | |
| 212 | |
| 213 // Don't continue if we failed to load the module. | |
| 214 if (LogIfError(Dart_FinalizeLoading(true))) | |
| 215 return; | |
| 216 const char* name = module->isApplication() ? "main" : "_init"; | |
| 217 | |
| 218 // main() is required, but init() is not: | |
| 219 // TODO(rmacnak): Dart_LookupFunction won't find re-exports, etc. | |
| 220 Dart_Handle entry = Dart_LookupFunction(library, ToDart(name)); | |
| 221 if (module->isApplication()) { | |
| 222 DartInvokeAppField(library, ToDart(name), 0, nullptr); | |
| 223 return; | |
| 224 } | |
| 225 | |
| 226 if (!Dart_IsFunction(entry)) | |
| 227 return; | |
| 228 | |
| 229 Dart_Handle args[] = { | |
| 230 ToDart(script), | |
| 231 }; | |
| 232 DartInvokeAppField(library, ToDart(name), arraysize(args), args); | |
| 233 } | |
| 234 | |
| 235 static void UnhandledExceptionCallback(Dart_Handle error) { | 123 static void UnhandledExceptionCallback(Dart_Handle error) { |
| 236 LOG(ERROR) << Dart_GetError(error); | 124 LOG(ERROR) << Dart_GetError(error); |
| 237 } | 125 } |
| 238 | 126 |
| 239 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 127 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 240 Dart_Handle library, | 128 Dart_Handle library, |
| 241 Dart_Handle url) { | 129 Dart_Handle url) { |
| 242 return DartLibraryLoader::HandleLibraryTag(tag, library, url); | 130 return DartLibraryLoader::HandleLibraryTag(tag, library, url); |
| 243 } | 131 } |
| 244 | 132 |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 nullptr, // Isolate interrupt callback. | 317 nullptr, // Isolate interrupt callback. |
| 430 UnhandledExceptionCallback, IsolateShutdownCallback, | 318 UnhandledExceptionCallback, IsolateShutdownCallback, |
| 431 // File IO callbacks. | 319 // File IO callbacks. |
| 432 nullptr, nullptr, nullptr, nullptr, nullptr)); | 320 nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 433 // Wait for load port- ensures handle watcher and service isolates are | 321 // Wait for load port- ensures handle watcher and service isolates are |
| 434 // running. | 322 // running. |
| 435 Dart_ServiceWaitForLoadPort(); | 323 Dart_ServiceWaitForLoadPort(); |
| 436 } | 324 } |
| 437 | 325 |
| 438 } // namespace blink | 326 } // namespace blink |
| OLD | NEW |