forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_converter.cc
89 lines (68 loc) · 2.66 KB
/
frame_converter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2020 Samuel Maddock <sam@samuelmaddock.com>.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_converters/frame_converter.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "shell/browser/api/electron_api_web_frame_main.h"
#include "shell/common/gin_helper/accessor.h"
namespace gin {
namespace {
v8::Persistent<v8::ObjectTemplate> rfh_templ;
} // namespace
// static
v8::Local<v8::Value> Converter<content::RenderFrameHost*>::ToV8(
v8::Isolate* isolate,
content::RenderFrameHost* val) {
if (!val)
return v8::Null(isolate);
return electron::api::WebFrameMain::From(isolate, val).ToV8();
}
// static
v8::Local<v8::Value>
Converter<gin_helper::AccessorValue<content::RenderFrameHost*>>::ToV8(
v8::Isolate* isolate,
gin_helper::AccessorValue<content::RenderFrameHost*> val) {
content::RenderFrameHost* rfh = val.Value;
if (!rfh)
return v8::Null(isolate);
const int process_id = rfh->GetProcess()->GetID();
const int routing_id = rfh->GetRoutingID();
if (rfh_templ.IsEmpty()) {
v8::EscapableHandleScope inner(isolate);
v8::Local<v8::ObjectTemplate> local = v8::ObjectTemplate::New(isolate);
local->SetInternalFieldCount(2);
rfh_templ.Reset(isolate, inner.Escape(local));
}
v8::Local<v8::Object> rfh_obj =
v8::Local<v8::ObjectTemplate>::New(isolate, rfh_templ)
->NewInstance(isolate->GetCurrentContext())
.ToLocalChecked();
rfh_obj->SetInternalField(0, v8::Number::New(isolate, process_id));
rfh_obj->SetInternalField(1, v8::Number::New(isolate, routing_id));
return rfh_obj;
}
// static
bool Converter<gin_helper::AccessorValue<content::RenderFrameHost*>>::FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
gin_helper::AccessorValue<content::RenderFrameHost*>* out) {
v8::Local<v8::Object> rfh_obj;
if (!ConvertFromV8(isolate, val, &rfh_obj))
return false;
if (rfh_obj->InternalFieldCount() != 2)
return false;
v8::Local<v8::Value> process_id_wrapper = rfh_obj->GetInternalField(0);
v8::Local<v8::Value> routing_id_wrapper = rfh_obj->GetInternalField(1);
if (process_id_wrapper.IsEmpty() || !process_id_wrapper->IsNumber() ||
routing_id_wrapper.IsEmpty() || !routing_id_wrapper->IsNumber())
return false;
const int process_id = process_id_wrapper.As<v8::Number>()->Value();
const int routing_id = routing_id_wrapper.As<v8::Number>()->Value();
auto* rfh = content::RenderFrameHost::FromID(process_id, routing_id);
if (!rfh)
return false;
out->Value = rfh;
return true;
}
} // namespace gin