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