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" |
(...skipping 15 matching lines...) Expand all Loading... | |
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 class Delegate { | |
37 public: | |
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 // Context information available to the UI implementation. | 46 // Context information available to the UI implementation. |
37 class Prompt : public base::RefCounted<Prompt>, | 47 class Prompt : public base::RefCounted<Prompt>, |
38 public device::UsbService::Observer { | 48 public device::UsbService::Observer { |
39 public: | 49 public: |
40 // Displayed properties of a device. | 50 // Displayed properties of a device. |
41 struct DeviceInfo { | 51 struct DeviceInfo { |
42 DeviceInfo(scoped_refptr<device::UsbDevice> device); | 52 DeviceInfo(scoped_refptr<device::UsbDevice> device); |
43 ~DeviceInfo(); | 53 ~DeviceInfo(); |
44 | 54 |
45 scoped_refptr<device::UsbDevice> device; | 55 scoped_refptr<device::UsbDevice> device; |
46 base::string16 name; | 56 base::string16 name; |
57 bool granted = false; | |
47 }; | 58 }; |
48 | 59 |
49 // 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 |
50 // implementation should register an observer. | 61 // implementation should register an observer. |
51 class Observer { | 62 class Observer { |
52 public: | 63 public: |
53 virtual void OnDevicesChanged() = 0; | 64 virtual void OnDevicesChanged() = 0; |
65 | |
66 protected: | |
67 virtual ~Observer(); | |
54 }; | 68 }; |
55 | 69 |
56 Prompt(); | 70 Prompt(Delegate* delegate, |
71 const Extension* extension, | |
72 content::BrowserContext* context); | |
57 | 73 |
58 // Only one observer may be registered at a time. | 74 // Only one observer may be registered at a time. |
59 void SetObserver(Observer* observer); | 75 void SetObserver(Observer* observer); |
60 | 76 |
61 base::string16 GetHeading() const; | 77 base::string16 GetHeading() const; |
62 base::string16 GetPromptMessage() const; | 78 base::string16 GetPromptMessage() const; |
63 size_t GetDeviceCount() const { return devices_.size(); } | 79 size_t GetDeviceCount() const { return devices_.size(); } |
64 scoped_refptr<device::UsbDevice> GetDevice(size_t index) const; | 80 base::string16 GetDeviceName(size_t index) const; |
65 base::string16 GetDeviceName(size_t index) const { | 81 base::string16 GetDeviceSerialNumber(size_t index) const; |
66 DCHECK_LT(index, devices_.size()); | |
67 return devices_[index].name; | |
68 } | |
69 base::string16 GetDeviceSerialNumber(size_t index) const { | |
70 DCHECK_LT(index, devices_.size()); | |
71 return devices_[index].device->serial_number(); | |
72 } | |
73 | 82 |
74 // Notifies the DevicePermissionsManager for the current extension that | 83 // Notifies the DevicePermissionsManager for the current extension that |
75 // access to the device at the given index is now granted. | 84 // access to the device at the given index is now granted. |
76 void GrantDevicePermission(size_t index) const; | 85 void GrantDevicePermission(size_t index); |
77 | 86 void Dismissed(); |
78 const extensions::Extension* extension() const { return extension_; } | |
79 void set_extension(const extensions::Extension* extension) { | |
80 extension_ = extension; | |
81 } | |
82 | |
83 void set_browser_context(content::BrowserContext* context) { | |
84 browser_context_ = context; | |
85 } | |
86 | 87 |
87 bool multiple() const { return multiple_; } | 88 bool multiple() const { return multiple_; } |
89 | |
88 void set_multiple(bool multiple) { multiple_ = multiple; } | 90 void set_multiple(bool multiple) { multiple_ = multiple; } |
89 | |
90 const std::vector<device::UsbDeviceFilter>& filters() const { | |
91 return filters_; | |
92 } | |
93 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); | 91 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); |
94 | 92 |
95 private: | 93 private: |
96 friend class base::RefCounted<Prompt>; | 94 friend class base::RefCounted<Prompt>; |
97 | 95 |
98 virtual ~Prompt(); | 96 virtual ~Prompt(); |
99 | 97 |
100 // device::UsbService::Observer implementation: | 98 // device::UsbService::Observer implementation: |
101 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; | 99 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; |
102 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; | 100 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; |
103 | 101 |
104 void OnDevicesEnumerated( | 102 void OnDevicesEnumerated( |
105 const std::vector<scoped_refptr<device::UsbDevice>>& devices); | 103 const std::vector<scoped_refptr<device::UsbDevice>>& devices); |
106 void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device, | 104 void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device, |
107 bool allowed); | 105 bool allowed); |
108 | 106 |
109 const extensions::Extension* extension_ = nullptr; | 107 const extensions::Extension* extension_ = nullptr; |
110 content::BrowserContext* browser_context_ = nullptr; | 108 content::BrowserContext* browser_context_ = nullptr; |
109 Delegate* delegate_; | |
Devlin
2015/04/17 19:39:25
init to null?
Reilly Grant (use Gerrit)
2015/04/17 20:05:03
Done.
| |
111 bool multiple_ = false; | 110 bool multiple_ = false; |
112 std::vector<device::UsbDeviceFilter> filters_; | 111 std::vector<device::UsbDeviceFilter> filters_; |
113 std::vector<DeviceInfo> devices_; | 112 std::vector<DeviceInfo> devices_; |
114 Observer* observer_ = nullptr; | 113 Observer* observer_ = nullptr; |
115 ScopedObserver<device::UsbService, device::UsbService::Observer> | 114 ScopedObserver<device::UsbService, device::UsbService::Observer> |
116 usb_service_observer_; | 115 usb_service_observer_; |
117 }; | 116 }; |
118 | 117 |
119 class Delegate { | |
120 public: | |
121 // Called with the list of selected USB devices. | |
122 virtual void OnUsbDevicesChosen( | |
123 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0; | |
124 | |
125 protected: | |
126 virtual ~Delegate() {} | |
127 }; | |
128 | |
129 DevicePermissionsPrompt(content::WebContents* web_contents); | 118 DevicePermissionsPrompt(content::WebContents* web_contents); |
130 virtual ~DevicePermissionsPrompt(); | 119 virtual ~DevicePermissionsPrompt(); |
131 | 120 |
132 void AskForUsbDevices(Delegate* delegate, | 121 void AskForUsbDevices(Delegate* delegate, |
133 const Extension* extension, | 122 const Extension* extension, |
134 content::BrowserContext* context, | 123 content::BrowserContext* context, |
135 bool multiple, | 124 bool multiple, |
136 const std::vector<device::UsbDeviceFilter>& filters); | 125 const std::vector<device::UsbDeviceFilter>& filters); |
137 | 126 |
138 protected: | 127 protected: |
139 virtual void ShowDialog() = 0; | 128 virtual void ShowDialog() = 0; |
140 | 129 |
141 content::WebContents* web_contents() { return web_contents_; } | 130 content::WebContents* web_contents() { return web_contents_; } |
142 Delegate* delegate() { return delegate_; } | |
143 scoped_refptr<Prompt> prompt() { return prompt_; } | 131 scoped_refptr<Prompt> prompt() { return prompt_; } |
144 | 132 |
145 private: | 133 private: |
146 // Parent web contents of the device permissions UI dialog. | 134 // Parent web contents of the device permissions UI dialog. |
147 content::WebContents* web_contents_; | 135 content::WebContents* web_contents_; |
148 | 136 |
149 // The delegate called after the UI has been dismissed. | |
150 Delegate* delegate_; | |
151 | |
152 // Parameters available to the UI implementation. | 137 // Parameters available to the UI implementation. |
153 scoped_refptr<Prompt> prompt_; | 138 scoped_refptr<Prompt> prompt_; |
154 }; | 139 }; |
155 | 140 |
156 } // namespace extensions | 141 } // namespace extensions |
157 | 142 |
158 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ | 143 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ |
OLD | NEW |