| 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 namespace { |
| 24 // Helper to erase a T* from a container of std::unique_ptr<T>s. |
| 25 template<typename T, typename C> |
| 26 void EraseUniquePtr(C& container, T* item) { |
| 27 std::unique_ptr<T> key = std::unique_ptr<T>(item); |
| 28 container.erase(key); |
| 29 key.release(); |
| 30 } |
| 31 } |
| 32 |
| 23 // A DartLibraryLoader::Job represents a network load. It fetches data from the | 33 // 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 | 34 // network and buffers the data in std::vector. To cancel the job, delete this |
| 25 // object. | 35 // object. |
| 26 class DartLibraryLoader::Job : public DartDependency, | 36 class DartLibraryLoader::Job : public DartDependency, |
| 27 public DataPipeDrainer::Client { | 37 public DataPipeDrainer::Client { |
| 28 public: | 38 public: |
| 29 Job(DartLibraryLoader* loader, const std::string& name) | 39 Job(DartLibraryLoader* loader, const std::string& name) |
| 30 : loader_(loader), name_(name), weak_factory_(this) { | 40 : loader_(loader), name_(name), weak_factory_(this) { |
| 31 loader->library_provider()->GetLibraryAsStream( | 41 loader->library_provider()->GetLibraryAsStream( |
| 32 name, base::Bind(&Job::OnStreamAvailable, weak_factory_.GetWeakPtr())); | 42 name, base::Bind(&Job::OnStreamAvailable, weak_factory_.GetWeakPtr())); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 105 |
| 96 DartPersistentValue library_; | 106 DartPersistentValue library_; |
| 97 }; | 107 }; |
| 98 | 108 |
| 99 // A DependencyWatcher represents a request to watch for when a given set of | 109 // A DependencyWatcher represents a request to watch for when a given set of |
| 100 // dependencies (either libraries or parts of libraries) have finished loading. | 110 // dependencies (either libraries or parts of libraries) have finished loading. |
| 101 // When the dependencies are satisfied (including transitive dependencies), then | 111 // When the dependencies are satisfied (including transitive dependencies), then |
| 102 // the |callback| will be invoked. | 112 // the |callback| will be invoked. |
| 103 class DartLibraryLoader::DependencyWatcher { | 113 class DartLibraryLoader::DependencyWatcher { |
| 104 public: | 114 public: |
| 105 DependencyWatcher(const HashSet<DartDependency*>& dependencies, | 115 DependencyWatcher(const std::unordered_set<DartDependency*>& dependencies, |
| 106 const base::Closure& callback) | 116 const base::Closure& callback) |
| 107 : dependencies_(dependencies), callback_(callback) { | 117 : dependencies_(dependencies), callback_(callback) { |
| 108 DCHECK(!dependencies_.isEmpty()); | 118 DCHECK(!dependencies_.empty()); |
| 109 } | 119 } |
| 110 | 120 |
| 111 bool DidResolveDependency(DartDependency* resolved_dependency, | 121 bool DidResolveDependency( |
| 112 const HashSet<DartDependency*>& new_dependencies) { | 122 DartDependency* resolved_dependency, |
| 123 const std::unordered_set<DartDependency*>& new_dependencies) { |
| 113 const auto& it = dependencies_.find(resolved_dependency); | 124 const auto& it = dependencies_.find(resolved_dependency); |
| 114 if (it == dependencies_.end()) | 125 if (it == dependencies_.end()) |
| 115 return false; | 126 return false; |
| 116 dependencies_.remove(it); | 127 dependencies_.erase(it); |
| 117 for (const auto& dependency : new_dependencies) | 128 for (const auto& dependency : new_dependencies) |
| 118 dependencies_.add(dependency); | 129 dependencies_.insert(dependency); |
| 119 return dependencies_.isEmpty(); | 130 return dependencies_.empty(); |
| 120 } | 131 } |
| 121 | 132 |
| 122 const base::Closure& callback() const { return callback_; } | 133 const base::Closure& callback() const { return callback_; } |
| 123 | 134 |
| 124 private: | 135 private: |
| 125 HashSet<DartDependency*> dependencies_; | 136 std::unordered_set<DartDependency*> dependencies_; |
| 126 base::Closure callback_; | 137 base::Closure callback_; |
| 127 }; | 138 }; |
| 128 | 139 |
| 129 // A WatcherSignaler is responsible for signaling DependencyWatchers when their | 140 // A WatcherSignaler is responsible for signaling DependencyWatchers when their |
| 130 // dependencies resolve and for calling the DependencyWatcher's callback. We use | 141 // dependencies resolve and for calling the DependencyWatcher's callback. We use |
| 131 // a separate object of this task because we want to carefully manage when we | 142 // a separate object of this task because we want to carefully manage when we |
| 132 // call the callbacks, which can call into us again reentrantly. | 143 // call the callbacks, which can call into us again reentrantly. |
| 133 // | 144 // |
| 134 // WatcherSignaler is designed to be placed on the stack as a RAII. After its | 145 // WatcherSignaler is designed to be placed on the stack as a RAII. After its |
| 135 // destructor runs, we might have executed aribitrary script. | 146 // destructor runs, we might have executed aribitrary script. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 149 completed_watchers.push_back(watcher.get()); | 160 completed_watchers.push_back(watcher.get()); |
| 150 } | 161 } |
| 151 | 162 |
| 152 // Notice that we remove the dependency catcher and extract all the | 163 // Notice that we remove the dependency catcher and extract all the |
| 153 // callbacks before running any of them. We don't want to be re-entered | 164 // callbacks before running any of them. We don't want to be re-entered |
| 154 // below the callbacks and end up in an inconsistent state. | 165 // below the callbacks and end up in an inconsistent state. |
| 155 catcher_.clear(); | 166 catcher_.clear(); |
| 156 std::vector<base::Closure> callbacks; | 167 std::vector<base::Closure> callbacks; |
| 157 for (const auto& watcher : completed_watchers) { | 168 for (const auto& watcher : completed_watchers) { |
| 158 callbacks.push_back(watcher->callback()); | 169 callbacks.push_back(watcher->callback()); |
| 159 loader_.dependency_watchers_.remove(watcher); | 170 EraseUniquePtr(loader_.dependency_watchers_, watcher); |
| 160 } | 171 } |
| 161 | 172 |
| 162 // Finally, run all the callbacks while touching only data on the stack. | 173 // Finally, run all the callbacks while touching only data on the stack. |
| 163 for (const auto& callback : callbacks) | 174 for (const auto& callback : callbacks) |
| 164 callback.Run(); | 175 callback.Run(); |
| 165 } | 176 } |
| 166 | 177 |
| 167 private: | 178 private: |
| 168 DartLibraryLoader& loader_; | 179 DartLibraryLoader& loader_; |
| 169 OwnPtr<DartDependencyCatcher> catcher_; | 180 OwnPtr<DartDependencyCatcher> catcher_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 192 } | 203 } |
| 193 if (tag == Dart_kSourceTag) { | 204 if (tag == Dart_kSourceTag) { |
| 194 CHECK(WTF::isMainThread()); | 205 CHECK(WTF::isMainThread()); |
| 195 return DartState::Current()->library_loader().Source(library, url); | 206 return DartState::Current()->library_loader().Source(library, url); |
| 196 } | 207 } |
| 197 DCHECK(false); | 208 DCHECK(false); |
| 198 return Dart_NewApiError("Unknown library tag."); | 209 return Dart_NewApiError("Unknown library tag."); |
| 199 } | 210 } |
| 200 | 211 |
| 201 void DartLibraryLoader::WaitForDependencies( | 212 void DartLibraryLoader::WaitForDependencies( |
| 202 const HashSet<DartDependency*>& dependencies, | 213 const std::unordered_set<DartDependency*>& dependencies, |
| 203 const base::Closure& callback) { | 214 const base::Closure& callback) { |
| 204 if (dependencies.isEmpty()) | 215 if (dependencies.empty()) |
| 205 return callback.Run(); | 216 return callback.Run(); |
| 206 dependency_watchers_.add( | 217 dependency_watchers_.insert( |
| 207 adoptPtr(new DependencyWatcher(dependencies, callback))); | 218 std::unique_ptr<DependencyWatcher>( |
| 219 new DependencyWatcher(dependencies, callback))); |
| 208 } | 220 } |
| 209 | 221 |
| 210 void DartLibraryLoader::LoadLibrary(const std::string& name) { | 222 void DartLibraryLoader::LoadLibrary(const std::string& name) { |
| 211 const auto& result = pending_libraries_.insert(std::make_pair(name, nullptr)); | 223 const auto& result = pending_libraries_.insert(std::make_pair(name, nullptr)); |
| 212 if (result.second) { | 224 if (result.second) { |
| 213 // New entry. | 225 // New entry. |
| 214 OwnPtr<Job> job = adoptPtr(new ImportJob(this, name)); | 226 std::unique_ptr<Job> job = std::unique_ptr<Job>(new ImportJob(this, name)); |
| 215 result.first->second = job.get(); | 227 result.first->second = job.get(); |
| 216 jobs_.add(job.release()); | 228 jobs_.insert(std::move(job)); |
| 217 } | 229 } |
| 218 if (dependency_catcher_) | 230 if (dependency_catcher_) |
| 219 dependency_catcher_->AddDependency(result.first->second); | 231 dependency_catcher_->AddDependency(result.first->second); |
| 220 } | 232 } |
| 221 | 233 |
| 222 Dart_Handle DartLibraryLoader::Import(Dart_Handle library, Dart_Handle url) { | 234 Dart_Handle DartLibraryLoader::Import(Dart_Handle library, Dart_Handle url) { |
| 223 LoadLibrary(StdStringFromDart(url)); | 235 LoadLibrary(StdStringFromDart(url)); |
| 224 return Dart_True(); | 236 return Dart_True(); |
| 225 } | 237 } |
| 226 | 238 |
| 227 Dart_Handle DartLibraryLoader::Source(Dart_Handle library, Dart_Handle url) { | 239 Dart_Handle DartLibraryLoader::Source(Dart_Handle library, Dart_Handle url) { |
| 228 OwnPtr<Job> job = | 240 std::unique_ptr<Job> job = std::unique_ptr<Job>( |
| 229 adoptPtr(new SourceJob(this, StdStringFromDart(url), library)); | 241 new SourceJob(this, StdStringFromDart(url), library)); |
| 230 if (dependency_catcher_) | 242 if (dependency_catcher_) |
| 231 dependency_catcher_->AddDependency(job.get()); | 243 dependency_catcher_->AddDependency(job.get()); |
| 232 jobs_.add(job.release()); | 244 jobs_.insert(std::move(job)); |
| 233 return Dart_True(); | 245 return Dart_True(); |
| 234 } | 246 } |
| 235 | 247 |
| 236 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, | 248 Dart_Handle DartLibraryLoader::CanonicalizeURL(Dart_Handle library, |
| 237 Dart_Handle url) { | 249 Dart_Handle url) { |
| 238 return library_provider_->CanonicalizeURL(library, url); | 250 return library_provider_->CanonicalizeURL(library, url); |
| 239 } | 251 } |
| 240 | 252 |
| 241 void DartLibraryLoader::DidCompleteImportJob( | 253 void DartLibraryLoader::DidCompleteImportJob( |
| 242 ImportJob* job, | 254 ImportJob* job, |
| 243 const std::vector<uint8_t>& buffer) { | 255 const std::vector<uint8_t>& buffer) { |
| 244 DartIsolateScope scope(dart_state_->isolate()); | 256 DartIsolateScope scope(dart_state_->isolate()); |
| 245 DartApiScope api_scope; | 257 DartApiScope api_scope; |
| 246 | 258 |
| 247 WatcherSignaler watcher_signaler(*this, job); | 259 WatcherSignaler watcher_signaler(*this, job); |
| 248 | 260 |
| 249 Dart_Handle result = Dart_LoadLibrary( | 261 Dart_Handle result = Dart_LoadLibrary( |
| 250 StdStringToDart(job->name()), | 262 StdStringToDart(job->name()), |
| 251 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 263 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
| 252 if (Dart_IsError(result)) { | 264 if (Dart_IsError(result)) { |
| 253 LOG(ERROR) << "Error Loading " << job->name() << " " | 265 LOG(ERROR) << "Error Loading " << job->name() << " " |
| 254 << Dart_GetError(result); | 266 << Dart_GetError(result); |
| 255 } | 267 } |
| 256 | 268 |
| 257 pending_libraries_.erase(job->name()); | 269 pending_libraries_.erase(job->name()); |
| 258 jobs_.remove(job); | 270 EraseUniquePtr<Job>(jobs_, job); |
| 259 } | 271 } |
| 260 | 272 |
| 261 void DartLibraryLoader::DidCompleteSourceJob( | 273 void DartLibraryLoader::DidCompleteSourceJob( |
| 262 SourceJob* job, | 274 SourceJob* job, |
| 263 const std::vector<uint8_t>& buffer) { | 275 const std::vector<uint8_t>& buffer) { |
| 264 DartIsolateScope scope(dart_state_->isolate()); | 276 DartIsolateScope scope(dart_state_->isolate()); |
| 265 DartApiScope api_scope; | 277 DartApiScope api_scope; |
| 266 | 278 |
| 267 WatcherSignaler watcher_signaler(*this, job); | 279 WatcherSignaler watcher_signaler(*this, job); |
| 268 | 280 |
| 269 Dart_Handle result = Dart_LoadSource( | 281 Dart_Handle result = Dart_LoadSource( |
| 270 Dart_HandleFromPersistent(job->library()), | 282 Dart_HandleFromPersistent(job->library()), |
| 271 StdStringToDart(job->name()), | 283 StdStringToDart(job->name()), |
| 272 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); | 284 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0); |
| 273 | 285 |
| 274 if (Dart_IsError(result)) { | 286 if (Dart_IsError(result)) { |
| 275 LOG(ERROR) << "Error Loading " << job->name() << " " | 287 LOG(ERROR) << "Error Loading " << job->name() << " " |
| 276 << Dart_GetError(result); | 288 << Dart_GetError(result); |
| 277 } | 289 } |
| 278 | 290 |
| 279 jobs_.remove(job); | 291 EraseUniquePtr<Job>(jobs_, job); |
| 280 } | 292 } |
| 281 | 293 |
| 282 void DartLibraryLoader::DidFailJob(Job* job) { | 294 void DartLibraryLoader::DidFailJob(Job* job) { |
| 283 DartIsolateScope scope(dart_state_->isolate()); | 295 DartIsolateScope scope(dart_state_->isolate()); |
| 284 DartApiScope api_scope; | 296 DartApiScope api_scope; |
| 285 | 297 |
| 286 WatcherSignaler watcher_signaler(*this, job); | 298 WatcherSignaler watcher_signaler(*this, job); |
| 287 | 299 |
| 288 LOG(ERROR) << "Library Load failed: " << job->name(); | 300 LOG(ERROR) << "Library Load failed: " << job->name(); |
| 289 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? | 301 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? |
| 290 | 302 |
| 291 jobs_.remove(job); | 303 EraseUniquePtr<Job>(jobs_, job); |
| 292 } | 304 } |
| 293 | 305 |
| 294 } // namespace blink | 306 } // namespace blink |
| OLD | NEW |