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

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

Issue 926753002: Improve DartLoader error handling. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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/core/script/dart_loader.h ('k') | sky/engine/tonic/dart_error.cc » ('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/config.h" 5 #include "sky/engine/config.h"
6 #include "sky/engine/core/script/dart_loader.h" 6 #include "sky/engine/core/script/dart_loader.h"
7 7
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "mojo/common/data_pipe_drainer.h" 9 #include "mojo/common/data_pipe_drainer.h"
10 #include "sky/engine/core/script/dart_dependency_catcher.h" 10 #include "sky/engine/core/script/dart_dependency_catcher.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 const KURL& url() const { return url_; } 52 const KURL& url() const { return url_; }
53 53
54 protected: 54 protected:
55 DartLoader* loader_; 55 DartLoader* loader_;
56 // TODO(abarth): Should we be using SharedBuffer to buffer the data? 56 // TODO(abarth): Should we be using SharedBuffer to buffer the data?
57 Vector<uint8_t> buffer_; 57 Vector<uint8_t> buffer_;
58 58
59 private: 59 private:
60 // MojoFetcher::Client 60 // MojoFetcher::Client
61 void OnReceivedResponse(mojo::URLResponsePtr response) override { 61 void OnReceivedResponse(mojo::URLResponsePtr response) override {
62 if (response->status_code != 200) {
63 loader_->DidFailJob(this);
64 return;
65 }
62 // TODO(abarth): Handle network errors. 66 // TODO(abarth): Handle network errors.
63 drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass())); 67 drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass()));
64 } 68 }
65 69
66 // DataPipeDrainer::Client 70 // DataPipeDrainer::Client
67 void OnDataAvailable(const void* data, size_t num_bytes) override { 71 void OnDataAvailable(const void* data, size_t num_bytes) override {
68 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); 72 buffer_.append(static_cast<const uint8_t*>(data), num_bytes);
69 } 73 }
70 // Subclasses must implement OnDataComplete. 74 // Subclasses must implement OnDataComplete.
71 75
72 KURL url_; 76 KURL url_;
73 MojoFetcher fetcher_; 77 MojoFetcher fetcher_;
74 OwnPtr<DataPipeDrainer> drainer_; 78 OwnPtr<DataPipeDrainer> drainer_;
75 }; 79 };
76 80
77 class DartLoader::ImportJob : public Job { 81 class DartLoader::ImportJob : public Job {
78 public: 82 public:
79 using Job::Job; 83 using Job::Job;
80 84
81 private: 85 private:
82 // DataPipeDrainer::Client 86 // DataPipeDrainer::Client
83 void OnDataComplete() override { 87 void OnDataComplete() override {
84 loader_->DidCompleteImportJob(this, buffer_); 88 // DataPipeDrainer calls OnDataComplete regardless of errors.
89 if (!loader_->saw_error())
90 loader_->DidCompleteImportJob(this, buffer_);
85 } 91 }
86 }; 92 };
87 93
88 class DartLoader::SourceJob : public Job { 94 class DartLoader::SourceJob : public Job {
89 public: 95 public:
90 SourceJob(DartLoader* loader, const KURL& url, Dart_Handle library) 96 SourceJob(DartLoader* loader, const KURL& url, Dart_Handle library)
91 : Job(loader, url), library_(loader->dart_state(), library) {} 97 : Job(loader, url), library_(loader->dart_state(), library) {}
92 98
93 Dart_PersistentHandle library() const { return library_.value(); } 99 Dart_PersistentHandle library() const { return library_.value(); }
94 100
95 private: 101 private:
96 // DataPipeDrainer::Client 102 // DataPipeDrainer::Client
97 void OnDataComplete() override { 103 void OnDataComplete() override {
98 loader_->DidCompleteSourceJob(this, buffer_); 104 // DataPipeDrainer calls OnDataComplete regardless of errors.
105 if (!loader_->saw_error())
abarth-chromium 2015/02/13 23:50:29 Why do we only do this once for the entire lifetim
106 loader_->DidCompleteSourceJob(this, buffer_);
99 } 107 }
100 108
101 DartPersistentValue library_; 109 DartPersistentValue library_;
102 }; 110 };
103 111
104 // A DependencyWatcher represents a request to watch for when a given set of 112 // A DependencyWatcher represents a request to watch for when a given set of
105 // dependencies (either libraries or parts of libraries) have finished loading. 113 // dependencies (either libraries or parts of libraries) have finished loading.
106 // When the dependencies are satisfied (including transitive dependencies), then 114 // When the dependencies are satisfied (including transitive dependencies), then
107 // the |callback| will be invoked. 115 // the |callback| will be invoked.
108 class DartLoader::DependencyWatcher { 116 class DartLoader::DependencyWatcher {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 callback.Run(); 176 callback.Run();
169 } 177 }
170 178
171 private: 179 private:
172 DartLoader& loader_; 180 DartLoader& loader_;
173 OwnPtr<DartDependencyCatcher> catcher_; 181 OwnPtr<DartDependencyCatcher> catcher_;
174 DartDependency* resolved_dependency_; 182 DartDependency* resolved_dependency_;
175 }; 183 };
176 184
177 DartLoader::DartLoader(DartState* dart_state) 185 DartLoader::DartLoader(DartState* dart_state)
178 : dart_state_(dart_state->GetWeakPtr()), dependency_catcher_(nullptr) { 186 : dart_state_(dart_state->GetWeakPtr()),
187 dependency_catcher_(nullptr),
188 saw_error_(false) {
179 } 189 }
180 190
181 DartLoader::~DartLoader() { 191 DartLoader::~DartLoader() {
182 } 192 }
183 193
184 Dart_Handle DartLoader::HandleLibraryTag(Dart_LibraryTag tag, 194 Dart_Handle DartLoader::HandleLibraryTag(Dart_LibraryTag tag,
185 Dart_Handle library, 195 Dart_Handle library,
186 Dart_Handle url) { 196 Dart_Handle url) {
187 DCHECK(Dart_IsLibrary(library)); 197 DCHECK(Dart_IsLibrary(library));
188 DCHECK(Dart_IsString(url)); 198 DCHECK(Dart_IsString(url));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 KURL parsed_url(ParsedURLString, StringFromDart(url)); 234 KURL parsed_url(ParsedURLString, StringFromDart(url));
225 OwnPtr<Job> job = adoptPtr(new SourceJob(this, parsed_url, library)); 235 OwnPtr<Job> job = adoptPtr(new SourceJob(this, parsed_url, library));
226 if (dependency_catcher_) 236 if (dependency_catcher_)
227 dependency_catcher_->AddDependency(job.get()); 237 dependency_catcher_->AddDependency(job.get());
228 jobs_.add(job.release()); 238 jobs_.add(job.release());
229 return Dart_True(); 239 return Dart_True();
230 } 240 }
231 241
232 void DartLoader::DidCompleteImportJob(ImportJob* job, 242 void DartLoader::DidCompleteImportJob(ImportJob* job,
233 const Vector<uint8_t>& buffer) { 243 const Vector<uint8_t>& buffer) {
244 DCHECK(!saw_error_);
234 DCHECK(dart_state_); 245 DCHECK(dart_state_);
235 DartIsolateScope scope(dart_state_->isolate()); 246 DartIsolateScope scope(dart_state_->isolate());
236 DartApiScope api_scope; 247 DartApiScope api_scope;
237 248
238 WatcherSignaler watcher_signaler(*this, job); 249 WatcherSignaler watcher_signaler(*this, job);
239 250
240 String url_string = job->url().string(); 251 String url_string = job->url().string();
241 LogIfError(Dart_LoadLibrary( 252 LogIfError(Dart_LoadLibrary(
242 StringToDart(dart_state_.get(), url_string), 253 StringToDart(dart_state_.get(), url_string),
243 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0)); 254 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0));
244 255
245 pending_libraries_.remove(url_string); 256 pending_libraries_.remove(url_string);
246 jobs_.remove(job); 257 jobs_.remove(job);
247 } 258 }
248 259
249 void DartLoader::DidCompleteSourceJob(SourceJob* job, 260 void DartLoader::DidCompleteSourceJob(SourceJob* job,
250 const Vector<uint8_t>& buffer) { 261 const Vector<uint8_t>& buffer) {
262 DCHECK(!saw_error_);
251 DCHECK(dart_state_); 263 DCHECK(dart_state_);
252 DartIsolateScope scope(dart_state_->isolate()); 264 DartIsolateScope scope(dart_state_->isolate());
253 DartApiScope api_scope; 265 DartApiScope api_scope;
254 266
255 WatcherSignaler watcher_signaler(*this, job); 267 WatcherSignaler watcher_signaler(*this, job);
256 268
257 LogIfError(Dart_LoadSource( 269 LogIfError(Dart_LoadSource(
258 Dart_HandleFromPersistent(job->library()), 270 Dart_HandleFromPersistent(job->library()),
259 StringToDart(dart_state_.get(), job->url().string()), 271 StringToDart(dart_state_.get(), job->url().string()),
260 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0)); 272 Dart_NewStringFromUTF8(buffer.data(), buffer.size()), 0, 0));
261 273
262 jobs_.remove(job); 274 jobs_.remove(job);
263 } 275 }
264 276
277 void DartLoader::DidFailJob(Job* job) {
278 DCHECK(!saw_error_);
279 saw_error_ = true;
280 DCHECK(dart_state_);
281 DartIsolateScope scope(dart_state_->isolate());
282 DartApiScope api_scope;
283
284 WatcherSignaler watcher_signaler(*this, job);
285
286 LOG(ERROR) << "Library Load failed: " << job->url().string().utf8().data();
287 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case?
288
289 jobs_.remove(job);
290 }
291
265 } // namespace blink 292 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/core/script/dart_loader.h ('k') | sky/engine/tonic/dart_error.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698