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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/serial/chrome_serial_delegate.h"
#include <utility>
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/serial/serial_chooser_context.h"
#include "chrome/browser/serial/serial_chooser_context_factory.h"
#include "chrome/browser/serial/web_serial_chooser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/serial/serial_chooser_controller.h"
#include "components/guest_view/buildflags/buildflags.h"
#include "content/public/browser/render_frame_host.h"
#if BUILDFLAG(ENABLE_GUEST_VIEW)
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#endif // BUILDFLAG(ENABLE_GUEST_VIEW)
namespace {
SerialChooserContext* GetChooserContext(content::RenderFrameHost* frame) {
auto* profile = Profile::FromBrowserContext(frame->GetBrowserContext());
return SerialChooserContextFactory::GetForProfile(profile);
}
} // namespace
ChromeSerialDelegate::ChromeSerialDelegate() = default;
ChromeSerialDelegate::~ChromeSerialDelegate() = default;
std::unique_ptr<content::SerialChooser> ChromeSerialDelegate::RunChooser(
content::RenderFrameHost* frame,
std::vector<blink::mojom::SerialPortFilterPtr> filters,
std::vector<device::BluetoothUUID> allowed_bluetooth_service_class_ids,
content::SerialChooser::Callback callback) {
return WebSerialChooser::Create(
frame,
std::make_unique<SerialChooserController>(
frame, std::move(filters),
std::move(allowed_bluetooth_service_class_ids), std::move(callback)));
}
bool ChromeSerialDelegate::CanRequestPortPermission(
content::RenderFrameHost* frame) {
#if BUILDFLAG(ENABLE_GUEST_VIEW)
// <webview> and <controlledframe> can not isolate origin-based permissions
// from the rest of profile, therefore serial is disabled inside.
if (extensions::WebViewGuest::FromRenderFrameHost(frame)) {
return false;
}
#endif // BUILDFLAG(ENABLE_GUEST_VIEW)
return GetChooserContext(frame)->CanRequestObjectPermission(
frame->GetMainFrame()->GetLastCommittedOrigin());
}
bool ChromeSerialDelegate::HasPortPermission(
content::RenderFrameHost* frame,
const device::mojom::SerialPortInfo& port) {
#if BUILDFLAG(ENABLE_GUEST_VIEW)
// <webview> and <controlledframe> can not isolate origin-based permissions
// from the rest of profile, therefore serial is disabled inside.
if (extensions::WebViewGuest::FromRenderFrameHost(frame)) {
return false;
}
#endif // BUILDFLAG(ENABLE_GUEST_VIEW)
return GetChooserContext(frame)->HasPortPermission(
frame->GetMainFrame()->GetLastCommittedOrigin(), port);
}
void ChromeSerialDelegate::RevokePortPermissionWebInitiated(
content::RenderFrameHost* frame,
const base::UnguessableToken& token) {
return GetChooserContext(frame)->RevokePortPermissionWebInitiated(
frame->GetMainFrame()->GetLastCommittedOrigin(), token);
}
const device::mojom::SerialPortInfo* ChromeSerialDelegate::GetPortInfo(
content::RenderFrameHost* frame,
const base::UnguessableToken& token) {
return GetChooserContext(frame)->GetPortInfo(token);
}
device::mojom::SerialPortManager* ChromeSerialDelegate::GetPortManager(
content::RenderFrameHost* frame) {
return GetChooserContext(frame)->GetPortManager();
}
void ChromeSerialDelegate::AddObserver(content::RenderFrameHost* frame,
Observer* observer) {
return GetChooserContext(frame)->AddPortObserver(observer);
}
void ChromeSerialDelegate::RemoveObserver(content::RenderFrameHost* frame,
Observer* observer) {
return GetChooserContext(frame)->RemovePortObserver(observer);
}
|