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

Side by Side Diff: sky/engine/tonic/dart_library_loader.cc

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

Powered by Google App Engine
This is Rietveld 408576698