| 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 blink::WebURLRequest::FrameTypeNone, | 189 blink::WebURLRequest::FrameTypeNone, |
| 189 base::Bind(&MojoContextState::OnFetchModuleComplete, | 190 base::Bind(&MojoContextState::OnFetchModuleComplete, |
| 190 base::Unretained(this), fetcher, id)); | 191 base::Unretained(this), fetcher, id)); |
| 191 } | 192 } |
| 192 | 193 |
| 193 void MojoContextState::OnFetchModuleComplete( | 194 void MojoContextState::OnFetchModuleComplete( |
| 194 ResourceFetcher* fetcher, | 195 ResourceFetcher* fetcher, |
| 195 const std::string& id, | 196 const std::string& id, |
| 196 const blink::WebURLResponse& response, | 197 const blink::WebURLResponse& response, |
| 197 const std::string& data) { | 198 const std::string& data) { |
| 198 if (response.isNull()) { | 199 if (response.isNull()) { |
| 199 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; | 200 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; |
| 200 return; | 201 return; |
| 201 } | 202 } |
| 202 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); | 203 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); |
| 203 // We can't delete fetch right now as the arguments to this function come from | 204 // We can't delete fetch right now as the arguments to this function come from |
| 204 // it and are used below. Instead use a scope_ptr to cleanup. | 205 // it and are used below. Instead use a scope_ptr to cleanup. |
| 205 std::unique_ptr<ResourceFetcher> deleter(fetcher); | 206 auto iter = |
| 206 module_fetchers_.weak_erase( | 207 std::find_if(module_fetchers_.begin(), module_fetchers_.end(), |
| 207 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); | 208 [fetcher](const std::unique_ptr<ResourceFetcher>& item) { |
| 209 return item.get() == fetcher; |
| 210 }); |
| 211 std::unique_ptr<ResourceFetcher> deleter = std::move(*iter); |
| 212 module_fetchers_.erase(iter); |
| 213 |
| 208 if (data.empty()) { | 214 if (data.empty()) { |
| 209 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; | 215 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; |
| 210 return; | 216 return; |
| 211 } | 217 } |
| 212 | 218 |
| 213 runner_->Run(data, id); | 219 runner_->Run(data, id); |
| 214 } | 220 } |
| 215 | 221 |
| 216 void MojoContextState::OnDidAddPendingModule( | 222 void MojoContextState::OnDidAddPendingModule( |
| 217 const std::string& id, | 223 const std::string& id, |
| 218 const std::vector<std::string>& dependencies) { | 224 const std::vector<std::string>& dependencies) { |
| 219 FetchModules(dependencies); | 225 FetchModules(dependencies); |
| 220 | 226 |
| 221 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 227 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 222 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 228 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 223 context_holder->context()); | 229 context_holder->context()); |
| 224 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 230 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 225 } | 231 } |
| 226 | 232 |
| 227 } // namespace content | 233 } // namespace content |
| OLD | NEW |