| 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" | 21 #include "sky/engine/core/html/HTMLScriptElement.h" |
| 22 #include "sky/engine/core/html/imports/HTMLImport.h" | 22 #include "sky/engine/core/html/imports/HTMLImport.h" |
| 23 #include "sky/engine/core/html/imports/HTMLImportChild.h" | 23 #include "sky/engine/core/html/imports/HTMLImportChild.h" |
| 24 #include "sky/engine/core/loader/FrameLoaderClient.h" | 24 #include "sky/engine/core/loader/FrameLoaderClient.h" |
| 25 #include "sky/engine/core/script/dart_debugger.h" | 25 #include "sky/engine/core/script/dart_debugger.h" |
| 26 #include "sky/engine/core/script/dart_library_provider_network.h" | 26 #include "sky/engine/core/script/dart_library_provider_webview.h" |
| 27 #include "sky/engine/core/script/dart_service_isolate.h" | 27 #include "sky/engine/core/script/dart_service_isolate.h" |
| 28 #include "sky/engine/core/script/dom_dart_state.h" | 28 #include "sky/engine/core/script/dom_dart_state.h" |
| 29 #include "sky/engine/public/platform/Platform.h" | 29 #include "sky/engine/public/platform/Platform.h" |
| 30 #include "sky/engine/tonic/dart_api_scope.h" | 30 #include "sky/engine/tonic/dart_api_scope.h" |
| 31 #include "sky/engine/tonic/dart_class_library.h" | 31 #include "sky/engine/tonic/dart_class_library.h" |
| 32 #include "sky/engine/tonic/dart_dependency_catcher.h" | 32 #include "sky/engine/tonic/dart_dependency_catcher.h" |
| 33 #include "sky/engine/tonic/dart_error.h" | 33 #include "sky/engine/tonic/dart_error.h" |
| 34 #include "sky/engine/tonic/dart_gc_controller.h" | 34 #include "sky/engine/tonic/dart_gc_controller.h" |
| 35 #include "sky/engine/tonic/dart_invoke.h" | 35 #include "sky/engine/tonic/dart_invoke.h" |
| 36 #include "sky/engine/tonic/dart_isolate_scope.h" | 36 #include "sky/engine/tonic/dart_isolate_scope.h" |
| 37 #include "sky/engine/tonic/dart_library_loader.h" | 37 #include "sky/engine/tonic/dart_library_loader.h" |
| 38 #include "sky/engine/tonic/dart_snapshot_loader.h" | 38 #include "sky/engine/tonic/dart_snapshot_loader.h" |
| 39 #include "sky/engine/tonic/dart_state.h" | 39 #include "sky/engine/tonic/dart_state.h" |
| 40 #include "sky/engine/tonic/dart_wrappable.h" | 40 #include "sky/engine/tonic/dart_wrappable.h" |
| 41 #include "sky/engine/wtf/text/TextPosition.h" | 41 #include "sky/engine/wtf/text/TextPosition.h" |
| 42 | 42 |
| 43 namespace blink { | 43 namespace blink { |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 void CreateEmptyRootLibraryIfNeeded() { | 46 void CreateEmptyRootLibraryIfNeeded() { |
| 47 if (Dart_IsNull(Dart_RootLibrary())) { | 47 if (Dart_IsNull(Dart_RootLibrary())) { |
| 48 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), Dart_EmptyString(), | 48 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), Dart_EmptyString(), |
| 49 0, 0); | 49 0, 0); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 PassOwnPtr<DartLibraryProviderNetwork::PrefetchedLibrary> | |
| 54 CreatePrefetchedLibraryIfNeeded(const KURL& url, | |
| 55 mojo::URLResponsePtr response) { | |
| 56 OwnPtr<DartLibraryProviderNetwork::PrefetchedLibrary> prefetched; | |
| 57 if (response && response->status_code == 200) { | |
| 58 prefetched = adoptPtr(new DartLibraryProviderNetwork::PrefetchedLibrary()); | |
| 59 prefetched->name = url.string(); | |
| 60 prefetched->pipe = response->body.Pass(); | |
| 61 } | |
| 62 return prefetched.release(); | |
| 63 } | |
| 64 | |
| 65 } // namespace | 53 } // namespace |
| 66 | 54 |
| 67 #if ENABLE(DART_STRICT) | 55 #if ENABLE(DART_STRICT) |
| 68 static const char* kCheckedModeArgs[] = {"--enable_asserts", | 56 static const char* kCheckedModeArgs[] = {"--enable_asserts", |
| 69 "--enable_type_checks", | 57 "--enable_type_checks", |
| 70 "--error_on_bad_type", | 58 "--error_on_bad_type", |
| 71 "--error_on_bad_override", | 59 "--error_on_bad_override", |
| 72 #if WTF_OS_IOS | 60 #if WTF_OS_IOS |
| 73 "--no-profile" | 61 "--no-profile" |
| 74 #endif | 62 #endif |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 114 |
| 127 if (LogIfError(library)) | 115 if (LogIfError(library)) |
| 128 return nullptr; | 116 return nullptr; |
| 129 | 117 |
| 130 if (!ImportChildLibraries(module, library)) | 118 if (!ImportChildLibraries(module, library)) |
| 131 return nullptr; | 119 return nullptr; |
| 132 | 120 |
| 133 return library; | 121 return library; |
| 134 } | 122 } |
| 135 | 123 |
| 136 void DartController::DidLoadMainLibrary(KURL url) { | 124 void DartController::DidLoadMainLibrary(String name) { |
| 137 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); | 125 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); |
| 138 DartApiScope dart_api_scope; | 126 DartApiScope dart_api_scope; |
| 139 | 127 |
| 140 if (LogIfError(Dart_FinalizeLoading(true))) | 128 if (LogIfError(Dart_FinalizeLoading(true))) |
| 141 return; | 129 return; |
| 142 | 130 |
| 143 Dart_Handle library = Dart_LookupLibrary( | 131 Dart_Handle library = Dart_LookupLibrary(StringToDart(dart_state(), name)); |
| 144 StringToDart(dart_state(), url.string())); | |
| 145 // TODO(eseidel): We need to load a 404 page instead! | 132 // TODO(eseidel): We need to load a 404 page instead! |
| 146 if (LogIfError(library)) | 133 if (LogIfError(library)) |
| 147 return; | 134 return; |
| 148 DartInvokeAppField(library, ToDart("main"), 0, nullptr); | 135 DartInvokeAppField(library, ToDart("main"), 0, nullptr); |
| 149 } | 136 } |
| 150 | 137 |
| 151 void DartController::LoadMainLibrary(const KURL& url, mojo::URLResponsePtr respo
nse) { | |
| 152 DartState::Scope scope(dart_state()); | |
| 153 CreateEmptyRootLibraryIfNeeded(); | |
| 154 | |
| 155 library_provider_ = adoptPtr(new DartLibraryProviderNetwork( | |
| 156 CreatePrefetchedLibraryIfNeeded(url, response.Pass()))); | |
| 157 | |
| 158 DartLibraryLoader& loader = dart_state()->library_loader(); | |
| 159 loader.set_library_provider(library_provider_.get()); | |
| 160 | |
| 161 DartDependencyCatcher dependency_catcher(loader); | |
| 162 loader.LoadLibrary(url.string()); | |
| 163 loader.WaitForDependencies(dependency_catcher.dependencies(), | |
| 164 base::Bind(&DartController::DidLoadMainLibrary, wea
k_factory_.GetWeakPtr(), url)); | |
| 165 } | |
| 166 | |
| 167 void DartController::DidLoadSnapshot() { | 138 void DartController::DidLoadSnapshot() { |
| 168 DCHECK(Dart_CurrentIsolate() == nullptr); | 139 DCHECK(Dart_CurrentIsolate() == nullptr); |
| 169 snapshot_loader_ = nullptr; | 140 snapshot_loader_ = nullptr; |
| 170 | 141 |
| 171 Dart_Isolate isolate = dart_state()->isolate(); | 142 Dart_Isolate isolate = dart_state()->isolate(); |
| 172 DartIsolateScope isolate_scope(isolate); | 143 DartIsolateScope isolate_scope(isolate); |
| 173 DartApiScope dart_api_scope; | 144 DartApiScope dart_api_scope; |
| 174 | 145 |
| 175 Dart_Handle library = Dart_RootLibrary(); | 146 Dart_Handle library = Dart_RootLibrary(); |
| 176 if (LogIfError(library)) | 147 if (LogIfError(library)) |
| 177 return; | 148 return; |
| 178 DartInvokeAppField(library, ToDart("main"), 0, nullptr); | 149 DartInvokeAppField(library, ToDart("main"), 0, nullptr); |
| 179 } | 150 } |
| 180 | 151 |
| 181 void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response
) { | 152 void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response
) { |
| 182 snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state())); | 153 snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state())); |
| 183 snapshot_loader_->LoadSnapshot( | 154 snapshot_loader_->LoadSnapshot( |
| 184 response->body.Pass(), | 155 response->body.Pass(), |
| 185 base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr())); | 156 base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr())); |
| 186 } | 157 } |
| 187 | 158 |
| 159 void DartController::RunFromLibrary(const String& name, |
| 160 DartLibraryProvider* library_provider) { |
| 161 DartState::Scope scope(dart_state()); |
| 162 CreateEmptyRootLibraryIfNeeded(); |
| 163 |
| 164 DartLibraryLoader& loader = dart_state()->library_loader(); |
| 165 loader.set_library_provider(library_provider); |
| 166 |
| 167 DartDependencyCatcher dependency_catcher(loader); |
| 168 loader.LoadLibrary(name); |
| 169 loader.WaitForDependencies(dependency_catcher.dependencies(), |
| 170 base::Bind(&DartController::DidLoadMainLibrary, |
| 171 weak_factory_.GetWeakPtr(), name)); |
| 172 } |
| 173 |
| 188 void DartController::LoadScriptInModule( | 174 void DartController::LoadScriptInModule( |
| 189 AbstractModule* module, | 175 AbstractModule* module, |
| 190 const String& source, | 176 const String& source, |
| 191 const TextPosition& position, | 177 const TextPosition& position, |
| 192 const LoadFinishedCallback& finished_callback) { | 178 const LoadFinishedCallback& finished_callback) { |
| 193 DartState::Scope scope(dart_state()); | 179 DartState::Scope scope(dart_state()); |
| 194 CreateEmptyRootLibraryIfNeeded(); | 180 CreateEmptyRootLibraryIfNeeded(); |
| 195 | 181 |
| 196 DartLibraryLoader& loader = dart_state()->library_loader(); | 182 DartLibraryLoader& loader = dart_state()->library_loader(); |
| 197 | 183 |
| 198 if (!library_provider_) { | 184 if (!library_provider_) { |
| 199 library_provider_ = adoptPtr(new DartLibraryProviderNetwork(nullptr)); | 185 library_provider_ = adoptPtr(new DartLibraryProviderWebView()); |
| 200 loader.set_library_provider(library_provider_.get()); | 186 loader.set_library_provider(library_provider_.get()); |
| 201 } | 187 } |
| 202 | 188 |
| 203 DartDependencyCatcher dependency_catcher(loader); | 189 DartDependencyCatcher dependency_catcher(loader); |
| 204 Dart_Handle library_handle = CreateLibrary(module, source, position); | 190 Dart_Handle library_handle = CreateLibrary(module, source, position); |
| 205 if (!library_handle) | 191 if (!library_handle) |
| 206 return finished_callback.Run(nullptr, nullptr); | 192 return finished_callback.Run(nullptr, nullptr); |
| 207 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); | 193 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); |
| 208 module->AddLibrary(library, position); | 194 module->AddLibrary(library, position); |
| 209 | 195 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 DCHECK(Platform::current()); | 360 DCHECK(Platform::current()); |
| 375 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE, | 361 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE, |
| 376 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr()
)); | 362 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr()
)); |
| 377 } | 363 } |
| 378 | 364 |
| 379 void DartController::CreateIsolateFor(PassOwnPtr<DOMDartState> state) { | 365 void DartController::CreateIsolateFor(PassOwnPtr<DOMDartState> state) { |
| 380 CHECK(kDartIsolateSnapshotBuffer); | 366 CHECK(kDartIsolateSnapshotBuffer); |
| 381 char* error = nullptr; | 367 char* error = nullptr; |
| 382 dom_dart_state_ = state; | 368 dom_dart_state_ = state; |
| 383 Dart_Isolate isolate = Dart_CreateIsolate( | 369 Dart_Isolate isolate = Dart_CreateIsolate( |
| 384 dom_dart_state_->url().string().utf8().data(), "main", | 370 dom_dart_state_->url().utf8().data(), "main", kDartIsolateSnapshotBuffer, |
| 385 kDartIsolateSnapshotBuffer, nullptr, | 371 nullptr, static_cast<DartState*>(dom_dart_state_.get()), &error); |
| 386 static_cast<DartState*>(dom_dart_state_.get()), &error); | |
| 387 Dart_SetMessageNotifyCallback(MessageNotifyCallback); | 372 Dart_SetMessageNotifyCallback(MessageNotifyCallback); |
| 388 CHECK(isolate) << error; | 373 CHECK(isolate) << error; |
| 389 dom_dart_state_->SetIsolate(isolate); | 374 dom_dart_state_->SetIsolate(isolate); |
| 390 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); | 375 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); |
| 391 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | 376 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
| 392 | 377 |
| 393 { | 378 { |
| 394 DartApiScope apiScope; | 379 DartApiScope apiScope; |
| 395 | 380 |
| 396 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 381 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 nullptr, // Isolate interrupt callback. | 428 nullptr, // Isolate interrupt callback. |
| 444 UnhandledExceptionCallback, IsolateShutdownCallback, | 429 UnhandledExceptionCallback, IsolateShutdownCallback, |
| 445 // File IO callbacks. | 430 // File IO callbacks. |
| 446 nullptr, nullptr, nullptr, nullptr, nullptr)); | 431 nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 447 // Wait for load port- ensures handle watcher and service isolates are | 432 // Wait for load port- ensures handle watcher and service isolates are |
| 448 // running. | 433 // running. |
| 449 Dart_ServiceWaitForLoadPort(); | 434 Dart_ServiceWaitForLoadPort(); |
| 450 } | 435 } |
| 451 | 436 |
| 452 } // namespace blink | 437 } // namespace blink |
| OLD | NEW |