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

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

Issue 1457623004: Serve mojo WebUI resources from the same origin as the WebUI itself. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
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/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
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 context_holder->context()); 79 context_holder->context());
83 for (size_t i = 0; i < ids.size(); ++i) { 80 for (size_t i = 0; i < ids.size(); ++i) {
84 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && 81 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() &&
85 registry->available_modules().count(ids[i]) == 0) { 82 registry->available_modules().count(ids[i]) == 0) {
86 FetchModule(ids[i]); 83 FetchModule(ids[i]);
87 } 84 }
88 } 85 }
89 } 86 }
90 87
91 void WebUIMojoContextState::FetchModule(const std::string& id) { 88 void WebUIMojoContextState::FetchModule(const std::string& id) {
92 const GURL url(kModulePrefix + id); 89 const GURL url(GetModulePrefix() + id);
93 // TODO(sky): better error checks here? 90 // TODO(sky): better error checks here?
94 DCHECK(url.is_valid() && !url.is_empty()); 91 DCHECK(url.is_valid() && !url.is_empty());
95 DCHECK(fetched_modules_.find(id) == fetched_modules_.end()); 92 DCHECK(fetched_modules_.find(id) == fetched_modules_.end());
96 fetched_modules_.insert(id); 93 fetched_modules_.insert(id);
97 ResourceFetcher* fetcher = ResourceFetcher::Create(url); 94 ResourceFetcher* fetcher = ResourceFetcher::Create(url);
98 module_fetchers_.push_back(fetcher); 95 module_fetchers_.push_back(fetcher);
99 fetcher->Start(frame_, 96 fetcher->Start(frame_,
100 blink::WebURLRequest::RequestContextScript, 97 blink::WebURLRequest::RequestContextScript,
101 blink::WebURLRequest::FrameTypeNone, 98 blink::WebURLRequest::FrameTypeNone,
102 ResourceFetcher::PLATFORM_LOADER, 99 ResourceFetcher::PLATFORM_LOADER,
103 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete, 100 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete,
104 base::Unretained(this), 101 base::Unretained(this),
105 fetcher)); 102 fetcher));
106 } 103 }
107 104
108 void WebUIMojoContextState::OnFetchModuleComplete( 105 void WebUIMojoContextState::OnFetchModuleComplete(
109 ResourceFetcher* fetcher, 106 ResourceFetcher* fetcher,
110 const blink::WebURLResponse& response, 107 const blink::WebURLResponse& response,
111 const std::string& data) { 108 const std::string& data) {
112 DCHECK_EQ(kModulePrefix, 109 DCHECK_EQ(GetModulePrefix(),
113 response.url().string().utf8().substr(0, arraysize(kModulePrefix) - 1)); 110 response.url().string().utf8().substr(0, GetModulePrefix().size()));
114 const std::string module = 111 const std::string module =
115 response.url().string().utf8().substr(arraysize(kModulePrefix) - 1); 112 response.url().string().utf8().substr(GetModulePrefix().size());
116 // We can't delete fetch right now as the arguments to this function come from 113 // 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. 114 // it and are used below. Instead use a scope_ptr to cleanup.
118 scoped_ptr<ResourceFetcher> deleter(fetcher); 115 scoped_ptr<ResourceFetcher> deleter(fetcher);
119 module_fetchers_.weak_erase( 116 module_fetchers_.weak_erase(
120 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher)); 117 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher));
121 if (data.empty()) { 118 if (data.empty()) {
122 NOTREACHED(); 119 NOTREACHED();
123 return; // TODO(sky): log something? 120 return; // TODO(sky): log something?
124 } 121 }
125 122
126 runner_->Run(data, module); 123 runner_->Run(data, module);
127 } 124 }
128 125
129 void WebUIMojoContextState::OnDidAddPendingModule( 126 void WebUIMojoContextState::OnDidAddPendingModule(
130 const std::string& id, 127 const std::string& id,
131 const std::vector<std::string>& dependencies) { 128 const std::vector<std::string>& dependencies) {
132 FetchModules(dependencies); 129 FetchModules(dependencies);
133 130
134 gin::ContextHolder* context_holder = runner_->GetContextHolder(); 131 gin::ContextHolder* context_holder = runner_->GetContextHolder();
135 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( 132 gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
136 context_holder->context()); 133 context_holder->context());
137 registry->AttemptToLoadMoreModules(context_holder->isolate()); 134 registry->AttemptToLoadMoreModules(context_holder->isolate());
138 } 135 }
139 136
137 const std::string& WebUIMojoContextState::GetModulePrefix() {
138 if (module_prefix_.empty())
sky 2015/11/19 17:43:38 Can module_prefix_ be set correctly in the constru
Sam McNally 2015/11/22 23:43:30 Done.
139 module_prefix_ = frame_->securityOrigin().toString().utf8() + "/";
140 return module_prefix_;
141 }
142
140 } // namespace content 143 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/web_ui_mojo_context_state.h ('k') | content/test/data/web_ui_mojo_shell_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698