Make WebContents cache object template

This commit is contained in:
Cheng Zhao 2014-10-23 13:31:10 +08:00
parent 97139b7487
commit c72d769ff0

View file

@ -19,6 +19,12 @@ namespace atom {
namespace api {
namespace {
v8::Persistent<v8::ObjectTemplate> template_;
} // namespace
WebContents::WebContents(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
web_contents_(web_contents) {
@ -166,7 +172,8 @@ bool WebContents::SendIPCMessage(const base::string16& channel,
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
if (template_.IsEmpty())
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
.SetMethod("isAlive", &WebContents::IsAlive)
.SetMethod("loadUrl", &WebContents::LoadURL)
.SetMethod("getUrl", &WebContents::GetURL)
@ -187,7 +194,11 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
.SetMethod("getProcessId", &WebContents::GetProcessID)
.SetMethod("isCrashed", &WebContents::IsCrashed)
.SetMethod("_executeJavaScript", &WebContents::ExecuteJavaScript)
.SetMethod("_send", &WebContents::SendIPCMessage);
.SetMethod("_send", &WebContents::SendIPCMessage)
.Build());
return mate::ObjectTemplateBuilder(
isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_));
}
void WebContents::OnRendererMessage(const base::string16& channel,