// 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_API_HID_HID_DEVICE_MANAGER_H_ #define EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ #include #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/memory/scoped_vector.h" #include "base/scoped_observer.h" #include "base/threading/thread_checker.h" #include "device/hid/hid_device_info.h" #include "device/hid/hid_service.h" #include "extensions/browser/browser_context_keyed_api_factory.h" #include "extensions/browser/event_router.h" #include "extensions/common/api/hid.h" namespace device { class HidDeviceFilter; } namespace extensions { class Extension; // This service maps devices enumerated by device::HidService to resource IDs // returned by the chrome.hid API. class HidDeviceManager : public BrowserContextKeyedAPI, public device::HidService::Observer, public EventRouter::Observer { public: typedef base::Callback)> GetApiDevicesCallback; explicit HidDeviceManager(content::BrowserContext* context); ~HidDeviceManager() override; // BrowserContextKeyedAPI implementation. static BrowserContextKeyedAPIFactory* GetFactoryInstance(); // Convenience method to get the HidDeviceManager for a profile. static HidDeviceManager* Get(content::BrowserContext* context) { return BrowserContextKeyedAPIFactory::Get(context); } // Enumerates available devices, taking into account the permissions held by // the given extension and the filters provided. The provided callback will // be posted to the calling thread's task runner with a list of device info // objects. void GetApiDevices(const Extension* extension, const std::vector& filters, const GetApiDevicesCallback& callback); scoped_refptr GetDeviceInfo(int resource_id); static bool HasPermission(const Extension* extension, scoped_refptr device_info); private: friend class BrowserContextKeyedAPIFactory; typedef std::map ResourceIdToDeviceIdMap; typedef std::map DeviceIdToResourceIdMap; struct GetApiDevicesParams; // KeyedService: void Shutdown() override; // BrowserContextKeyedAPI: static const char* service_name() { return "HidDeviceManager"; } static const bool kServiceHasOwnInstanceInIncognito = true; static const bool kServiceIsNULLWhileTesting = true; // EventRouter::Observer: void OnListenerAdded(const EventListenerInfo& details) override; // HidService::Observer: void OnDeviceAdded(scoped_refptr device_info) override; void OnDeviceRemoved( scoped_refptr device_info) override; // Wait to perform an initial enumeration and register a HidService::Observer // until the first API customer makes a request or registers an event // listener. void LazyInitialize(); // Builds a list of device info objects representing the currently enumerated // devices, taking into account the permissions held by the given extension // and the filters provided. scoped_ptr CreateApiDeviceList( const Extension* extension, const std::vector& filters); void OnEnumerationComplete( const std::vector>& devices); void DispatchEvent(const std::string& event_name, scoped_ptr event_args, scoped_refptr device_info); base::ThreadChecker thread_checker_; EventRouter* event_router_; bool initialized_; ScopedObserver hid_service_observer_; bool enumeration_ready_; ScopedVector pending_enumerations_; int next_resource_id_; ResourceIdToDeviceIdMap device_ids_; DeviceIdToResourceIdMap resource_ids_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(HidDeviceManager); }; } // namespace extensions #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_