summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/device_permissions_prompt.h
blob: e7d11ca901478736a67c8427eed90d70768b8a52 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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_service.h"

namespace content {
class BrowserContext;
class WebContents;
}

namespace device {
class UsbDevice;
class UsbDeviceFilter;
}

namespace extensions {

class Extension;

// Platform-independent interface for displaing a UI for choosing devices
// (similar to choosing files).
class DevicePermissionsPrompt {
 public:
  // Context information available to the UI implementation.
  class Prompt : public base::RefCountedThreadSafe<
                     Prompt,
                     content::BrowserThread::DeleteOnFileThread>,
                 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;
      base::string16 original_manufacturer_string;
      base::string16 original_product_string;
      base::string16 serial_number;
    };

    // 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;
    };

    Prompt();

    // 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(); }
    scoped_refptr<device::UsbDevice> GetDevice(size_t index) const;
    base::string16 GetDeviceName(size_t index) const {
      DCHECK_LT(index, devices_.size());
      return devices_[index].name;
    }
    base::string16 GetDeviceSerialNumber(size_t index) const {
      DCHECK_LT(index, devices_.size());
      return devices_[index].serial_number;
    }

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

    const extensions::Extension* extension() const { return extension_; }
    void set_extension(const extensions::Extension* extension) {
      extension_ = extension;
    }

    void set_browser_context(content::BrowserContext* context) {
      browser_context_ = context;
    }

    bool multiple() const { return multiple_; }
    void set_multiple(bool multiple) { multiple_ = multiple; }

    const std::vector<device::UsbDeviceFilter>& filters() const {
      return filters_;
    }
    void set_filters(const std::vector<device::UsbDeviceFilter>& filters);

   private:
    friend struct content::BrowserThread::DeleteOnThread<
        content::BrowserThread::FILE>;
    friend class base::DeleteHelper<Prompt>;

    virtual ~Prompt();

    // Querying for devices must be done asynchronously on the FILE thread.
    void DoDeviceQuery();
    void AppendCheckedUsbDevice(std::vector<DeviceInfo>* device_info,
                                scoped_refptr<device::UsbDevice> device,
                                const base::Closure& callback,
                                bool allowed);
    void AddCheckedUsbDevice(scoped_refptr<device::UsbDevice> device,
                             bool allowed);
    void DeviceQueryComplete(std::vector<DeviceInfo>* device_info);
    void SetDevices(const std::vector<DeviceInfo>& devices);
    void AddDevice(const DeviceInfo& device);
    void RemoveDevice(scoped_refptr<device::UsbDevice> device);

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

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

  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() {}
  };

  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_; }
  Delegate* delegate() { return delegate_; }
  scoped_refptr<Prompt> prompt() { return prompt_; }

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

  // The delegate called after the UI has been dismissed.
  Delegate* delegate_;

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

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_DEVICE_PERMISSIONS_PROMPT_H_