2012-07-31 01:59:05 +04: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 10:25:25 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-09-21 14:32:18 +04:00
|
|
|
#include "nsDebug.h"
|
2012-10-01 04:37:47 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsObserverService.h"
|
2012-09-21 14:32:18 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-09-05 05:01:56 +04:00
|
|
|
#include "DOMCameraControl.h"
|
2012-07-31 01:59:05 +04:00
|
|
|
#include "DOMCameraManager.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "DictionaryHelpers.h"
|
|
|
|
#include "CameraCommon.h"
|
2013-05-10 10:25:25 +04:00
|
|
|
#include "mozilla/dom/CameraManagerBinding.h"
|
2012-07-31 01:59:05 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-05-10 10:25:25 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-07-31 01:59:05 +04:00
|
|
|
|
2013-05-10 10:25:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsDOMCameraManager, mWindow)
|
2012-10-01 04:37:47 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCameraManager)
|
2013-05-14 14:59:11 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
2012-10-01 04:37:47 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2013-05-10 10:25:25 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2012-07-31 01:59:05 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-10-01 04:37:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCameraManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCameraManager)
|
2012-07-31 01:59:05 +04:00
|
|
|
|
2012-09-05 05:01:56 +04: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-30 03:32:10 +04:00
|
|
|
PRLogModuleInfo*
|
|
|
|
GetCameraLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("Camera");
|
|
|
|
return sLog;
|
|
|
|
}
|
2012-09-05 05:01:56 +04:00
|
|
|
|
2012-07-31 01:59:05 +04:00
|
|
|
/**
|
|
|
|
* nsDOMCameraManager::GetListOfCameras
|
|
|
|
* is implementation-specific, and can be found in (e.g.)
|
|
|
|
* GonkCameraManager.cpp and FallbackCameraManager.cpp.
|
|
|
|
*/
|
|
|
|
|
2012-10-01 04:37:47 +04:00
|
|
|
WindowTable nsDOMCameraManager::sActiveWindows;
|
|
|
|
bool nsDOMCameraManager::sActiveWindowsInitialized = false;
|
|
|
|
|
2013-05-10 10:25:25 +04:00
|
|
|
nsDOMCameraManager::nsDOMCameraManager(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindowId(aWindow->WindowID())
|
2012-10-01 04:37:47 +04:00
|
|
|
, mCameraThread(nullptr)
|
2013-05-10 10:25:25 +04:00
|
|
|
, mWindow(aWindow)
|
2012-07-31 01:59:05 +04:00
|
|
|
{
|
|
|
|
/* member initializers and constructor code */
|
2012-09-05 05:01:56 +04:00
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p, windowId=%llx\n", __func__, __LINE__, this, mWindowId);
|
2013-05-10 10:25:25 +04:00
|
|
|
SetIsDOMBinding();
|
2012-07-31 01:59:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMCameraManager::~nsDOMCameraManager()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
2012-09-05 05:01:56 +04:00
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
2012-10-01 04:37:47 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "xpcom-shutdown");
|
2012-07-31 01:59:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static creator
|
|
|
|
already_AddRefed<nsDOMCameraManager>
|
2012-09-21 14:32:18 +04:00
|
|
|
nsDOMCameraManager::CheckPermissionAndCreateInstance(nsPIDOMWindow* aWindow)
|
2012-07-31 01:59:05 +04:00
|
|
|
{
|
2012-09-21 14:32:18 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(permMgr, nullptr);
|
2012-07-31 01:59:05 +04:00
|
|
|
|
2012-09-21 14:32:18 +04:00
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "camera", &permission);
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
NS_WARNING("No permission to access camera");
|
|
|
|
return nullptr;
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
2012-09-21 14:32:18 +04:00
|
|
|
|
2012-10-01 04:37:47 +04:00
|
|
|
// Initialize the shared active window tracker
|
|
|
|
if (!sActiveWindowsInitialized) {
|
|
|
|
sActiveWindows.Init();
|
|
|
|
sActiveWindowsInitialized = true;
|
|
|
|
}
|
|
|
|
|
2012-09-21 14:32:18 +04:00
|
|
|
nsRefPtr<nsDOMCameraManager> cameraManager =
|
2013-05-10 10:25:25 +04:00
|
|
|
new nsDOMCameraManager(aWindow);
|
2012-10-01 04:37:47 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->AddObserver(cameraManager, "xpcom-shutdown", true);
|
|
|
|
|
2012-07-31 01:59:05 +04:00
|
|
|
return cameraManager.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-10 10:25:25 +04:00
|
|
|
void
|
|
|
|
nsDOMCameraManager::GetCamera(JSContext* aCx, const JS::Value aOptions,
|
|
|
|
nsICameraGetCameraCallback* onSuccess,
|
|
|
|
const Optional<nsICameraErrorCallback*>& onError,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t cameraId = 0; // back (or forward-facing) camera by default
|
|
|
|
mozilla::idl::CameraSelector selector;
|
|
|
|
|
|
|
|
aRv = selector.Init(aCx, &aOptions);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selector.camera.EqualsLiteral("front")) {
|
|
|
|
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 04:37:47 +04: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-02 00:04:09 +04:00
|
|
|
uint32_t length = controls->Length();
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2012-10-01 04:37:47 +04: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 10:25:25 +04:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
nsDOMCameraManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return CameraManagerBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|