Skip to content

Commit

Permalink
[cros] In-Chrome camera presence check for avatar picker.
Browse files Browse the repository at this point in the history
BUG=157387
TBR=sky


Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=168824

Review URL: https://chromiumcodereview.appspot.com/11416097

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169067 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
ivankr@chromium.org committed Nov 21, 2012
1 parent b66c249 commit 6de1cb8
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/login/camera_detector.h"
#include "chrome/browser/chromeos/camera_detector.h"

#include "base/bind.h"
#include "base/file_util.h"
#include "base/location.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/threading/worker_pool.h"
#include "chrome/browser/chromeos/system/udev_info_provider.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace chromeos {

Expand All @@ -34,25 +33,20 @@ CameraDetector::CameraPresence CameraDetector::camera_presence_ =
bool CameraDetector::presence_check_in_progress_ = false;

void CameraDetector::StartPresenceCheck(const base::Closure& check_done) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

DVLOG(1) << "Starting camera presence check";

if (!presence_check_in_progress_) {
presence_check_in_progress_ = true;
BrowserThread::PostTaskAndReply(
BrowserThread::FILE, FROM_HERE,
base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(&CameraDetector::CheckPresence),
check_done);
check_done,
/* task_is_slow= */ false);
}
}

void CameraDetector::CheckPresence() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

bool present = false;

system::UdevInfoProvider* udev_info = system::UdevInfoProvider::GetInstance();
// We do a quick check using udev database because opening each /dev/videoX
// device may trigger costly device initialization.
using file_util::FileEnumerator;
Expand All @@ -62,8 +56,8 @@ void CameraDetector::CheckPresence() {
for (FilePath path = file_enum.Next(); !path.empty();
path = file_enum.Next()) {
std::string v4l_capabilities;
if (udev_info->QueryDeviceProperty(path.value(), kV4LCapabilities,
&v4l_capabilities)) {
if (system::UdevInfoProvider::QueryDeviceProperty(
path.value(), kV4LCapabilities, &v4l_capabilities)) {
std::vector<std::string> caps;
base::SplitString(v4l_capabilities, kV4LCapabilitiesDelim, &caps);
if (find(caps.begin(), caps.end(), kV4LCaptureCapability) != caps.end()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_DETECTOR_H_
#define CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_DETECTOR_H_
#ifndef CHROME_BROWSER_CHROMEOS_CAMERA_DETECTOR_H_
#define CHROME_BROWSER_CHROMEOS_CAMERA_DETECTOR_H_

#include "base/callback.h"

Expand All @@ -26,11 +26,11 @@ class CameraDetector {

// Checks asynchronously for camera device presence. Only one
// presence check can be running at a time. Calls |check_done|
// on UI thread when the check has been completed.
// on current thread when the check has been completed.
static void StartPresenceCheck(const base::Closure& check_done);

private:
// Checks for camera presence. Runs of the FILE thread.
// Checks for camera presence. Runs on a worker pool.
static void CheckPresence();

// Result of the last presence check.
Expand All @@ -43,4 +43,4 @@ class CameraDetector {

} // namespace chromeos

#endif // CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_DETECTOR_H_
#endif // CHROME_BROWSER_CHROMEOS_CAMERA_DETECTOR_H_
39 changes: 3 additions & 36 deletions chrome/browser/chromeos/system/udev_info_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
#include "chrome/browser/chromeos/system/udev_info_provider.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace chromeos {
namespace system {

Expand All @@ -28,29 +23,9 @@ const char kUdevadmCommentDelim[] = "";

} // namespace

class UdevInfoProviderImpl : public UdevInfoProvider {
public:
UdevInfoProviderImpl() {}

// UdevInfoProvider implementation.
virtual bool QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) const OVERRIDE;

static UdevInfoProvider* GetInstance();

private:
DISALLOW_COPY_AND_ASSIGN(UdevInfoProviderImpl);
};

base::LazyInstance<UdevInfoProviderImpl> g_udev_info_provider =
LAZY_INSTANCE_INITIALIZER;

bool UdevInfoProviderImpl::QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

bool UdevInfoProvider::QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) {
const char* kUdevadmTool[] = {
kUdevadmToolPath,
"info",
Expand Down Expand Up @@ -78,13 +53,5 @@ bool UdevInfoProviderImpl::QueryDeviceProperty(const std::string& sys_path,
return true;
}

UdevInfoProvider* UdevInfoProviderImpl::GetInstance() {
return &g_udev_info_provider.Get();
}

UdevInfoProvider* UdevInfoProvider::GetInstance() {
return UdevInfoProviderImpl::GetInstance();
}

} // namespace system
} // namespace chromeos
13 changes: 4 additions & 9 deletions chrome/browser/chromeos/system/udev_info_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ namespace system {
// Provides access to udev database.
class UdevInfoProvider {
public:
static UdevInfoProvider* GetInstance();

// Query a named device udev property (e.g., "ID_TYPE", "DEVNAME").
// Returns |true| if property was successfully queried, |false| otherwise.
// Must be run on the FILE thread.
virtual bool QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) const = 0;

protected:
virtual ~UdevInfoProvider() {}
// Must be run on a thread that allows I/O.
static bool QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result);
};

} // namespace system
Expand Down
34 changes: 10 additions & 24 deletions chrome/browser/resources/chromeos/login/oobe_screen_user_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ cr.define('oobe', function() {
this.profileImage_.type = 'profile';
this.profileImageLoading = true;

// Add camera stream element.
imageGrid.cameraImage = null;

$('take-photo').addEventListener(
'click', this.handleTakePhoto_.bind(this));
$('discard-photo').addEventListener(
Expand Down Expand Up @@ -202,14 +199,11 @@ cr.define('oobe', function() {
imageGrid.selectionType != e.oldSelectionType) {
if (imageGrid.selectionType == 'camera') {
// Programmatic selection of camera item is done in
// checkCameraPresence callback where streaming is started by itself.
imageGrid.checkCameraPresence(
function() { // When present.
// startCamera callback where streaming is started by itself.
imageGrid.startCamera(
function() {
// Start capture if camera is still the selected item.
return imageGrid.selectedItem == imageGrid.cameraImage;
},
function() { // When absent.
return true; // Check again after some time.
});
} else {
imageGrid.stopCamera();
Expand Down Expand Up @@ -242,21 +236,6 @@ cr.define('oobe', function() {
Oobe.getInstance().headerHidden = true;
var imageGrid = $('user-image-grid');
imageGrid.updateAndFocus();
if (PRESELECT_CAMERA) {
// Check for camera presence and select it, if present.
imageGrid.checkCameraPresence(
function() { // When present.
imageGrid.selectedItem = imageGrid.cameraImage;
return true; // Start capture if ready.
},
function() { // When absent.
return true; // Check again after some time.
});
} else {
// Check continuously for camera presence but don't select it.
imageGrid.checkCameraPresence(function() { return false; },
function() { return true; });
}
chrome.send('onUserImageScreenShown');
},

Expand Down Expand Up @@ -295,6 +274,13 @@ cr.define('oobe', function() {
}
},

/**
* @param {boolean} present Whether camera is detected.
*/
setCameraPresent_: function(present) {
$('user-image-grid').cameraPresent = present;
},

/**
* Appends default images to the image grid. Should only be called once.
* @param {Array.<{url: string, author: string, website: string}>} images
Expand Down
Loading

0 comments on commit 6de1cb8

Please sign in to comment.