Reilly Grant | 0d28232 | 2019-01-29 02:42:58 | [diff] [blame] | 1 | // 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 | |
| 19 | ChromeSerialDelegate::ChromeSerialDelegate() = default; |
| 20 | |
| 21 | ChromeSerialDelegate::~ChromeSerialDelegate() = default; |
| 22 | |
| 23 | std::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 Grant | 87d6eb02 | 2019-04-19 23:55:59 | [diff] [blame] | 43 | bool 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 Grant | 2e64da3 | 2019-04-23 18:06:47 | [diff] [blame] | 50 | frame->GetLastCommittedOrigin(), |
| 51 | web_contents->GetMainFrame()->GetLastCommittedOrigin()); |
Reilly Grant | 87d6eb02 | 2019-04-19 23:55:59 | [diff] [blame] | 52 | } |
| 53 | |
Reilly Grant | 0d28232 | 2019-01-29 02:42:58 | [diff] [blame] | 54 | bool 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 | |
| 66 | device::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 | } |