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/config.h" | 5 #include "sky/engine/config.h" |
6 #include "sky/engine/core/script/dart_controller.h" | 6 #include "sky/engine/core/script/dart_controller.h" |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "dart/runtime/bin/embedded_dart_io.h" | 12 #include "dart/runtime/bin/embedded_dart_io.h" |
13 #include "dart/runtime/include/dart_mirrors_api.h" | 13 #include "dart/runtime/include/dart_mirrors_api.h" |
14 #include "gen/sky/platform/RuntimeEnabledFeatures.h" | 14 #include "gen/sky/platform/RuntimeEnabledFeatures.h" |
15 #include "sky/engine/bindings/builtin.h" | 15 #include "sky/engine/bindings/builtin.h" |
16 #include "sky/engine/bindings/builtin_natives.h" | 16 #include "sky/engine/bindings/builtin_natives.h" |
17 #include "sky/engine/bindings/builtin_sky.h" | 17 #include "sky/engine/bindings/builtin_sky.h" |
18 #include "sky/engine/core/app/AbstractModule.h" | 18 #include "sky/engine/core/app/AbstractModule.h" |
19 #include "sky/engine/core/app/Module.h" | 19 #include "sky/engine/core/app/Module.h" |
20 #include "sky/engine/core/dom/Element.h" | 20 #include "sky/engine/core/dom/Element.h" |
21 #include "sky/engine/core/frame/LocalFrame.h" | 21 #include "sky/engine/core/frame/LocalFrame.h" |
22 #include "sky/engine/core/html/HTMLScriptElement.h" | 22 #include "sky/engine/core/html/HTMLScriptElement.h" |
23 #include "sky/engine/core/html/imports/HTMLImport.h" | 23 #include "sky/engine/core/html/imports/HTMLImport.h" |
24 #include "sky/engine/core/html/imports/HTMLImportChild.h" | 24 #include "sky/engine/core/html/imports/HTMLImportChild.h" |
25 #include "sky/engine/core/loader/FrameLoaderClient.h" | 25 #include "sky/engine/core/loader/FrameLoaderClient.h" |
26 #include "sky/engine/core/script/dart_debugger.h" | 26 #include "sky/engine/core/script/dart_debugger.h" |
27 #include "sky/engine/core/script/dart_dependency_catcher.h" | 27 #include "sky/engine/core/script/dart_dependency_catcher.h" |
28 #include "sky/engine/core/script/dart_loader.h" | 28 #include "sky/engine/core/script/dart_loader.h" |
29 #include "sky/engine/core/script/dart_service_isolate.h" | 29 #include "sky/engine/core/script/dart_service_isolate.h" |
| 30 #include "sky/engine/core/script/dart_snapshot_loader.h" |
30 #include "sky/engine/core/script/dom_dart_state.h" | 31 #include "sky/engine/core/script/dom_dart_state.h" |
31 #include "sky/engine/public/platform/Platform.h" | 32 #include "sky/engine/public/platform/Platform.h" |
32 #include "sky/engine/tonic/dart_api_scope.h" | 33 #include "sky/engine/tonic/dart_api_scope.h" |
33 #include "sky/engine/tonic/dart_class_library.h" | 34 #include "sky/engine/tonic/dart_class_library.h" |
34 #include "sky/engine/tonic/dart_error.h" | 35 #include "sky/engine/tonic/dart_error.h" |
35 #include "sky/engine/tonic/dart_gc_controller.h" | 36 #include "sky/engine/tonic/dart_gc_controller.h" |
36 #include "sky/engine/tonic/dart_invoke.h" | 37 #include "sky/engine/tonic/dart_invoke.h" |
37 #include "sky/engine/tonic/dart_isolate_scope.h" | 38 #include "sky/engine/tonic/dart_isolate_scope.h" |
38 #include "sky/engine/tonic/dart_state.h" | 39 #include "sky/engine/tonic/dart_state.h" |
39 #include "sky/engine/tonic/dart_wrappable.h" | 40 #include "sky/engine/tonic/dart_wrappable.h" |
40 #include "sky/engine/wtf/text/TextPosition.h" | 41 #include "sky/engine/wtf/text/TextPosition.h" |
41 | 42 |
42 namespace blink { | 43 namespace blink { |
| 44 namespace { |
| 45 |
| 46 void CreateEmptyRootLibraryIfNeeded() { |
| 47 if (Dart_IsNull(Dart_RootLibrary())) { |
| 48 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), Dart_EmptyString(), |
| 49 0, 0); |
| 50 } |
| 51 } |
| 52 |
| 53 } // namespace |
43 | 54 |
44 #if ENABLE(DART_STRICT) | 55 #if ENABLE(DART_STRICT) |
45 static const char* kCheckedModeArgs[] = {"--enable_asserts", | 56 static const char* kCheckedModeArgs[] = {"--enable_asserts", |
46 "--enable_type_checks", | 57 "--enable_type_checks", |
47 "--error_on_bad_type", | 58 "--error_on_bad_type", |
48 "--error_on_bad_override", | 59 "--error_on_bad_override", |
49 #if WTF_OS_IOS | 60 #if WTF_OS_IOS |
50 "--no-profile" | 61 "--no-profile" |
51 #endif | 62 #endif |
52 }; | 63 }; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 130 |
120 Dart_Handle library = Dart_LookupLibrary( | 131 Dart_Handle library = Dart_LookupLibrary( |
121 StringToDart(dart_state(), url.string())); | 132 StringToDart(dart_state(), url.string())); |
122 // TODO(eseidel): We need to load a 404 page instead! | 133 // TODO(eseidel): We need to load a 404 page instead! |
123 if (LogIfError(library)) | 134 if (LogIfError(library)) |
124 return; | 135 return; |
125 DartInvokeAppField(library, ToDart("main"), 0, nullptr); | 136 DartInvokeAppField(library, ToDart("main"), 0, nullptr); |
126 } | 137 } |
127 | 138 |
128 void DartController::LoadMainLibrary(const KURL& url, mojo::URLResponsePtr respo
nse) { | 139 void DartController::LoadMainLibrary(const KURL& url, mojo::URLResponsePtr respo
nse) { |
| 140 DartState::Scope scope(dart_state()); |
| 141 CreateEmptyRootLibraryIfNeeded(); |
| 142 |
129 DartLoader& loader = dart_state()->loader(); | 143 DartLoader& loader = dart_state()->loader(); |
130 DartDependencyCatcher dependency_catcher(loader); | 144 DartDependencyCatcher dependency_catcher(loader); |
131 loader.LoadLibrary(url, response.Pass()); | 145 loader.LoadLibrary(url, response.Pass()); |
132 loader.WaitForDependencies(dependency_catcher.dependencies(), | 146 loader.WaitForDependencies(dependency_catcher.dependencies(), |
133 base::Bind(&DartController::DidLoadMainLibrary, wea
k_factory_.GetWeakPtr(), url)); | 147 base::Bind(&DartController::DidLoadMainLibrary, wea
k_factory_.GetWeakPtr(), url)); |
134 } | 148 } |
135 | 149 |
| 150 void DartController::DidLoadSnapshot() { |
| 151 DCHECK(Dart_CurrentIsolate() == nullptr); |
| 152 snapshot_loader_ = nullptr; |
| 153 |
| 154 Dart_Isolate isolate = dart_state()->isolate(); |
| 155 DartIsolateScope isolate_scope(isolate); |
| 156 DartApiScope dart_api_scope; |
| 157 |
| 158 Dart_Handle library = Dart_RootLibrary(); |
| 159 if (LogIfError(library)) |
| 160 return; |
| 161 DartInvokeAppField(library, ToDart("main"), 0, nullptr); |
| 162 } |
| 163 |
| 164 void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response
) { |
| 165 snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state())); |
| 166 snapshot_loader_->LoadSnapshot(url, response.Pass(), |
| 167 base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr())); |
| 168 } |
| 169 |
136 void DartController::LoadScriptInModule( | 170 void DartController::LoadScriptInModule( |
137 AbstractModule* module, | 171 AbstractModule* module, |
138 const String& source, | 172 const String& source, |
139 const TextPosition& position, | 173 const TextPosition& position, |
140 const LoadFinishedCallback& finished_callback) { | 174 const LoadFinishedCallback& finished_callback) { |
141 DartIsolateScope isolate_scope(dart_state()->isolate()); | 175 DartState::Scope scope(dart_state()); |
142 DartApiScope dart_api_scope; | 176 CreateEmptyRootLibraryIfNeeded(); |
143 | 177 |
144 DartDependencyCatcher dependency_catcher(dart_state()->loader()); | 178 DartDependencyCatcher dependency_catcher(dart_state()->loader()); |
145 Dart_Handle library_handle = CreateLibrary(module, source, position); | 179 Dart_Handle library_handle = CreateLibrary(module, source, position); |
146 if (!library_handle) | 180 if (!library_handle) |
147 return finished_callback.Run(nullptr, nullptr); | 181 return finished_callback.Run(nullptr, nullptr); |
148 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); | 182 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); |
149 module->AddLibrary(library, position); | 183 module->AddLibrary(library, position); |
150 | 184 |
151 // TODO(eseidel): Better if the library/module retained its dependencies and | 185 // TODO(eseidel): Better if the library/module retained its dependencies and |
152 // dependency waiting could be separate from library creation. | 186 // dependency waiting could be separate from library creation. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 DartServiceIsolate::Startup(ip, port, LibraryTagHandler, error); | 301 DartServiceIsolate::Startup(ip, port, LibraryTagHandler, error); |
268 CHECK(service_isolate_booted) << error; | 302 CHECK(service_isolate_booted) << error; |
269 } | 303 } |
270 } | 304 } |
271 Dart_ExitIsolate(); | 305 Dart_ExitIsolate(); |
272 return isolate; | 306 return isolate; |
273 } | 307 } |
274 | 308 |
275 // Create & start the handle watcher isolate | 309 // Create & start the handle watcher isolate |
276 CHECK(kDartIsolateSnapshotBuffer); | 310 CHECK(kDartIsolateSnapshotBuffer); |
| 311 // TODO(abarth): Who deletes this DartState instance? |
277 DartState* dart_state = new DartState(); | 312 DartState* dart_state = new DartState(); |
278 Dart_Isolate isolate = | 313 Dart_Isolate isolate = |
279 Dart_CreateIsolate("sky:handle_watcher", "", kDartIsolateSnapshotBuffer, | 314 Dart_CreateIsolate("sky:handle_watcher", "", kDartIsolateSnapshotBuffer, |
280 nullptr, dart_state, error); | 315 nullptr, dart_state, error); |
281 CHECK(isolate) << error; | 316 CHECK(isolate) << error; |
282 dart_state->SetIsolate(isolate); | 317 dart_state->SetIsolate(isolate); |
283 | 318 |
284 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | 319 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
285 | 320 |
286 { | 321 { |
287 DartApiScope apiScope; | 322 DartApiScope apiScope; |
288 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 323 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
289 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); | 324 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); |
290 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 325 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
291 | 326 |
292 // Ensure the isolate has a root library. | 327 if (!script_uri) |
293 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), | 328 CreateEmptyRootLibraryIfNeeded(); |
294 Dart_NewStringFromCString(""), 0, 0); | |
295 } | 329 } |
296 | 330 |
297 Dart_ExitIsolate(); | 331 Dart_ExitIsolate(); |
298 | 332 |
299 CHECK(Dart_IsolateMakeRunnable(isolate)); | 333 CHECK(Dart_IsolateMakeRunnable(isolate)); |
300 return isolate; | 334 return isolate; |
301 } | 335 } |
302 | 336 |
303 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) { | 337 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) { |
304 TRACE_EVENT0("sky", "CallHandleMessage"); | 338 TRACE_EVENT0("sky", "CallHandleMessage"); |
(...skipping 22 matching lines...) Expand all Loading... |
327 static_cast<DartState*>(dom_dart_state_.get()), &error); | 361 static_cast<DartState*>(dom_dart_state_.get()), &error); |
328 Dart_SetMessageNotifyCallback(MessageNotifyCallback); | 362 Dart_SetMessageNotifyCallback(MessageNotifyCallback); |
329 CHECK(isolate) << error; | 363 CHECK(isolate) << error; |
330 dom_dart_state_->SetIsolate(isolate); | 364 dom_dart_state_->SetIsolate(isolate); |
331 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); | 365 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); |
332 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | 366 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
333 | 367 |
334 { | 368 { |
335 DartApiScope apiScope; | 369 DartApiScope apiScope; |
336 | 370 |
337 // Ensure the isolate has a root library. | |
338 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), | |
339 Dart_NewStringFromCString(""), 0, 0); | |
340 | |
341 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 371 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
342 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); | 372 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); |
343 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 373 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
344 BuiltinNatives::Init(BuiltinNatives::MainIsolate); | 374 BuiltinNatives::Init(BuiltinNatives::MainIsolate); |
345 | 375 |
346 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); | 376 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); |
347 dart_state()->class_library().set_provider(builtin_sky_.get()); | 377 dart_state()->class_library().set_provider(builtin_sky_.get()); |
348 | 378 |
349 if (dart_state()->document()) | 379 if (dart_state()->document()) |
350 builtin_sky_->InstallWindow(dart_state()); | 380 builtin_sky_->InstallWindow(dart_state()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 nullptr, // Isolate interrupt callback. | 418 nullptr, // Isolate interrupt callback. |
389 UnhandledExceptionCallback, IsolateShutdownCallback, | 419 UnhandledExceptionCallback, IsolateShutdownCallback, |
390 // File IO callbacks. | 420 // File IO callbacks. |
391 nullptr, nullptr, nullptr, nullptr, nullptr)); | 421 nullptr, nullptr, nullptr, nullptr, nullptr)); |
392 // Wait for load port- ensures handle watcher and service isolates are | 422 // Wait for load port- ensures handle watcher and service isolates are |
393 // running. | 423 // running. |
394 Dart_ServiceWaitForLoadPort(); | 424 Dart_ServiceWaitForLoadPort(); |
395 } | 425 } |
396 | 426 |
397 } // namespace blink | 427 } // namespace blink |
OLD | NEW |