Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: sky/engine/core/script/dart_controller.cc

Issue 1107803002: Add Observatory to sky dart_controller (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/include/dart_mirrors_api.h" 12 #include "dart/runtime/include/dart_mirrors_api.h"
13 #include "sky/engine/bindings/builtin.h" 13 #include "sky/engine/bindings/builtin.h"
14 #include "sky/engine/bindings/builtin_natives.h" 14 #include "sky/engine/bindings/builtin_natives.h"
15 #include "sky/engine/bindings/builtin_sky.h" 15 #include "sky/engine/bindings/builtin_sky.h"
16 #include "sky/engine/bindings/dart_io.h"
16 #include "sky/engine/core/app/AbstractModule.h" 17 #include "sky/engine/core/app/AbstractModule.h"
17 #include "sky/engine/core/app/Module.h" 18 #include "sky/engine/core/app/Module.h"
18 #include "sky/engine/core/dom/Element.h" 19 #include "sky/engine/core/dom/Element.h"
19 #include "sky/engine/core/frame/LocalFrame.h" 20 #include "sky/engine/core/frame/LocalFrame.h"
20 #include "sky/engine/core/html/HTMLScriptElement.h" 21 #include "sky/engine/core/html/HTMLScriptElement.h"
21 #include "sky/engine/core/html/imports/HTMLImport.h" 22 #include "sky/engine/core/html/imports/HTMLImport.h"
22 #include "sky/engine/core/html/imports/HTMLImportChild.h" 23 #include "sky/engine/core/html/imports/HTMLImportChild.h"
23 #include "sky/engine/core/loader/FrameLoaderClient.h" 24 #include "sky/engine/core/loader/FrameLoaderClient.h"
25 #include "sky/engine/core/script/dart_debugger.h"
24 #include "sky/engine/core/script/dart_dependency_catcher.h" 26 #include "sky/engine/core/script/dart_dependency_catcher.h"
25 #include "sky/engine/core/script/dart_loader.h" 27 #include "sky/engine/core/script/dart_loader.h"
28 #include "sky/engine/core/script/dart_service_isolate.h"
26 #include "sky/engine/core/script/dom_dart_state.h" 29 #include "sky/engine/core/script/dom_dart_state.h"
27 #include "sky/engine/public/platform/Platform.h" 30 #include "sky/engine/public/platform/Platform.h"
28 #include "sky/engine/tonic/dart_api_scope.h" 31 #include "sky/engine/tonic/dart_api_scope.h"
29 #include "sky/engine/tonic/dart_class_library.h" 32 #include "sky/engine/tonic/dart_class_library.h"
30 #include "sky/engine/tonic/dart_error.h" 33 #include "sky/engine/tonic/dart_error.h"
31 #include "sky/engine/tonic/dart_gc_controller.h" 34 #include "sky/engine/tonic/dart_gc_controller.h"
32 #include "sky/engine/tonic/dart_invoke.h" 35 #include "sky/engine/tonic/dart_invoke.h"
33 #include "sky/engine/tonic/dart_isolate_scope.h" 36 #include "sky/engine/tonic/dart_isolate_scope.h"
34 #include "sky/engine/tonic/dart_state.h" 37 #include "sky/engine/tonic/dart_state.h"
35 #include "sky/engine/tonic/dart_wrappable.h" 38 #include "sky/engine/tonic/dart_wrappable.h"
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 172
170 static void IsolateShutdownCallback(void* callback_data) { 173 static void IsolateShutdownCallback(void* callback_data) {
171 // TODO(dart) 174 // TODO(dart)
172 } 175 }
173 176
174 static bool IsServiceIsolateURL(const char* url_name) { 177 static bool IsServiceIsolateURL(const char* url_name) {
175 return url_name != nullptr && 178 return url_name != nullptr &&
176 String(url_name) == DART_VM_SERVICE_ISOLATE_NAME; 179 String(url_name) == DART_VM_SERVICE_ISOLATE_NAME;
177 } 180 }
178 181
182 static void EnsureHandleWatcherStarted() {
183 static bool handle_watcher_started = false;
184 if (handle_watcher_started)
185 return;
186
187 // TODO(dart): Call Dart_Cleanup (ensure the handle watcher isolate is closed)
188 // during shutdown.
189 Dart_Handle mojo_core_lib =
190 Builtin::LoadAndCheckLibrary(Builtin::kMojoInternalLibrary);
191 CHECK(!LogIfError((mojo_core_lib)));
192 Dart_Handle handle_watcher_type = Dart_GetType(
193 mojo_core_lib,
194 Dart_NewStringFromCString("MojoHandleWatcher"),
195 0,
196 nullptr);
197 CHECK(!LogIfError(handle_watcher_type));
198 CHECK(!LogIfError(Dart_Invoke(
199 handle_watcher_type,
200 Dart_NewStringFromCString("_start"),
201 0,
202 nullptr)));
203
204 // RunLoop until the handle watcher isolate is spun-up.
205 CHECK(!LogIfError(Dart_RunLoop()));
206 handle_watcher_started = true;
207 }
208
179 // TODO(rafaelw): Right now this only supports the creation of the handle 209 // TODO(rafaelw): Right now this only supports the creation of the handle
180 // watcher isolate. Presumably, we'll want application isolates to spawn their 210 // watcher isolate and the service isolate. Presumably, we'll want application
181 // own isolates. 211 // isolates to spawn their own isolates.
182 static Dart_Isolate IsolateCreateCallback(const char* script_uri, 212 static Dart_Isolate IsolateCreateCallback(const char* script_uri,
183 const char* main, 213 const char* main,
184 const char* package_root, 214 const char* package_root,
185 void* callback_data, 215 void* callback_data,
186 char** error) { 216 char** error) {
187 217
188 if (IsServiceIsolateURL(script_uri)) { 218 if (IsServiceIsolateURL(script_uri)) {
189 return Dart_CreateIsolate(script_uri, "main", kDartIsolateSnapshotBuffer, 219 CHECK(kDartIsolateSnapshotBuffer);
190 nullptr, error); 220 DartState* dart_state = new DartState();
221 Dart_Isolate isolate = Dart_CreateIsolate(script_uri,
222 "main",
223 kDartIsolateSnapshotBuffer,
224 nullptr,
225 error);
226 CHECK(isolate) << error;
227 dart_state->set_isolate(isolate);
228 CHECK(Dart_IsServiceIsolate(isolate));
229 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler)));
230 {
231 DartApiScope apiScope;
232 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
233 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
234 Builtin::SetNativeResolver(Builtin::kIOLibrary);
235 BuiltinNatives::Init(BuiltinNatives::DartIOIsolate);
236 // Start the handle watcher from the service isolate so it isn't available
237 // for debugging or general Observatory interaction.
238 EnsureHandleWatcherStarted();
239 std::string ip = "127.0.0.1";
240 const intptr_t port = 0; // Automatic port assignment.
241 const bool service_isolate_booted =
242 DartServiceIsolate::Startup(ip, port, LibraryTagHandler, error);
243 CHECK(service_isolate_booted) << error;
244 }
245 Dart_ExitIsolate();
246 return isolate;
191 } 247 }
192 248
193 // Create & start the handle watcher isolate 249 // Create & start the handle watcher isolate
194 CHECK(kDartIsolateSnapshotBuffer); 250 CHECK(kDartIsolateSnapshotBuffer);
195 DartState* dart_state = new DartState(); 251 DartState* dart_state = new DartState();
196 Dart_Isolate isolate = Dart_CreateIsolate("sky:handle_watcher", "", 252 Dart_Isolate isolate = Dart_CreateIsolate("sky:handle_watcher", "",
197 kDartIsolateSnapshotBuffer, dart_state, error); 253 kDartIsolateSnapshotBuffer, dart_state, error);
198 CHECK(isolate) << error; 254 CHECK(isolate) << error;
199 dart_state->set_isolate(isolate); 255 dart_state->set_isolate(isolate);
200 256
201 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); 257 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler)));
202 258
203 { 259 {
204 DartApiScope apiScope; 260 DartApiScope apiScope;
205 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); 261 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
206 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); 262 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
263 Builtin::SetNativeResolver(Builtin::kIOLibrary);
207 264
208 // Ensure the isolate has a root library. 265 // Ensure the isolate has a root library.
209 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), 266 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"),
210 Dart_NewStringFromCString(""), 0, 0); 267 Dart_NewStringFromCString(""), 0, 0);
211 } 268 }
212 269
213 Dart_ExitIsolate(); 270 Dart_ExitIsolate();
214 271
215 CHECK(Dart_IsolateMakeRunnable(isolate)); 272 CHECK(Dart_IsolateMakeRunnable(isolate));
216 return isolate; 273 return isolate;
217 } 274 }
218 275
219 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) { 276 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) {
220 TRACE_EVENT0("sky", "CallHandleMessage"); 277 TRACE_EVENT0("sky", "CallHandleMessage");
221 278
222 if (!dart_state) 279 if (!dart_state)
223 return; 280 return;
224 281
225 DartIsolateScope scope(dart_state->isolate()); 282 DartIsolateScope scope(dart_state->isolate());
226 DartApiScope api_scope; 283 DartApiScope api_scope;
227 LogIfError(Dart_HandleMessage()); 284 LogIfError(Dart_HandleMessage());
228 } 285 }
229 286
230 static void MessageNotifyCallback(Dart_Isolate dest_isolate) { 287 static void MessageNotifyCallback(Dart_Isolate dest_isolate) {
231 DCHECK(Platform::current()); 288 DCHECK(Platform::current());
232 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE, 289 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE,
233 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr() )); 290 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr() ));
234 } 291 }
235 292
236 static void EnsureHandleWatcherStarted() {
237 static bool handle_watcher_started = false;
238 if (handle_watcher_started)
239 return;
240
241 // TODO(dart): Call Dart_Cleanup (ensure the handle watcher isolate is closed)
242 // during shutdown.
243 Dart_Handle mojo_core_lib =
244 Builtin::LoadAndCheckLibrary(Builtin::kMojoInternalLibrary);
245 CHECK(!LogIfError((mojo_core_lib)));
246 Dart_Handle handle_watcher_type = Dart_GetType(
247 mojo_core_lib,
248 Dart_NewStringFromCString("MojoHandleWatcher"),
249 0,
250 nullptr);
251 CHECK(!LogIfError(handle_watcher_type));
252 CHECK(!LogIfError(Dart_Invoke(
253 handle_watcher_type,
254 Dart_NewStringFromCString("_start"),
255 0,
256 nullptr)));
257
258 // RunLoop until the handle watcher isolate is spun-up.
259 CHECK(!LogIfError(Dart_RunLoop()));
260 handle_watcher_started = true;
261 }
262
263 void DartController::CreateIsolateFor(Document* document) { 293 void DartController::CreateIsolateFor(Document* document) {
264 DCHECK(document); 294 DCHECK(document);
265 CHECK(kDartIsolateSnapshotBuffer); 295 CHECK(kDartIsolateSnapshotBuffer);
266 char* error = nullptr; 296 char* error = nullptr;
267 dom_dart_state_ = adoptPtr(new DOMDartState(document)); 297 dom_dart_state_ = adoptPtr(new DOMDartState(document));
268 Dart_Isolate isolate = Dart_CreateIsolate( 298 Dart_Isolate isolate = Dart_CreateIsolate(
269 document->url().string().utf8().data(), "main", kDartIsolateSnapshotBuffer , 299 document->url().string().utf8().data(), "main", kDartIsolateSnapshotBuffer ,
270 static_cast<DartState*>(dom_dart_state_.get()), &error); 300 static_cast<DartState*>(dom_dart_state_.get()), &error);
271 Dart_SetMessageNotifyCallback(MessageNotifyCallback); 301 Dart_SetMessageNotifyCallback(MessageNotifyCallback);
272 CHECK(isolate) << error; 302 CHECK(isolate) << error;
273 dom_dart_state_->set_isolate(isolate); 303 dom_dart_state_->set_isolate(isolate);
274 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); 304 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue);
275 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); 305 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler)));
276 306
277 { 307 {
278 DartApiScope apiScope; 308 DartApiScope apiScope;
279 309
280 // Ensure the isolate has a root library. 310 // Ensure the isolate has a root library.
281 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), 311 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"),
282 Dart_NewStringFromCString(""), 0, 0); 312 Dart_NewStringFromCString(""), 0, 0);
283 313
284 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); 314 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
285 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary); 315 Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
286 BuiltinNatives::Init(); 316 Builtin::SetNativeResolver(Builtin::kIOLibrary);
317 BuiltinNatives::Init(BuiltinNatives::MainIsolate);
287 318
288 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); 319 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state()));
289 dart_state()->class_library().set_provider(builtin_sky_.get()); 320 dart_state()->class_library().set_provider(builtin_sky_.get());
290 builtin_sky_->InstallWindow(dart_state()); 321 builtin_sky_->InstallWindow(dart_state());
291 322
292 document->frame()->loaderClient()->didCreateIsolate(isolate); 323 document->frame()->loaderClient()->didCreateIsolate(isolate);
293 324
294 EnsureHandleWatcherStarted(); 325 EnsureHandleWatcherStarted();
295 } 326 }
296 Dart_ExitIsolate(); 327 Dart_ExitIsolate();
297 } 328 }
298 329
299 void DartController::ClearForClose() { 330 void DartController::ClearForClose() {
300 // Don't use a DartIsolateScope here since we never exit the isolate. 331 // Don't use a DartIsolateScope here since we never exit the isolate.
301 Dart_EnterIsolate(dom_dart_state_->isolate()); 332 Dart_EnterIsolate(dom_dart_state_->isolate());
302 Dart_ShutdownIsolate(); 333 Dart_ShutdownIsolate();
303 dom_dart_state_.clear(); 334 dom_dart_state_.clear();
304 } 335 }
305 336
306 void DartController::InitVM() { 337 void DartController::InitVM() {
307 int argc = 0; 338 int argc = 0;
308 const char** argv = nullptr; 339 const char** argv = nullptr;
309 340
310 #if ENABLE(ASSERT) 341 #if ENABLE(ASSERT)
311 argc = arraysize(kCheckedModeArgs); 342 argc = arraysize(kCheckedModeArgs);
312 argv = kCheckedModeArgs; 343 argv = kCheckedModeArgs;
313 #endif 344 #endif
314 345
346 BootstrapDartIo();
347
315 CHECK(Dart_SetVMFlags(argc, argv)); 348 CHECK(Dart_SetVMFlags(argc, argv));
349 // This should be called before calling Dart_Initialize.
350 DartDebugger::InitDebugger();
316 CHECK(Dart_Initialize(kDartVmIsolateSnapshotBuffer, 351 CHECK(Dart_Initialize(kDartVmIsolateSnapshotBuffer,
317 IsolateCreateCallback, 352 IsolateCreateCallback,
318 nullptr, // Isolate interrupt callback. 353 nullptr, // Isolate interrupt callback.
319 UnhandledExceptionCallback, IsolateShutdownCallback, 354 UnhandledExceptionCallback, IsolateShutdownCallback,
320 // File IO callbacks. 355 // File IO callbacks.
321 nullptr, nullptr, nullptr, nullptr, nullptr)); 356 nullptr, nullptr, nullptr, nullptr, nullptr));
357 // Wait for load port- ensures handle watcher and service isolates are
358 // running.
359 Dart_ServiceWaitForLoadPort();
322 } 360 }
323 361
324 } // namespace blink 362 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698