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/. */
|
|
|
|
|
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"
|
2014-02-14 13:27:44 +00:00
|
|
|
#include "DOMCameraManager.h"
|
2012-07-30 21:59:05 +00:00
|
|
|
#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;
|
2014-02-14 13:27:44 +00:00
|
|
|
if (!sLog)
|
2012-10-29 23:32:10 +00:00
|
|
|
sLog = PR_NewLogModule("Camera");
|
|
|
|
return sLog;
|
|
|
|
}
|
2012-09-05 01:01:56 +00:00
|
|
|
|
2014-02-14 13:27:44 +00:00
|
|
|
/**
|
|
|
|
* nsDOMCameraManager::GetListOfCameras
|
|
|
|
* is implementation-specific, and can be found in (e.g.)
|
|
|
|
* GonkCameraManager.cpp and FallbackCameraManager.cpp.
|
|
|
|
*/
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
WindowTable* nsDOMCameraManager::sActiveWindows = nullptr;
|
2012-10-01 00:37:47 +00:00
|
|
|
|
2013-05-10 06:25:25 +00:00
|
|
|
nsDOMCameraManager::nsDOMCameraManager(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindowId(aWindow->WindowID())
|
2014-02-14 13:27:44 +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);
|
2014-02-14 13:27:44 +00:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
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) {
|
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
|
2013-09-02 08:41:57 +00:00
|
|
|
if (!sActiveWindows) {
|
|
|
|
sActiveWindows = new WindowTable();
|
2012-10-01 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-02-14 13:27:44 +00:00
|
|
|
nsDOMCameraManager::GetCamera(const CameraSelector& aOptions,
|
|
|
|
nsICameraGetCameraCallback* onSuccess,
|
|
|
|
const Optional<nsICameraErrorCallback*>& onError,
|
2013-05-10 06:25:25 +00:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t cameraId = 0; // back (or forward-facing) camera by default
|
2014-02-14 13:27:44 +00:00
|
|
|
if (aOptions.mCamera.EqualsLiteral("front")) {
|
2013-05-10 06:25:25 +00:00
|
|
|
cameraId = 1;
|
|
|
|
}
|
|
|
|
|
2014-02-14 13:27:44 +00:00
|
|
|
// reuse the same camera thread to conserve resources
|
|
|
|
if (!mCameraThread) {
|
|
|
|
aRv = NS_NewThread(getter_AddRefs(mCameraThread));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-10 07:38:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 13:27:44 +00:00
|
|
|
DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
|
|
|
|
|
|
|
|
// Creating this object will trigger the onSuccess handler
|
2013-07-06 20:55:10 +00:00
|
|
|
nsRefPtr<nsDOMCameraControl> cameraControl =
|
2014-02-14 13:27:44 +00:00
|
|
|
new nsDOMCameraControl(cameraId, mCameraThread,
|
|
|
|
onSuccess, onError.WasPassed() ? onError.Value() : nullptr, mWindow);
|
2013-12-10 07:38:43 +00:00
|
|
|
|
|
|
|
Register(cameraControl);
|
2013-05-10 06:25:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
2013-09-02 08:41:57 +00:00
|
|
|
CameraControls* controls = sActiveWindows->Get(mWindowId);
|
2012-10-01 00:37:47 +00:00
|
|
|
if (!controls) {
|
|
|
|
controls = new CameraControls;
|
2013-09-02 08:41:57 +00:00
|
|
|
sActiveWindows->Put(mWindowId, controls);
|
2012-10-01 00:37:47 +00:00
|
|
|
}
|
|
|
|
controls->AppendElement(aDOMCameraControl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMCameraManager::Shutdown(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> Shutdown( aWindowId = 0x%llx )\n", aWindowId);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
CameraControls* controls = sActiveWindows->Get(aWindowId);
|
2012-10-01 00:37:47 +00:00
|
|
|
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();
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
sActiveWindows->Remove(aWindowId);
|
2012-10-01 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMCameraManager::XpComShutdown()
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGI(">>> XPCOM Shutdown\n");
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "xpcom-shutdown");
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
delete sActiveWindows;
|
|
|
|
sActiveWindows = nullptr;
|
2012-10-01 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 15:02:17 +00:00
|
|
|
nsDOMCameraManager::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
|
2012-10-01 00:37:47 +00:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
if (!sActiveWindows) {
|
2012-10-01 00:37:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
return !!sActiveWindows->Get(aWindowId);
|
2012-10-01 00:37:47 +00:00
|
|
|
}
|
2013-05-10 06:25:25 +00:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
nsDOMCameraManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return CameraManagerBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|