2012-07-30 21:59:05 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-05-10 06:25:25 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-09-21 10:32:18 +00:00
|
|
|
#include "nsDebug.h"
|
2012-10-01 00:37:47 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsObserverService.h"
|
2012-09-21 10:32:18 +00:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-09-05 01:01:56 +00:00
|
|
|
#include "DOMCameraControl.h"
|
2012-07-30 21:59:05 +00:00
|
|
|
#include "DOMCameraManager.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "DictionaryHelpers.h"
|
|
|
|
#include "CameraCommon.h"
|
2013-05-10 06:25:25 +00:00
|
|
|
#include "mozilla/dom/CameraManagerBinding.h"
|
2012-07-30 21:59:05 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-05-10 06:25:25 +00:00
|
|
|
using namespace mozilla::dom;
|
2012-07-30 21:59:05 +00:00
|
|
|
|
2013-05-10 06:25:25 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsDOMCameraManager, mWindow)
|
2012-10-01 00:37:47 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCameraManager)
|
2013-05-14 10:59:11 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
2012-10-01 00:37:47 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2013-05-10 06:25:25 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2012-07-30 21:59:05 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-10-01 00:37:47 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCameraManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCameraManager)
|
2012-07-30 21:59:05 +00:00
|
|
|
|
2012-09-05 01:01:56 +00:00
|
|
|
/**
|
|
|
|
* Global camera logging object
|
|
|
|
*
|
|
|
|
* Set the NSPR_LOG_MODULES environment variable to enable logging
|
|
|
|
* in a debug build, e.g. NSPR_LOG_MODULES=Camera:5
|
|
|
|
*/
|
2012-10-29 23:32:10 +00:00
|
|
|
PRLogModuleInfo*
|
|
|
|
GetCameraLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("Camera");
|
|
|
|
return sLog;
|
|
|
|
}
|
2012-09-05 01:01:56 +00:00
|
|
|
|
2012-07-30 21:59:05 +00:00
|
|
|
/**
|
|
|
|
* nsDOMCameraManager::GetListOfCameras
|
|
|
|
* is implementation-specific, and can be found in (e.g.)
|
|
|
|
* GonkCameraManager.cpp and FallbackCameraManager.cpp.
|
|
|
|
*/
|
|
|
|
|
2012-10-01 00:37:47 +00:00
|
|
|
WindowTable nsDOMCameraManager::sActiveWindows;
|
|
|
|
bool nsDOMCameraManager::sActiveWindowsInitialized = false;
|
|
|
|
|
2013-05-10 06:25:25 +00:00
|
|
|
nsDOMCameraManager::nsDOMCameraManager(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindowId(aWindow->WindowID())
|
2012-10-01 00:37:47 +00:00
|
|
|
, mCameraThread(nullptr)
|
2013-05-10 06:25:25 +00:00
|
|
|
, mWindow(aWindow)
|
2012-07-30 21:59:05 +00:00
|
|
|
{
|
|
|
|
/* member initializers and constructor code */
|
2012-09-05 01:01:56 +00:00
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p, windowId=%llx\n", __func__, __LINE__, this, mWindowId);
|
2013-05-10 06:25:25 +00:00
|
|
|
SetIsDOMBinding();
|
2012-07-30 21:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMCameraManager::~nsDOMCameraManager()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
2012-09-05 01:01:56 +00:00
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
2012-10-01 00:37:47 +00:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "xpcom-shutdown");
|
2012-07-30 21:59:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:35:44 +00:00
|
|
|
bool
|
|
|
|
nsDOMCameraManager::CheckPermission(nsPIDOMWindow* aWindow)
|
2012-07-30 21:59:05 +00:00
|
|
|
{
|
2012-09-21 10:32:18 +00:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2013-07-12 14:35:44 +00:00
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
2012-07-30 21:59:05 +00:00
|
|
|
|
2012-09-21 10:32:18 +00:00
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "camera", &permission);
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
NS_WARNING("No permission to access camera");
|
2013-07-12 14:35:44 +00:00
|
|
|
return false;
|
2012-09-05 01:01:56 +00:00
|
|
|
}
|
2012-09-21 10:32:18 +00:00
|
|
|
|
2013-07-12 14:35:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static creator
|
|
|
|
already_AddRefed<nsDOMCameraManager>
|
|
|
|
nsDOMCameraManager::CreateInstance(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2012-10-01 00:37:47 +00:00
|
|
|
// Initialize the shared active window tracker
|
|
|
|
if (!sActiveWindowsInitialized) {
|
|
|
|
sActiveWindows.Init();
|
|
|
|
sActiveWindowsInitialized = true;
|
|
|
|
}
|
|
|
|
|
2012-09-21 10:32:18 +00:00
|
|
|
nsRefPtr<nsDOMCameraManager> cameraManager =
|
2013-05-10 06:25:25 +00:00
|
|
|
new nsDOMCameraManager(aWindow);
|
2012-10-01 00:37:47 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->AddObserver(cameraManager, "xpcom-shutdown", true);
|
|
|
|
|
2012-07-30 21:59:05 +00:00
|
|
|
return cameraManager.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-10 06:25:25 +00:00
|
|
|
void
|
2013-05-15 11:07:08 +00:00
|
|
|
nsDOMCameraManager::GetCamera(const CameraSelector& aOptions,
|
2013-05-10 06:25:25 +00:00
|
|
|
nsICameraGetCameraCallback* onSuccess,
|
|
|
|
const Optional<nsICameraErrorCallback*>& onError,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t cameraId = 0; // back (or forward-facing) camera by default
|
2013-05-15 11:07:08 +00:00
|
|
|
if (aOptions.mCamera.EqualsLiteral("front")) {
|
2013-05-10 06:25:25 +00:00
|
|
|
cameraId = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reuse the same camera thread to conserve resources
|
|
|
|
if (!mCameraThread) {
|
|
|
|
aRv = NS_NewThread(getter_AddRefs(mCameraThread));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
|
|
|
|
|
|
|
|
// Creating this object will trigger the onSuccess handler
|
|
|
|
nsCOMPtr<nsDOMCameraControl> cameraControl =
|
|
|
|
new nsDOMCameraControl(cameraId, mCameraThread,
|
|
|
|
onSuccess, onError.WasPassed() ? onError.Value() : nullptr, mWindowId);
|
|
|
|
|
|
|
|
Register(cameraControl);
|
|
|
|
}
|
|
|
|
|
2012-10-01 00:37:47 +00:00
|
|
|
void
|
|
|
|
nsDOMCameraManager::Register(nsDOMCameraControl* aDOMCameraControl)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> Register( aDOMCameraControl = %p ) mWindowId = 0x%llx\n", aDOMCameraControl, mWindowId);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Put the camera control into the hash table
|
|
|
|
CameraControls* controls = sActiveWindows.Get(mWindowId);
|
|
|
|
if (!controls) {
|
|
|
|
controls = new CameraControls;
|
|
|
|
sActiveWindows.Put(mWindowId, controls);
|
|
|
|
}
|
|
|
|
controls->AppendElement(aDOMCameraControl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMCameraManager::Shutdown(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> Shutdown( aWindowId = 0x%llx )\n", aWindowId);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
CameraControls* controls = sActiveWindows.Get(aWindowId);
|
|
|
|
if (!controls) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:04:09 +00:00
|
|
|
uint32_t length = controls->Length();
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2012-10-01 00:37:47 +00:00
|
|
|
nsRefPtr<nsDOMCameraControl> cameraControl = controls->ElementAt(i);
|
|
|
|
cameraControl->Shutdown();
|
|
|
|
}
|
|
|
|
controls->Clear();
|
|
|
|
|
|
|
|
sActiveWindows.Remove(aWindowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMCameraManager::XpComShutdown()
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> XPCOM Shutdown\n");
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "xpcom-shutdown");
|
|
|
|
|
|
|
|
sActiveWindows.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMCameraManager::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (strcmp(aTopic, "xpcom-shutdown") == 0) {
|
|
|
|
XpComShutdown();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMCameraManager::OnNavigation(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> OnNavigation event\n");
|
|
|
|
Shutdown(aWindowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsDOMCameraManager::IsWindowStillActive(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sActiveWindowsInitialized) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!sActiveWindows.Get(aWindowId);
|
|
|
|
}
|
2013-05-10 06:25:25 +00:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
nsDOMCameraManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return CameraManagerBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|