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 "sky/engine/bindings2/builtin.h" | 11 #include "sky/engine/bindings2/builtin.h" |
11 #include "sky/engine/bindings2/builtin_natives.h" | 12 #include "sky/engine/bindings2/builtin_natives.h" |
12 #include "sky/engine/bindings2/builtin_sky.h" | 13 #include "sky/engine/bindings2/builtin_sky.h" |
13 #include "sky/engine/core/app/AbstractModule.h" | 14 #include "sky/engine/core/app/AbstractModule.h" |
14 #include "sky/engine/core/app/Module.h" | 15 #include "sky/engine/core/app/Module.h" |
15 #include "sky/engine/core/dom/Element.h" | 16 #include "sky/engine/core/dom/Element.h" |
16 #include "sky/engine/core/frame/LocalFrame.h" | 17 #include "sky/engine/core/frame/LocalFrame.h" |
17 #include "sky/engine/core/html/imports/HTMLImport.h" | 18 #include "sky/engine/core/html/imports/HTMLImport.h" |
18 #include "sky/engine/core/html/imports/HTMLImportChild.h" | 19 #include "sky/engine/core/html/imports/HTMLImportChild.h" |
19 #include "sky/engine/core/loader/FrameLoaderClient.h" | 20 #include "sky/engine/core/loader/FrameLoaderClient.h" |
20 #include "sky/engine/core/script/dart_dependency_catcher.h" | 21 #include "sky/engine/core/script/dart_dependency_catcher.h" |
21 #include "sky/engine/core/script/dart_loader.h" | 22 #include "sky/engine/core/script/dart_loader.h" |
22 #include "sky/engine/core/script/dom_dart_state.h" | 23 #include "sky/engine/core/script/dom_dart_state.h" |
| 24 #include "sky/engine/public/platform/Platform.h" |
23 #include "sky/engine/tonic/dart_api_scope.h" | 25 #include "sky/engine/tonic/dart_api_scope.h" |
24 #include "sky/engine/tonic/dart_class_library.h" | 26 #include "sky/engine/tonic/dart_class_library.h" |
25 #include "sky/engine/tonic/dart_error.h" | 27 #include "sky/engine/tonic/dart_error.h" |
26 #include "sky/engine/tonic/dart_gc_controller.h" | 28 #include "sky/engine/tonic/dart_gc_controller.h" |
27 #include "sky/engine/tonic/dart_isolate_scope.h" | 29 #include "sky/engine/tonic/dart_isolate_scope.h" |
28 #include "sky/engine/tonic/dart_state.h" | 30 #include "sky/engine/tonic/dart_state.h" |
29 #include "sky/engine/wtf/text/TextPosition.h" | 31 #include "sky/engine/wtf/text/TextPosition.h" |
30 | 32 |
31 namespace blink { | 33 namespace blink { |
32 | 34 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 113 } |
112 | 114 |
113 static void UnhandledExceptionCallback(Dart_Handle error) { | 115 static void UnhandledExceptionCallback(Dart_Handle error) { |
114 DCHECK(!Dart_IsError(error)); | 116 DCHECK(!Dart_IsError(error)); |
115 LOG(ERROR) << Dart_GetError(error); | 117 LOG(ERROR) << Dart_GetError(error); |
116 } | 118 } |
117 | 119 |
118 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 120 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
119 Dart_Handle library, | 121 Dart_Handle library, |
120 Dart_Handle url) { | 122 Dart_Handle url) { |
121 DOMDartState* dart_state = DOMDartState::Current(); | 123 return DartLoader::HandleLibraryTag(tag, library, url); |
122 return dart_state->loader().HandleLibraryTag(tag, library, url); | |
123 } | 124 } |
124 | 125 |
125 static void IsolateShutdownCallback(void* callback_data) { | 126 static void IsolateShutdownCallback(void* callback_data) { |
126 DartState* dart_state = static_cast<DartState*>(callback_data); | |
127 DCHECK(dart_state); | |
128 // TODO(dart) | 127 // TODO(dart) |
129 } | 128 } |
130 | 129 |
| 130 static bool IsServiceIsolateURL(const char* url_name) { |
| 131 return url_name != nullptr && |
| 132 String(url_name) == DART_VM_SERVICE_ISOLATE_NAME; |
| 133 } |
| 134 |
| 135 // TODO(rafaelw): Right now this only supports the creation of the handle |
| 136 // watcher isolate. Presumably, we'll want application isolates to spawn their |
| 137 // own isolates. |
| 138 static Dart_Isolate IsolateCreateCallback(const char* script_uri, |
| 139 const char* main, |
| 140 const char* package_root, |
| 141 void* callback_data, |
| 142 char** error) { |
| 143 |
| 144 if (IsServiceIsolateURL(script_uri)) { |
| 145 return Dart_CreateIsolate(script_uri, "main", kDartSnapshotBuffer, nullptr, |
| 146 error); |
| 147 } |
| 148 |
| 149 // Create & start the handle watcher isolate |
| 150 CHECK(kDartSnapshotBuffer); |
| 151 DartState* dart_state = new DartState(); |
| 152 Dart_Isolate isolate = Dart_CreateIsolate("sky:handle_watcher", "", |
| 153 kDartSnapshotBuffer, dart_state, error); |
| 154 CHECK(isolate) << error; |
| 155 dart_state->set_isolate(isolate); |
| 156 |
| 157 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
| 158 |
| 159 { |
| 160 DartApiScope apiScope; |
| 161 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 162 Builtin::SetNativeResolver(Builtin::kMojoCoreLibrary); |
| 163 } |
| 164 |
| 165 Dart_ExitIsolate(); |
| 166 |
| 167 CHECK(Dart_IsolateMakeRunnable(isolate)); |
| 168 return isolate; |
| 169 } |
| 170 |
| 171 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) { |
| 172 if (!dart_state) |
| 173 return; |
| 174 |
| 175 DartIsolateScope scope(dart_state->isolate()); |
| 176 DartApiScope api_scope; |
| 177 LogIfError(Dart_HandleMessage()); |
| 178 } |
| 179 |
| 180 static void MessageNotifyCallback(Dart_Isolate dest_isolate) { |
| 181 DCHECK(Platform::current()); |
| 182 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE, |
| 183 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr()
)); |
| 184 } |
| 185 |
| 186 static void EnsureHandleWatcherStarted() { |
| 187 static bool handle_watcher_started = false; |
| 188 if (handle_watcher_started) |
| 189 return; |
| 190 |
| 191 // TODO(dart): Call Dart_Cleanup (ensure the handle watcher isolate is closed) |
| 192 // during shutdown. |
| 193 Dart_Handle mojo_core_lib = |
| 194 Builtin::LoadAndCheckLibrary(Builtin::kMojoCoreLibrary); |
| 195 CHECK(!LogIfError((mojo_core_lib))); |
| 196 Dart_Handle handle_watcher_type = Dart_GetType( |
| 197 mojo_core_lib, |
| 198 Dart_NewStringFromCString("MojoHandleWatcher"), |
| 199 0, |
| 200 nullptr); |
| 201 CHECK(!LogIfError(handle_watcher_type)); |
| 202 CHECK(!LogIfError(Dart_Invoke( |
| 203 handle_watcher_type, |
| 204 Dart_NewStringFromCString("_start"), |
| 205 0, |
| 206 nullptr))); |
| 207 |
| 208 // RunLoop until the handle watcher isolate is spun-up. |
| 209 CHECK(!LogIfError(Dart_RunLoop())); |
| 210 handle_watcher_started = true; |
| 211 } |
| 212 |
131 void DartController::CreateIsolateFor(Document* document) { | 213 void DartController::CreateIsolateFor(Document* document) { |
132 DCHECK(document); | 214 DCHECK(document); |
133 CHECK(kDartSnapshotBuffer); | 215 CHECK(kDartSnapshotBuffer); |
134 char* error = nullptr; | 216 char* error = nullptr; |
135 dom_dart_state_ = adoptPtr(new DOMDartState(document)); | 217 dom_dart_state_ = adoptPtr(new DOMDartState(document)); |
136 Dart_Isolate isolate = Dart_CreateIsolate( | 218 Dart_Isolate isolate = Dart_CreateIsolate( |
137 document->url().string().utf8().data(), "main", kDartSnapshotBuffer, | 219 document->url().string().utf8().data(), "main", kDartSnapshotBuffer, |
138 static_cast<DartState*>(dom_dart_state_.get()), &error); | 220 static_cast<DartState*>(dom_dart_state_.get()), &error); |
| 221 Dart_SetMessageNotifyCallback(MessageNotifyCallback); |
139 CHECK(isolate) << error; | 222 CHECK(isolate) << error; |
140 dom_dart_state_->set_isolate(isolate); | 223 dom_dart_state_->set_isolate(isolate); |
141 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); | 224 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); |
142 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | 225 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
143 | 226 |
144 { | 227 { |
145 DartApiScope apiScope; | 228 DartApiScope apiScope; |
146 | 229 |
147 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 230 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 231 Builtin::SetNativeResolver(Builtin::kMojoCoreLibrary); |
148 BuiltinNatives::Init(); | 232 BuiltinNatives::Init(); |
149 | 233 |
150 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); | 234 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); |
151 dart_state()->class_library().set_provider(builtin_sky_.get()); | 235 dart_state()->class_library().set_provider(builtin_sky_.get()); |
152 builtin_sky_->InstallWindow(dart_state()); | 236 builtin_sky_->InstallWindow(dart_state()); |
153 | 237 |
154 document->frame()->loaderClient()->didCreateIsolate(isolate); | 238 document->frame()->loaderClient()->didCreateIsolate(isolate); |
| 239 |
| 240 EnsureHandleWatcherStarted(); |
155 } | 241 } |
156 Dart_ExitIsolate(); | 242 Dart_ExitIsolate(); |
157 } | 243 } |
158 | 244 |
159 void DartController::ClearForClose() { | 245 void DartController::ClearForClose() { |
160 DartIsolateScope scope(dom_dart_state_->isolate()); | 246 DartIsolateScope scope(dom_dart_state_->isolate()); |
161 Dart_ShutdownIsolate(); | 247 Dart_ShutdownIsolate(); |
162 dom_dart_state_.clear(); | 248 dom_dart_state_.clear(); |
163 } | 249 } |
164 | 250 |
165 void DartController::InitVM() { | 251 void DartController::InitVM() { |
166 int argc = 0; | 252 int argc = 0; |
167 const char** argv = nullptr; | 253 const char** argv = nullptr; |
168 | 254 |
169 #if ENABLE(ASSERT) | 255 #if ENABLE(ASSERT) |
170 argc = arraysize(kCheckedModeArgs); | 256 argc = arraysize(kCheckedModeArgs); |
171 argv = kCheckedModeArgs; | 257 argv = kCheckedModeArgs; |
172 #endif | 258 #endif |
173 | 259 |
174 CHECK(Dart_SetVMFlags(argc, argv)); | 260 CHECK(Dart_SetVMFlags(argc, argv)); |
175 CHECK(Dart_Initialize(nullptr, | 261 CHECK(Dart_Initialize(IsolateCreateCallback, |
176 nullptr, // Isolate interrupt callback. | 262 nullptr, // Isolate interrupt callback. |
177 UnhandledExceptionCallback, IsolateShutdownCallback, | 263 UnhandledExceptionCallback, IsolateShutdownCallback, |
178 // File IO callbacks. | 264 // File IO callbacks. |
179 nullptr, nullptr, nullptr, nullptr, nullptr)); | 265 nullptr, nullptr, nullptr, nullptr, nullptr)); |
180 } | 266 } |
181 | 267 |
182 } // namespace blink | 268 } // namespace blink |
OLD | NEW |