OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ |
| 6 #define EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/strings/string16.h" |
| 14 |
| 15 namespace content { |
| 16 class BrowserContext; |
| 17 class WebContents; |
| 18 } |
| 19 |
| 20 namespace device { |
| 21 class UsbDevice; |
| 22 class UsbDeviceFilter; |
| 23 } |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 class Extension; |
| 28 |
| 29 // Platform-independent interface for displaing a UI for choosing devices |
| 30 // (similar to choosing files). |
| 31 class DevicePermissionsPrompt { |
| 32 public: |
| 33 // Context information available to the UI implementation. |
| 34 class Prompt : public base::RefCountedThreadSafe<Prompt> { |
| 35 public: |
| 36 // Displayed properties of a device. |
| 37 struct DeviceInfo { |
| 38 DeviceInfo(scoped_refptr<device::UsbDevice> device, |
| 39 const base::string16& name, |
| 40 const base::string16& serial_number, |
| 41 const base::string16& tooltip); |
| 42 ~DeviceInfo(); |
| 43 |
| 44 scoped_refptr<device::UsbDevice> device; |
| 45 base::string16 name; |
| 46 base::string16 serial_number; |
| 47 base::string16 tooltip; |
| 48 }; |
| 49 |
| 50 // Since the set of devices can change while the UI is visible an |
| 51 // implementation should register an observer. |
| 52 class Observer { |
| 53 public: |
| 54 virtual void OnDevicesChanged() = 0; |
| 55 }; |
| 56 |
| 57 Prompt(); |
| 58 |
| 59 // Only one observer may be registered at a time. |
| 60 void SetObserver(Observer* observer); |
| 61 |
| 62 base::string16 GetHeading() const; |
| 63 base::string16 GetPromptMessage() const; |
| 64 size_t GetDeviceCount() const { return devices_.size(); } |
| 65 scoped_refptr<device::UsbDevice> GetDevice(size_t index) const; |
| 66 base::string16 GetDeviceName(size_t index) const { |
| 67 DCHECK_LT(index, devices_.size()); |
| 68 return devices_[index].name; |
| 69 } |
| 70 base::string16 GetDeviceSerialNumber(size_t index) const { |
| 71 DCHECK_LT(index, devices_.size()); |
| 72 return devices_[index].serial_number; |
| 73 } |
| 74 base::string16 GetDeviceTooltip(size_t index) const { |
| 75 DCHECK_LT(index, devices_.size()); |
| 76 return devices_[index].tooltip; |
| 77 } |
| 78 |
| 79 // Notifies the DevicePermissionsManager for the current extension that |
| 80 // access to the device at the given index is now granted. |
| 81 void GrantDevicePermission(size_t index) const; |
| 82 |
| 83 const extensions::Extension* extension() const { return extension_; } |
| 84 void set_extension(const extensions::Extension* extension) { |
| 85 extension_ = extension; |
| 86 } |
| 87 |
| 88 void set_browser_context(content::BrowserContext* context) { |
| 89 browser_context_ = context; |
| 90 } |
| 91 |
| 92 bool multiple() const { return multiple_; } |
| 93 void set_multiple(bool multiple) { multiple_ = multiple; } |
| 94 |
| 95 const std::vector<device::UsbDeviceFilter>& filters() const { |
| 96 return filters_; |
| 97 } |
| 98 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); |
| 99 |
| 100 private: |
| 101 friend class base::RefCountedThreadSafe<Prompt>; |
| 102 |
| 103 virtual ~Prompt(); |
| 104 |
| 105 // Querying for devices must be done asynchronously on the FILE thread. |
| 106 void DoDeviceQuery(); |
| 107 void SetDevices(const std::vector<DeviceInfo>& devices); |
| 108 |
| 109 const extensions::Extension* extension_; |
| 110 content::BrowserContext* browser_context_; |
| 111 bool multiple_; |
| 112 std::vector<device::UsbDeviceFilter> filters_; |
| 113 std::vector<DeviceInfo> devices_; |
| 114 Observer* observer_; |
| 115 }; |
| 116 |
| 117 class Delegate { |
| 118 public: |
| 119 // Called with the list of selected USB devices. |
| 120 virtual void OnUsbDevicesChosen( |
| 121 const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0; |
| 122 |
| 123 protected: |
| 124 virtual ~Delegate() {} |
| 125 }; |
| 126 |
| 127 typedef base::Callback<void(content::WebContents*, |
| 128 DevicePermissionsPrompt::Delegate*, |
| 129 scoped_refptr<DevicePermissionsPrompt::Prompt>)> |
| 130 ShowDialogCallback; |
| 131 |
| 132 static ShowDialogCallback GetDefaultShowDialogCallback(); |
| 133 |
| 134 DevicePermissionsPrompt(content::WebContents* web_contents); |
| 135 virtual ~DevicePermissionsPrompt(); |
| 136 |
| 137 virtual void AskForUsbDevices( |
| 138 Delegate* delegate, |
| 139 const Extension* extension, |
| 140 content::BrowserContext* context, |
| 141 bool multiple, |
| 142 const std::vector<device::UsbDeviceFilter>& filters); |
| 143 |
| 144 private: |
| 145 // Parent web contents of the device permissions UI dialog. |
| 146 content::WebContents* web_contents_; |
| 147 |
| 148 // The delegate called after the UI has been dismissed. |
| 149 Delegate* delegate_; |
| 150 |
| 151 // Parameters available to the UI implementation. |
| 152 scoped_refptr<Prompt> prompt_; |
| 153 }; |
| 154 |
| 155 } // namespace extensions |
| 156 |
| 157 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ |
OLD | NEW |