summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/device_permissions_prompt.h
blob: 5e93155bf47a7d89c4bd5ba1ddebcfe81ba113cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_
#define EXTENSIONS_BROWSER_DEVICE_PERMISSIONS_PROMPT_H_

#include <vector>

#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "content/public/browser/browser_thread.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_service.h"

namespace content {
class BrowserContext;
class WebContents;
}

namespace device {
class UsbDeviceFilter;
}

namespace extensions {

class Extension;

// Platform-independent interface for displaing a UI for choosing devices
// (similar to choosing files).
class DevicePermissionsPrompt {
 public:
  class Delegate {
   public:
    // Called with the list of selected USB devices.
    virtual void OnUsbDevicesChosen(
        const std::vector<scoped_refptr<device::UsbDevice>>& devices) = 0;

   protected:
    virtual ~Delegate();
  };

  // Context information available to the UI implementation.
  class Prompt : public base::RefCounted<Prompt>,
                 public device::UsbService::Observer {
   public:
    // Displayed properties of a device.
    struct DeviceInfo {
      DeviceInfo(scoped_refptr<device::UsbDevice> device);
      ~DeviceInfo();

      scoped_refptr<device::UsbDevice> device;
      base::string16 name;
      bool granted = false;
    };

    // Since the set of devices can change while the UI is visible an
    // implementation should register an observer.
    class Observer {
     public:
      virtual void OnDevicesChanged() = 0;

     protected:
      virtual ~Observer();
    };

    Prompt(Delegate* delegate,
           const Extension* extension,
           content::BrowserContext* context);

    // Only one observer may be registered at a time.
    void SetObserver(Observer* observer);

    base::string16 GetHeading() const;
    base::string16 GetPromptMessage() const;
    size_t GetDeviceCount() const { return devices_.size(); }
    base::string16 GetDeviceName(size_t index) const;
    base::string16 GetDeviceSerialNumber(size_t index) const;

    // Notifies the DevicePermissionsManager for the current extension that
    // access to the device at the given index is now granted.
    void GrantDevicePermission(size_t index);
    void Dismissed();

    bool multiple() const { return multiple_; }

    void set_multiple(bool multiple) { multiple_ = multiple; }
    void set_filters(const std::vector<device::UsbDeviceFilter>& filters);

   private:
    friend class base::RefCounted<Prompt>;

    virtual ~Prompt();

    // device::UsbService::Observer implementation:
    void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override;
    void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override;

    void OnDevicesEnumerated(
        const std::vector<scoped_refptr<device::UsbDevice>>& devices);
    void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device,
                             bool allowed);

    const extensions::Extension* extension_ = nullptr;
    content::BrowserContext* browser_context_ = nullptr;
    Delegate* delegate_ = nullptr;
    bool multiple_ = false;
    std::vector<device::UsbDeviceFilter> filters_;
    std::vector<DeviceInfo> devices_;
    Observer* observer_ = nullptr;
    ScopedObserver<device::UsbService, device::UsbService::Observer>
        usb_service_observer_;
  };

  DevicePermissionsPrompt(content::WebContents* web_contents);
  virtual ~DevicePermissionsPrompt();

  void AskForUsbDevices(Delegate* delegate,
                        const Extension* extension,
                        content::BrowserContext* context,
                        bool multiple,
                        const std::vector<device::UsbDeviceFilter>& filters);

 protected:
  virtual void ShowDialog() = 0;

  content::WebContents* web_contents() { return web_contents_; }
  scoped_refptr<Prompt> prompt() { return prompt_; }

 private:
  // Parent web contents of the device permissions UI dialog.
  content::WebContents* web_contents_;

  // Parameters available to the UI implementation.
  scoped_refptr<Prompt> prompt_;
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_