Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: content/renderer/mojo_context_state.cc

Issue 1470823002: Enable builtin Mojo JS modules in layout tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-testing
Patch Set: oops, clumsy Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/mojo_context_state.h ('k') | content/renderer/render_frame_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
10 #include <string>
11
9 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/ref_counted_memory.h"
10 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "content/grit/content_resources.h"
18 #include "content/public/common/content_client.h"
11 #include "content/public/renderer/render_frame.h" 19 #include "content/public/renderer/render_frame.h"
12 #include "content/public/renderer/resource_fetcher.h" 20 #include "content/public/renderer/resource_fetcher.h"
13 #include "content/renderer/mojo_main_runner.h" 21 #include "content/renderer/mojo_main_runner.h"
14 #include "gin/converter.h" 22 #include "gin/converter.h"
15 #include "gin/modules/module_registry.h" 23 #include "gin/modules/module_registry.h"
16 #include "gin/per_context_data.h" 24 #include "gin/per_context_data.h"
17 #include "gin/public/context_holder.h" 25 #include "gin/public/context_holder.h"
18 #include "gin/try_catch.h" 26 #include "gin/try_catch.h"
27 #include "mojo/public/js/constants.h"
19 #include "third_party/WebKit/public/platform/WebURLResponse.h" 28 #include "third_party/WebKit/public/platform/WebURLResponse.h"
20 #include "third_party/WebKit/public/web/WebFrame.h" 29 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebScriptSource.h" 30 #include "third_party/WebKit/public/web/WebScriptSource.h"
22 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 31 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
23 32
24 using v8::Context; 33 using v8::Context;
25 using v8::HandleScope; 34 using v8::HandleScope;
26 using v8::Isolate; 35 using v8::Isolate;
27 using v8::Object; 36 using v8::Object;
28 using v8::ObjectTemplate; 37 using v8::ObjectTemplate;
29 using v8::Script; 38 using v8::Script;
30 39
31 namespace content { 40 namespace content {
32 41
33 namespace { 42 namespace {
34 43
35 void RunMain(base::WeakPtr<gin::Runner> runner, 44 void RunMain(base::WeakPtr<gin::Runner> runner,
36 v8::Local<v8::Value> module) { 45 v8::Local<v8::Value> module) {
37 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); 46 v8::Isolate* isolate = runner->GetContextHolder()->isolate();
38 v8::Local<v8::Function> start; 47 v8::Local<v8::Function> start;
39 CHECK(gin::ConvertFromV8(isolate, module, &start)); 48 CHECK(gin::ConvertFromV8(isolate, module, &start));
40 runner->Call(start, runner->global(), 0, NULL); 49 runner->Call(start, runner->global(), 0, NULL);
41 } 50 }
42 51
52 using ModuleSourceMap =
53 std::map<std::string, scoped_refptr<base::RefCountedMemory>>;
54
55 base::LazyInstance<scoped_ptr<ModuleSourceMap>>::Leaky g_module_sources;
56
57 scoped_refptr<base::RefCountedMemory> GetBuiltinModuleData(
58 const std::string& path) {
59 static const struct {
60 const char* path;
61 const int id;
62 } kBuiltinModuleResources[] = {
63 { mojo::kBindingsModuleName, IDR_MOJO_BINDINGS_JS },
64 { mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS },
65 { mojo::kCodecModuleName, IDR_MOJO_CODEC_JS },
66 { mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS },
67 { mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS },
68 { mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS },
69 { mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS },
70 { mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS },
71 };
72
73 scoped_ptr<ModuleSourceMap>& module_sources = g_module_sources.Get();
74 if (!module_sources) {
75 // Initialize the module source map on first access.
76 module_sources.reset(new ModuleSourceMap);
77 for (size_t i = 0; i < arraysize(kBuiltinModuleResources); ++i) {
78 const auto& resource = kBuiltinModuleResources[i];
79 scoped_refptr<base::RefCountedMemory> data =
80 GetContentClient()->GetDataResourceBytes(resource.id);
81 DCHECK_GT(data->size(), 0u);
82 module_sources->insert(std::make_pair(std::string(resource.path), data));
83 }
84 }
85
86 DCHECK(module_sources);
87 auto source_iter = module_sources->find(path);
88 if (source_iter == module_sources->end())
89 return nullptr;
90 return source_iter->second;
91 }
92
43 } // namespace 93 } // namespace
44 94
45 MojoContextState::MojoContextState(blink::WebFrame* frame, 95 MojoContextState::MojoContextState(blink::WebFrame* frame,
46 v8::Local<v8::Context> context) 96 v8::Local<v8::Context> context,
97 bool for_layout_tests)
47 : frame_(frame), 98 : frame_(frame),
48 module_added_(false), 99 module_added_(false),
49 module_prefix_(frame_->securityOrigin().toString().utf8() + "/") { 100 module_prefix_(frame_->securityOrigin().toString().utf8() + "/") {
50 gin::PerContextData* context_data = gin::PerContextData::From(context); 101 gin::PerContextData* context_data = gin::PerContextData::From(context);
51 gin::ContextHolder* context_holder = context_data->context_holder(); 102 gin::ContextHolder* context_holder = context_data->context_holder();
52 runner_.reset(new MojoMainRunner(frame_, context_holder)); 103 runner_.reset(new MojoMainRunner(frame_, context_holder));
53 gin::Runner::Scope scoper(runner_.get()); 104 gin::Runner::Scope scoper(runner_.get());
54 gin::ModuleRegistry::From(context)->AddObserver(this); 105 gin::ModuleRegistry::From(context)->AddObserver(this);
55 content::RenderFrame::FromWebFrame(frame) 106 content::RenderFrame::FromWebFrame(frame)
56 ->EnsureMojoBuiltinsAreAvailable(context_holder->isolate(), context); 107 ->EnsureMojoBuiltinsAreAvailable(context_holder->isolate(), context);
57 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); 108 v8::Local<v8::Object> install_target;
109 if (for_layout_tests) {
110 // In layout tests we install the module system under 'mojo.define'
111 // for now to avoid globally exposing something as generic as 'define'.
112 //
113 // TODO(rockot): Remove this if/when we can integrate gin + ES6 modules.
114 install_target = v8::Object::New(context->GetIsolate());
115 gin::SetProperty(context->GetIsolate(), context->Global(),
116 gin::StringToSymbol(context->GetIsolate(), "mojo"),
117 install_target);
118 } else {
119 // Otherwise we're fine installing a global 'define'.
120 install_target = context->Global();
121 }
122 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), install_target);
58 // Warning |frame| may be destroyed. 123 // Warning |frame| may be destroyed.
59 // TODO(sky): add test for this. 124 // TODO(sky): add test for this.
60 } 125 }
61 126
62 MojoContextState::~MojoContextState() { 127 MojoContextState::~MojoContextState() {
63 gin::Runner::Scope scoper(runner_.get()); 128 gin::Runner::Scope scoper(runner_.get());
64 gin::ModuleRegistry::From( 129 gin::ModuleRegistry::From(
65 runner_->GetContextHolder()->context())->RemoveObserver(this); 130 runner_->GetContextHolder()->context())->RemoveObserver(this);
66 } 131 }
67 132
68 void MojoContextState::Run() { 133 void MojoContextState::Run() {
69 gin::ContextHolder* context_holder = runner_->GetContextHolder(); 134 gin::ContextHolder* context_holder = runner_->GetContextHolder();
70 gin::ModuleRegistry::From(context_holder->context())->LoadModule( 135 gin::ModuleRegistry::From(context_holder->context())->LoadModule(
71 context_holder->isolate(), 136 context_holder->isolate(),
72 "main", 137 "main",
73 base::Bind(RunMain, runner_->GetWeakPtr())); 138 base::Bind(RunMain, runner_->GetWeakPtr()));
74 } 139 }
75 140
76 void MojoContextState::FetchModules(const std::vector<std::string>& ids) { 141 void MojoContextState::FetchModules(const std::vector<std::string>& ids) {
77 gin::Runner::Scope scoper(runner_.get()); 142 gin::Runner::Scope scoper(runner_.get());
78 gin::ContextHolder* context_holder = runner_->GetContextHolder(); 143 gin::ContextHolder* context_holder = runner_->GetContextHolder();
79 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( 144 gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
80 context_holder->context()); 145 context_holder->context());
81 for (size_t i = 0; i < ids.size(); ++i) { 146 for (size_t i = 0; i < ids.size(); ++i) {
82 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && 147 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() &&
83 registry->available_modules().count(ids[i]) == 0) { 148 registry->available_modules().count(ids[i]) == 0) {
84 FetchModule(ids[i]); 149 scoped_refptr<base::RefCountedMemory> data = GetBuiltinModuleData(ids[i]);
150 if (data)
151 runner_->Run(std::string(data->front_as<char>(), data->size()), ids[i]);
152 else
153 FetchModule(ids[i]);
85 } 154 }
86 } 155 }
87 } 156 }
88 157
89 void MojoContextState::FetchModule(const std::string& id) { 158 void MojoContextState::FetchModule(const std::string& id) {
90 const GURL url(module_prefix_ + id); 159 const GURL url(module_prefix_ + id);
91 // TODO(sky): better error checks here? 160 // TODO(sky): better error checks here?
92 DCHECK(url.is_valid() && !url.is_empty()); 161 DCHECK(url.is_valid() && !url.is_empty());
93 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); 162 DCHECK(fetched_modules_.find(id) == fetched_modules_.end());
94 fetched_modules_.insert(id); 163 fetched_modules_.insert(id);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 const std::vector<std::string>& dependencies) { 197 const std::vector<std::string>& dependencies) {
129 FetchModules(dependencies); 198 FetchModules(dependencies);
130 199
131 gin::ContextHolder* context_holder = runner_->GetContextHolder(); 200 gin::ContextHolder* context_holder = runner_->GetContextHolder();
132 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( 201 gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
133 context_holder->context()); 202 context_holder->context());
134 registry->AttemptToLoadMoreModules(context_holder->isolate()); 203 registry->AttemptToLoadMoreModules(context_holder->isolate());
135 } 204 }
136 205
137 } // namespace content 206 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/mojo_context_state.h ('k') | content/renderer/render_frame_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698