Chromium Code Reviews| 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 // Displayed properties of a device. |
| 51 struct DeviceInfo { | 41 struct DeviceInfo { |
|
Jeffrey Yasskin
2015/04/23 20:40:07
Describe how this is extended. Probably also mark
Reilly Grant (use Gerrit)
2015/04/23 23:02:47
Done.
| |
| 52 DeviceInfo(scoped_refptr<device::UsbDevice> device); | 42 DeviceInfo(); |
| 53 ~DeviceInfo(); | 43 virtual ~DeviceInfo(); |
| 54 | 44 |
| 55 scoped_refptr<device::UsbDevice> device; | |
| 56 base::string16 name; | 45 base::string16 name; |
| 46 base::string16 serial_number; | |
| 57 bool granted = false; | 47 bool granted = false; |
| 58 }; | 48 }; |
| 59 | 49 |
| 60 // Since the set of devices can change while the UI is visible an | 50 // Since the set of devices can change while the UI is visible an |
| 61 // implementation should register an observer. | 51 // implementation should register an observer. |
| 62 class Observer { | 52 class Observer { |
| 63 public: | 53 public: |
| 64 virtual void OnDevicesChanged() = 0; | 54 virtual void OnDevicesChanged() = 0; |
| 65 | 55 |
| 66 protected: | 56 protected: |
| 67 virtual ~Observer(); | 57 virtual ~Observer(); |
| 68 }; | 58 }; |
| 69 | 59 |
| 70 Prompt(Delegate* delegate, | 60 Prompt(const Extension* extension, |
| 71 const Extension* extension, | 61 content::BrowserContext* context, |
| 72 content::BrowserContext* context); | 62 bool multiple); |
|
Jeffrey Yasskin
2015/04/23 20:40:07
Comment what "multiple" means.
Reilly Grant (use Gerrit)
2015/04/23 23:02:47
Done.
| |
| 73 | 63 |
| 74 // Only one observer may be registered at a time. | 64 // Only one observer may be registered at a time. |
| 75 void SetObserver(Observer* observer); | 65 virtual void SetObserver(Observer* observer); |
| 76 | 66 |
| 77 base::string16 GetHeading() const; | 67 virtual base::string16 GetHeading() const = 0; |
| 78 base::string16 GetPromptMessage() const; | 68 base::string16 GetPromptMessage() const; |
| 79 size_t GetDeviceCount() const { return devices_.size(); } | 69 size_t GetDeviceCount() const { return devices_.size(); } |
| 80 base::string16 GetDeviceName(size_t index) const; | 70 base::string16 GetDeviceName(size_t index) const; |
| 81 base::string16 GetDeviceSerialNumber(size_t index) const; | 71 base::string16 GetDeviceSerialNumber(size_t index) const; |
| 82 | 72 |
| 83 // Notifies the DevicePermissionsManager for the current extension that | 73 // Notifies the DevicePermissionsManager for the current extension that |
| 84 // access to the device at the given index is now granted. | 74 // access to the device at the given index is now granted. |
| 85 void GrantDevicePermission(size_t index); | 75 void GrantDevicePermission(size_t index); |
| 86 void Dismissed(); | 76 |
| 77 virtual void Dismissed() = 0; | |
| 87 | 78 |
| 88 bool multiple() const { return multiple_; } | 79 bool multiple() const { return multiple_; } |
| 89 | 80 |
| 90 void set_multiple(bool multiple) { multiple_ = multiple; } | 81 protected: |
| 91 void set_filters(const std::vector<device::UsbDeviceFilter>& filters); | 82 virtual ~Prompt(); |
| 83 | |
| 84 const Extension* extension() const { return extension_; } | |
| 85 Observer* observer() const { return observer_; } | |
| 86 content::BrowserContext* browser_context() const { | |
| 87 return browser_context_; | |
| 88 } | |
| 89 | |
| 90 ScopedVector<DeviceInfo> devices_; | |
|
Jeffrey Yasskin
2015/04/23 20:40:07
Please comment the rules on the type of the object
Reilly Grant (use Gerrit)
2015/04/23 23:02:47
Done.
| |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 friend class base::RefCounted<Prompt>; | 93 friend class base::RefCounted<Prompt>; |
| 95 | 94 |
| 96 virtual ~Prompt(); | 95 const extensions::Extension* extension_ = nullptr; |
| 96 Observer* observer_ = nullptr; | |
| 97 content::BrowserContext* browser_context_ = nullptr; | |
| 98 bool multiple_ = false; | |
| 97 | 99 |
| 98 // device::UsbService::Observer implementation: | 100 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 }; | 101 }; |
| 117 | 102 |
| 118 DevicePermissionsPrompt(content::WebContents* web_contents); | 103 DevicePermissionsPrompt(content::WebContents* web_contents); |
| 119 virtual ~DevicePermissionsPrompt(); | 104 virtual ~DevicePermissionsPrompt(); |
| 120 | 105 |
| 121 void AskForUsbDevices(Delegate* delegate, | 106 void AskForUsbDevices(const Extension* extension, |
| 122 const Extension* extension, | |
| 123 content::BrowserContext* context, | 107 content::BrowserContext* context, |
| 124 bool multiple, | 108 bool multiple, |
| 125 const std::vector<device::UsbDeviceFilter>& filters); | 109 const std::vector<device::UsbDeviceFilter>& filters, |
| 110 const UsbDevicesCallback& callback); | |
| 126 | 111 |
| 127 protected: | 112 protected: |
| 128 virtual void ShowDialog() = 0; | 113 virtual void ShowDialog() = 0; |
| 129 | 114 |
| 130 content::WebContents* web_contents() { return web_contents_; } | 115 content::WebContents* web_contents() { return web_contents_; } |
| 131 scoped_refptr<Prompt> prompt() { return prompt_; } | 116 scoped_refptr<Prompt> prompt() { return prompt_; } |
| 132 | 117 |
| 133 private: | 118 private: |
| 134 // Parent web contents of the device permissions UI dialog. | 119 // Parent web contents of the device permissions UI dialog. |
| 135 content::WebContents* web_contents_; | 120 content::WebContents* web_contents_; |
| 136 | 121 |
| 137 // Parameters available to the UI implementation. | 122 // Parameters available to the UI implementation. |
| 138 scoped_refptr<Prompt> prompt_; | 123 scoped_refptr<Prompt> prompt_; |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(DevicePermissionsPrompt); | |
| 139 }; | 126 }; |
| 140 | 127 |
| 141 } // namespace extensions | 128 } // namespace extensions |
| 142 | 129 |
| 143 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ | 130 #endif // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_ |
| OLD | NEW |