OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/mojo_context_state.h" | 5 #include "content/renderer/mojo_context_state.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
17 #include "content/grit/content_resources.h" | 18 #include "content/grit/content_resources.h" |
18 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
19 #include "content/public/renderer/render_frame.h" | 20 #include "content/public/renderer/render_frame.h" |
20 #include "content/public/renderer/resource_fetcher.h" | 21 #include "content/public/renderer/resource_fetcher.h" |
21 #include "content/renderer/mojo_bindings_controller.h" | 22 #include "content/renderer/mojo_bindings_controller.h" |
22 #include "content/renderer/mojo_main_runner.h" | 23 #include "content/renderer/mojo_main_runner.h" |
23 #include "gin/converter.h" | 24 #include "gin/converter.h" |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 176 } |
176 } | 177 } |
177 | 178 |
178 void MojoContextState::FetchModule(const std::string& id) { | 179 void MojoContextState::FetchModule(const std::string& id) { |
179 const GURL url(module_prefix_ + id); | 180 const GURL url(module_prefix_ + id); |
180 // TODO(sky): better error checks here? | 181 // TODO(sky): better error checks here? |
181 DCHECK(url.is_valid() && !url.is_empty()); | 182 DCHECK(url.is_valid() && !url.is_empty()); |
182 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); | 183 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); |
183 fetched_modules_.insert(id); | 184 fetched_modules_.insert(id); |
184 ResourceFetcher* fetcher = ResourceFetcher::Create(url); | 185 ResourceFetcher* fetcher = ResourceFetcher::Create(url); |
185 module_fetchers_.push_back(fetcher); | 186 module_fetchers_.push_back(base::WrapUnique(fetcher)); |
186 fetcher->Start(frame_, | 187 fetcher->Start(frame_, |
187 blink::WebURLRequest::RequestContextScript, | 188 blink::WebURLRequest::RequestContextScript, |
188 base::Bind(&MojoContextState::OnFetchModuleComplete, | 189 base::Bind(&MojoContextState::OnFetchModuleComplete, |
189 base::Unretained(this), fetcher, id)); | 190 base::Unretained(this), fetcher, id)); |
190 } | 191 } |
191 | 192 |
192 void MojoContextState::OnFetchModuleComplete( | 193 void MojoContextState::OnFetchModuleComplete( |
193 ResourceFetcher* fetcher, | 194 ResourceFetcher* fetcher, |
194 const std::string& id, | 195 const std::string& id, |
195 const blink::WebURLResponse& response, | 196 const blink::WebURLResponse& response, |
196 const std::string& data) { | 197 const std::string& data) { |
197 if (response.isNull()) { | 198 if (response.isNull()) { |
198 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; | 199 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; |
199 return; | 200 return; |
200 } | 201 } |
201 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); | 202 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); |
202 // We can't delete fetch right now as the arguments to this function come from | 203 // We can't delete fetch right now as the arguments to this function come from |
203 // it and are used below. Instead use a scope_ptr to cleanup. | 204 // it and are used below. Instead use a scope_ptr to cleanup. |
204 std::unique_ptr<ResourceFetcher> deleter(fetcher); | 205 auto iter = |
205 module_fetchers_.weak_erase( | 206 std::find_if(module_fetchers_.begin(), module_fetchers_.end(), |
206 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); | 207 [fetcher](const std::unique_ptr<ResourceFetcher>& item) { |
| 208 return item.get() == fetcher; |
| 209 }); |
| 210 std::unique_ptr<ResourceFetcher> deleter = std::move(*iter); |
| 211 module_fetchers_.erase(iter); |
| 212 |
207 if (data.empty()) { | 213 if (data.empty()) { |
208 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; | 214 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; |
209 return; | 215 return; |
210 } | 216 } |
211 | 217 |
212 runner_->Run(data, id); | 218 runner_->Run(data, id); |
213 } | 219 } |
214 | 220 |
215 void MojoContextState::OnDidAddPendingModule( | 221 void MojoContextState::OnDidAddPendingModule( |
216 const std::string& id, | 222 const std::string& id, |
217 const std::vector<std::string>& dependencies) { | 223 const std::vector<std::string>& dependencies) { |
218 FetchModules(dependencies); | 224 FetchModules(dependencies); |
219 | 225 |
220 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 226 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
221 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 227 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
222 context_holder->context()); | 228 context_holder->context()); |
223 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 229 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
224 } | 230 } |
225 | 231 |
226 } // namespace content | 232 } // namespace content |
OLD | NEW |