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

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

Issue 1202283004: Factor DartLibraryProvider out of DartLoader (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 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/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_dependency_catcher.h" 26 #include "sky/engine/core/script/dart_library_provider_network.h"
27 #include "sky/engine/core/script/dart_loader.h"
28 #include "sky/engine/core/script/dart_service_isolate.h" 27 #include "sky/engine/core/script/dart_service_isolate.h"
29 #include "sky/engine/core/script/dart_snapshot_loader.h"
30 #include "sky/engine/core/script/dom_dart_state.h" 28 #include "sky/engine/core/script/dom_dart_state.h"
31 #include "sky/engine/public/platform/Platform.h" 29 #include "sky/engine/public/platform/Platform.h"
32 #include "sky/engine/tonic/dart_api_scope.h" 30 #include "sky/engine/tonic/dart_api_scope.h"
33 #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"
34 #include "sky/engine/tonic/dart_error.h" 33 #include "sky/engine/tonic/dart_error.h"
35 #include "sky/engine/tonic/dart_gc_controller.h" 34 #include "sky/engine/tonic/dart_gc_controller.h"
36 #include "sky/engine/tonic/dart_invoke.h" 35 #include "sky/engine/tonic/dart_invoke.h"
37 #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"
38 #include "sky/engine/tonic/dart_snapshot_loader.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 {
43 namespace { 44 namespace {
44 45
45 void CreateEmptyRootLibraryIfNeeded() { 46 void CreateEmptyRootLibraryIfNeeded() {
46 if (Dart_IsNull(Dart_RootLibrary())) { 47 if (Dart_IsNull(Dart_RootLibrary())) {
47 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), Dart_EmptyString(), 48 Dart_LoadScript(Dart_NewStringFromCString("dart:empty"), Dart_EmptyString(),
48 0, 0); 49 0, 0);
49 } 50 }
50 } 51 }
51 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
52 } // namespace 65 } // namespace
53 66
54 #if ENABLE(DART_STRICT) 67 #if ENABLE(DART_STRICT)
55 static const char* kCheckedModeArgs[] = {"--enable_asserts", 68 static const char* kCheckedModeArgs[] = {"--enable_asserts",
56 "--enable_type_checks", 69 "--enable_type_checks",
57 "--error_on_bad_type", 70 "--error_on_bad_type",
58 "--error_on_bad_override", 71 "--error_on_bad_override",
59 #if WTF_OS_IOS 72 #if WTF_OS_IOS
60 "--no-profile" 73 "--no-profile"
61 #endif 74 #endif
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 // TODO(eseidel): We need to load a 404 page instead! 145 // TODO(eseidel): We need to load a 404 page instead!
133 if (LogIfError(library)) 146 if (LogIfError(library))
134 return; 147 return;
135 DartInvokeAppField(library, ToDart("main"), 0, nullptr); 148 DartInvokeAppField(library, ToDart("main"), 0, nullptr);
136 } 149 }
137 150
138 void DartController::LoadMainLibrary(const KURL& url, mojo::URLResponsePtr respo nse) { 151 void DartController::LoadMainLibrary(const KURL& url, mojo::URLResponsePtr respo nse) {
139 DartState::Scope scope(dart_state()); 152 DartState::Scope scope(dart_state());
140 CreateEmptyRootLibraryIfNeeded(); 153 CreateEmptyRootLibraryIfNeeded();
141 154
142 DartLoader& loader = dart_state()->loader(); 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
143 DartDependencyCatcher dependency_catcher(loader); 161 DartDependencyCatcher dependency_catcher(loader);
144 loader.LoadLibrary(url, response.Pass()); 162 loader.LoadLibrary(url.string());
145 loader.WaitForDependencies(dependency_catcher.dependencies(), 163 loader.WaitForDependencies(dependency_catcher.dependencies(),
146 base::Bind(&DartController::DidLoadMainLibrary, wea k_factory_.GetWeakPtr(), url)); 164 base::Bind(&DartController::DidLoadMainLibrary, wea k_factory_.GetWeakPtr(), url));
147 } 165 }
148 166
149 void DartController::DidLoadSnapshot() { 167 void DartController::DidLoadSnapshot() {
150 DCHECK(Dart_CurrentIsolate() == nullptr); 168 DCHECK(Dart_CurrentIsolate() == nullptr);
151 snapshot_loader_ = nullptr; 169 snapshot_loader_ = nullptr;
152 170
153 Dart_Isolate isolate = dart_state()->isolate(); 171 Dart_Isolate isolate = dart_state()->isolate();
154 DartIsolateScope isolate_scope(isolate); 172 DartIsolateScope isolate_scope(isolate);
155 DartApiScope dart_api_scope; 173 DartApiScope dart_api_scope;
156 174
157 Dart_Handle library = Dart_RootLibrary(); 175 Dart_Handle library = Dart_RootLibrary();
158 if (LogIfError(library)) 176 if (LogIfError(library))
159 return; 177 return;
160 DartInvokeAppField(library, ToDart("main"), 0, nullptr); 178 DartInvokeAppField(library, ToDart("main"), 0, nullptr);
161 } 179 }
162 180
163 void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response ) { 181 void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response ) {
164 snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state())); 182 snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state()));
165 snapshot_loader_->LoadSnapshot(url, response.Pass(), 183 snapshot_loader_->LoadSnapshot(
184 response->body.Pass(),
166 base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr())); 185 base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr()));
167 } 186 }
168 187
169 void DartController::LoadScriptInModule( 188 void DartController::LoadScriptInModule(
170 AbstractModule* module, 189 AbstractModule* module,
171 const String& source, 190 const String& source,
172 const TextPosition& position, 191 const TextPosition& position,
173 const LoadFinishedCallback& finished_callback) { 192 const LoadFinishedCallback& finished_callback) {
174 DartState::Scope scope(dart_state()); 193 DartState::Scope scope(dart_state());
175 CreateEmptyRootLibraryIfNeeded(); 194 CreateEmptyRootLibraryIfNeeded();
176 195
177 DartDependencyCatcher dependency_catcher(dart_state()->loader()); 196 DartLibraryLoader& loader = dart_state()->library_loader();
197
198 if (!library_provider_) {
199 library_provider_ = adoptPtr(new DartLibraryProviderNetwork(nullptr));
200 loader.set_library_provider(library_provider_.get());
201 }
202
203 DartDependencyCatcher dependency_catcher(loader);
178 Dart_Handle library_handle = CreateLibrary(module, source, position); 204 Dart_Handle library_handle = CreateLibrary(module, source, position);
179 if (!library_handle) 205 if (!library_handle)
180 return finished_callback.Run(nullptr, nullptr); 206 return finished_callback.Run(nullptr, nullptr);
181 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle); 207 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle);
182 module->AddLibrary(library, position); 208 module->AddLibrary(library, position);
183 209
184 // TODO(eseidel): Better if the library/module retained its dependencies and 210 // TODO(eseidel): Better if the library/module retained its dependencies and
185 // dependency waiting could be separate from library creation. 211 // dependency waiting could be separate from library creation.
186 dart_state()->loader().WaitForDependencies( 212 dart_state()->library_loader().WaitForDependencies(
187 dependency_catcher.dependencies(), 213 dependency_catcher.dependencies(),
188 base::Bind(finished_callback, module, library)); 214 base::Bind(finished_callback, module, library));
189 } 215 }
190 216
191 void DartController::ExecuteLibraryInModule(AbstractModule* module, 217 void DartController::ExecuteLibraryInModule(AbstractModule* module,
192 Dart_Handle library, 218 Dart_Handle library,
193 HTMLScriptElement* script) { 219 HTMLScriptElement* script) {
194 TRACE_EVENT1("sky", "DartController::ExecuteLibraryInModule", 220 TRACE_EVENT1("sky", "DartController::ExecuteLibraryInModule",
195 "url", module->url().ascii().toStdString()); 221 "url", module->url().ascii().toStdString());
196 ASSERT(library); 222 ASSERT(library);
(...skipping 22 matching lines...) Expand all
219 DartInvokeAppField(library, ToDart(name), arraysize(args), args); 245 DartInvokeAppField(library, ToDart(name), arraysize(args), args);
220 } 246 }
221 247
222 static void UnhandledExceptionCallback(Dart_Handle error) { 248 static void UnhandledExceptionCallback(Dart_Handle error) {
223 LOG(ERROR) << Dart_GetError(error); 249 LOG(ERROR) << Dart_GetError(error);
224 } 250 }
225 251
226 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, 252 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
227 Dart_Handle library, 253 Dart_Handle library,
228 Dart_Handle url) { 254 Dart_Handle url) {
229 return DartLoader::HandleLibraryTag(tag, library, url); 255 return DartLibraryLoader::HandleLibraryTag(tag, library, url);
230 } 256 }
231 257
232 static void IsolateShutdownCallback(void* callback_data) { 258 static void IsolateShutdownCallback(void* callback_data) {
233 // TODO(dart) 259 // TODO(dart)
234 } 260 }
235 261
236 static bool IsServiceIsolateURL(const char* url_name) { 262 static bool IsServiceIsolateURL(const char* url_name) {
237 return url_name != nullptr && 263 return url_name != nullptr &&
238 String(url_name) == DART_VM_SERVICE_ISOLATE_NAME; 264 String(url_name) == DART_VM_SERVICE_ISOLATE_NAME;
239 } 265 }
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 nullptr, // Isolate interrupt callback. 443 nullptr, // Isolate interrupt callback.
418 UnhandledExceptionCallback, IsolateShutdownCallback, 444 UnhandledExceptionCallback, IsolateShutdownCallback,
419 // File IO callbacks. 445 // File IO callbacks.
420 nullptr, nullptr, nullptr, nullptr, nullptr)); 446 nullptr, nullptr, nullptr, nullptr, nullptr));
421 // Wait for load port- ensures handle watcher and service isolates are 447 // Wait for load port- ensures handle watcher and service isolates are
422 // running. 448 // running.
423 Dart_ServiceWaitForLoadPort(); 449 Dart_ServiceWaitForLoadPort();
424 } 450 }
425 451
426 } // namespace blink 452 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/core/script/dart_controller.h ('k') | sky/engine/core/script/dart_dependency_catcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698