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* kCastGinRunnerKey = static_cast<const void*>(kCastGinRunnerKey); | 
| 
 
slan
2016/11/30 19:26:09
kCastContextData? (with declaration above?)
 
derekjchow1
2016/11/30 22:32:34
Done.
 
 | |
| 18 frame_(render_frame->GetWebFrame()), | 19 } | 
| 19 context_holder_( | 20 | 
| 20 gin::PerContextData::From(frame_->mainWorldScriptContext()) | 21 // static | 
| 21 ->context_holder()) { | 22 CastGinRunner* CastGinRunner::Get(content::RenderFrame* render_frame) { | 
| 23 blink::WebFrame* frame = render_frame->GetWebFrame(); | |
| 
 
slan
2016/11/30 19:26:09
nit: DCHECK(render_frame)
 
derekjchow1
2016/11/30 22:32:34
Done.
 
 | |
| 24 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 25 gin::PerContextData* context_data = | |
| 26 gin::PerContextData::From(frame->mainWorldScriptContext()); | |
| 27 CastGinRunner* runner = | |
| 28 static_cast<CastGinRunner*>(context_data->GetUserData(kCastGinRunnerKey)); | |
| 29 return runner ? runner : new CastGinRunner(frame, context_data); | |
| 30 } | |
| 31 | |
| 32 CastGinRunner::CastGinRunner(blink::WebFrame* frame, | |
| 33 gin::PerContextData* context_data) | |
| 34 : frame_(frame), context_holder_(context_data->context_holder()) { | |
| 22 DCHECK(frame_); | 35 DCHECK(frame_); | 
| 23 DCHECK(context_holder_); | 36 DCHECK(context_holder_); | 
| 24 gin::PerContextData* context_data = | |
| 25 gin::PerContextData::From(frame_->mainWorldScriptContext()); | |
| 26 | 37 | 
| 27 v8::Isolate::Scope isolate_scope(context_holder_->isolate()); | 38 context_data->SetUserData(kCastGinRunnerKey, this); | 
| 
 
slan
2016/11/30 19:26:09
Can you add a comment here that |context_data| tak
 
derekjchow1
2016/11/30 22:32:34
Done.
 
 | |
| 28 v8::HandleScope handle_scope(context_holder_->isolate()); | |
| 29 | |
| 30 context_data->SetUserData(kCastContextData, this); | |
| 31 | 39 | 
| 32 // Note: this installs the runner globally. If we need to support more than | 40 // 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. | 41 // one runner at a time we'll have to revisit this. | 
| 34 context_data->set_runner(this); | 42 context_data->set_runner(this); | 
| 35 } | 43 } | 
| 36 | 44 | 
| 37 CastGinRunner::~CastGinRunner() { | 45 CastGinRunner::~CastGinRunner() {} | 
| 38 RemoveUserData(render_frame()->GetWebFrame()->mainWorldScriptContext()); | |
| 39 } | |
| 40 | 46 | 
| 41 void CastGinRunner::Run(const std::string& source, | 47 void CastGinRunner::Run(const std::string& source, | 
| 42 const std::string& resource_name) { | 48 const std::string& resource_name) { | 
| 43 frame_->executeScript( | 49 frame_->executeScript( | 
| 44 blink::WebScriptSource(blink::WebString::fromUTF8(source))); | 50 blink::WebScriptSource(blink::WebString::fromUTF8(source))); | 
| 45 } | 51 } | 
| 46 | 52 | 
| 47 v8::Local<v8::Value> CastGinRunner::Call(v8::Local<v8::Function> function, | 53 v8::Local<v8::Value> CastGinRunner::Call(v8::Local<v8::Function> function, | 
| 48 v8::Local<v8::Value> receiver, | 54 v8::Local<v8::Value> receiver, | 
| 49 int argc, | 55 int argc, | 
| 50 v8::Local<v8::Value> argv[]) { | 56 v8::Local<v8::Value> argv[]) { | 
| 51 return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc, | 57 return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc, | 
| 52 argv); | 58 argv); | 
| 53 } | 59 } | 
| 54 | 60 | 
| 55 gin::ContextHolder* CastGinRunner::GetContextHolder() { | 61 gin::ContextHolder* CastGinRunner::GetContextHolder() { | 
| 56 return context_holder_; | 62 return context_holder_; | 
| 57 } | 63 } | 
| 58 | 64 | 
| 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 | 65 } // namespace shell | 
| 86 } // namespace chromecast | 66 } // namespace chromecast | 
| OLD | NEW |