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/memory/scoped_vector.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "device/usb/usb_device.h" | |
17 #include "device/usb/usb_service.h" | |
18 | 15 |
19 namespace content { | 16 namespace content { |
20 class BrowserContext; | 17 class BrowserContext; |
21 class WebContents; | 18 class WebContents; |
22 } | 19 } |
23 | 20 |
24 namespace device { | 21 namespace device { |
| 22 class UsbDevice; |
25 class UsbDeviceFilter; | 23 class UsbDeviceFilter; |
26 } | 24 } |
27 | 25 |
28 namespace extensions { | 26 namespace extensions { |
29 | 27 |
30 class Extension; | 28 class Extension; |
31 | 29 |
32 // Platform-independent interface for displaing a UI for choosing devices | 30 // Platform-independent interface for displaing a UI for choosing devices |
33 // (similar to choosing files). | 31 // (similar to choosing files). |
34 class DevicePermissionsPrompt { | 32 class DevicePermissionsPrompt { |
35 public: | 33 public: |
36 class Delegate { | 34 using UsbDevicesCallback = base::Callback<void( |
37 public: | 35 const std::vector<scoped_refptr<device::UsbDevice>>&)>; |
38 // Called with the list of selected USB devices. | |
39 virtual void OnUsbDevicesChosen( | |
40 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0; | |
41 | |
42 protected: | |
43 virtual ~Delegate(); | |
44 }; | |
45 | 36 |
46 // Context information available to the UI implementation. | 37 // Context information available to the UI implementation. |
47 class Prompt : public base::RefCounted<Prompt>, | 38 class Prompt : public base::RefCounted<Prompt> { |
48 public device::UsbService::Observer { | |
49 public: | 39 public: |
50 // Displayed properties of a device. | 40 // This class stores the device information displayed in the UI. It should |
51 struct DeviceInfo { | 41 // be extended to support particular device types. |
52 DeviceInfo(scoped_refptr<device::UsbDevice> device); | 42 class DeviceInfo { |
53 ~DeviceInfo(); | 43 public: |
| 44 DeviceInfo(); |
| 45 virtual ~DeviceInfo(); |
54 | 46 |
55 scoped_refptr<device::UsbDevice> device; | 47 const base::string16& name() const { return name_; } |
56 base::string16 name; | 48 const base::string16& serial_number() const { return serial_number_; } |
57 bool granted = false; | 49 bool granted() const { return granted_; } |
| 50 void set_granted() { granted_ = true; } |
| 51 |
| 52 protected: |
| 53 base::string16 name_; |
| 54 base::string16 serial_number_; |
| 55 |
| 56 private: |
| 57 bool granted_ = false; |
58 }; | 58 }; |
59 | 59 |
60 // Since the set of devices can change while the UI is visible an | 60 // Since the set of devices can change while the UI is visible an |
61 // implementation should register an observer. | 61 // implementation should register an observer. |
62 class Observer { | 62 class Observer { |
63 public: | 63 public: |
64 virtual void OnDevicesChanged() = 0; | 64 virtual void OnDevicesChanged() = 0; |
65 | 65 |
66 protected: | 66 protected: |
67 virtual ~Observer(); | 67 virtual ~Observer(); |
68 }; | 68 }; |
69 | 69 |
70 Prompt(Delegate* delegate, | 70 Prompt(const Extension* extension, |
71 const Extension* extension, | 71 content::BrowserContext* context, |
72 content::BrowserContext* context); | 72 bool multiple); |
73 | 73 |
74 // Only one observer may be registered at a time. | 74 // Only one observer may be registered at a time. |
75 void SetObserver(Observer* observer); | 75 virtual void SetObserver(Observer* observer); |
76 | 76 |
77 base::string16 GetHeading() const; | 77 virtual base::string16 GetHeading() const = 0; |
78 base::string16 GetPromptMessage() const; | 78 base::string16 GetPromptMessage() const; |
79 size_t GetDeviceCount() const { return devices_.size(); } | 79 size_t GetDeviceCount() const { return devices_.size(); } |
80 base::string16 GetDeviceName(size_t index) const; | 80 base::string16 GetDeviceName(size_t index) const; |
81 base::string16 GetDeviceSerialNumber(size_t index) const; | 81 base::string16 GetDeviceSerialNumber(size_t index) const; |
82 | 82 |
83 // Notifies the DevicePermissionsManager for the current extension that | 83 // Notifies the DevicePermissionsManager for the current extension that |
84 // access to the device at the given index is now granted. | 84 // access to the device at the given index is now granted. |
85 void GrantDevicePermission(size_t index); | 85 void GrantDevicePermission(size_t index); |
86 void Dismissed(); | |
87 | 86 |
| 87 virtual void Dismissed() = 0; |
| 88 |
| 89 // Allow the user to select multiple devices. |
88 bool multiple() const { return multiple_; } | 90 bool multiple() const { return multiple_; } |
89 | 91 |
90 void set_multiple(bool multiple) { multiple_ = multiple; } | 92 protected: |
91 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); | 93 virtual ~Prompt(); |
| 94 |
| 95 const Extension* extension() const { return extension_; } |
| 96 Observer* observer() const { return observer_; } |
| 97 content::BrowserContext* browser_context() const { |
| 98 return browser_context_; |
| 99 } |
| 100 |
| 101 // Subclasses may fill this with a particular subclass of DeviceInfo and may |
| 102 // assume that only that instances of that type are stored here. |
| 103 ScopedVector<DeviceInfo> devices_; |
92 | 104 |
93 private: | 105 private: |
94 friend class base::RefCounted<Prompt>; | 106 friend class base::RefCounted<Prompt>; |
95 | 107 |
96 virtual ~Prompt(); | 108 const extensions::Extension* extension_ = nullptr; |
| 109 Observer* observer_ = nullptr; |
| 110 content::BrowserContext* browser_context_ = nullptr; |
| 111 bool multiple_ = false; |
97 | 112 |
98 // device::UsbService::Observer implementation: | 113 DISALLOW_COPY_AND_ASSIGN(Prompt); |
99 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; | |
100 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; | |
101 | |
102 void OnDevicesEnumerated( | |
103 const std::vector<scoped_refptr<device::UsbDevice>>& devices); | |
104 void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device, | |
105 bool allowed); | |
106 | |
107 const extensions::Extension* extension_ = nullptr; | |
108 content::BrowserContext* browser_context_ = nullptr; | |
109 Delegate* delegate_ = nullptr; | |
110 bool multiple_ = false; | |
111 std::vector<device::UsbDeviceFilter> filters_; | |
112 std::vector<DeviceInfo> devices_; | |
113 Observer* observer_ = nullptr; | |
114 ScopedObserver<device::UsbService, device::UsbService::Observer> | |
115 usb_service_observer_; | |
116 }; | 114 }; |
117 | 115 |
118 DevicePermissionsPrompt(content::WebContents* web_contents); | 116 DevicePermissionsPrompt(content::WebContents* web_contents); |
119 virtual ~DevicePermissionsPrompt(); | 117 virtual ~DevicePermissionsPrompt(); |
120 | 118 |
121 void AskForUsbDevices(Delegate* delegate, | 119 void AskForUsbDevices(const Extension* extension, |
122 const Extension* extension, | |
123 content::BrowserContext* context, | 120 content::BrowserContext* context, |
124 bool multiple, | 121 bool multiple, |
125 const std::vector<device::UsbDeviceFilter>& filters); | 122 const std::vector<device::UsbDeviceFilter>& filters, |
| 123 const UsbDevicesCallback& callback); |
126 | 124 |
127 protected: | 125 protected: |
128 virtual void ShowDialog() = 0; | 126 virtual void ShowDialog() = 0; |
129 | 127 |
130 content::WebContents* web_contents() { return web_contents_; } | 128 content::WebContents* web_contents() { return web_contents_; } |
131 scoped_refptr<Prompt> prompt() { return prompt_; } | 129 scoped_refptr<Prompt> prompt() { return prompt_; } |
132 | 130 |
133 private: | 131 private: |
134 // Parent web contents of the device permissions UI dialog. | 132 // Parent web contents of the device permissions UI dialog. |
135 content::WebContents* web_contents_; | 133 content::WebContents* web_contents_; |
136 | 134 |
137 // Parameters available to the UI implementation. | 135 // Parameters available to the UI implementation. |
138 scoped_refptr<Prompt> prompt_; | 136 scoped_refptr<Prompt> prompt_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsPrompt); |
139 }; | 139 }; |
140 | 140 |
141 } // namespace extensions | 141 } // namespace extensions |
142 | 142 |
143 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ | 143 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ |
OLD | NEW |