OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromecast/renderer/cast_gin_runner.h" |
| 6 |
| 7 #include "content/public/renderer/render_frame.h" |
| 8 #include "gin/per_context_data.h" |
| 9 #include "third_party/WebKit/public/web/WebFrame.h" |
| 10 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 11 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 12 |
| 13 namespace chromecast { |
| 14 namespace shell { |
| 15 |
| 16 CastGinRunner::CastGinRunner(content::RenderFrame* render_frame) |
| 17 : content::RenderFrameObserver(render_frame), |
| 18 frame_(render_frame->GetWebFrame()), |
| 19 context_holder_( |
| 20 gin::PerContextData::From(frame_->mainWorldScriptContext()) |
| 21 ->context_holder()) { |
| 22 DCHECK(frame_); |
| 23 DCHECK(context_holder_); |
| 24 gin::PerContextData* context_data = |
| 25 gin::PerContextData::From(frame_->mainWorldScriptContext()); |
| 26 |
| 27 v8::Isolate::Scope isolate_scope(context_holder_->isolate()); |
| 28 v8::HandleScope handle_scope(context_holder_->isolate()); |
| 29 |
| 30 context_data->SetUserData(kCastContextData, this); |
| 31 |
| 32 // Note: this installs the runner globally. If we need to support more than |
| 33 // one runner at a time we'll have to revisit this. |
| 34 context_data->set_runner(this); |
| 35 } |
| 36 |
| 37 CastGinRunner::~CastGinRunner() { |
| 38 RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); |
| 39 } |
| 40 |
| 41 void CastGinRunner::Run(const std::string& source, |
| 42 const std::string& resource_name) { |
| 43 frame_->executeScript( |
| 44 blink::WebScriptSource(blink::WebString::fromUTF8(source))); |
| 45 } |
| 46 |
| 47 v8::Local<v8::Value> CastGinRunner::Call(v8::Local<v8::Function> function, |
| 48 v8::Local<v8::Value> receiver, |
| 49 int argc, |
| 50 v8::Local<v8::Value> argv[]) { |
| 51 return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc, |
| 52 argv); |
| 53 } |
| 54 |
| 55 gin::ContextHolder* CastGinRunner::GetContextHolder() { |
| 56 return context_holder_; |
| 57 } |
| 58 |
| 59 void CastGinRunner::WillReleaseScriptContext(v8::Local<v8::Context> context, |
| 60 int world_id) { |
| 61 RemoveUserData(context); |
| 62 } |
| 63 |
| 64 void CastGinRunner::DidClearWindowObject() { |
| 65 RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); |
| 66 } |
| 67 |
| 68 void CastGinRunner::OnDestruct() { |
| 69 RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); |
| 70 } |
| 71 |
| 72 void CastGinRunner::RemoveUserData(v8::Local<v8::Context> context) { |
| 73 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 74 if (!context_data) |
| 75 return; |
| 76 |
| 77 context_data->RemoveUserData(kCastContextData); |
| 78 } |
| 79 |
| 80 } // namespace shell |
| 81 } // namespace chromecast |
OLD | NEW |