2014-09-19 23:20:41 -07: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/MediaDevices.h"
|
|
|
|
#include "mozilla/dom/MediaStreamBinding.h"
|
|
|
|
#include "mozilla/dom/MediaDevicesBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/MediaManager.h"
|
2015-07-02 11:21:49 -07:00
|
|
|
#include "MediaTrackConstraints.h"
|
2014-09-19 23:20:41 -07:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2015-03-03 06:51:05 -08:00
|
|
|
#include "nsIPermissionManager.h"
|
2014-09-19 23:20:41 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-15 09:47:03 -07:00
|
|
|
#include "nsQueryObject.h"
|
2014-09-19 23:20:41 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class MediaDevices::GumResolver : public nsIDOMGetUserMediaSuccessCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit GumResolver(Promise* aPromise) : mPromise(aPromise) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 09:28:04 -07:00
|
|
|
OnSuccess(nsISupports* aStream) override
|
2014-09-19 23:20:41 -07:00
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DOMLocalMediaStream> stream = do_QueryObject(aStream);
|
2014-09-19 23:20:41 -07:00
|
|
|
if (!stream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mPromise->MaybeResolve(stream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~GumResolver() {}
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
2014-09-19 23:20:41 -07:00
|
|
|
};
|
|
|
|
|
2015-03-03 06:51:05 -08:00
|
|
|
class MediaDevices::EnumDevResolver : public nsIGetUserMediaDevicesSuccessCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-03-03 06:51:05 -08:00
|
|
|
EnumDevResolver(Promise* aPromise, uint64_t aWindowId)
|
|
|
|
: mPromise(aPromise), mWindowId(aWindowId) {}
|
2015-03-03 06:51:05 -08:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnSuccess(nsIVariant* aDevices) override
|
|
|
|
{
|
|
|
|
// Cribbed from MediaPermissionGonk.cpp
|
|
|
|
|
|
|
|
// Create array for nsIMediaDevice
|
|
|
|
nsTArray<nsCOMPtr<nsIMediaDevice>> devices;
|
|
|
|
// Contain the fumes
|
|
|
|
{
|
2015-07-22 10:04:12 -07:00
|
|
|
uint16_t vtype;
|
|
|
|
nsresult rv = aDevices->GetDataType(&vtype);
|
2015-03-03 06:51:05 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-07-22 10:04:12 -07:00
|
|
|
if (vtype != nsIDataType::VTYPE_EMPTY_ARRAY) {
|
|
|
|
nsIID elementIID;
|
|
|
|
uint16_t elementType;
|
|
|
|
void* rawArray;
|
|
|
|
uint32_t arrayLen;
|
|
|
|
rv = aDevices->GetAsArray(&elementType, &elementIID, &arrayLen, &rawArray);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (elementType != nsIDataType::VTYPE_INTERFACE) {
|
|
|
|
free(rawArray);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-03-03 06:51:05 -08:00
|
|
|
|
2015-07-22 10:04:12 -07:00
|
|
|
nsISupports **supportsArray = reinterpret_cast<nsISupports **>(rawArray);
|
|
|
|
for (uint32_t i = 0; i < arrayLen; ++i) {
|
|
|
|
nsCOMPtr<nsIMediaDevice> device(do_QueryInterface(supportsArray[i]));
|
|
|
|
devices.AppendElement(device);
|
|
|
|
NS_IF_RELEASE(supportsArray[i]); // explicitly decrease refcount for rawptr
|
|
|
|
}
|
|
|
|
free(rawArray); // explicitly free memory from nsIVariant::GetAsArray
|
2015-03-03 06:51:05 -08:00
|
|
|
}
|
|
|
|
}
|
2015-10-17 22:24:48 -07:00
|
|
|
nsTArray<RefPtr<MediaDeviceInfo>> infos;
|
2015-03-03 06:51:05 -08:00
|
|
|
for (auto& device : devices) {
|
|
|
|
nsString type;
|
|
|
|
device->GetType(type);
|
|
|
|
bool isVideo = type.EqualsLiteral("video");
|
|
|
|
bool isAudio = type.EqualsLiteral("audio");
|
|
|
|
if (isVideo || isAudio) {
|
|
|
|
MediaDeviceKind kind = isVideo ?
|
|
|
|
MediaDeviceKind::Videoinput : MediaDeviceKind::Audioinput;
|
2015-03-03 06:51:05 -08:00
|
|
|
nsString id;
|
|
|
|
nsString name;
|
|
|
|
device->GetId(id);
|
|
|
|
// Include name only if page currently has a gUM stream active or
|
|
|
|
// persistent permissions (audio or video) have been granted
|
2015-07-09 20:07:42 -07:00
|
|
|
if (MediaManager::Get()->IsActivelyCapturingOrHasAPermission(mWindowId) ||
|
2015-03-03 06:51:05 -08:00
|
|
|
Preferences::GetBool("media.navigator.permission.disabled", false)) {
|
|
|
|
device->GetName(name);
|
|
|
|
}
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<MediaDeviceInfo> info = new MediaDeviceInfo(id, kind, name);
|
2015-03-03 06:51:05 -08:00
|
|
|
infos.AppendElement(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mPromise->MaybeResolve(infos);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~EnumDevResolver() {}
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
2015-03-03 06:51:05 -08:00
|
|
|
uint64_t mWindowId;
|
2015-03-03 06:51:05 -08:00
|
|
|
};
|
|
|
|
|
2014-09-19 23:20:41 -07:00
|
|
|
class MediaDevices::GumRejecter : public nsIDOMGetUserMediaErrorCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit GumRejecter(Promise* aPromise) : mPromise(aPromise) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 09:28:04 -07:00
|
|
|
OnError(nsISupports* aError) override
|
2014-09-19 23:20:41 -07:00
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<MediaStreamError> error = do_QueryObject(aError);
|
2014-09-19 23:20:41 -07:00
|
|
|
if (!error) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mPromise->MaybeReject(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~GumRejecter() {}
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
2014-09-19 23:20:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(MediaDevices::GumResolver, nsIDOMGetUserMediaSuccessCallback)
|
2015-03-03 06:51:05 -08:00
|
|
|
NS_IMPL_ISUPPORTS(MediaDevices::EnumDevResolver, nsIGetUserMediaDevicesSuccessCallback)
|
2014-09-19 23:20:41 -07:00
|
|
|
NS_IMPL_ISUPPORTS(MediaDevices::GumRejecter, nsIDOMGetUserMediaErrorCallback)
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaDevices::GetUserMedia(const MediaStreamConstraints& aConstraints,
|
|
|
|
ErrorResult &aRv)
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* window = GetOwner();
|
|
|
|
nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(window);
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> p = Promise::Create(go, aRv);
|
2015-03-03 06:51:05 -08:00
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
2014-09-19 23:20:41 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<GumResolver> resolver = new GumResolver(p);
|
|
|
|
RefPtr<GumRejecter> rejecter = new GumRejecter(p);
|
2014-09-19 23:20:41 -07:00
|
|
|
|
|
|
|
aRv = MediaManager::Get()->GetUserMedia(window, aConstraints,
|
|
|
|
resolver, rejecter);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:51:05 -08:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaDevices::EnumerateDevices(ErrorResult &aRv)
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* window = GetOwner();
|
|
|
|
nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(window);
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> p = Promise::Create(go, aRv);
|
2015-03-03 06:51:05 -08:00
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<EnumDevResolver> resolver = new EnumDevResolver(p, window->WindowID());
|
|
|
|
RefPtr<GumRejecter> rejecter = new GumRejecter(p);
|
2015-03-03 06:51:05 -08:00
|
|
|
|
|
|
|
aRv = MediaManager::Get()->EnumerateDevices(window, resolver, rejecter);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:20:41 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaDevices, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaDevices, DOMEventTargetHelper)
|
|
|
|
NS_INTERFACE_MAP_BEGIN(MediaDevices)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(MediaDevices)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
MediaDevices::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-09-19 23:20:41 -07:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
return MediaDevicesBinding::Wrap(aCx, this, aGivenProto);
|
2014-09-19 23:20:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|