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_EVENT_ROUTER_H_
6 #define EXTENSIONS_BROWSER_API_USB_USB_EVENT_ROUTER_H_
8 #include "content/public/browser/browser_thread.h"
9 #include "device/usb/usb_service.h"
10 #include "extensions/browser/browser_context_keyed_api_factory.h"
11 #include "extensions/browser/event_router.h"
17 namespace extensions
{
19 // A BrowserContext-scoped object which is registered as an observer of the
20 // EventRouter and UsbService in order to generate device add/remove events.
21 class UsbEventRouter
: public BrowserContextKeyedAPI
,
22 public EventRouter::Observer
,
23 public device::UsbService::Observer
{
25 // BrowserContextKeyedAPI implementation.
26 static BrowserContextKeyedAPIFactory
<UsbEventRouter
>* GetFactoryInstance();
29 friend class BrowserContextKeyedAPIFactory
<UsbEventRouter
>;
31 explicit UsbEventRouter(content::BrowserContext
* context
);
32 ~UsbEventRouter() override
;
34 // BrowserContextKeyedAPI implementation.
35 static const char* service_name() { return "UsbEventRouter"; }
36 static const bool kServiceIsNULLWhileTesting
= true;
38 // KeyedService implementation.
39 void Shutdown() override
;
41 // EventRouter::Observer implementation.
42 void OnListenerAdded(const EventListenerInfo
& details
) override
;
44 // UsbService::Observer implementation.
45 void OnDeviceAdded(scoped_refptr
<device::UsbDevice
> device
) override
;
46 void OnDeviceRemoved(scoped_refptr
<device::UsbDevice
> device
) override
;
48 // Broadcasts a device add or remove event for the given device.
49 void DispatchEvent(const std::string
& event_name
,
50 scoped_refptr
<device::UsbDevice
> device
);
52 content::BrowserContext
* const browser_context_
;
54 ScopedObserver
<device::UsbService
, device::UsbService::Observer
> observer_
;
56 DISALLOW_COPY_AND_ASSIGN(UsbEventRouter
);
60 void BrowserContextKeyedAPIFactory
<
61 UsbEventRouter
>::DeclareFactoryDependencies();
63 } // namespace extensions
65 #endif // EXTENSIONS_BROWSER_API_USB_USB_EVENT_ROUTER_H_