OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ | 5 #ifndef EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ |
6 #define EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ | 6 #define EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/scoped_observer.h" | 13 #include "base/scoped_observer.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "device/usb/usb_device.h" |
16 #include "device/usb/usb_service.h" | 17 #include "device/usb/usb_service.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 class BrowserContext; | 20 class BrowserContext; |
20 class WebContents; | 21 class WebContents; |
21 } | 22 } |
22 | 23 |
23 namespace device { | 24 namespace device { |
24 class UsbDevice; | |
25 class UsbDeviceFilter; | 25 class UsbDeviceFilter; |
26 } | 26 } |
27 | 27 |
28 namespace extensions { | 28 namespace extensions { |
29 | 29 |
30 class Extension; | 30 class Extension; |
31 | 31 |
32 // Platform-independent interface for displaing a UI for choosing devices | 32 // Platform-independent interface for displaing a UI for choosing devices |
33 // (similar to choosing files). | 33 // (similar to choosing files). |
34 class DevicePermissionsPrompt { | 34 class DevicePermissionsPrompt { |
35 public: | 35 public: |
36 // Context information available to the UI implementation. | 36 // Context information available to the UI implementation. |
37 class Prompt : public base::RefCountedThreadSafe< | 37 class Prompt : public base::RefCounted<Prompt>, |
38 Prompt, | |
39 content::BrowserThread::DeleteOnFileThread>, | |
40 public device::UsbService::Observer { | 38 public device::UsbService::Observer { |
41 public: | 39 public: |
42 // Displayed properties of a device. | 40 // Displayed properties of a device. |
43 struct DeviceInfo { | 41 struct DeviceInfo { |
44 DeviceInfo(scoped_refptr<device::UsbDevice> device); | 42 DeviceInfo(scoped_refptr<device::UsbDevice> device); |
45 ~DeviceInfo(); | 43 ~DeviceInfo(); |
46 | 44 |
47 scoped_refptr<device::UsbDevice> device; | 45 scoped_refptr<device::UsbDevice> device; |
48 base::string16 name; | 46 base::string16 name; |
49 base::string16 original_manufacturer_string; | |
50 base::string16 original_product_string; | |
51 base::string16 serial_number; | |
52 }; | 47 }; |
53 | 48 |
54 // Since the set of devices can change while the UI is visible an | 49 // Since the set of devices can change while the UI is visible an |
55 // implementation should register an observer. | 50 // implementation should register an observer. |
56 class Observer { | 51 class Observer { |
57 public: | 52 public: |
58 virtual void OnDevicesChanged() = 0; | 53 virtual void OnDevicesChanged() = 0; |
59 }; | 54 }; |
60 | 55 |
61 Prompt(); | 56 Prompt(); |
62 | 57 |
63 // Only one observer may be registered at a time. | 58 // Only one observer may be registered at a time. |
64 void SetObserver(Observer* observer); | 59 void SetObserver(Observer* observer); |
65 | 60 |
66 base::string16 GetHeading() const; | 61 base::string16 GetHeading() const; |
67 base::string16 GetPromptMessage() const; | 62 base::string16 GetPromptMessage() const; |
68 size_t GetDeviceCount() const { return devices_.size(); } | 63 size_t GetDeviceCount() const { return devices_.size(); } |
69 scoped_refptr<device::UsbDevice> GetDevice(size_t index) const; | 64 scoped_refptr<device::UsbDevice> GetDevice(size_t index) const; |
70 base::string16 GetDeviceName(size_t index) const { | 65 base::string16 GetDeviceName(size_t index) const { |
71 DCHECK_LT(index, devices_.size()); | 66 DCHECK_LT(index, devices_.size()); |
72 return devices_[index].name; | 67 return devices_[index].name; |
73 } | 68 } |
74 base::string16 GetDeviceSerialNumber(size_t index) const { | 69 base::string16 GetDeviceSerialNumber(size_t index) const { |
75 DCHECK_LT(index, devices_.size()); | 70 DCHECK_LT(index, devices_.size()); |
76 return devices_[index].serial_number; | 71 return devices_[index].device->serial_number(); |
77 } | 72 } |
78 | 73 |
79 // Notifies the DevicePermissionsManager for the current extension that | 74 // Notifies the DevicePermissionsManager for the current extension that |
80 // access to the device at the given index is now granted. | 75 // access to the device at the given index is now granted. |
81 void GrantDevicePermission(size_t index) const; | 76 void GrantDevicePermission(size_t index) const; |
82 | 77 |
83 const extensions::Extension* extension() const { return extension_; } | 78 const extensions::Extension* extension() const { return extension_; } |
84 void set_extension(const extensions::Extension* extension) { | 79 void set_extension(const extensions::Extension* extension) { |
85 extension_ = extension; | 80 extension_ = extension; |
86 } | 81 } |
87 | 82 |
88 void set_browser_context(content::BrowserContext* context) { | 83 void set_browser_context(content::BrowserContext* context) { |
89 browser_context_ = context; | 84 browser_context_ = context; |
90 } | 85 } |
91 | 86 |
92 bool multiple() const { return multiple_; } | 87 bool multiple() const { return multiple_; } |
93 void set_multiple(bool multiple) { multiple_ = multiple; } | 88 void set_multiple(bool multiple) { multiple_ = multiple; } |
94 | 89 |
95 const std::vector<device::UsbDeviceFilter>& filters() const { | 90 const std::vector<device::UsbDeviceFilter>& filters() const { |
96 return filters_; | 91 return filters_; |
97 } | 92 } |
98 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); | 93 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); |
99 | 94 |
100 private: | 95 private: |
101 friend struct content::BrowserThread::DeleteOnThread< | 96 friend class base::RefCounted<Prompt>; |
102 content::BrowserThread::FILE>; | |
103 friend class base::DeleteHelper<Prompt>; | |
104 | 97 |
105 virtual ~Prompt(); | 98 virtual ~Prompt(); |
106 | 99 |
107 // Querying for devices must be done asynchronously on the FILE thread. | |
108 void DoDeviceQuery(); | |
109 void AppendCheckedUsbDevice(std::vector<DeviceInfo>* device_info, | |
110 scoped_refptr<device::UsbDevice> device, | |
111 const base::Closure& callback, | |
112 bool allowed); | |
113 void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device, | |
114 bool allowed); | |
115 void DeviceQueryComplete(std::vector<DeviceInfo>* device_info); | |
116 void SetDevices(const std::vector<DeviceInfo>& devices); | |
117 void AddDevice(const DeviceInfo& device); | |
118 void RemoveDevice(scoped_refptr<device::UsbDevice> device); | |
119 | |
120 // device::UsbService::Observer implementation: | 100 // device::UsbService::Observer implementation: |
121 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; | 101 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; |
122 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; | 102 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; |
123 | 103 |
124 const extensions::Extension* extension_; | 104 void OnDevicesEnumerated( |
125 content::BrowserContext* browser_context_; | 105 const std::vector<scoped_refptr<device::UsbDevice>>& devices); |
126 bool multiple_; | 106 void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device, |
| 107 bool allowed); |
| 108 |
| 109 const extensions::Extension* extension_ = nullptr; |
| 110 content::BrowserContext* browser_context_ = nullptr; |
| 111 bool multiple_ = false; |
127 std::vector<device::UsbDeviceFilter> filters_; | 112 std::vector<device::UsbDeviceFilter> filters_; |
128 std::vector<DeviceInfo> devices_; | 113 std::vector<DeviceInfo> devices_; |
129 Observer* observer_; | 114 Observer* observer_ = nullptr; |
130 ScopedObserver<device::UsbService, device::UsbService::Observer> | 115 ScopedObserver<device::UsbService, device::UsbService::Observer> |
131 usb_service_observer_; | 116 usb_service_observer_; |
132 }; | 117 }; |
133 | 118 |
134 class Delegate { | 119 class Delegate { |
135 public: | 120 public: |
136 // Called with the list of selected USB devices. | 121 // Called with the list of selected USB devices. |
137 virtual void OnUsbDevicesChosen( | 122 virtual void OnUsbDevicesChosen( |
138 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0; | 123 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0; |
139 | 124 |
(...skipping 24 matching lines...) Expand all Loading... |
164 // The delegate called after the UI has been dismissed. | 149 // The delegate called after the UI has been dismissed. |
165 Delegate* delegate_; | 150 Delegate* delegate_; |
166 | 151 |
167 // Parameters available to the UI implementation. | 152 // Parameters available to the UI implementation. |
168 scoped_refptr<Prompt> prompt_; | 153 scoped_refptr<Prompt> prompt_; |
169 }; | 154 }; |
170 | 155 |
171 } // namespace extensions | 156 } // namespace extensions |
172 | 157 |
173 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ | 158 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ |
OLD | NEW |