GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / device / usb / usb_service.cc
blob10338fc7e14e45f832ae342e43c2447d10604828
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 #include "device/usb/usb_service.h"
7 #include "base/message_loop/message_loop.h"
8 #include "components/device_event_log/device_event_log.h"
9 #include "device/usb/usb_device.h"
10 #include "device/usb/usb_service_impl.h"
12 namespace device {
14 namespace {
16 UsbService* g_service;
18 } // namespace
20 // This class manages the lifetime of the global UsbService instance so that
21 // it is destroyed when the current message loop is destroyed. A lazy instance
22 // cannot be used because this object does not live on the main thread.
23 class UsbService::Destroyer : private base::MessageLoop::DestructionObserver {
24 public:
25 explicit Destroyer(UsbService* usb_service) : usb_service_(usb_service) {
26 base::MessageLoop::current()->AddDestructionObserver(this);
28 ~Destroyer() override {}
30 private:
31 // base::MessageLoop::DestructionObserver implementation.
32 void WillDestroyCurrentMessageLoop() override {
33 base::MessageLoop::current()->RemoveDestructionObserver(this);
34 delete usb_service_;
35 delete this;
36 g_service = nullptr;
39 UsbService* usb_service_;
42 void UsbService::Observer::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
45 void UsbService::Observer::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
48 void UsbService::Observer::OnDeviceRemovedCleanup(
49 scoped_refptr<UsbDevice> device) {
52 // static
53 UsbService* UsbService::GetInstance(
54 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
55 if (!g_service) {
56 g_service = UsbServiceImpl::Create(ui_task_runner);
57 // This object will clean itself up when the message loop is destroyed.
58 new Destroyer(g_service);
60 return g_service;
63 // static
64 void UsbService::SetInstanceForTest(UsbService* instance) {
65 g_service = instance;
66 new Destroyer(instance);
69 UsbService::UsbService() {
72 UsbService::~UsbService() {
75 void UsbService::AddObserver(Observer* observer) {
76 DCHECK(CalledOnValidThread());
77 observer_list_.AddObserver(observer);
80 void UsbService::RemoveObserver(Observer* observer) {
81 DCHECK(CalledOnValidThread());
82 observer_list_.RemoveObserver(observer);
85 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
86 DCHECK(CalledOnValidThread());
88 USB_LOG(USER) << "USB device added: vendorId = " << device->vendor_id()
89 << ", productId = " << device->product_id()
90 << ", uniqueId = " << device->unique_id();
92 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device));
95 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
96 DCHECK(CalledOnValidThread());
98 USB_LOG(USER) << "USB device removed: uniqueId = " << device->unique_id();
100 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
101 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device));
104 } // namespace device