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/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 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 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 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 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 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); | 53 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
|
kulakowski
2015/07/15 15:00:01
Why change |static_cast| to |reinterpret_cast|?
| |
| 54 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); | |
|
abarth-chromium
2015/07/15 14:49:32
Does this do exponential growth?
kulakowski
2015/07/15 15:00:01
Yes, std::vector guarantees it.
| |
| 54 } | 55 } |
| 55 // Subclasses must implement OnDataComplete. | 56 // Subclasses must implement OnDataComplete. |
| 56 | 57 |
| 57 String name_; | 58 String name_; |
| 58 OwnPtr<DataPipeDrainer> drainer_; | 59 OwnPtr<DataPipeDrainer> drainer_; |
| 59 | 60 |
| 60 base::WeakPtrFactory<Job> weak_factory_; | 61 base::WeakPtrFactory<Job> weak_factory_; |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 class DartLibraryLoader::ImportJob : public Job { | 64 class DartLibraryLoader::ImportJob : public Job { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 // destructor runs, we might have executed aribitrary script. | 135 // destructor runs, we might have executed aribitrary script. |
| 135 class DartLibraryLoader::WatcherSignaler { | 136 class DartLibraryLoader::WatcherSignaler { |
| 136 public: | 137 public: |
| 137 WatcherSignaler(DartLibraryLoader& loader, | 138 WatcherSignaler(DartLibraryLoader& loader, |
| 138 DartDependency* resolved_dependency) | 139 DartDependency* resolved_dependency) |
| 139 : loader_(loader), | 140 : loader_(loader), |
| 140 catcher_(adoptPtr(new DartDependencyCatcher(loader))), | 141 catcher_(adoptPtr(new DartDependencyCatcher(loader))), |
| 141 resolved_dependency_(resolved_dependency) {} | 142 resolved_dependency_(resolved_dependency) {} |
| 142 | 143 |
| 143 ~WatcherSignaler() { | 144 ~WatcherSignaler() { |
| 144 Vector<DependencyWatcher*> completed_watchers; | 145 std::vector<DependencyWatcher*> completed_watchers; |
| 145 for (const auto& watcher : loader_.dependency_watchers_) { | 146 for (const auto& watcher : loader_.dependency_watchers_) { |
| 146 if (watcher->DidResolveDependency(resolved_dependency_, | 147 if (watcher->DidResolveDependency(resolved_dependency_, |
| 147 catcher_->dependencies())) | 148 catcher_->dependencies())) |
| 148 completed_watchers.append(watcher.get()); | 149 completed_watchers.push_back(watcher.get()); |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Notice that we remove the dependency catcher and extract all the | 152 // Notice that we remove the dependency catcher and extract all the |
| 152 // callbacks before running any of them. We don't want to be re-entered | 153 // callbacks before running any of them. We don't want to be re-entered |
| 153 // below the callbacks and end up in an inconsistent state. | 154 // below the callbacks and end up in an inconsistent state. |
| 154 catcher_.clear(); | 155 catcher_.clear(); |
| 155 Vector<base::Closure> callbacks; | 156 std::vector<base::Closure> callbacks; |
| 156 for (const auto& watcher : completed_watchers) { | 157 for (const auto& watcher : completed_watchers) { |
| 157 callbacks.append(watcher->callback()); | 158 callbacks.push_back(watcher->callback()); |
| 158 loader_.dependency_watchers_.remove(watcher); | 159 loader_.dependency_watchers_.remove(watcher); |
| 159 } | 160 } |
| 160 | 161 |
| 161 // Finally, run all the callbacks while touching only data on the stack. | 162 // Finally, run all the callbacks while touching only data on the stack. |
| 162 for (const auto& callback : callbacks) | 163 for (const auto& callback : callbacks) |
| 163 callback.Run(); | 164 callback.Run(); |
| 164 } | 165 } |
| 165 | 166 |
| 166 private: | 167 private: |
| 167 DartLibraryLoader& loader_; | 168 DartLibraryLoader& loader_; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 dependency_catcher_->AddDependency(job.get()); | 229 dependency_catcher_->AddDependency(job.get()); |
| 229 jobs_.add(job.release()); | 230 jobs_.add(job.release()); |
| 230 return Dart_True(); | 231 return Dart_True(); |
| 231 } | 232 } |
| 232 | 233 |
| 233 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, | 234 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, |
| 234 Dart_Handle url) { | 235 Dart_Handle url) { |
| 235 return library_provider_->CanonicalizeURL(library, url); | 236 return library_provider_->CanonicalizeURL(library, url); |
| 236 } | 237 } |
| 237 | 238 |
| 238 void DartLibraryLoader::DidCompleteImportJob(ImportJob* job, | 239 void DartLibraryLoader::DidCompleteImportJob( |
| 239 const Vector<uint8_t>& buffer) { | 240 ImportJob* job, |
| 241 const std::vector<uint8_t>& buffer) { | |
| 240 DartIsolateScope scope(dart_state_->isolate()); | 242 DartIsolateScope scope(dart_state_->isolate()); |
| 241 DartApiScope api_scope; | 243 DartApiScope api_scope; |
| 242 | 244 |
| 243 WatcherSignaler watcher_signaler(*this, job); | 245 WatcherSignaler watcher_signaler(*this, job); |
| 244 | 246 |
| 245 Dart_Handle result = Dart_LoadLibrary( | 247 Dart_Handle result = Dart_LoadLibrary( |
| 246 StringToDart(dart_state_, job->name()), | 248 StringToDart(dart_state_, job->name()), |
| 247 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 249 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
| 248 if (Dart_IsError(result)) { | 250 if (Dart_IsError(result)) { |
| 249 LOG(ERROR) << "Error Loading " << job->name().utf8().data() << " " | 251 LOG(ERROR) << "Error Loading " << job->name().utf8().data() << " " |
| 250 << Dart_GetError(result); | 252 << Dart_GetError(result); |
| 251 } | 253 } |
| 252 | 254 |
| 253 pending_libraries_.remove(job->name()); | 255 pending_libraries_.remove(job->name()); |
| 254 jobs_.remove(job); | 256 jobs_.remove(job); |
| 255 } | 257 } |
| 256 | 258 |
| 257 void DartLibraryLoader::DidCompleteSourceJob(SourceJob* job, | 259 void DartLibraryLoader::DidCompleteSourceJob( |
| 258 const Vector<uint8_t>& buffer) { | 260 SourceJob* job, |
| 261 const std::vector<uint8_t>& buffer) { | |
| 259 DartIsolateScope scope(dart_state_->isolate()); | 262 DartIsolateScope scope(dart_state_->isolate()); |
| 260 DartApiScope api_scope; | 263 DartApiScope api_scope; |
| 261 | 264 |
| 262 WatcherSignaler watcher_signaler(*this, job); | 265 WatcherSignaler watcher_signaler(*this, job); |
| 263 | 266 |
| 264 Dart_Handle result = Dart_LoadSource( | 267 Dart_Handle result = Dart_LoadSource( |
| 265 Dart_HandleFromPersistent(job->library()), | 268 Dart_HandleFromPersistent(job->library()), |
| 266 StringToDart(dart_state_, job->name()), | 269 StringToDart(dart_state_, job->name()), |
| 267 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 270 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
| 268 | 271 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 280 | 283 |
| 281 WatcherSignaler watcher_signaler(*this, job); | 284 WatcherSignaler watcher_signaler(*this, job); |
| 282 | 285 |
| 283 LOG(ERROR) << "Library Load failed: " << job->name().utf8().data(); | 286 LOG(ERROR) << "Library Load failed: " << job->name().utf8().data(); |
| 284 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? | 287 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? |
| 285 | 288 |
| 286 jobs_.remove(job); | 289 jobs_.remove(job); |
| 287 } | 290 } |
| 288 | 291 |
| 289 } // namespace blink | 292 } // namespace blink |
| OLD | NEW |