-
Notifications
You must be signed in to change notification settings - Fork 15k
/
electron_serial_delegate.cc
142 lines (118 loc) · 5.19 KB
/
electron_serial_delegate.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2020 Microsoft, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/serial/electron_serial_delegate.h"
#include <utility>
#include "base/feature_list.h"
#include "content/public/browser/web_contents.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/serial/serial_chooser_context.h"
#include "shell/browser/serial/serial_chooser_context_factory.h"
#include "shell/browser/serial/serial_chooser_controller.h"
#include "shell/browser/web_contents_permission_helper.h"
namespace electron {
SerialChooserContext* GetChooserContext(content::RenderFrameHost* frame) {
auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
auto* browser_context = web_contents->GetBrowserContext();
return SerialChooserContextFactory::GetForBrowserContext(browser_context);
}
ElectronSerialDelegate::ElectronSerialDelegate() = default;
ElectronSerialDelegate::~ElectronSerialDelegate() = default;
std::unique_ptr<content::SerialChooser> ElectronSerialDelegate::RunChooser(
content::RenderFrameHost* frame,
std::vector<blink::mojom::SerialPortFilterPtr> filters,
content::SerialChooser::Callback callback) {
SerialChooserController* controller = ControllerForFrame(frame);
if (controller) {
DeleteControllerForFrame(frame);
}
AddControllerForFrame(frame, std::move(filters), std::move(callback));
// Return a nullptr because the return value isn't used for anything, eg
// there is no mechanism to cancel navigator.serial.requestPort(). The return
// value is simply used in Chromium to cleanup the chooser UI once the serial
// service is destroyed.
return nullptr;
}
bool ElectronSerialDelegate::CanRequestPortPermission(
content::RenderFrameHost* frame) {
auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
auto* permission_helper =
WebContentsPermissionHelper::FromWebContents(web_contents);
return permission_helper->CheckSerialAccessPermission(
web_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
}
bool ElectronSerialDelegate::HasPortPermission(
content::RenderFrameHost* frame,
const device::mojom::SerialPortInfo& port) {
auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
return GetChooserContext(frame)->HasPortPermission(
web_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin(), port,
frame);
}
device::mojom::SerialPortManager* ElectronSerialDelegate::GetPortManager(
content::RenderFrameHost* frame) {
return GetChooserContext(frame)->GetPortManager();
}
void ElectronSerialDelegate::AddObserver(content::RenderFrameHost* frame,
Observer* observer) {
observer_list_.AddObserver(observer);
auto* chooser_context = GetChooserContext(frame);
if (!port_observation_.IsObserving())
port_observation_.Observe(chooser_context);
}
void ElectronSerialDelegate::RemoveObserver(content::RenderFrameHost* frame,
Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void ElectronSerialDelegate::RevokePortPermissionWebInitiated(
content::RenderFrameHost* frame,
const base::UnguessableToken& token) {
// TODO(nornagon/jkleinsc): pass this on to the chooser context
}
const device::mojom::SerialPortInfo* ElectronSerialDelegate::GetPortInfo(
content::RenderFrameHost* frame,
const base::UnguessableToken& token) {
return GetChooserContext(frame)->GetPortInfo(token);
}
SerialChooserController* ElectronSerialDelegate::ControllerForFrame(
content::RenderFrameHost* render_frame_host) {
auto mapping = controller_map_.find(render_frame_host);
return mapping == controller_map_.end() ? nullptr : mapping->second.get();
}
SerialChooserController* ElectronSerialDelegate::AddControllerForFrame(
content::RenderFrameHost* render_frame_host,
std::vector<blink::mojom::SerialPortFilterPtr> filters,
content::SerialChooser::Callback callback) {
auto* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
auto controller = std::make_unique<SerialChooserController>(
render_frame_host, std::move(filters), std::move(callback), web_contents,
weak_factory_.GetWeakPtr());
controller_map_.insert(
std::make_pair(render_frame_host, std::move(controller)));
return ControllerForFrame(render_frame_host);
}
void ElectronSerialDelegate::DeleteControllerForFrame(
content::RenderFrameHost* render_frame_host) {
controller_map_.erase(render_frame_host);
}
// SerialChooserContext::PortObserver:
void ElectronSerialDelegate::OnPortAdded(
const device::mojom::SerialPortInfo& port) {
for (auto& observer : observer_list_)
observer.OnPortAdded(port);
}
void ElectronSerialDelegate::OnPortRemoved(
const device::mojom::SerialPortInfo& port) {
for (auto& observer : observer_list_)
observer.OnPortRemoved(port);
}
void ElectronSerialDelegate::OnPortManagerConnectionError() {
port_observation_.Reset();
for (auto& observer : observer_list_)
observer.OnPortManagerConnectionError();
}
void ElectronSerialDelegate::OnSerialChooserContextShutdown() {
port_observation_.Reset();
}
} // namespace electron