1 // Copyright 2015 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.
5 #ifndef EXTENSIONS_BROWSER_API_USB_USB_GUID_MAP_H_
6 #define EXTENSIONS_BROWSER_API_USB_USB_GUID_MAP_H_
11 #include "base/scoped_observer.h"
12 #include "device/usb/usb_service.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "extensions/common/api/usb.h"
20 namespace extensions
{
22 // A BrowserContext-scoped object which maps USB device GUIDs to legacy integer
23 // IDs for use with the extensions API. This observes device removal to keep
24 // the mapping from growing indefinitely.
25 class UsbGuidMap
: public BrowserContextKeyedAPI
,
26 public device::UsbService::Observer
{
28 static UsbGuidMap
* Get(content::BrowserContext
* browser_context
);
30 // BrowserContextKeyedAPI implementation.
31 static BrowserContextKeyedAPIFactory
<UsbGuidMap
>* GetFactoryInstance();
33 // Returns an ID for this device GUID. If the GUID is unknown to the
34 // UsbGuidMap a new ID is generated for it.
35 int GetIdFromGuid(const std::string
& guid
);
37 // Looks up a device GUID for a given extensions USB device ID. If the ID is
38 // unknown (e.g., the corresponding device was unplugged), this returns
39 // |false|; otherwise it returns |true|.
40 bool GetGuidFromId(int id
, std::string
* guid
);
42 // Populates an instance of the chrome.usb.Device object from the given
44 void GetApiDevice(scoped_refptr
<const device::UsbDevice
> device_in
,
45 extensions::api::usb::Device
* device_out
);
48 friend class BrowserContextKeyedAPIFactory
<UsbGuidMap
>;
50 explicit UsbGuidMap(content::BrowserContext
* context
);
51 ~UsbGuidMap() override
;
53 // BrowserContextKeyedAPI implementation.
54 static const char* service_name() { return "UsbGuidMap"; }
55 static const bool kServiceIsCreatedWithBrowserContext
= false;
56 static const bool kServiceRedirectedInIncognito
= true;
58 // UsbService::Observer implementation.
59 void OnDeviceRemovedCleanup(scoped_refptr
<device::UsbDevice
> device
) override
;
61 content::BrowserContext
* const browser_context_
;
64 std::map
<std::string
, int> guid_to_id_map_
;
65 std::map
<int, std::string
> id_to_guid_map_
;
67 ScopedObserver
<device::UsbService
, device::UsbService::Observer
> observer_
;
69 DISALLOW_COPY_AND_ASSIGN(UsbGuidMap
);
72 } // namespace extensions
74 #endif // EXTENSIONS_BROWSER_API_USB_USB_GUID_MAP_H_