| 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/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 "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "mojo/common/data_pipe_drainer.h" | 10 #include "mojo/common/data_pipe_drainer.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 // A DartLoader::Job represents a network load. It fetches data from the network | 44 // A DartLoader::Job represents a network load. It fetches data from the network |
| 45 // and buffers the data in Vector. To cancel the job, delete this object. | 45 // and buffers the data in Vector. To cancel the job, delete this object. |
| 46 class DartLoader::Job : public DartDependency, | 46 class DartLoader::Job : public DartDependency, |
| 47 public MojoFetcher::Client, | 47 public MojoFetcher::Client, |
| 48 public DataPipeDrainer::Client { | 48 public DataPipeDrainer::Client { |
| 49 public: | 49 public: |
| 50 Job(DartLoader* loader, const KURL& url) | 50 Job(DartLoader* loader, const KURL& url, mojo::URLResponsePtr response) |
| 51 : loader_(loader), url_(url), fetcher_(this, url) {} | 51 : loader_(loader), url_(url) |
| 52 { |
| 53 if (!response) { |
| 54 fetcher_ = adoptPtr(new MojoFetcher(this, url)); |
| 55 } else { |
| 56 OnReceivedResponse(response.Pass()); |
| 57 } |
| 58 } |
| 52 | 59 |
| 53 const KURL& url() const { return url_; } | 60 const KURL& url() const { return url_; } |
| 54 | 61 |
| 55 protected: | 62 protected: |
| 56 DartLoader* loader_; | 63 DartLoader* loader_; |
| 57 // TODO(abarth): Should we be using SharedBuffer to buffer the data? | 64 // TODO(abarth): Should we be using SharedBuffer to buffer the data? |
| 58 Vector<uint8_t> buffer_; | 65 Vector<uint8_t> buffer_; |
| 59 | 66 |
| 60 private: | 67 private: |
| 61 // MojoFetcher::Client | 68 // MojoFetcher::Client |
| 62 void OnReceivedResponse(mojo::URLResponsePtr response) override { | 69 void OnReceivedResponse(mojo::URLResponsePtr response) override { |
| 63 if (response->status_code != 200) { | 70 if (response->status_code != 200) { |
| 64 loader_->DidFailJob(this); | 71 loader_->DidFailJob(this); |
| 65 return; | 72 return; |
| 66 } | 73 } |
| 67 drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass())); | 74 drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass())); |
| 68 } | 75 } |
| 69 | 76 |
| 70 // DataPipeDrainer::Client | 77 // DataPipeDrainer::Client |
| 71 void OnDataAvailable(const void* data, size_t num_bytes) override { | 78 void OnDataAvailable(const void* data, size_t num_bytes) override { |
| 72 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); | 79 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); |
| 73 } | 80 } |
| 74 // Subclasses must implement OnDataComplete. | 81 // Subclasses must implement OnDataComplete. |
| 75 | 82 |
| 76 KURL url_; | 83 KURL url_; |
| 77 MojoFetcher fetcher_; | 84 OwnPtr<MojoFetcher> fetcher_; |
| 78 OwnPtr<DataPipeDrainer> drainer_; | 85 OwnPtr<DataPipeDrainer> drainer_; |
| 79 }; | 86 }; |
| 80 | 87 |
| 81 class DartLoader::ImportJob : public Job { | 88 class DartLoader::ImportJob : public Job { |
| 82 public: | 89 public: |
| 83 ImportJob(DartLoader* loader, const KURL& url) | 90 ImportJob(DartLoader* loader, const KURL& url, mojo::URLResponsePtr response =
nullptr) |
| 84 : Job(loader, url) { | 91 : Job(loader, url, response.Pass()) { |
| 85 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLoader::ImportJob", this, | 92 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLoader::ImportJob", this, |
| 86 "url", url.string().ascii().toStdString()); | 93 "url", url.string().ascii().toStdString()); |
| 87 } | 94 } |
| 88 | 95 |
| 89 private: | 96 private: |
| 90 // DataPipeDrainer::Client | 97 // DataPipeDrainer::Client |
| 91 void OnDataComplete() override { | 98 void OnDataComplete() override { |
| 92 TRACE_EVENT_ASYNC_END0("sky", "DartLoader::ImportJob", this); | 99 TRACE_EVENT_ASYNC_END0("sky", "DartLoader::ImportJob", this); |
| 93 loader_->DidCompleteImportJob(this, buffer_); | 100 loader_->DidCompleteImportJob(this, buffer_); |
| 94 } | 101 } |
| 95 }; | 102 }; |
| 96 | 103 |
| 97 class DartLoader::SourceJob : public Job { | 104 class DartLoader::SourceJob : public Job { |
| 98 public: | 105 public: |
| 99 SourceJob(DartLoader* loader, const KURL& url, Dart_Handle library) | 106 SourceJob(DartLoader* loader, const KURL& url, Dart_Handle library) |
| 100 : Job(loader, url), library_(loader->dart_state(), library) { | 107 : Job(loader, url, nullptr), library_(loader->dart_state(), library) { |
| 101 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLoader::SourceJob", this, | 108 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLoader::SourceJob", this, |
| 102 "url", url.string().ascii().toStdString()); | 109 "url", url.string().ascii().toStdString()); |
| 103 } | 110 } |
| 104 | 111 |
| 105 Dart_PersistentHandle library() const { return library_.value(); } | 112 Dart_PersistentHandle library() const { return library_.value(); } |
| 106 | 113 |
| 107 private: | 114 private: |
| 108 // DataPipeDrainer::Client | 115 // DataPipeDrainer::Client |
| 109 void OnDataComplete() override { | 116 void OnDataComplete() override { |
| 110 TRACE_EVENT_ASYNC_END0("sky", "DartLoader::SourceJob", this); | 117 TRACE_EVENT_ASYNC_END0("sky", "DartLoader::SourceJob", this); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 231 |
| 225 void DartLoader::WaitForDependencies( | 232 void DartLoader::WaitForDependencies( |
| 226 const HashSet<DartDependency*>& dependencies, | 233 const HashSet<DartDependency*>& dependencies, |
| 227 const base::Closure& callback) { | 234 const base::Closure& callback) { |
| 228 if (dependencies.isEmpty()) | 235 if (dependencies.isEmpty()) |
| 229 return callback.Run(); | 236 return callback.Run(); |
| 230 dependency_watchers_.add( | 237 dependency_watchers_.add( |
| 231 adoptPtr(new DependencyWatcher(dependencies, callback))); | 238 adoptPtr(new DependencyWatcher(dependencies, callback))); |
| 232 } | 239 } |
| 233 | 240 |
| 234 void DartLoader::LoadLibrary(const KURL& url) { | 241 void DartLoader::LoadLibrary(const KURL& url, mojo::URLResponsePtr response) { |
| 235 const auto& result = pending_libraries_.add(url.string(), nullptr); | 242 const auto& result = pending_libraries_.add(url.string(), nullptr); |
| 236 if (result.isNewEntry) { | 243 if (result.isNewEntry) { |
| 237 OwnPtr<Job> job = adoptPtr(new ImportJob(this, url)); | 244 OwnPtr<Job> job = adoptPtr(new ImportJob(this, url)); |
| 238 result.storedValue->value = job.get(); | 245 result.storedValue->value = job.get(); |
| 239 jobs_.add(job.release()); | 246 jobs_.add(job.release()); |
| 240 } | 247 } |
| 241 if (dependency_catcher_) | 248 if (dependency_catcher_) |
| 242 dependency_catcher_->AddDependency(result.storedValue->value); | 249 dependency_catcher_->AddDependency(result.storedValue->value); |
| 243 } | 250 } |
| 244 | 251 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 303 |
| 297 WatcherSignaler watcher_signaler(*this, job); | 304 WatcherSignaler watcher_signaler(*this, job); |
| 298 | 305 |
| 299 LOG(ERROR) << "Library Load failed: " << job->url().string().utf8().data(); | 306 LOG(ERROR) << "Library Load failed: " << job->url().string().utf8().data(); |
| 300 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? | 307 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? |
| 301 | 308 |
| 302 jobs_.remove(job); | 309 jobs_.remove(job); |
| 303 } | 310 } |
| 304 | 311 |
| 305 } // namespace blink | 312 } // namespace blink |
| OLD | NEW |