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 "tonic/dart_library_loader.h" | 5 #include "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 "tonic/dart_api_scope.h" | 10 #include "tonic/dart_api_scope.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 DartLibraryLoader* loader_; | 49 DartLibraryLoader* loader_; |
50 // TODO(abarth): Should we be using SharedBuffer to buffer the data? | 50 // TODO(abarth): Should we be using SharedBuffer to buffer the data? |
51 std::vector<uint8_t> buffer_; | 51 std::vector<uint8_t> buffer_; |
52 | 52 |
53 private: | 53 private: |
54 void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) { | 54 void OnStreamAvailable(mojo::ScopedDataPipeConsumerHandle pipe) { |
55 if (!pipe.is_valid()) { | 55 if (!pipe.is_valid()) { |
56 loader_->DidFailJob(this); | 56 loader_->DidFailJob(this); |
57 return; | 57 return; |
58 } | 58 } |
59 drainer_ = adoptPtr(new DataPipeDrainer(this, pipe.Pass())); | 59 drainer_ = std::unique_ptr<DataPipeDrainer>( |
| 60 new DataPipeDrainer(this, pipe.Pass())); |
60 } | 61 } |
61 | 62 |
62 // DataPipeDrainer::Client | 63 // DataPipeDrainer::Client |
63 void OnDataAvailable(const void* data, size_t num_bytes) override { | 64 void OnDataAvailable(const void* data, size_t num_bytes) override { |
64 const uint8_t* bytes = static_cast<const uint8_t*>(data); | 65 const uint8_t* bytes = static_cast<const uint8_t*>(data); |
65 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); | 66 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); |
66 } | 67 } |
67 // Subclasses must implement OnDataComplete. | 68 // Subclasses must implement OnDataComplete. |
68 | 69 |
69 std::string name_; | 70 std::string name_; |
70 OwnPtr<DataPipeDrainer> drainer_; | 71 std::unique_ptr<DataPipeDrainer> drainer_; |
71 | 72 |
72 base::WeakPtrFactory<Job> weak_factory_; | 73 base::WeakPtrFactory<Job> weak_factory_; |
73 }; | 74 }; |
74 | 75 |
75 class DartLibraryLoader::ImportJob : public Job { | 76 class DartLibraryLoader::ImportJob : public Job { |
76 public: | 77 public: |
77 ImportJob(DartLibraryLoader* loader, const std::string& name) : Job(loader, na
me) { | 78 ImportJob(DartLibraryLoader* loader, const std::string& name) : Job(loader, na
me) { |
78 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::ImportJob", this, "url", | 79 TRACE_EVENT_ASYNC_BEGIN1("sky", "DartLibraryLoader::ImportJob", this, "url", |
79 name); | 80 name); |
80 } | 81 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // a separate object of this task because we want to carefully manage when we | 144 // a separate object of this task because we want to carefully manage when we |
144 // call the callbacks, which can call into us again reentrantly. | 145 // call the callbacks, which can call into us again reentrantly. |
145 // | 146 // |
146 // WatcherSignaler is designed to be placed on the stack as a RAII. After its | 147 // WatcherSignaler is designed to be placed on the stack as a RAII. After its |
147 // destructor runs, we might have executed aribitrary script. | 148 // destructor runs, we might have executed aribitrary script. |
148 class DartLibraryLoader::WatcherSignaler { | 149 class DartLibraryLoader::WatcherSignaler { |
149 public: | 150 public: |
150 WatcherSignaler(DartLibraryLoader& loader, | 151 WatcherSignaler(DartLibraryLoader& loader, |
151 DartDependency* resolved_dependency) | 152 DartDependency* resolved_dependency) |
152 : loader_(loader), | 153 : loader_(loader), |
153 catcher_(adoptPtr(new DartDependencyCatcher(loader))), | 154 catcher_(std::unique_ptr<DartDependencyCatcher>( |
| 155 new DartDependencyCatcher(loader))), |
154 resolved_dependency_(resolved_dependency) {} | 156 resolved_dependency_(resolved_dependency) {} |
155 | 157 |
156 ~WatcherSignaler() { | 158 ~WatcherSignaler() { |
157 std::vector<DependencyWatcher*> completed_watchers; | 159 std::vector<DependencyWatcher*> completed_watchers; |
158 for (const auto& watcher : loader_.dependency_watchers_) { | 160 for (const auto& watcher : loader_.dependency_watchers_) { |
159 if (watcher->DidResolveDependency(resolved_dependency_, | 161 if (watcher->DidResolveDependency(resolved_dependency_, |
160 catcher_->dependencies())) | 162 catcher_->dependencies())) |
161 completed_watchers.push_back(watcher.get()); | 163 completed_watchers.push_back(watcher.get()); |
162 } | 164 } |
163 | 165 |
164 // Notice that we remove the dependency catcher and extract all the | 166 // Notice that we remove the dependency catcher and extract all the |
165 // callbacks before running any of them. We don't want to be re-entered | 167 // callbacks before running any of them. We don't want to be re-entered |
166 // below the callbacks and end up in an inconsistent state. | 168 // below the callbacks and end up in an inconsistent state. |
167 catcher_.clear(); | 169 catcher_.reset(); |
168 std::vector<base::Closure> callbacks; | 170 std::vector<base::Closure> callbacks; |
169 for (const auto& watcher : completed_watchers) { | 171 for (const auto& watcher : completed_watchers) { |
170 callbacks.push_back(watcher->callback()); | 172 callbacks.push_back(watcher->callback()); |
171 EraseUniquePtr(loader_.dependency_watchers_, watcher); | 173 EraseUniquePtr(loader_.dependency_watchers_, watcher); |
172 } | 174 } |
173 | 175 |
174 // Finally, run all the callbacks while touching only data on the stack. | 176 // Finally, run all the callbacks while touching only data on the stack. |
175 for (const auto& callback : callbacks) | 177 for (const auto& callback : callbacks) |
176 callback.Run(); | 178 callback.Run(); |
177 } | 179 } |
178 | 180 |
179 private: | 181 private: |
180 DartLibraryLoader& loader_; | 182 DartLibraryLoader& loader_; |
181 OwnPtr<DartDependencyCatcher> catcher_; | 183 std::unique_ptr<DartDependencyCatcher> catcher_; |
182 DartDependency* resolved_dependency_; | 184 DartDependency* resolved_dependency_; |
183 }; | 185 }; |
184 | 186 |
185 DartLibraryLoader::DartLibraryLoader(DartState* dart_state) | 187 DartLibraryLoader::DartLibraryLoader(DartState* dart_state) |
186 : dart_state_(dart_state), | 188 : dart_state_(dart_state), |
187 library_provider_(nullptr), | 189 library_provider_(nullptr), |
188 dependency_catcher_(nullptr) { | 190 dependency_catcher_(nullptr) { |
189 } | 191 } |
190 | 192 |
191 DartLibraryLoader::~DartLibraryLoader() { | 193 DartLibraryLoader::~DartLibraryLoader() { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 | 298 |
297 WatcherSignaler watcher_signaler(*this, job); | 299 WatcherSignaler watcher_signaler(*this, job); |
298 | 300 |
299 LOG(ERROR) << "Library Load failed: " << job->name(); | 301 LOG(ERROR) << "Library Load failed: " << job->name(); |
300 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? | 302 // TODO(eseidel): Call Dart_LibraryHandleError in the SourceJob case? |
301 | 303 |
302 EraseUniquePtr<Job>(jobs_, job); | 304 EraseUniquePtr<Job>(jobs_, job); |
303 } | 305 } |
304 | 306 |
305 } // namespace blink | 307 } // namespace blink |
OLD | NEW |