summaryrefslogtreecommitdiffstats
path: root/device/usb/usb_service.cc
blob: 18732b50d3e14d581dca34d2641184e6b9cf7908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015 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.

#include "device/usb/usb_service.h"

#include "base/message_loop/message_loop.h"
#include "components/device_event_log/device_event_log.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_service_impl.h"

namespace device {

namespace {

UsbService* g_service;

}  // namespace

void UsbService::Observer::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
}

void UsbService::Observer::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
}

void UsbService::Observer::OnDeviceRemovedCleanup(
    scoped_refptr<UsbDevice> device) {
}

// static
UsbService* UsbService::GetInstance(
    scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
  if (!g_service) {
    // UsbService constructor saves the pointer this returns and UsbServiceImpl
    // will destroy itself when the current message loop exits.
    UsbServiceImpl::Create(blocking_task_runner);
  }
  return g_service;
}

UsbService::UsbService() {
  DCHECK(!g_service);
  g_service = this;
}

UsbService::~UsbService() {
  DCHECK(g_service);
  g_service = nullptr;
}

void UsbService::AddObserver(Observer* observer) {
  DCHECK(CalledOnValidThread());
  observer_list_.AddObserver(observer);
}

void UsbService::RemoveObserver(Observer* observer) {
  DCHECK(CalledOnValidThread());
  observer_list_.RemoveObserver(observer);
}

void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
  DCHECK(CalledOnValidThread());

  FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device));
}

void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
  DCHECK(CalledOnValidThread());

  FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
  FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device));
}

}  // namespace device