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