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/web_ui_mojo_context_state.h" | 5 #include "content/renderer/web_ui_mojo_context_state.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "content/public/renderer/render_frame.h" | 9 #include "content/public/renderer/render_frame.h" |
10 #include "content/public/renderer/resource_fetcher.h" | 10 #include "content/public/renderer/resource_fetcher.h" |
11 #include "content/renderer/web_ui_runner.h" | 11 #include "content/renderer/web_ui_runner.h" |
12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
13 #include "gin/modules/module_registry.h" | 13 #include "gin/modules/module_registry.h" |
14 #include "gin/per_context_data.h" | 14 #include "gin/per_context_data.h" |
15 #include "gin/public/context_holder.h" | 15 #include "gin/public/context_holder.h" |
16 #include "gin/try_catch.h" | 16 #include "gin/try_catch.h" |
17 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 17 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
18 #include "third_party/WebKit/public/web/WebFrame.h" | 18 #include "third_party/WebKit/public/web/WebFrame.h" |
19 #include "third_party/WebKit/public/web/WebScriptSource.h" | 19 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 20 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
20 | 21 |
21 using v8::Context; | 22 using v8::Context; |
22 using v8::HandleScope; | 23 using v8::HandleScope; |
23 using v8::Isolate; | 24 using v8::Isolate; |
24 using v8::Object; | 25 using v8::Object; |
25 using v8::ObjectTemplate; | 26 using v8::ObjectTemplate; |
26 using v8::Script; | 27 using v8::Script; |
27 | 28 |
28 namespace content { | 29 namespace content { |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 // All modules have this prefixed to them when downloading. | |
33 // TODO(sky): move this into some common place. | |
34 const char kModulePrefix[] = "chrome://mojo/"; | |
35 | |
36 void RunMain(base::WeakPtr<gin::Runner> runner, | 33 void RunMain(base::WeakPtr<gin::Runner> runner, |
37 v8::Local<v8::Value> module) { | 34 v8::Local<v8::Value> module) { |
38 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); | 35 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); |
39 v8::Local<v8::Function> start; | 36 v8::Local<v8::Function> start; |
40 CHECK(gin::ConvertFromV8(isolate, module, &start)); | 37 CHECK(gin::ConvertFromV8(isolate, module, &start)); |
41 runner->Call(start, runner->global(), 0, NULL); | 38 runner->Call(start, runner->global(), 0, NULL); |
42 } | 39 } |
43 | 40 |
44 } // namespace | 41 } // namespace |
45 | 42 |
46 // WebUIMojo ------------------------------------------------------------------- | 43 // WebUIMojo ------------------------------------------------------------------- |
47 | 44 |
48 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, | 45 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, |
49 v8::Local<v8::Context> context) | 46 v8::Local<v8::Context> context) |
50 : frame_(frame), | 47 : frame_(frame), |
51 module_added_(false) { | 48 module_added_(false), |
| 49 module_prefix_(frame_->securityOrigin().toString().utf8() + "/") { |
52 gin::PerContextData* context_data = gin::PerContextData::From(context); | 50 gin::PerContextData* context_data = gin::PerContextData::From(context); |
53 gin::ContextHolder* context_holder = context_data->context_holder(); | 51 gin::ContextHolder* context_holder = context_data->context_holder(); |
54 runner_.reset(new WebUIRunner(frame_, context_holder)); | 52 runner_.reset(new WebUIRunner(frame_, context_holder)); |
55 gin::Runner::Scope scoper(runner_.get()); | 53 gin::Runner::Scope scoper(runner_.get()); |
56 gin::ModuleRegistry::From(context)->AddObserver(this); | 54 gin::ModuleRegistry::From(context)->AddObserver(this); |
57 content::RenderFrame::FromWebFrame(frame) | 55 content::RenderFrame::FromWebFrame(frame) |
58 ->EnsureMojoBuiltinsAreAvailable(context_holder->isolate(), context); | 56 ->EnsureMojoBuiltinsAreAvailable(context_holder->isolate(), context); |
59 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); | 57 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); |
60 // Warning |frame| may be destroyed. | 58 // Warning |frame| may be destroyed. |
61 // TODO(sky): add test for this. | 59 // TODO(sky): add test for this. |
(...skipping 20 matching lines...) Expand all Loading... |
82 context_holder->context()); | 80 context_holder->context()); |
83 for (size_t i = 0; i < ids.size(); ++i) { | 81 for (size_t i = 0; i < ids.size(); ++i) { |
84 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && | 82 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
85 registry->available_modules().count(ids[i]) == 0) { | 83 registry->available_modules().count(ids[i]) == 0) { |
86 FetchModule(ids[i]); | 84 FetchModule(ids[i]); |
87 } | 85 } |
88 } | 86 } |
89 } | 87 } |
90 | 88 |
91 void WebUIMojoContextState::FetchModule(const std::string& id) { | 89 void WebUIMojoContextState::FetchModule(const std::string& id) { |
92 const GURL url(kModulePrefix + id); | 90 const GURL url(module_prefix_ + id); |
93 // TODO(sky): better error checks here? | 91 // TODO(sky): better error checks here? |
94 DCHECK(url.is_valid() && !url.is_empty()); | 92 DCHECK(url.is_valid() && !url.is_empty()); |
95 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); | 93 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); |
96 fetched_modules_.insert(id); | 94 fetched_modules_.insert(id); |
97 ResourceFetcher* fetcher = ResourceFetcher::Create(url); | 95 ResourceFetcher* fetcher = ResourceFetcher::Create(url); |
98 module_fetchers_.push_back(fetcher); | 96 module_fetchers_.push_back(fetcher); |
99 fetcher->Start(frame_, | 97 fetcher->Start(frame_, |
100 blink::WebURLRequest::RequestContextScript, | 98 blink::WebURLRequest::RequestContextScript, |
101 blink::WebURLRequest::FrameTypeNone, | 99 blink::WebURLRequest::FrameTypeNone, |
102 ResourceFetcher::PLATFORM_LOADER, | 100 ResourceFetcher::PLATFORM_LOADER, |
103 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete, | 101 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete, |
104 base::Unretained(this), | 102 base::Unretained(this), |
105 fetcher)); | 103 fetcher)); |
106 } | 104 } |
107 | 105 |
108 void WebUIMojoContextState::OnFetchModuleComplete( | 106 void WebUIMojoContextState::OnFetchModuleComplete( |
109 ResourceFetcher* fetcher, | 107 ResourceFetcher* fetcher, |
110 const blink::WebURLResponse& response, | 108 const blink::WebURLResponse& response, |
111 const std::string& data) { | 109 const std::string& data) { |
112 DCHECK_EQ(kModulePrefix, | 110 DCHECK_EQ(module_prefix_, |
113 response.url().string().utf8().substr(0, arraysize(kModulePrefix) - 1)); | 111 response.url().string().utf8().substr(0, module_prefix_.size())); |
114 const std::string module = | 112 const std::string module = |
115 response.url().string().utf8().substr(arraysize(kModulePrefix) - 1); | 113 response.url().string().utf8().substr(module_prefix_.size()); |
116 // We can't delete fetch right now as the arguments to this function come from | 114 // We can't delete fetch right now as the arguments to this function come from |
117 // it and are used below. Instead use a scope_ptr to cleanup. | 115 // it and are used below. Instead use a scope_ptr to cleanup. |
118 scoped_ptr<ResourceFetcher> deleter(fetcher); | 116 scoped_ptr<ResourceFetcher> deleter(fetcher); |
119 module_fetchers_.weak_erase( | 117 module_fetchers_.weak_erase( |
120 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); | 118 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); |
121 if (data.empty()) { | 119 if (data.empty()) { |
122 NOTREACHED(); | 120 NOTREACHED(); |
123 return; // TODO(sky): log something? | 121 return; // TODO(sky): log something? |
124 } | 122 } |
125 | 123 |
126 runner_->Run(data, module); | 124 runner_->Run(data, module); |
127 } | 125 } |
128 | 126 |
129 void WebUIMojoContextState::OnDidAddPendingModule( | 127 void WebUIMojoContextState::OnDidAddPendingModule( |
130 const std::string& id, | 128 const std::string& id, |
131 const std::vector<std::string>& dependencies) { | 129 const std::vector<std::string>& dependencies) { |
132 FetchModules(dependencies); | 130 FetchModules(dependencies); |
133 | 131 |
134 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 132 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
135 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 133 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
136 context_holder->context()); | 134 context_holder->context()); |
137 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 135 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
138 } | 136 } |
139 | 137 |
140 } // namespace content | 138 } // namespace content |
OLD | NEW |