1 // Copyright 2014 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 "chrome/browser/chromeos/camera_presence_notifier.h"
8 #include "base/memory/singleton.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/chromeos/camera_detector.h"
16 // Interval between checks for camera presence.
17 const int kCameraCheckIntervalSeconds
= 3;
21 CameraPresenceNotifier::CameraPresenceNotifier() :
22 camera_present_on_last_check_(false),
23 weak_factory_(this) {}
25 CameraPresenceNotifier::~CameraPresenceNotifier() {}
28 CameraPresenceNotifier
* CameraPresenceNotifier::GetInstance() {
29 return Singleton
<CameraPresenceNotifier
>::get();
32 void CameraPresenceNotifier::AddObserver(
33 CameraPresenceNotifier::Observer
* observer
) {
34 bool had_no_observers
= !observers_
.might_have_observers();
35 observers_
.AddObserver(observer
);
36 observer
->OnCameraPresenceCheckDone(camera_present_on_last_check_
);
37 if (had_no_observers
) {
38 CheckCameraPresence();
39 camera_check_timer_
.Start(
41 base::TimeDelta::FromSeconds(kCameraCheckIntervalSeconds
),
43 &CameraPresenceNotifier::CheckCameraPresence
);
47 void CameraPresenceNotifier::RemoveObserver(
48 CameraPresenceNotifier::Observer
* observer
) {
49 observers_
.RemoveObserver(observer
);
50 if (!observers_
.might_have_observers()) {
51 camera_check_timer_
.Stop();
52 camera_present_on_last_check_
= false;
56 void CameraPresenceNotifier::CheckCameraPresence() {
57 CameraDetector::StartPresenceCheck(
58 base::Bind(&CameraPresenceNotifier::OnCameraPresenceCheckDone
,
59 weak_factory_
.GetWeakPtr()));
62 void CameraPresenceNotifier::OnCameraPresenceCheckDone() {
63 bool is_camera_present
=
64 CameraDetector::camera_presence() == CameraDetector::kCameraPresent
;
65 if (is_camera_present
!= camera_present_on_last_check_
) {
66 camera_present_on_last_check_
= is_camera_present
;
67 FOR_EACH_OBSERVER(Observer
,
69 OnCameraPresenceCheckDone(camera_present_on_last_check_
));
73 } // namespace chromeos