Chromium Code Reviews| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 } | 104 } |
| 103 | 105 |
| 104 static void UnhandledExceptionCallback(Dart_Handle error) { | 106 static void UnhandledExceptionCallback(Dart_Handle error) { |
| 105 DCHECK(!Dart_IsError(error)); | 107 DCHECK(!Dart_IsError(error)); |
| 106 LOG(ERROR) << Dart_GetError(error); | 108 LOG(ERROR) << Dart_GetError(error); |
| 107 } | 109 } |
| 108 | 110 |
| 109 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 111 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 110 Dart_Handle library, | 112 Dart_Handle library, |
| 111 Dart_Handle url) { | 113 Dart_Handle url) { |
| 112 DOMDartState* dart_state = DOMDartState::Current(); | 114 return DartLoader::HandleLibraryTag(tag, library, url); |
| 113 return dart_state->loader().HandleLibraryTag(tag, library, url); | |
| 114 } | 115 } |
| 115 | 116 |
| 116 static void IsolateShutdownCallback(void* callback_data) { | 117 static void IsolateShutdownCallback(void* callback_data) { |
| 117 DartState* dart_state = static_cast<DartState*>(callback_data); | |
| 118 DCHECK(dart_state); | |
| 119 // TODO(dart) | 118 // TODO(dart) |
| 120 } | 119 } |
| 121 | 120 |
| 121 static bool IsServiceIsolateURL(const char* url_name) { | |
| 122 if (url_name == nullptr) { | |
| 123 return false; | |
| 124 } | |
| 125 static const intptr_t kServiceIsolateNameLen = | |
| 126 strlen(DART_VM_SERVICE_ISOLATE_NAME); | |
| 127 return (strncmp(url_name, | |
| 128 DART_VM_SERVICE_ISOLATE_NAME, | |
| 129 kServiceIsolateNameLen) == 0); | |
|
abarth-chromium
2015/02/12 03:29:33
Is there some reason we can't use WTF in this func
rafaelw
2015/02/12 18:54:55
Done.
| |
| 130 } | |
| 131 | |
| 132 // TODO(rafaelw): Right now this only supports the creation of the handle watche r | |
| 133 // isolate. Presumably, we'll want application isolates to spawn their own isola tes. | |
| 134 static Dart_Isolate IsolateCreateCallback(const char* script_uri, | |
| 135 const char* main, | |
| 136 const char* package_root, | |
| 137 void* callback_data, | |
| 138 char** error) { | |
| 139 | |
| 140 if (IsServiceIsolateURL(script_uri)) { | |
| 141 return Dart_CreateIsolate(script_uri, "main", kDartSnapshotBuffer, nullptr, | |
| 142 error); | |
| 143 } | |
| 144 | |
| 145 // Create & start the handle watcher isolate | |
| 146 CHECK(kDartSnapshotBuffer); | |
| 147 DartState* dart_state = new DartState(); | |
| 148 Dart_Isolate isolate = Dart_CreateIsolate("", "", kDartSnapshotBuffer, | |
|
abarth-chromium
2015/02/12 03:29:33
Should we give this some sort of name to help debu
rafaelw
2015/02/12 18:54:55
Done.
| |
| 149 dart_state, error); | |
| 150 CHECK(isolate) << error; | |
| 151 dart_state->set_isolate(isolate); | |
| 152 | |
| 153 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | |
| 154 | |
| 155 { | |
| 156 DartApiScope apiScope; | |
| 157 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | |
| 158 Builtin::SetNativeResolver(Builtin::kMojoCoreLibrary); | |
| 159 } | |
| 160 | |
| 161 Dart_ExitIsolate(); | |
| 162 | |
| 163 CHECK(Dart_IsolateMakeRunnable(isolate)); | |
| 164 return isolate; | |
| 165 } | |
| 166 | |
| 167 static void CallHandleMessage(base::WeakPtr<DartState> dart_state) { | |
| 168 if (!dart_state) | |
| 169 return; | |
| 170 | |
| 171 DartIsolateScope scope(dart_state->isolate()); | |
| 172 DartApiScope api_scope; | |
| 173 LogIfError(Dart_HandleMessage()); | |
| 174 } | |
| 175 | |
| 176 static void MessageNotifyCallback(Dart_Isolate dest_isolate) { | |
| 177 DCHECK(Platform::current()); | |
| 178 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE, | |
| 179 base::Bind(&CallHandleMessage, DartState::From(dest_isolate)->GetWeakPtr() )); | |
| 180 } | |
| 181 | |
| 182 static bool handle_watcher_started = false; | |
|
abarth-chromium
2015/02/12 03:29:33
handle_watcher_started -> g_handle_watcher_started
rafaelw
2015/02/12 18:54:55
Done.
| |
| 183 | |
| 122 void DartController::CreateIsolateFor(Document* document) { | 184 void DartController::CreateIsolateFor(Document* document) { |
| 123 DCHECK(document); | 185 DCHECK(document); |
| 124 CHECK(kDartSnapshotBuffer); | 186 CHECK(kDartSnapshotBuffer); |
| 125 char* error = nullptr; | 187 char* error = nullptr; |
| 126 dom_dart_state_ = adoptPtr(new DOMDartState(document)); | 188 dom_dart_state_ = adoptPtr(new DOMDartState(document)); |
| 127 Dart_Isolate isolate = Dart_CreateIsolate( | 189 Dart_Isolate isolate = Dart_CreateIsolate( |
| 128 document->url().string().utf8().data(), "main", kDartSnapshotBuffer, | 190 document->url().string().utf8().data(), "main", kDartSnapshotBuffer, |
| 129 static_cast<DartState*>(dom_dart_state_.get()), &error); | 191 static_cast<DartState*>(dom_dart_state_.get()), &error); |
| 192 Dart_SetMessageNotifyCallback(MessageNotifyCallback); | |
| 130 CHECK(isolate) << error; | 193 CHECK(isolate) << error; |
| 131 dom_dart_state_->set_isolate(isolate); | 194 dom_dart_state_->set_isolate(isolate); |
| 132 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); | 195 Dart_SetGcCallbacks(DartGCPrologue, DartGCEpilogue); |
| 133 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); | 196 CHECK(!LogIfError(Dart_SetLibraryTagHandler(LibraryTagHandler))); |
| 134 | 197 |
| 135 { | 198 { |
| 136 DartApiScope apiScope; | 199 DartApiScope apiScope; |
| 137 | 200 |
| 138 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 201 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 202 Builtin::SetNativeResolver(Builtin::kMojoCoreLibrary); | |
| 139 BuiltinNatives::Init(); | 203 BuiltinNatives::Init(); |
| 140 | 204 |
| 141 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); | 205 builtin_sky_ = adoptPtr(new BuiltinSky(dart_state())); |
| 142 dart_state()->class_library().set_provider(builtin_sky_.get()); | 206 dart_state()->class_library().set_provider(builtin_sky_.get()); |
| 143 builtin_sky_->InstallWindow(dart_state()); | 207 builtin_sky_->InstallWindow(dart_state()); |
| 144 | 208 |
| 145 document->frame()->loaderClient()->didCreateIsolate(isolate); | 209 document->frame()->loaderClient()->didCreateIsolate(isolate); |
| 210 | |
| 211 if (!handle_watcher_started) { | |
|
abarth-chromium
2015/02/12 03:29:33
Can you move this code into an EnsureHandleWatcher
rafaelw
2015/02/12 18:54:55
Done.
| |
| 212 // TODO(dart): We need to shutdown the handle watcher isolate when sky is exiting | |
|
abarth-chromium
2015/02/12 03:29:33
Looks like we need to call Dart_Cleanup during bli
rafaelw
2015/02/12 18:54:55
Changed the comment
| |
| 213 Dart_Handle mojo_core_lib = | |
| 214 Builtin::LoadAndCheckLibrary(Builtin::kMojoCoreLibrary); | |
| 215 CHECK(!LogIfError((mojo_core_lib))); | |
| 216 Dart_Handle handle_watcher_type = Dart_GetType( | |
| 217 mojo_core_lib, | |
| 218 Dart_NewStringFromCString("MojoHandleWatcher"), | |
| 219 0, | |
| 220 nullptr); | |
| 221 CHECK(!LogIfError(handle_watcher_type)); | |
| 222 CHECK(!LogIfError(Dart_Invoke( | |
| 223 handle_watcher_type, | |
| 224 Dart_NewStringFromCString("_start"), | |
| 225 0, | |
| 226 nullptr))); | |
| 227 | |
| 228 // RunLoop until the handle watcher isolate is spun-up. | |
| 229 CHECK(!LogIfError(Dart_RunLoop())); | |
| 230 | |
| 231 handle_watcher_started = true; | |
| 232 } | |
| 146 } | 233 } |
| 147 Dart_ExitIsolate(); | 234 Dart_ExitIsolate(); |
| 148 } | 235 } |
| 149 | 236 |
| 150 void DartController::ClearForClose() { | 237 void DartController::ClearForClose() { |
| 151 DartIsolateScope scope(dom_dart_state_->isolate()); | 238 DartIsolateScope scope(dom_dart_state_->isolate()); |
| 152 Dart_ShutdownIsolate(); | 239 Dart_ShutdownIsolate(); |
| 153 dom_dart_state_.clear(); | 240 dom_dart_state_.clear(); |
| 154 } | 241 } |
| 155 | 242 |
| 156 void DartController::InitVM() { | 243 void DartController::InitVM() { |
| 157 CHECK(Dart_SetVMFlags(0, NULL)); | 244 CHECK(Dart_SetVMFlags(0, NULL)); |
| 158 CHECK(Dart_Initialize(nullptr, | 245 CHECK(Dart_Initialize(IsolateCreateCallback, |
| 159 nullptr, // Isolate interrupt callback. | 246 nullptr, // Isolate interrupt callback. |
| 160 UnhandledExceptionCallback, IsolateShutdownCallback, | 247 UnhandledExceptionCallback, IsolateShutdownCallback, |
| 161 // File IO callbacks. | 248 // File IO callbacks. |
| 162 nullptr, nullptr, nullptr, nullptr, nullptr)); | 249 nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 163 } | 250 } |
| 164 | 251 |
| 165 } // namespace blink | 252 } // namespace blink |
| OLD | NEW |