Chromium Code Reviews| Index: chromecast/renderer/cast_gin_runner.cc |
| diff --git a/chromecast/renderer/cast_gin_runner.cc b/chromecast/renderer/cast_gin_runner.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fe86f1e44c01652341b7514f334e7703726d3848 |
| --- /dev/null |
| +++ b/chromecast/renderer/cast_gin_runner.cc |
| @@ -0,0 +1,78 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromecast/renderer/cast_gin_runner.h" |
| + |
| +#include "content/public/renderer/render_frame.h" |
| +#include "gin/per_context_data.h" |
| +#include "third_party/WebKit/public/web/WebFrame.h" |
| +#include "third_party/WebKit/public/web/WebLocalFrame.h" |
| +#include "third_party/WebKit/public/web/WebScriptSource.h" |
| + |
| +namespace chromecast { |
| +namespace shell { |
| + |
| +const char kCastContextData[] = "CastContextData"; |
| + |
| +CastGinRunner::CastGinRunner(content::RenderFrame* render_frame, |
| + blink::WebFrame* frame, |
| + gin::ContextHolder* context_holder) |
| + : content::RenderFrameObserver(render_frame), |
| + frame_(frame), |
| + context_holder_(context_holder) { |
| + DCHECK(context_holder); |
| + DCHECK(frame_); |
| + v8::Isolate::Scope isolate_scope(context_holder->isolate()); |
| + v8::HandleScope handle_scope(context_holder->isolate()); |
| + // Note: this installs the runner globally. If we need to support more than |
| + // one runner at a time we'll have to revisit this. |
| + gin::PerContextData::From(context_holder->context())->set_runner(this); |
| +} |
| + |
| +void CastGinRunner::Run(const std::string& source, |
| + const std::string& resource_name) { |
| + frame_->executeScript( |
| + blink::WebScriptSource(blink::WebString::fromUTF8(source))); |
| +} |
| + |
| +v8::Local<v8::Value> CastGinRunner::Call(v8::Local<v8::Function> function, |
| + v8::Local<v8::Value> receiver, |
| + int argc, |
| + v8::Local<v8::Value> argv[]) { |
| + return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc, |
| + argv); |
| +} |
| + |
| +gin::ContextHolder* CastGinRunner::GetContextHolder() { |
| + return context_holder_; |
| +} |
| + |
| +void CastGinRunner::WillReleaseScriptContext(v8::Local<v8::Context> context, |
| + int world_id) { |
| + gin::PerContextData* context_data = gin::PerContextData::From(context); |
| + if (!context_data) |
| + return; |
| + context_data->RemoveUserData(kCastContextData); |
| +} |
| + |
| +void CastGinRunner::DidClearWindowObject() { |
| + v8::Local<v8::Context> context = |
| + render_frame()->GetWebFrame()->mainWorldScriptContext(); |
| + gin::PerContextData* context_data = gin::PerContextData::From(context); |
| + if (!context_data) |
| + return; |
| + context_data->RemoveUserData(kCastContextData); |
| +} |
| + |
| +void CastGinRunner::OnDestruct() { |
| + v8::Local<v8::Context> context = |
|
derekjchow1
2016/09/16 17:02:01
This function should call the destructor of this c
|
| + render_frame()->GetWebFrame()->mainWorldScriptContext(); |
| + gin::PerContextData* context_data = gin::PerContextData::From(context); |
| + if (!context_data) |
| + return; |
| + context_data->RemoveUserData(kCastContextData); |
| +} |
| + |
| +} // namespace shell |
| +} // namespace chromecast |