Index: content/renderer/mojo_context_state.cc |
diff --git a/content/renderer/mojo_context_state.cc b/content/renderer/mojo_context_state.cc |
index 2d3bdea933b3f75caae3aa0f4943e1385f042253..db5fb25a0a7c21b247a22ec750475beebd5d0706 100644 |
--- a/content/renderer/mojo_context_state.cc |
+++ b/content/renderer/mojo_context_state.cc |
@@ -6,8 +6,16 @@ |
#include <stddef.h> |
+#include <map> |
+#include <string> |
+ |
#include "base/bind.h" |
+#include "base/lazy_instance.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/ref_counted_memory.h" |
#include "base/stl_util.h" |
+#include "content/grit/content_resources.h" |
+#include "content/public/common/content_client.h" |
#include "content/public/renderer/render_frame.h" |
#include "content/public/renderer/resource_fetcher.h" |
#include "content/renderer/mojo_main_runner.h" |
@@ -16,6 +24,7 @@ |
#include "gin/per_context_data.h" |
#include "gin/public/context_holder.h" |
#include "gin/try_catch.h" |
+#include "mojo/public/js/constants.h" |
#include "third_party/WebKit/public/platform/WebURLResponse.h" |
#include "third_party/WebKit/public/web/WebFrame.h" |
#include "third_party/WebKit/public/web/WebScriptSource.h" |
@@ -40,6 +49,47 @@ void RunMain(base::WeakPtr<gin::Runner> runner, |
runner->Call(start, runner->global(), 0, NULL); |
} |
+using ModuleSourceMap = |
+ std::map<std::string, scoped_refptr<base::RefCountedMemory>>; |
+ |
+base::LazyInstance<scoped_ptr<ModuleSourceMap>>::Leaky g_module_sources; |
+ |
+scoped_refptr<base::RefCountedMemory> GetBuildinModuleData( |
+ const std::string& path) { |
+ static const struct { |
+ const char* path; |
+ const int id; |
+ } kBuiltinModuleResources[] = { |
+ { mojo::kBindingsModuleName, IDR_MOJO_BINDINGS_JS }, |
+ { mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS }, |
+ { mojo::kCodecModuleName, IDR_MOJO_CODEC_JS }, |
+ { mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS }, |
+ { mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS }, |
+ { mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS }, |
+ { mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS }, |
+ { mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS }, |
+ }; |
+ |
+ scoped_ptr<ModuleSourceMap>& module_sources = g_module_sources.Get(); |
+ if (!module_sources) { |
+ // Initialize the module source map on first access. |
+ module_sources.reset(new ModuleSourceMap); |
+ for (size_t i = 0; i < arraysize(kBuiltinModuleResources); ++i) { |
+ const auto& resource = kBuiltinModuleResources[i]; |
+ scoped_refptr<base::RefCountedMemory> data = |
+ GetContentClient()->GetDataResourceBytes(resource.id); |
+ DCHECK_GT(data->size(), 0u); |
+ module_sources->insert(std::make_pair(std::string(resource.path), data)); |
+ } |
+ } |
+ |
+ DCHECK(module_sources); |
+ auto source_iter = module_sources->find(path); |
+ if (source_iter == module_sources->end()) |
+ return nullptr; |
+ return source_iter->second; |
+} |
+ |
} // namespace |
MojoContextState::MojoContextState(blink::WebFrame* frame, |
@@ -81,7 +131,11 @@ void MojoContextState::FetchModules(const std::vector<std::string>& ids) { |
for (size_t i = 0; i < ids.size(); ++i) { |
if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
registry->available_modules().count(ids[i]) == 0) { |
- FetchModule(ids[i]); |
+ scoped_refptr<base::RefCountedMemory> data = GetBuildinModuleData(ids[i]); |
+ if (data) |
+ runner_->Run(std::string(data->front_as<char>(), data->size()), ids[i]); |
+ else |
+ FetchModule(ids[i]); |
} |
} |
} |