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