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"
19 namespace extensions
{
21 // A BrowserContext-scoped object which maps USB device GUIDs to legacy integer
22 // IDs for use with the extensions API. This observes device removal to keep
23 // the mapping from growing indefinitely.
24 class UsbGuidMap
: public BrowserContextKeyedAPI
,
25 public device::UsbService::Observer
{
27 static UsbGuidMap
* Get(content::BrowserContext
* browser_context
);
29 // BrowserContextKeyedAPI implementation.
30 static BrowserContextKeyedAPIFactory
<UsbGuidMap
>* GetFactoryInstance();
32 // Returns an ID for this device GUID. If the GUID is unknown to the
33 // UsbGuidMap a new ID is generated for it.
34 int GetIdFromGuid(const std::string
& guid
);
36 // Looks up a device GUID for a given extensions USB device ID. If the ID is
37 // unknown (e.g., the corresponding device was unplugged), this returns
38 // |false|; otherwise it returns |true|.
39 bool GetGuidFromId(int id
, std::string
* guid
);
42 friend class BrowserContextKeyedAPIFactory
<UsbGuidMap
>;
44 explicit UsbGuidMap(content::BrowserContext
* context
);
45 ~UsbGuidMap() override
;
47 // BrowserContextKeyedAPI implementation.
48 static const char* service_name() { return "UsbGuidMap"; }
49 static const bool kServiceIsCreatedWithBrowserContext
= false;
50 static const bool kServiceRedirectedInIncognito
= true;
52 // UsbService::Observer implementation.
53 void OnDeviceRemovedCleanup(scoped_refptr
<device::UsbDevice
> device
) override
;
55 content::BrowserContext
* const browser_context_
;
58 std::map
<std::string
, int> guid_to_id_map_
;
59 std::map
<int, std::string
> id_to_guid_map_
;
61 ScopedObserver
<device::UsbService
, device::UsbService::Observer
> observer_
;
63 DISALLOW_COPY_AND_ASSIGN(UsbGuidMap
);
66 } // namespace extensions
68 #endif // EXTENSIONS_BROWSER_API_USB_USB_GUID_MAP_H_