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/tonic/dart_library_loader.h" | 5 #include "sky/engine/tonic/dart_library_loader.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
9 #include "mojo/common/data_pipe_drainer.h" | 9 #include "mojo/common/data_pipe_drainer.h" |
10 #include "sky/engine/tonic/dart_api_scope.h" | 10 #include "sky/engine/tonic/dart_api_scope.h" |
11 #include "sky/engine/tonic/dart_converter.h" | 11 #include "sky/engine/tonic/dart_converter.h" |
12 #include "sky/engine/tonic/dart_dependency_catcher.h" | 12 #include "sky/engine/tonic/dart_dependency_catcher.h" |
13 #include "sky/engine/tonic/dart_error.h" | 13 #include "sky/engine/tonic/dart_error.h" |
14 #include "sky/engine/tonic/dart_isolate_scope.h" | 14 #include "sky/engine/tonic/dart_isolate_scope.h" |
15 #include "sky/engine/tonic/dart_library_provider.h" | 15 #include "sky/engine/tonic/dart_library_provider.h" |
16 #include "sky/engine/tonic/dart_state.h" | 16 #include "sky/engine/tonic/dart_state.h" |
17 #include "sky/engine/wtf/MainThread.h" | 17 #include "sky/engine/wtf/MainThread.h" |
18 | 18 |
19 using mojo::common::DataPipeDrainer; | 19 using mojo::common::DataPipeDrainer; |
20 | 20 |
21 namespace blink { | 21 namespace blink { |
22 | 22 |
23 // A DartLibraryLoader::Job represents a network load. It fetches data from the | 23 // A DartLibraryLoader::Job represents a network load. It fetches data from the |
24 // network and buffers the data in std::vector. To cancel the job, delete this | 24 // network and buffers the data in std::vector. To cancel the job, delete this |
25 // object. | 25 // object. |
26 class DartLibraryLoader::Job : public DartDependency, | 26 class DartLibraryLoader::Job : public DartDependency, |
27 public DataPipeDrainer::Client { | 27 public DataPipeDrainer::Client { |
28 public: | 28 public: |
29 Job(DartLibraryLoader* loader, const String& name) | 29 Job(DartLibraryLoader* loader, const std::string& name) |
30 : loader_(loader), name_(name), weak_factory_(this) { | 30 : loader_(loader), name_(name), weak_factory_(this) { |
31 loader->library_provider()->GetLibraryAsStream( | 31 loader->library_provider()->GetLibraryAsStream( |
32 name, base::Bind(&Job::OnStreamAvailable, weak_factory_.GetWeakPtr())); | 32 name, base::Bind(&Job::OnStreamAvailable, weak_factory_.GetWeakPtr())); |
33 } | 33 } |
34 | 34 |
35 const String& name() const { return name_; } | 35 const std::string& name() const { return name_; } |
36 | 36 |
37 protected: | 37 protected: |
38 DartLibraryLoader* loader_; | 38 DartLibraryLoader* loader_; |
39 // TODO(abarth): Should we be using SharedBuffer to buffer the data? | 39 // TODO(abarth): Should we be using SharedBuffer to buffer the data? |
40 std::vector<uint8_t> buffer_; | 40 std::vector<uint8_t> buffer_; |
41 | 41 |
42 private: | 42 private: |
43 void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) { | 43 void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) { |
44 if (!pipe.is_valid()) { | 44 if (!pipe.is_valid()) { |
45 loader_->DidFailJob(this); | 45 loader_->DidFailJob(this); |
46 return; | 46 return; |
47 } | 47 } |
48 drainer_ = adoptPtr(new DataPipeDrainer(this, pipe.Pass())); | 48 drainer_ = adoptPtr(new DataPipeDrainer(this, pipe.Pass())); |
49 } | 49 } |
50 | 50 |
51 // DataPipeDrainer::Client | 51 // DataPipeDrainer::Client |
52 void OnDataAvailable(const void* data, size_t num_bytes) override { | 52 void OnDataAvailable(const void* data, size_t num_bytes) override { |
53 const uint8_t* bytes = static_cast<const uint8_t*>(data); | 53 const uint8_t* bytes = static_cast<const uint8_t*>(data); |
54 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); | 54 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); |
55 } | 55 } |
56 // Subclasses must implement OnDataComplete. | 56 // Subclasses must implement OnDataComplete. |
57 | 57 |
58 String name_; | 58 std::string name_; |
59 OwnPtr<DataPipeDrainer> drainer_; | 59 OwnPtr<DataPipeDrainer> drainer_; |
60 | 60 |
61 base::WeakPtrFactory<Job> weak_factory_; | 61 base::WeakPtrFactory<Job> weak_factory_; |
62 }; | 62 }; |
63 | 63 |
64 class DartLibraryLoader::ImportJob : public Job { | 64 class DartLibraryLoader::ImportJob : public Job { |
65 public: | 65 public: |
66 ImportJob(DartLibraryLoader* loader, const String& name) : Job(loader, name) { | 66 ImportJob(DartLibraryLoader* loader, const std::string& name) : Job(loader, na
me) { |
67 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::ImportJob", this, "url", | 67 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::ImportJob", this, "url", |
68 name.ascii().toStdString()); | 68 name); |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 // DataPipeDrainer::Client | 72 // DataPipeDrainer::Client |
73 void OnDataComplete() override { | 73 void OnDataComplete() override { |
74 TRACE_EVENT_ASYNC_END0("sky", "DartLibraryLoader::ImportJob", this); | 74 TRACE_EVENT_ASYNC_END0("sky", "DartLibraryLoader::ImportJob", this); |
75 loader_->DidCompleteImportJob(this, buffer_); | 75 loader_->DidCompleteImportJob(this, buffer_); |
76 } | 76 } |
77 }; | 77 }; |
78 | 78 |
79 class DartLibraryLoader::SourceJob : public Job { | 79 class DartLibraryLoader::SourceJob : public Job { |
80 public: | 80 public: |
81 SourceJob(DartLibraryLoader* loader, const String& name, Dart_Handle library) | 81 SourceJob(DartLibraryLoader* loader, const std::string& name, Dart_Handle libr
ary) |
82 : Job(loader, name), library_(loader->dart_state(), library) { | 82 : Job(loader, name), library_(loader->dart_state(), library) { |
83 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::SourceJob", this, "url", | 83 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::SourceJob", this, "url", |
84 name.ascii().toStdString()); | 84 name); |
85 } | 85 } |
86 | 86 |
87 Dart_PersistentHandle library() const { return library_.value(); } | 87 Dart_PersistentHandle library() const { return library_.value(); } |
88 | 88 |
89 private: | 89 private: |
90 // DataPipeDrainer::Client | 90 // DataPipeDrainer::Client |
91 void OnDataComplete() override { | 91 void OnDataComplete() override { |
92 TRACE_EVENT_ASYNC_END0("sky", "DartLibraryLoader::SourceJob", this); | 92 TRACE_EVENT_ASYNC_END0("sky", "DartLibraryLoader::SourceJob", this); |
93 loader_->DidCompleteSourceJob(this, buffer_); | 93 loader_->DidCompleteSourceJob(this, buffer_); |
94 } | 94 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 200 |
201 void DartLibraryLoader::WaitForDependencies( | 201 void DartLibraryLoader::WaitForDependencies( |
202 const HashSet<DartDependency*>& dependencies, | 202 const HashSet<DartDependency*>& dependencies, |
203 const base::Closure& callback) { | 203 const base::Closure& callback) { |
204 if (dependencies.isEmpty()) | 204 if (dependencies.isEmpty()) |
205 return callback.Run(); | 205 return callback.Run(); |
206 dependency_watchers_.add( | 206 dependency_watchers_.add( |
207 adoptPtr(new DependencyWatcher(dependencies, callback))); | 207 adoptPtr(new DependencyWatcher(dependencies, callback))); |
208 } | 208 } |
209 | 209 |
210 void DartLibraryLoader::LoadLibrary(const String& name) { | 210 void DartLibraryLoader::LoadLibrary(const std::string& name) { |
211 const auto& result = pending_libraries_.add(name, nullptr); | 211 const auto& result = pending_libraries_.insert(std::make_pair(name, nullptr)); |
212 if (result.isNewEntry) { | 212 if (result.second) { |
| 213 // New entry. |
213 OwnPtr<Job> job = adoptPtr(new ImportJob(this, name)); | 214 OwnPtr<Job> job = adoptPtr(new ImportJob(this, name)); |
214 result.storedValue->value = job.get(); | 215 result.first->second = job.get(); |
215 jobs_.add(job.release()); | 216 jobs_.add(job.release()); |
216 } | 217 } |
217 if (dependency_catcher_) | 218 if (dependency_catcher_) |
218 dependency_catcher_->AddDependency(result.storedValue->value); | 219 dependency_catcher_->AddDependency(result.first->second); |
219 } | 220 } |
220 | 221 |
221 Dart_Handle DartLibraryLoader::Import(Dart_Handle library, Dart_Handle url) { | 222 Dart_Handle DartLibraryLoader::Import(Dart_Handle library, Dart_Handle url) { |
222 LoadLibrary(StringFromDart(url)); | 223 LoadLibrary(StdStringFromDart(url)); |
223 return Dart_True(); | 224 return Dart_True(); |
224 } | 225 } |
225 | 226 |
226 Dart_Handle DartLibraryLoader::Source(Dart_Handle library, Dart_Handle url) { | 227 Dart_Handle DartLibraryLoader::Source(Dart_Handle library, Dart_Handle url) { |
227 OwnPtr<Job> job = adoptPtr(new SourceJob(this, StringFromDart(url), library)); | 228 OwnPtr<Job> job = |
| 229 adoptPtr(new SourceJob(this, StdStringFromDart(url), library)); |
228 if (dependency_catcher_) | 230 if (dependency_catcher_) |
229 dependency_catcher_->AddDependency(job.get()); | 231 dependency_catcher_->AddDependency(job.get()); |
230 jobs_.add(job.release()); | 232 jobs_.add(job.release()); |
231 return Dart_True(); | 233 return Dart_True(); |
232 } | 234 } |
233 | 235 |
234 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, | 236 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, |
235 Dart_Handle url) { | 237 Dart_Handle url) { |
236 return library_provider_->CanonicalizeURL(library, url); | 238 return library_provider_->CanonicalizeURL(library, url); |
237 } | 239 } |
238 | 240 |
239 void DartLibraryLoader::DidCompleteImportJob( | 241 void DartLibraryLoader::DidCompleteImportJob( |
240 ImportJob* job, | 242 ImportJob* job, |
241 const std::vector<uint8_t>& buffer) { | 243 const std::vector<uint8_t>& buffer) { |
242 DartIsolateScope scope(dart_state_->isolate()); | 244 DartIsolateScope scope(dart_state_->isolate()); |
243 DartApiScope api_scope; | 245 DartApiScope api_scope; |
244 | 246 |
245 WatcherSignaler watcher_signaler(*this, job); | 247 WatcherSignaler watcher_signaler(*this, job); |
246 | 248 |
247 Dart_Handle result = Dart_LoadLibrary( | 249 Dart_Handle result = Dart_LoadLibrary( |
248 StringToDart(dart_state_, job->name()), | 250 StdStringToDart(job->name()), |
249 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 251 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
250 if (Dart_IsError(result)) { | 252 if (Dart_IsError(result)) { |
251 LOG(ERROR) << "Error Loading " << job->name().utf8().data() << " " | 253 LOG(ERROR) << "Error Loading " << job->name() << " " |
252 << Dart_GetError(result); | 254 << Dart_GetError(result); |
253 } | 255 } |
254 | 256 |
255 pending_libraries_.remove(job->name()); | 257 pending_libraries_.erase(job->name()); |
256 jobs_.remove(job); | 258 jobs_.remove(job); |
257 } | 259 } |
258 | 260 |
259 void DartLibraryLoader::DidCompleteSourceJob( | 261 void DartLibraryLoader::DidCompleteSourceJob( |
260 SourceJob* job, | 262 SourceJob* job, |
261 const std::vector<uint8_t>& buffer) { | 263 const std::vector<uint8_t>& buffer) { |
262 DartIsolateScope scope(dart_state_->isolate()); | 264 DartIsolateScope scope(dart_state_->isolate()); |
263 DartApiScope api_scope; | 265 DartApiScope api_scope; |
264 | 266 |
265 WatcherSignaler watcher_signaler(*this, job); | 267 WatcherSignaler watcher_signaler(*this, job); |
266 | 268 |
267 Dart_Handle result = Dart_LoadSource( | 269 Dart_Handle result = Dart_LoadSource( |
268 Dart_HandleFromPersistent(job->library()), | 270 Dart_HandleFromPersistent(job->library()), |
269 StringToDart(dart_state_, job->name()), | 271 StdStringToDart(job->name()), |
270 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 272 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
271 | 273 |
272 if (Dart_IsError(result)) { | 274 if (Dart_IsError(result)) { |
273 LOG(ERROR) << "Error Loading " << job->name().utf8().data() << " " | 275 LOG(ERROR) << "Error Loading " << job->name() << " " |
274 << Dart_GetError(result); | 276 << Dart_GetError(result); |
275 } | 277 } |
276 | 278 |
277 jobs_.remove(job); | 279 jobs_.remove(job); |
278 } | 280 } |
279 | 281 |
280 void DartLibraryLoader::DidFailJob(Job* job) { | 282 void DartLibraryLoader::DidFailJob(Job* job) { |
281 DartIsolateScope scope(dart_state_->isolate()); | 283 DartIsolateScope scope(dart_state_->isolate()); |
282 DartApiScope api_scope; | 284 DartApiScope api_scope; |
283 | 285 |
284 WatcherSignaler watcher_signaler(*this, job); | 286 WatcherSignaler watcher_signaler(*this, job); |
285 | 287 |
286 LOG(ERROR) << "Library Load failed: " << job->name().utf8().data(); | 288 LOG(ERROR) << "Library Load failed: " << job->name(); |
287 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? | 289 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? |
288 | 290 |
289 jobs_.remove(job); | 291 jobs_.remove(job); |
290 } | 292 } |
291 | 293 |
292 } // namespace blink | 294 } // namespace blink |
OLD | NEW |