OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/usb/usb_chooser_bubble_delegate.h" | 5 #include "chrome/browser/usb/usb_chooser_bubble_delegate.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/usb/usb_chooser_context.h" | 10 #include "chrome/browser/usb/usb_chooser_context.h" |
11 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 11 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
12 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 #include "device/core/device_client.h" | 14 #include "device/core/device_client.h" |
15 #include "device/devices_app/usb/type_converters.h" | 15 #include "device/devices_app/usb/type_converters.h" |
16 #include "device/usb/usb_device.h" | 16 #include "device/usb/usb_device.h" |
17 #include "device/usb/usb_device_filter.h" | 17 #include "device/usb/usb_device_filter.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 #if defined(OS_MACOSX) | |
21 #include "components/bubble/bubble_ui.h" | |
22 #else | |
23 #include "chrome/browser/ui/views/website_settings/chooser_bubble_ui_view.h" | |
24 #endif | |
25 | |
26 namespace { | 20 namespace { |
27 | 21 |
28 // Check if the origin is in the description set. | 22 // Check if the origin is in the description set. |
29 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | 23 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, |
30 const GURL& origin) { | 24 const GURL& origin) { |
31 if (!set) | 25 if (!set) |
32 return false; | 26 return false; |
33 | 27 |
34 if (ContainsValue(set->origins, origin)) | 28 if (ContainsValue(set->origins, origin)) |
35 return true; | 29 return true; |
(...skipping 11 matching lines...) Expand all Loading... | |
47 return false; | 41 return false; |
48 } | 42 } |
49 | 43 |
50 } // namespace | 44 } // namespace |
51 | 45 |
52 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate( | 46 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate( |
53 Browser* browser, | 47 Browser* browser, |
54 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | 48 mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
55 content::RenderFrameHost* render_frame_host, | 49 content::RenderFrameHost* render_frame_host, |
56 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) | 50 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) |
57 : browser_(browser), | 51 : ChooserBubbleDelegate(browser), |
58 render_frame_host_(render_frame_host), | 52 render_frame_host_(render_frame_host), |
59 callback_(callback), | 53 callback_(callback), |
60 usb_service_observer_(this), | 54 usb_service_observer_(this), |
61 weak_factory_(this) { | 55 weak_factory_(this) { |
62 DCHECK(browser_); | 56 DCHECK(browser); |
Reilly Grant (use Gerrit)
2015/12/09 18:56:41
nit: This DCHECK isn't really necessary because th
juncai
2015/12/09 23:13:56
Done.
| |
63 device::UsbService* usb_service = | 57 device::UsbService* usb_service = |
64 device::DeviceClient::Get()->GetUsbService(); | 58 device::DeviceClient::Get()->GetUsbService(); |
65 if (!usb_service) | 59 if (!usb_service) |
66 return; | 60 return; |
67 | 61 |
68 if (!usb_service_observer_.IsObserving(usb_service)) | 62 if (!usb_service_observer_.IsObserving(usb_service)) |
69 usb_service_observer_.Add(usb_service); | 63 usb_service_observer_.Add(usb_service); |
70 | 64 |
71 if (!device_filters.is_null()) | 65 if (!device_filters.is_null()) |
72 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); | 66 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); |
73 | 67 |
74 usb_service->GetDevices(base::Bind( | 68 usb_service->GetDevices(base::Bind( |
75 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); | 69 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); |
76 } | 70 } |
77 | 71 |
78 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { | 72 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { |
79 if (!callback_.is_null()) | 73 if (!callback_.is_null()) |
80 callback_.Run(nullptr); | 74 callback_.Run(nullptr); |
81 } | 75 } |
82 | 76 |
83 scoped_ptr<BubbleUi> UsbChooserBubbleDelegate::BuildBubbleUi() { | |
84 scoped_ptr<BubbleUi> bubble_ui; | |
85 // TODO(juncai): Add chooser bubble ui cocoa code for Mac. | |
86 // Please refer to http://crbug.com/492204 for more information. | |
87 #if !defined(OS_MACOSX) | |
88 bubble_ui.reset(new ChooserBubbleUiView(browser_, this)); | |
89 #endif | |
90 return bubble_ui.Pass(); | |
91 } | |
92 | |
93 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions() | 77 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions() |
94 const { | 78 const { |
95 return devices_names_; | 79 return devices_names_; |
96 } | 80 } |
97 | 81 |
98 void UsbChooserBubbleDelegate::Select(int index) { | 82 void UsbChooserBubbleDelegate::Select(int index) { |
99 size_t idx = static_cast<size_t>(index); | 83 size_t idx = static_cast<size_t>(index); |
100 size_t num_options = devices_.size(); | 84 size_t num_options = devices_.size(); |
101 DCHECK_LT(idx, num_options); | 85 DCHECK_LT(idx, num_options); |
102 if (idx < num_options) { | 86 if (idx < num_options) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 FindOriginInDescriptorSet( | 144 FindOriginInDescriptorSet( |
161 device->webusb_allowed_origins(), | 145 device->webusb_allowed_origins(), |
162 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 146 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
163 devices_.push_back(device); | 147 devices_.push_back(device); |
164 devices_names_.push_back(device->product_string()); | 148 devices_names_.push_back(device->product_string()); |
165 } | 149 } |
166 } | 150 } |
167 if (observer()) | 151 if (observer()) |
168 observer()->OnOptionsInitialized(); | 152 observer()->OnOptionsInitialized(); |
169 } | 153 } |
OLD | NEW |