[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 5 | #include "content/renderer/mojo_context_state.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 6 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 12 | #include "base/bind.h" |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 17 | #include "base/stl_util.h" |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 18 | #include "content/grit/content_resources.h" |
Takashi Toyoshima | 2796e7e | 2017-08-31 08:13:01 | [diff] [blame] | 19 | #include "content/public/child/child_url_loader_factory_getter.h" |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 20 | #include "content/public/common/content_client.h" |
sammc | 853eb9c | 2014-10-14 16:27:59 | [diff] [blame] | 21 | #include "content/public/renderer/render_frame.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 22 | #include "content/public/renderer/resource_fetcher.h" |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 23 | #include "content/renderer/mojo_bindings_controller.h" |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 24 | #include "content/renderer/mojo_main_runner.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 25 | #include "gin/converter.h" |
| 26 | #include "gin/modules/module_registry.h" |
| 27 | #include "gin/per_context_data.h" |
| 28 | #include "gin/public/context_holder.h" |
| 29 | #include "gin/try_catch.h" |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 30 | #include "mojo/public/js/constants.h" |
mek | 966863c | 2016-02-04 23:39:05 | [diff] [blame] | 31 | #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 32 | #include "third_party/WebKit/public/platform/WebURLResponse.h" |
yhirano | 505d1e6 | 2017-05-08 10:37:35 | [diff] [blame] | 33 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 34 | #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 35 | |
| 36 | using v8::Context; |
| 37 | using v8::HandleScope; |
| 38 | using v8::Isolate; |
| 39 | using v8::Object; |
| 40 | using v8::ObjectTemplate; |
| 41 | using v8::Script; |
| 42 | |
| 43 | namespace content { |
| 44 | |
| 45 | namespace { |
| 46 | |
[email protected] | 2b57840 | 2014-07-29 19:51:36 | [diff] [blame] | 47 | void RunMain(base::WeakPtr<gin::Runner> runner, |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 48 | v8::Local<v8::Value> module) { |
[email protected] | 2b57840 | 2014-07-29 19:51:36 | [diff] [blame] | 49 | v8::Isolate* isolate = runner->GetContextHolder()->isolate(); |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 50 | v8::Local<v8::Function> start; |
[email protected] | 2b57840 | 2014-07-29 19:51:36 | [diff] [blame] | 51 | CHECK(gin::ConvertFromV8(isolate, module, &start)); |
[email protected] | 420c1ba1 | 2014-07-30 09:54:07 | [diff] [blame] | 52 | runner->Call(start, runner->global(), 0, NULL); |
[email protected] | 2b57840 | 2014-07-29 19:51:36 | [diff] [blame] | 53 | } |
| 54 | |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 55 | using ModuleSourceMap = |
| 56 | std::map<std::string, scoped_refptr<base::RefCountedMemory>>; |
| 57 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 58 | base::LazyInstance<std::unique_ptr<ModuleSourceMap>>::Leaky g_module_sources; |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 59 | |
| 60 | scoped_refptr<base::RefCountedMemory> GetBuiltinModuleData( |
| 61 | const std::string& path) { |
| 62 | static const struct { |
| 63 | const char* path; |
| 64 | const int id; |
| 65 | } kBuiltinModuleResources[] = { |
wangjimmy | 336e07d | 2017-04-13 18:46:32 | [diff] [blame] | 66 | {mojo::kAssociatedBindingsModuleName, IDR_MOJO_ASSOCIATED_BINDINGS_JS}, |
wangjimmy | 43ca78b | 2017-02-14 22:08:26 | [diff] [blame] | 67 | {mojo::kBindingsModuleName, IDR_MOJO_BINDINGS_JS}, |
| 68 | {mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS}, |
| 69 | {mojo::kCodecModuleName, IDR_MOJO_CODEC_JS}, |
| 70 | {mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS}, |
| 71 | {mojo::kControlMessageHandlerModuleName, |
| 72 | IDR_MOJO_CONTROL_MESSAGE_HANDLER_JS}, |
| 73 | {mojo::kControlMessageProxyModuleName, IDR_MOJO_CONTROL_MESSAGE_PROXY_JS}, |
| 74 | {mojo::kInterfaceControlMessagesMojom, |
| 75 | IDR_MOJO_INTERFACE_CONTROL_MESSAGES_MOJOM_JS}, |
wangjimmy | 087549e | 2017-03-30 17:49:57 | [diff] [blame] | 76 | {mojo::kInterfaceEndpointClientModuleName, |
| 77 | IDR_MOJO_INTERFACE_ENDPOINT_CLIENT_JS}, |
| 78 | {mojo::kInterfaceEndpointHandleModuleName, |
| 79 | IDR_MOJO_INTERFACE_ENDPOINT_HANDLE_JS}, |
wangjimmy | 43ca78b | 2017-02-14 22:08:26 | [diff] [blame] | 80 | {mojo::kInterfaceTypesModuleName, IDR_MOJO_INTERFACE_TYPES_JS}, |
wangjimmy | 087549e | 2017-03-30 17:49:57 | [diff] [blame] | 81 | {mojo::kPipeControlMessageHandlerModuleName, |
| 82 | IDR_MOJO_PIPE_CONTROL_MESSAGE_HANDLER_JS}, |
| 83 | {mojo::kPipeControlMessageProxyModuleName, |
| 84 | IDR_MOJO_PIPE_CONTROL_MESSAGE_PROXY_JS}, |
| 85 | {mojo::kPipeControlMessagesMojom, |
| 86 | IDR_MOJO_PIPE_CONTROL_MESSAGES_MOJOM_JS}, |
wangjimmy | 43ca78b | 2017-02-14 22:08:26 | [diff] [blame] | 87 | {mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS}, |
| 88 | {mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS}, |
| 89 | {mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS}, |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 90 | }; |
| 91 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 92 | std::unique_ptr<ModuleSourceMap>& module_sources = g_module_sources.Get(); |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 93 | if (!module_sources) { |
| 94 | // Initialize the module source map on first access. |
| 95 | module_sources.reset(new ModuleSourceMap); |
| 96 | for (size_t i = 0; i < arraysize(kBuiltinModuleResources); ++i) { |
| 97 | const auto& resource = kBuiltinModuleResources[i]; |
| 98 | scoped_refptr<base::RefCountedMemory> data = |
| 99 | GetContentClient()->GetDataResourceBytes(resource.id); |
| 100 | DCHECK_GT(data->size(), 0u); |
| 101 | module_sources->insert(std::make_pair(std::string(resource.path), data)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | DCHECK(module_sources); |
| 106 | auto source_iter = module_sources->find(path); |
| 107 | if (source_iter == module_sources->end()) |
| 108 | return nullptr; |
| 109 | return source_iter->second; |
| 110 | } |
| 111 | |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 112 | std::string GetModulePrefixForBindingsType(MojoBindingsType bindings_type, |
yhirano | 505d1e6 | 2017-05-08 10:37:35 | [diff] [blame] | 113 | blink::WebLocalFrame* frame) { |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 114 | switch (bindings_type) { |
| 115 | case MojoBindingsType::FOR_WEB_UI: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 116 | return frame->GetSecurityOrigin().ToString().Utf8() + "/"; |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 117 | case MojoBindingsType::FOR_LAYOUT_TESTS: |
| 118 | return "layout-test-mojom://"; |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 119 | } |
| 120 | NOTREACHED(); |
| 121 | return ""; |
| 122 | } |
| 123 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 124 | } // namespace |
| 125 | |
yhirano | 505d1e6 | 2017-05-08 10:37:35 | [diff] [blame] | 126 | MojoContextState::MojoContextState(blink::WebLocalFrame* frame, |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 127 | v8::Local<v8::Context> context, |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 128 | MojoBindingsType bindings_type) |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 129 | : frame_(frame), |
sammc | afd813ed | 2015-11-30 01:46:46 | [diff] [blame] | 130 | module_added_(false), |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 131 | module_prefix_(GetModulePrefixForBindingsType(bindings_type, frame)) { |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 132 | gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 133 | gin::ContextHolder* context_holder = context_data->context_holder(); |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 134 | runner_.reset(new MojoMainRunner(frame_, context_holder)); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 135 | gin::Runner::Scope scoper(runner_.get()); |
| 136 | gin::ModuleRegistry::From(context)->AddObserver(this); |
sammc | 853eb9c | 2014-10-14 16:27:59 | [diff] [blame] | 137 | content::RenderFrame::FromWebFrame(frame) |
| 138 | ->EnsureMojoBuiltinsAreAvailable(context_holder->isolate(), context); |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 139 | v8::Local<v8::Object> install_target; |
alexclarke | a50533ee | 2016-07-29 11:34:48 | [diff] [blame] | 140 | if (bindings_type == MojoBindingsType::FOR_LAYOUT_TESTS) { |
alokp | a3aeda6b | 2017-01-20 01:30:17 | [diff] [blame] | 141 | // In layout tests we install the module system under 'gin.define' |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 142 | // for now to avoid globally exposing something as generic as 'define'. |
| 143 | // |
| 144 | // TODO(rockot): Remove this if/when we can integrate gin + ES6 modules. |
| 145 | install_target = v8::Object::New(context->GetIsolate()); |
| 146 | gin::SetProperty(context->GetIsolate(), context->Global(), |
alokp | a3aeda6b | 2017-01-20 01:30:17 | [diff] [blame] | 147 | gin::StringToSymbol(context->GetIsolate(), "gin"), |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 148 | install_target); |
| 149 | } else { |
| 150 | // Otherwise we're fine installing a global 'define'. |
| 151 | install_target = context->Global(); |
| 152 | } |
| 153 | gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), install_target); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 154 | // Warning |frame| may be destroyed. |
| 155 | // TODO(sky): add test for this. |
| 156 | } |
| 157 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 158 | MojoContextState::~MojoContextState() { |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 159 | gin::Runner::Scope scoper(runner_.get()); |
| 160 | gin::ModuleRegistry::From( |
| 161 | runner_->GetContextHolder()->context())->RemoveObserver(this); |
| 162 | } |
| 163 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 164 | void MojoContextState::Run() { |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 165 | gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 166 | gin::ModuleRegistry::From(context_holder->context())->LoadModule( |
| 167 | context_holder->isolate(), |
| 168 | "main", |
[email protected] | 420c1ba1 | 2014-07-30 09:54:07 | [diff] [blame] | 169 | base::Bind(RunMain, runner_->GetWeakPtr())); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 170 | } |
| 171 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 172 | void MojoContextState::FetchModules(const std::vector<std::string>& ids) { |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 173 | gin::Runner::Scope scoper(runner_.get()); |
| 174 | gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 175 | gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 176 | context_holder->context()); |
| 177 | for (size_t i = 0; i < ids.size(); ++i) { |
| 178 | if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
| 179 | registry->available_modules().count(ids[i]) == 0) { |
rockot | 8df5c72 | 2016-01-26 07:15:14 | [diff] [blame] | 180 | scoped_refptr<base::RefCountedMemory> data = GetBuiltinModuleData(ids[i]); |
| 181 | if (data) |
| 182 | runner_->Run(std::string(data->front_as<char>(), data->size()), ids[i]); |
| 183 | else |
| 184 | FetchModule(ids[i]); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 189 | void MojoContextState::FetchModule(const std::string& id) { |
Takashi Toyoshima | 2796e7e | 2017-08-31 08:13:01 | [diff] [blame] | 190 | static const net::NetworkTrafficAnnotationTag network_traffic_annotation_tag = |
| 191 | net::DefineNetworkTrafficAnnotation("mojo_context_state", R"( |
| 192 | semantics { |
| 193 | sender: "MojoContextState" |
| 194 | description: |
| 195 | "Chrome does fetch for AMD-style module loading of Mojo JavaScript " |
| 196 | "bindings." |
| 197 | trigger: |
| 198 | "When AMD-style module loading of Mojo JavaScript bindings is used." |
| 199 | data: |
| 200 | "Load JavaScript files for Mojo bindings from embedded resources. " |
| 201 | "Nothing is sent over networks." |
| 202 | destination: OTHER |
| 203 | } |
| 204 | policy { |
| 205 | cookies_allowed: NO |
| 206 | setting: "These requests cannot be disabled in settings." |
| 207 | policy_exception_justification: |
| 208 | "Not implemented. Without these requests, Chrome will not work." |
| 209 | })"); |
| 210 | |
sammc | afd813ed | 2015-11-30 01:46:46 | [diff] [blame] | 211 | const GURL url(module_prefix_ + id); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 212 | // TODO(sky): better error checks here? |
| 213 | DCHECK(url.is_valid() && !url.is_empty()); |
| 214 | DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); |
| 215 | fetched_modules_.insert(id); |
Takashi Toyoshima | beba932 | 2017-09-07 06:27:14 | [diff] [blame] | 216 | module_fetchers_.push_back(ResourceFetcher::Create(url)); |
| 217 | module_fetchers_.back()->Start( |
| 218 | frame_, blink::WebURLRequest::kRequestContextScript, |
| 219 | RenderFrame::FromWebFrame(frame_) |
| 220 | ->GetDefaultURLLoaderFactoryGetter() |
| 221 | ->GetNetworkLoaderFactory(), |
| 222 | network_traffic_annotation_tag, |
| 223 | base::BindOnce(&MojoContextState::OnFetchModuleComplete, |
| 224 | base::Unretained(this), module_fetchers_.back().get(), |
| 225 | id)); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 226 | } |
| 227 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 228 | void MojoContextState::OnFetchModuleComplete( |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 229 | ResourceFetcher* fetcher, |
sammc | 2eb97bd | 2016-03-11 05:24:39 | [diff] [blame] | 230 | const std::string& id, |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 231 | const blink::WebURLResponse& response, |
| 232 | const std::string& data) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 233 | if (response.IsNull()) { |
sammc | 2eb97bd | 2016-03-11 05:24:39 | [diff] [blame] | 234 | LOG(ERROR) << "Failed to fetch source for module \"" << id << "\""; |
| 235 | return; |
| 236 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 237 | DCHECK_EQ(module_prefix_ + id, response.Url().GetString().Utf8()); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 238 | // We can't delete fetch right now as the arguments to this function come from |
| 239 | // it and are used below. Instead use a scope_ptr to cleanup. |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 240 | auto iter = |
| 241 | std::find_if(module_fetchers_.begin(), module_fetchers_.end(), |
| 242 | [fetcher](const std::unique_ptr<ResourceFetcher>& item) { |
| 243 | return item.get() == fetcher; |
| 244 | }); |
| 245 | std::unique_ptr<ResourceFetcher> deleter = std::move(*iter); |
| 246 | module_fetchers_.erase(iter); |
| 247 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 248 | if (data.empty()) { |
sammc | 2eb97bd | 2016-03-11 05:24:39 | [diff] [blame] | 249 | LOG(ERROR) << "Fetched empty source for module \"" << id << "\""; |
| 250 | return; |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 251 | } |
| 252 | |
sammc | 2eb97bd | 2016-03-11 05:24:39 | [diff] [blame] | 253 | runner_->Run(data, id); |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 254 | } |
| 255 | |
rockot | 4a038f9 | 2015-11-24 21:52:46 | [diff] [blame] | 256 | void MojoContextState::OnDidAddPendingModule( |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 257 | const std::string& id, |
| 258 | const std::vector<std::string>& dependencies) { |
| 259 | FetchModules(dependencies); |
| 260 | |
| 261 | gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 262 | gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 263 | context_holder->context()); |
| 264 | registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 265 | } |
| 266 | |
| 267 | } // namespace content |