2013-09-16 06:50:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "ImageCapture.h"
|
|
|
|
#include "mozilla/dom/BlobEvent.h"
|
|
|
|
#include "mozilla/dom/DOMException.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-09-16 06:50:24 +04:00
|
|
|
#include "mozilla/dom/ImageCaptureError.h"
|
|
|
|
#include "mozilla/dom/ImageCaptureErrorEvent.h"
|
|
|
|
#include "mozilla/dom/ImageCaptureErrorEventBinding.h"
|
|
|
|
#include "mozilla/dom/VideoStreamTrack.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "CaptureTask.h"
|
2014-09-09 02:17:00 +04:00
|
|
|
#include "MediaEngine.h"
|
2013-09-16 06:50:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
LogModule* GetICLog()
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
2015-11-15 16:49:01 +03:00
|
|
|
static LazyLogModule log("ImageCapture");
|
2013-09-16 06:50:24 +04:00
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ImageCapture, DOMEventTargetHelper,
|
|
|
|
mVideoStreamTrack)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ImageCapture)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ImageCapture, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ImageCapture, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
ImageCapture::ImageCapture(VideoStreamTrack* aVideoStreamTrack,
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aOwnerWindow)
|
2013-09-16 06:50:24 +04:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aOwnerWindow);
|
|
|
|
MOZ_ASSERT(aVideoStreamTrack);
|
|
|
|
|
|
|
|
mVideoStreamTrack = aVideoStreamTrack;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageCapture::~ImageCapture()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ImageCapture>
|
|
|
|
ImageCapture::Constructor(const GlobalObject& aGlobal,
|
|
|
|
VideoStreamTrack& aTrack,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-09-16 06:50:24 +04:00
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageCapture> object = new ImageCapture(&aTrack, win);
|
2013-09-16 06:50:24 +04:00
|
|
|
|
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoStreamTrack*
|
|
|
|
ImageCapture::GetVideoStreamTrack() const
|
|
|
|
{
|
|
|
|
return mVideoStreamTrack;
|
|
|
|
}
|
|
|
|
|
2014-09-09 02:17:00 +04:00
|
|
|
nsresult
|
|
|
|
ImageCapture::TakePhotoByMediaEngine()
|
|
|
|
{
|
|
|
|
// Callback for TakPhoto(), it also monitor the principal. If principal
|
|
|
|
// changes, it returns PHOTO_ERROR with security error.
|
2016-01-05 05:16:21 +03:00
|
|
|
class TakePhotoCallback : public MediaEnginePhotoCallback,
|
2016-01-22 06:26:29 +03:00
|
|
|
public PrincipalChangeObserver<MediaStreamTrack>
|
2014-09-09 02:17:00 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-04-06 15:46:56 +03:00
|
|
|
TakePhotoCallback(VideoStreamTrack* aVideoTrack, ImageCapture* aImageCapture)
|
|
|
|
: mVideoTrack(aVideoTrack)
|
2014-09-09 02:17:00 +04:00
|
|
|
, mImageCapture(aImageCapture)
|
|
|
|
, mPrincipalChanged(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-01-22 06:26:29 +03:00
|
|
|
mVideoTrack->AddPrincipalChangeObserver(this);
|
2014-09-09 02:17:00 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 06:26:29 +03:00
|
|
|
void PrincipalChanged(MediaStreamTrack* aMediaStream) override
|
2014-09-09 02:17:00 +04:00
|
|
|
{
|
|
|
|
mPrincipalChanged = true;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
nsresult PhotoComplete(already_AddRefed<Blob> aBlob) override
|
2014-09-09 02:17:00 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = aBlob;
|
2014-09-09 02:17:00 +04:00
|
|
|
|
|
|
|
if (mPrincipalChanged) {
|
|
|
|
return PhotoError(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
return mImageCapture->PostBlobEvent(blob);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult PhotoError(nsresult aRv) override
|
2014-09-09 02:17:00 +04:00
|
|
|
{
|
|
|
|
return mImageCapture->PostErrorEvent(ImageCaptureError::PHOTO_ERROR, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~TakePhotoCallback()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-01-22 06:26:29 +03:00
|
|
|
mVideoTrack->RemovePrincipalChangeObserver(this);
|
2014-09-09 02:17:00 +04:00
|
|
|
}
|
|
|
|
|
2016-04-06 15:46:56 +03:00
|
|
|
RefPtr<VideoStreamTrack> mVideoTrack;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageCapture> mImageCapture;
|
2014-09-09 02:17:00 +04:00
|
|
|
bool mPrincipalChanged;
|
|
|
|
};
|
|
|
|
|
2016-04-06 15:46:56 +03:00
|
|
|
RefPtr<MediaEnginePhotoCallback> callback =
|
|
|
|
new TakePhotoCallback(mVideoStreamTrack, this);
|
|
|
|
return mVideoStreamTrack->GetSource().TakePhoto(callback);
|
2014-09-09 02:17:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-16 06:50:24 +04:00
|
|
|
void
|
|
|
|
ImageCapture::TakePhoto(ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
// According to spec, VideoStreamTrack.readyState must be "live"; however
|
|
|
|
// gecko doesn't implement it yet (bug 910249). Instead of readyState, we
|
|
|
|
// check VideoStreamTrack.enable before bug 910249 is fixed.
|
|
|
|
// The error code should be INVALID_TRACK, but spec doesn't define it in
|
|
|
|
// ImageCaptureError. So it returns PHOTO_ERROR here before spec updates.
|
|
|
|
if (!mVideoStreamTrack->Enabled()) {
|
|
|
|
PostErrorEvent(ImageCaptureError::PHOTO_ERROR, NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-09 02:17:00 +04:00
|
|
|
// Try if MediaEngine supports taking photo.
|
|
|
|
nsresult rv = TakePhotoByMediaEngine();
|
2013-09-16 06:50:24 +04:00
|
|
|
|
2014-09-09 02:17:00 +04:00
|
|
|
// It falls back to MediaStreamGraph image capture if MediaEngine doesn't
|
|
|
|
// support TakePhoto().
|
|
|
|
if (rv == NS_ERROR_NOT_IMPLEMENTED) {
|
|
|
|
IC_LOG("MediaEngine doesn't support TakePhoto(), it falls back to MediaStreamGraph.");
|
2016-05-31 08:53:49 +03:00
|
|
|
RefPtr<CaptureTask> task = new CaptureTask(this);
|
2014-09-09 02:17:00 +04:00
|
|
|
|
|
|
|
// It adds itself into MediaStreamGraph, so ImageCapture doesn't need to hold
|
|
|
|
// the reference.
|
2016-01-22 07:29:54 +03:00
|
|
|
task->AttachTrack();
|
2014-09-09 02:17:00 +04:00
|
|
|
}
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 15:09:51 +03:00
|
|
|
ImageCapture::PostBlobEvent(Blob* aBlob)
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!CheckPrincipal()) {
|
|
|
|
// Media is not same-origin, don't allow the data out.
|
|
|
|
return PostErrorEvent(ImageCaptureError::PHOTO_ERROR, NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mData = aBlob;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobEvent> blob_event =
|
2013-09-16 06:50:24 +04:00
|
|
|
BlobEvent::Constructor(this, NS_LITERAL_STRING("photo"), init);
|
|
|
|
|
|
|
|
return DispatchTrustedEvent(blob_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ImageCapture::PostErrorEvent(uint16_t aErrorCode, nsresult aReason)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsString errorMsg;
|
|
|
|
if (NS_FAILED(aReason)) {
|
|
|
|
nsCString name, message;
|
|
|
|
rv = NS_GetNameAndMessageForDOMNSResult(aReason, name, message);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyASCIItoUTF16(message, errorMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageCaptureError> error =
|
2013-09-16 06:50:24 +04:00
|
|
|
new ImageCaptureError(this, aErrorCode, errorMsg);
|
|
|
|
|
|
|
|
ImageCaptureErrorEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mImageCaptureError = error;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event =
|
|
|
|
ImageCaptureErrorEvent::Constructor(this, NS_LITERAL_STRING("error"), init);
|
|
|
|
|
|
|
|
return DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImageCapture::CheckPrincipal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-22 06:26:29 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = mVideoStreamTrack->GetPrincipal();
|
2013-09-16 06:50:24 +04:00
|
|
|
|
|
|
|
if (!GetOwner()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocument> doc = GetOwner()->GetExtantDoc();
|
|
|
|
if (!doc || !principal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subsumes;
|
|
|
|
if (NS_FAILED(doc->NodePrincipal()->Subsumes(principal, &subsumes))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subsumes;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|