This source file includes following definitions.
- camera_present_on_last_check_
- GetInstance
- AddObserver
- RemoveObserver
- CheckCameraPresence
- OnCameraPresenceCheckDone
#include "chrome/browser/chromeos/camera_presence_notifier.h"
#include "base/bind.h"
#include "base/memory/singleton.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/camera_detector.h"
namespace chromeos {
namespace {
const int kCameraCheckIntervalSeconds = 3;
}
CameraPresenceNotifier::CameraPresenceNotifier() :
weak_factory_(this),
camera_present_on_last_check_(false) {}
CameraPresenceNotifier::~CameraPresenceNotifier() {}
CameraPresenceNotifier* CameraPresenceNotifier::GetInstance() {
return Singleton<CameraPresenceNotifier>::get();
}
void CameraPresenceNotifier::AddObserver(
CameraPresenceNotifier::Observer* observer) {
bool had_no_observers = !observers_.might_have_observers();
observers_.AddObserver(observer);
observer->OnCameraPresenceCheckDone(camera_present_on_last_check_);
if (had_no_observers) {
CheckCameraPresence();
camera_check_timer_.Start(
FROM_HERE,
base::TimeDelta::FromSeconds(kCameraCheckIntervalSeconds),
this,
&CameraPresenceNotifier::CheckCameraPresence);
}
}
void CameraPresenceNotifier::RemoveObserver(
CameraPresenceNotifier::Observer* observer) {
observers_.RemoveObserver(observer);
if (!observers_.might_have_observers()) {
camera_check_timer_.Stop();
camera_present_on_last_check_ = false;
}
}
void CameraPresenceNotifier::CheckCameraPresence() {
CameraDetector::StartPresenceCheck(
base::Bind(&CameraPresenceNotifier::OnCameraPresenceCheckDone,
weak_factory_.GetWeakPtr()));
}
void CameraPresenceNotifier::OnCameraPresenceCheckDone() {
bool is_camera_present =
CameraDetector::camera_presence() == CameraDetector::kCameraPresent;
if (is_camera_present != camera_present_on_last_check_) {
camera_present_on_last_check_ = is_camera_present;
FOR_EACH_OBSERVER(Observer,
observers_,
OnCameraPresenceCheckDone(camera_present_on_last_check_));
}
}
}