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