BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / camera_presence_notifier.cc
blob2d44dd912f1d1624554797ff2ff325bd5c5f0a40
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"
7 #include "base/bind.h"
8 #include "base/memory/singleton.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/chromeos/camera_detector.h"
12 namespace chromeos {
14 namespace {
16 // Interval between checks for camera presence.
17 const int kCameraCheckIntervalSeconds = 3;
19 } // namespace
21 CameraPresenceNotifier::CameraPresenceNotifier() :
22 camera_present_on_last_check_(false),
23 weak_factory_(this) {}
25 CameraPresenceNotifier::~CameraPresenceNotifier() {}
27 // static
28 CameraPresenceNotifier* CameraPresenceNotifier::GetInstance() {
29 return base::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(
40 FROM_HERE,
41 base::TimeDelta::FromSeconds(kCameraCheckIntervalSeconds),
42 this,
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,
68 observers_,
69 OnCameraPresenceCheckDone(camera_present_on_last_check_));
73 } // namespace chromeos