Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/usb/usb_chooser_bubble_controller.cc

Issue 1984923002: Refactor ChooserBubbleController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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/usb/usb_chooser_bubble_controller.h"
6
7 #include <stddef.h>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/usb/usb_chooser_context.h"
14 #include "chrome/browser/usb/usb_chooser_context_factory.h"
15 #include "chrome/browser/usb/web_usb_histograms.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/url_constants.h"
18 #include "components/bubble/bubble_controller.h"
19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "device/core/device_client.h"
22 #include "device/usb/mojo/type_converters.h"
23 #include "device/usb/usb_device.h"
24 #include "device/usb/usb_device_filter.h"
25 #include "device/usb/webusb_descriptors.h"
26 #include "url/gurl.h"
27
28 UsbChooserBubbleController::UsbChooserBubbleController(
29 content::RenderFrameHost* owner,
30 mojo::Array<device::usb::DeviceFilterPtr> device_filters,
31 content::RenderFrameHost* render_frame_host,
32 const device::usb::ChooserService::GetPermissionCallback& callback)
33 : ChooserBubbleController(owner),
34 render_frame_host_(render_frame_host),
35 callback_(callback),
36 usb_service_observer_(this),
37 weak_factory_(this) {
38 device::UsbService* usb_service =
39 device::DeviceClient::Get()->GetUsbService();
40 if (!usb_service)
41 return;
42
43 if (!usb_service_observer_.IsObserving(usb_service))
44 usb_service_observer_.Add(usb_service);
45
46 if (!device_filters.is_null())
47 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>();
48
49 usb_service->GetDevices(
50 base::Bind(&UsbChooserBubbleController::GotUsbDeviceList,
51 weak_factory_.GetWeakPtr()));
52 }
53
54 UsbChooserBubbleController::~UsbChooserBubbleController() {
55 if (!callback_.is_null())
56 callback_.Run(nullptr);
57 }
58
59 size_t UsbChooserBubbleController::NumOptions() const {
60 return devices_.size();
61 }
62
63 const base::string16& UsbChooserBubbleController::GetOption(
64 size_t index) const {
65 DCHECK_LT(index, devices_.size());
66 return devices_[index].second;
67 }
68
69 void UsbChooserBubbleController::Select(size_t index) {
70 DCHECK_LT(index, devices_.size());
71 content::WebContents* web_contents =
72 content::WebContents::FromRenderFrameHost(render_frame_host_);
73 GURL embedding_origin =
74 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin();
75 Profile* profile =
76 Profile::FromBrowserContext(web_contents->GetBrowserContext());
77 UsbChooserContext* chooser_context =
78 UsbChooserContextFactory::GetForProfile(profile);
79 chooser_context->GrantDevicePermission(
80 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin,
81 devices_[index].first->guid());
82
83 device::usb::DeviceInfoPtr device_info_ptr =
84 device::usb::DeviceInfo::From(*devices_[index].first);
85 callback_.Run(std::move(device_info_ptr));
86 callback_.reset(); // Reset |callback_| so that it is only run once.
87
88 RecordWebUsbChooserClosure(
89 devices_[index].first->serial_number().empty()
90 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED
91 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED);
92
93 if (bubble_reference_)
94 bubble_reference_->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
95 }
96
97 void UsbChooserBubbleController::Cancel() {
98 RecordWebUsbChooserClosure(devices_.size() == 0
99 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES
100 : WEBUSB_CHOOSER_CLOSED_CANCELLED);
101
102 if (bubble_reference_)
103 bubble_reference_->CloseBubble(BUBBLE_CLOSE_CANCELED);
104 }
105
106 void UsbChooserBubbleController::Close() {}
107
108 void UsbChooserBubbleController::OnDeviceAdded(
109 scoped_refptr<device::UsbDevice> device) {
110 if (DisplayDevice(device)) {
111 devices_.push_back(std::make_pair(device, device->product_string()));
112 if (observer())
113 observer()->OnOptionAdded(devices_.size() - 1);
114 }
115 }
116
117 GURL UsbChooserBubbleController::GetHelpCenterUrl() const {
118 return GURL(chrome::kChooserUsbOverviewURL);
119 }
120
121 void UsbChooserBubbleController::OnDeviceRemoved(
122 scoped_refptr<device::UsbDevice> device) {
123 for (auto it = devices_.begin(); it != devices_.end(); ++it) {
124 if (it->first == device) {
125 size_t index = it - devices_.begin();
126 devices_.erase(it);
127 if (observer())
128 observer()->OnOptionRemoved(index);
129 return;
130 }
131 }
132 }
133
134 void UsbChooserBubbleController::set_bubble_reference(
135 BubbleReference bubble_reference) {
136 bubble_reference_ = bubble_reference;
137 }
138
139 // Get a list of devices that can be shown in the chooser bubble UI for
140 // user to grant permsssion.
141 void UsbChooserBubbleController::GotUsbDeviceList(
142 const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
143 for (const auto& device : devices) {
144 if (DisplayDevice(device))
145 devices_.push_back(std::make_pair(device, device->product_string()));
146 }
147 if (observer())
148 observer()->OnOptionsInitialized();
149 }
150
151 bool UsbChooserBubbleController::DisplayDevice(
152 scoped_refptr<device::UsbDevice> device) const {
153 return device::UsbDeviceFilter::MatchesAny(device, filters_) &&
154 (base::CommandLine::ForCurrentProcess()->HasSwitch(
155 switches::kDisableWebUsbSecurity) ||
156 device::FindInWebUsbAllowedOrigins(
157 device->webusb_allowed_origins(),
158 render_frame_host_->GetLastCommittedURL().GetOrigin()));
159 }
OLDNEW
« no previous file with comments | « chrome/browser/usb/usb_chooser_bubble_controller.h ('k') | chrome/browser/usb/usb_chooser_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698