blob: 18f9064ba23a3605a6ea4eef6c309c6a51f0fc66 [file] [log] [blame]
Reilly Grant0d282322019-01-29 02:42:581// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/serial/chrome_serial_delegate.h"
6
7#include <utility>
8
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/serial/serial_chooser_context.h"
11#include "chrome/browser/serial/serial_chooser_context_factory.h"
12#include "chrome/browser/ui/browser_finder.h"
13#include "chrome/browser/ui/chrome_bubble_manager.h"
14#include "chrome/browser/ui/permission_bubble/chooser_bubble_delegate.h"
15#include "chrome/browser/ui/serial/serial_chooser.h"
16#include "chrome/browser/ui/serial/serial_chooser_controller.h"
17#include "content/public/browser/web_contents.h"
18
19ChromeSerialDelegate::ChromeSerialDelegate() = default;
20
21ChromeSerialDelegate::~ChromeSerialDelegate() = default;
22
23std::unique_ptr<content::SerialChooser> ChromeSerialDelegate::RunChooser(
24 content::RenderFrameHost* frame,
25 std::vector<blink::mojom::SerialPortFilterPtr> filters,
26 content::SerialChooser::Callback callback) {
27 Browser* browser = chrome::FindBrowserWithWebContents(
28 content::WebContents::FromRenderFrameHost(frame));
29 if (!browser) {
30 std::move(callback).Run(nullptr);
31 return nullptr;
32 }
33
34 auto chooser_controller = std::make_unique<SerialChooserController>(
35 frame, std::move(filters), std::move(callback));
36 auto chooser_bubble_delegate = std::make_unique<ChooserBubbleDelegate>(
37 frame, std::move(chooser_controller));
38 BubbleReference bubble_reference = browser->GetBubbleManager()->ShowBubble(
39 std::move(chooser_bubble_delegate));
40 return std::make_unique<SerialChooser>(std::move(bubble_reference));
41}
42
Reilly Grant87d6eb022019-04-19 23:55:5943bool ChromeSerialDelegate::CanRequestPortPermission(
44 content::RenderFrameHost* frame) {
45 auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
46 auto* profile =
47 Profile::FromBrowserContext(web_contents->GetBrowserContext());
48 auto* chooser_context = SerialChooserContextFactory::GetForProfile(profile);
49 return chooser_context->CanRequestObjectPermission(
Reilly Grant2e64da32019-04-23 18:06:4750 frame->GetLastCommittedOrigin(),
51 web_contents->GetMainFrame()->GetLastCommittedOrigin());
Reilly Grant87d6eb022019-04-19 23:55:5952}
53
Reilly Grant0d282322019-01-29 02:42:5854bool ChromeSerialDelegate::HasPortPermission(
55 content::RenderFrameHost* frame,
56 const device::mojom::SerialPortInfo& port) {
57 auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
58 auto* profile =
59 Profile::FromBrowserContext(web_contents->GetBrowserContext());
60 auto* chooser_context = SerialChooserContextFactory::GetForProfile(profile);
61 return chooser_context->HasPortPermission(
62 frame->GetLastCommittedOrigin(),
63 web_contents->GetMainFrame()->GetLastCommittedOrigin(), port);
64}
65
66device::mojom::SerialPortManager* ChromeSerialDelegate::GetPortManager(
67 content::RenderFrameHost* frame) {
68 auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
69 auto* profile =
70 Profile::FromBrowserContext(web_contents->GetBrowserContext());
71 auto* chooser_context = SerialChooserContextFactory::GetForProfile(profile);
72 return chooser_context->GetPortManager();
73}