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> | |
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> GetBuildinModuleData( | |
Charlie Reis
2016/01/26 01:01:02
nit: Builtin?
Ken Rockot(use gerrit already)
2016/01/26 01:32:55
Woops, fixed
| |
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) |
47 : frame_(frame), | 97 : frame_(frame), |
48 module_added_(false), | 98 module_added_(false), |
49 module_prefix_(frame_->securityOrigin().toString().utf8() + "/") { | 99 module_prefix_(frame_->securityOrigin().toString().utf8() + "/") { |
50 gin::PerContextData* context_data = gin::PerContextData::From(context); | 100 gin::PerContextData* context_data = gin::PerContextData::From(context); |
51 gin::ContextHolder* context_holder = context_data->context_holder(); | 101 gin::ContextHolder* context_holder = context_data->context_holder(); |
52 runner_.reset(new MojoMainRunner(frame_, context_holder)); | 102 runner_.reset(new MojoMainRunner(frame_, context_holder)); |
(...skipping 21 matching lines...) Expand all Loading... | |
74 } | 124 } |
75 | 125 |
76 void MojoContextState::FetchModules(const std::vector<std::string>& ids) { | 126 void MojoContextState::FetchModules(const std::vector<std::string>& ids) { |
77 gin::Runner::Scope scoper(runner_.get()); | 127 gin::Runner::Scope scoper(runner_.get()); |
78 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 128 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
79 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 129 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
80 context_holder->context()); | 130 context_holder->context()); |
81 for (size_t i = 0; i < ids.size(); ++i) { | 131 for (size_t i = 0; i < ids.size(); ++i) { |
82 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && | 132 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
83 registry->available_modules().count(ids[i]) == 0) { | 133 registry->available_modules().count(ids[i]) == 0) { |
84 FetchModule(ids[i]); | 134 scoped_refptr<base::RefCountedMemory> data = GetBuildinModuleData(ids[i]); |
135 if (data) | |
136 runner_->Run(std::string(data->front_as<char>(), data->size()), ids[i]); | |
137 else | |
138 FetchModule(ids[i]); | |
85 } | 139 } |
86 } | 140 } |
87 } | 141 } |
88 | 142 |
89 void MojoContextState::FetchModule(const std::string& id) { | 143 void MojoContextState::FetchModule(const std::string& id) { |
90 const GURL url(module_prefix_ + id); | 144 const GURL url(module_prefix_ + id); |
91 // TODO(sky): better error checks here? | 145 // TODO(sky): better error checks here? |
92 DCHECK(url.is_valid() && !url.is_empty()); | 146 DCHECK(url.is_valid() && !url.is_empty()); |
93 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); | 147 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); |
94 fetched_modules_.insert(id); | 148 fetched_modules_.insert(id); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 const std::vector<std::string>& dependencies) { | 182 const std::vector<std::string>& dependencies) { |
129 FetchModules(dependencies); | 183 FetchModules(dependencies); |
130 | 184 |
131 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 185 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
132 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 186 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
133 context_holder->context()); | 187 context_holder->context()); |
134 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 188 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
135 } | 189 } |
136 | 190 |
137 } // namespace content | 191 } // namespace content |
OLD | NEW |