-
Notifications
You must be signed in to change notification settings - Fork 15k
/
electron_api_ipc_handler_impl.cc
108 lines (93 loc) · 4.24 KB
/
electron_api_ipc_handler_impl.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2022 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/electron_api_ipc_handler_impl.h"
#include <utility>
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace electron {
ElectronApiIPCHandlerImpl::ElectronApiIPCHandlerImpl(
content::RenderFrameHost* frame_host,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver)
: render_process_id_(frame_host->GetProcess()->GetID()),
render_frame_id_(frame_host->GetRoutingID()) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame_host);
DCHECK(web_contents);
content::WebContentsObserver::Observe(web_contents);
receiver_.Bind(std::move(receiver));
receiver_.set_disconnect_handler(base::BindOnce(
&ElectronApiIPCHandlerImpl::OnConnectionError, GetWeakPtr()));
}
ElectronApiIPCHandlerImpl::~ElectronApiIPCHandlerImpl() = default;
void ElectronApiIPCHandlerImpl::WebContentsDestroyed() {
delete this;
}
void ElectronApiIPCHandlerImpl::OnConnectionError() {
delete this;
}
void ElectronApiIPCHandlerImpl::Message(bool internal,
const std::string& channel,
blink::CloneableMessage arguments) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->Message(internal, channel, std::move(arguments),
GetRenderFrameHost());
}
}
void ElectronApiIPCHandlerImpl::Invoke(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
InvokeCallback callback) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->Invoke(internal, channel, std::move(arguments),
std::move(callback), GetRenderFrameHost());
}
}
void ElectronApiIPCHandlerImpl::ReceivePostMessage(
const std::string& channel,
blink::TransferableMessage message) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->ReceivePostMessage(channel, std::move(message),
GetRenderFrameHost());
}
}
void ElectronApiIPCHandlerImpl::MessageSync(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
MessageSyncCallback callback) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->MessageSync(internal, channel, std::move(arguments),
std::move(callback), GetRenderFrameHost());
}
}
void ElectronApiIPCHandlerImpl::MessageTo(int32_t web_contents_id,
const std::string& channel,
blink::CloneableMessage arguments) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->MessageTo(web_contents_id, channel, std::move(arguments));
}
}
void ElectronApiIPCHandlerImpl::MessageHost(const std::string& channel,
blink::CloneableMessage arguments) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->MessageHost(channel, std::move(arguments),
GetRenderFrameHost());
}
}
content::RenderFrameHost* ElectronApiIPCHandlerImpl::GetRenderFrameHost() {
return content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
}
// static
void ElectronApiIPCHandlerImpl::Create(
content::RenderFrameHost* frame_host,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver) {
new ElectronApiIPCHandlerImpl(frame_host, std::move(receiver));
}
} // namespace electron