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 |
| index 7d9db16f9a8d2d198be730369e575f0b85ff6a94..ca93bf51dbc66ac244ec7073ea2782ecf772e95f 100644 |
| --- a/chromecast/renderer/cast_gin_runner.cc |
| +++ b/chromecast/renderer/cast_gin_runner.cc |
| @@ -7,36 +7,45 @@ |
| #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/WebKit.h" |
| #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| #include "third_party/WebKit/public/web/WebScriptSource.h" |
| namespace chromecast { |
| namespace shell { |
| -CastGinRunner::CastGinRunner(content::RenderFrame* render_frame) |
| - : content::RenderFrameObserver(render_frame), |
| - frame_(render_frame->GetWebFrame()), |
| - context_holder_( |
| - gin::PerContextData::From(frame_->mainWorldScriptContext()) |
| - ->context_holder()) { |
| - DCHECK(frame_); |
| - DCHECK(context_holder_); |
| +namespace { |
| +const void* kCastContextData; |
| +const void* kCastGinRunnerKey = static_cast<const void*>(&kCastContextData); |
|
slan
2016/12/01 01:33:22
nit: Could you add a comment explaining that the k
slan
2016/12/01 01:37:51
Alternatively, you could also key on |render_frame
|
| +} |
| + |
| +// static |
| +CastGinRunner* CastGinRunner::Get(content::RenderFrame* render_frame) { |
| + DCHECK(render_frame); |
| + blink::WebFrame* frame = render_frame->GetWebFrame(); |
| + v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| gin::PerContextData* context_data = |
| - gin::PerContextData::From(frame_->mainWorldScriptContext()); |
| + gin::PerContextData::From(frame->mainWorldScriptContext()); |
| + CastGinRunner* runner = |
| + static_cast<CastGinRunner*>(context_data->GetUserData(kCastGinRunnerKey)); |
| + return runner ? runner : new CastGinRunner(frame, context_data); |
| +} |
| - v8::Isolate::Scope isolate_scope(context_holder_->isolate()); |
| - v8::HandleScope handle_scope(context_holder_->isolate()); |
| +CastGinRunner::CastGinRunner(blink::WebFrame* frame, |
| + gin::PerContextData* context_data) |
| + : frame_(frame), context_holder_(context_data->context_holder()) { |
| + DCHECK(frame_); |
| + DCHECK(context_holder_); |
| - context_data->SetUserData(kCastContextData, this); |
| + // context_data takes ownership of this class. |
| + context_data->SetUserData(kCastGinRunnerKey, this); |
| // Note: this installs the runner globally. If we need to support more than |
| // one runner at a time we'll have to revisit this. |
| context_data->set_runner(this); |
| } |
| -CastGinRunner::~CastGinRunner() { |
| - RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); |
| -} |
| +CastGinRunner::~CastGinRunner() {} |
| void CastGinRunner::Run(const std::string& source, |
| const std::string& resource_name) { |
| @@ -56,31 +65,5 @@ gin::ContextHolder* CastGinRunner::GetContextHolder() { |
| return context_holder_; |
| } |
| -void CastGinRunner::WillReleaseScriptContext(v8::Local<v8::Context> context, |
| - int world_id) { |
| - RemoveUserDataFromMainWorldContext(); |
| -} |
| - |
| -void CastGinRunner::DidClearWindowObject() { |
| - RemoveUserDataFromMainWorldContext(); |
| -} |
| - |
| -void CastGinRunner::OnDestruct() { |
| - RemoveUserDataFromMainWorldContext(); |
| -} |
| - |
| -void CastGinRunner::RemoveUserDataFromMainWorldContext() { |
| - v8::HandleScope handle_scope(context_holder_->isolate()); |
| - RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); |
| -} |
| - |
| -void CastGinRunner::RemoveUserData(v8::Local<v8::Context> context) { |
| - gin::PerContextData* context_data = gin::PerContextData::From(context); |
| - if (!context_data) |
| - return; |
| - |
| - context_data->RemoveUserData(kCastContextData); |
| -} |
| - |
| } // namespace shell |
| } // namespace chromecast |