| 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> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 v8::Local<v8::Value> module) { | 45 v8::Local<v8::Value> module) { |
| 46 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); | 46 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); |
| 47 v8::Local<v8::Function> start; | 47 v8::Local<v8::Function> start; |
| 48 CHECK(gin::ConvertFromV8(isolate, module, &start)); | 48 CHECK(gin::ConvertFromV8(isolate, module, &start)); |
| 49 runner->Call(start, runner->global(), 0, NULL); | 49 runner->Call(start, runner->global(), 0, NULL); |
| 50 } | 50 } |
| 51 | 51 |
| 52 using ModuleSourceMap = | 52 using ModuleSourceMap = |
| 53 std::map<std::string, scoped_refptr<base::RefCountedMemory>>; | 53 std::map<std::string, scoped_refptr<base::RefCountedMemory>>; |
| 54 | 54 |
| 55 base::LazyInstance<scoped_ptr<ModuleSourceMap>>::Leaky g_module_sources; | 55 base::LazyInstance<std::unique_ptr<ModuleSourceMap>>::Leaky g_module_sources; |
| 56 | 56 |
| 57 scoped_refptr<base::RefCountedMemory> GetBuiltinModuleData( | 57 scoped_refptr<base::RefCountedMemory> GetBuiltinModuleData( |
| 58 const std::string& path) { | 58 const std::string& path) { |
| 59 static const struct { | 59 static const struct { |
| 60 const char* path; | 60 const char* path; |
| 61 const int id; | 61 const int id; |
| 62 } kBuiltinModuleResources[] = { | 62 } kBuiltinModuleResources[] = { |
| 63 { mojo::kBindingsModuleName, IDR_MOJO_BINDINGS_JS }, | 63 { mojo::kBindingsModuleName, IDR_MOJO_BINDINGS_JS }, |
| 64 { mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS }, | 64 { mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS }, |
| 65 { mojo::kCodecModuleName, IDR_MOJO_CODEC_JS }, | 65 { mojo::kCodecModuleName, IDR_MOJO_CODEC_JS }, |
| 66 { mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS }, | 66 { mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS }, |
| 67 { mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS }, | 67 { mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS }, |
| 68 { mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS }, | 68 { mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS }, |
| 69 { mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS }, | 69 { mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS }, |
| 70 { mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS }, | 70 { mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS }, |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 scoped_ptr<ModuleSourceMap>& module_sources = g_module_sources.Get(); | 73 std::unique_ptr<ModuleSourceMap>& module_sources = g_module_sources.Get(); |
| 74 if (!module_sources) { | 74 if (!module_sources) { |
| 75 // Initialize the module source map on first access. | 75 // Initialize the module source map on first access. |
| 76 module_sources.reset(new ModuleSourceMap); | 76 module_sources.reset(new ModuleSourceMap); |
| 77 for (size_t i = 0; i < arraysize(kBuiltinModuleResources); ++i) { | 77 for (size_t i = 0; i < arraysize(kBuiltinModuleResources); ++i) { |
| 78 const auto& resource = kBuiltinModuleResources[i]; | 78 const auto& resource = kBuiltinModuleResources[i]; |
| 79 scoped_refptr<base::RefCountedMemory> data = | 79 scoped_refptr<base::RefCountedMemory> data = |
| 80 GetContentClient()->GetDataResourceBytes(resource.id); | 80 GetContentClient()->GetDataResourceBytes(resource.id); |
| 81 DCHECK_GT(data->size(), 0u); | 81 DCHECK_GT(data->size(), 0u); |
| 82 module_sources->insert(std::make_pair(std::string(resource.path), data)); | 82 module_sources->insert(std::make_pair(std::string(resource.path), data)); |
| 83 } | 83 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 const std::string& id, | 179 const std::string& id, |
| 180 const blink::WebURLResponse& response, | 180 const blink::WebURLResponse& response, |
| 181 const std::string& data) { | 181 const std::string& data) { |
| 182 if (response.isNull()) { | 182 if (response.isNull()) { |
| 183 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; | 183 LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; |
| 184 return; | 184 return; |
| 185 } | 185 } |
| 186 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); | 186 DCHECK_EQ(module_prefix_ + id, response.url().string().utf8()); |
| 187 // We can't delete fetch right now as the arguments to this function come from | 187 // We can't delete fetch right now as the arguments to this function come from |
| 188 // it and are used below. Instead use a scope_ptr to cleanup. | 188 // it and are used below. Instead use a scope_ptr to cleanup. |
| 189 scoped_ptr<ResourceFetcher> deleter(fetcher); | 189 std::unique_ptr<ResourceFetcher> deleter(fetcher); |
| 190 module_fetchers_.weak_erase( | 190 module_fetchers_.weak_erase( |
| 191 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); | 191 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); |
| 192 if (data.empty()) { | 192 if (data.empty()) { |
| 193 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; | 193 LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; |
| 194 return; | 194 return; |
| 195 } | 195 } |
| 196 | 196 |
| 197 runner_->Run(data, id); | 197 runner_->Run(data, id); |
| 198 } | 198 } |
| 199 | 199 |
| 200 void MojoContextState::OnDidAddPendingModule( | 200 void MojoContextState::OnDidAddPendingModule( |
| 201 const std::string& id, | 201 const std::string& id, |
| 202 const std::vector<std::string>& dependencies) { | 202 const std::vector<std::string>& dependencies) { |
| 203 FetchModules(dependencies); | 203 FetchModules(dependencies); |
| 204 | 204 |
| 205 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 205 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 206 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 206 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 207 context_holder->context()); | 207 context_holder->context()); |
| 208 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 208 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 209 } | 209 } |
| 210 | 210 |
| 211 } // namespace content | 211 } // namespace content |
| OLD | NEW |