This source file includes following definitions.
- StartPresenceCheck
- OnPresenceCheckDone
- CheckPresence
#include "chrome/browser/chromeos/camera_detector.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/location.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/task_runner_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "components/storage_monitor/udev_util_linux.h"
#include "content/public/browser/browser_thread.h"
namespace chromeos {
namespace {
const char kV4LSubsystemDir[] = "/sys/class/video4linux/";
const char kV4LCapabilities[] = "ID_V4L_CAPABILITIES";
const char kV4LCapabilitiesDelim = ':';
const char kV4LCaptureCapability[] = "capture";
}
using content::BrowserThread;
CameraDetector::CameraPresence CameraDetector::camera_presence_ =
CameraDetector::kCameraPresenceUnknown;
bool CameraDetector::presence_check_in_progress_ = false;
void CameraDetector::StartPresenceCheck(const base::Closure& callback) {
DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (presence_check_in_progress_)
return;
DVLOG(1) << "Starting camera presence check";
presence_check_in_progress_ = true;
base::PostTaskAndReplyWithResult(
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN).get(),
FROM_HERE,
base::Bind(&CameraDetector::CheckPresence),
base::Bind(&CameraDetector::OnPresenceCheckDone, callback));
}
void CameraDetector::OnPresenceCheckDone(const base::Closure& callback,
bool present) {
DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI));
camera_presence_ = present ? kCameraPresent : kCameraAbsent;
presence_check_in_progress_ = false;
callback.Run();
}
bool CameraDetector::CheckPresence() {
base::FileEnumerator file_enum(
base::FilePath(kV4LSubsystemDir), false ,
base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS);
for (base::FilePath path = file_enum.Next(); !path.empty();
path = file_enum.Next()) {
std::string v4l_capabilities;
if (storage_monitor::GetUdevDevicePropertyValueByPath(
path, kV4LCapabilities, &v4l_capabilities)) {
std::vector<std::string> caps;
base::SplitString(v4l_capabilities, kV4LCapabilitiesDelim, &caps);
if (find(caps.begin(), caps.end(), kV4LCaptureCapability) != caps.end()) {
return true;
}
}
}
return false;
}
}