2014-08-21 13:15:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
#include "DOMMediaStream.h"
|
2014-08-21 13:15:18 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-08-22 08:08:34 +04:00
|
|
|
#include "mozilla/dom/TVCurrentSourceChangedEvent.h"
|
|
|
|
#include "mozilla/dom/TVServiceCallbacks.h"
|
|
|
|
#include "mozilla/dom/TVServiceFactory.h"
|
|
|
|
#include "mozilla/dom/TVSource.h"
|
|
|
|
#include "mozilla/dom/TVUtils.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsITVService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-08-21 13:15:18 +04:00
|
|
|
#include "TVTuner.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(TVTuner, DOMEventTargetHelper,
|
|
|
|
mTVService, mStream, mCurrentSource, mSources)
|
2014-08-21 13:15:18 +04:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(TVTuner, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TVTuner, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TVTuner)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
TVTuner::TVTuner(nsPIDOMWindow* aWindow)
|
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TVTuner::~TVTuner()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
/* static */ already_AddRefed<TVTuner>
|
|
|
|
TVTuner::Create(nsPIDOMWindow* aWindow,
|
|
|
|
nsITVTunerData* aData)
|
|
|
|
{
|
|
|
|
nsRefPtr<TVTuner> tuner = new TVTuner(aWindow);
|
|
|
|
return (tuner->Init(aData)) ? tuner.forget() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TVTuner::Init(nsITVTunerData* aData)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aData, false);
|
|
|
|
|
|
|
|
nsresult rv = aData->GetId(mId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
if (NS_WARN_IF(mId.IsEmpty())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t count;
|
|
|
|
char** supportedSourceTypes;
|
|
|
|
rv = aData->GetSupportedSourceTypes(&count, &supportedSourceTypes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
TVSourceType sourceType = ToTVSourceType(supportedSourceTypes[i]);
|
|
|
|
if (NS_WARN_IF(sourceType == TVSourceType::EndGuard_)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the source instance based on the supported source type.
|
|
|
|
nsRefPtr<TVSource> source = TVSource::Create(GetOwner(), sourceType, this);
|
|
|
|
if (NS_WARN_IF(!source)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSupportedSourceTypes.AppendElement(sourceType);
|
|
|
|
mSources.AppendElement(source);
|
|
|
|
}
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, supportedSourceTypes);
|
|
|
|
|
|
|
|
mTVService = TVServiceFactory::AutoCreateTVService();
|
|
|
|
NS_ENSURE_TRUE(mTVService, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:15:18 +04:00
|
|
|
/* virtual */ 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 17:13:33 +03:00
|
|
|
TVTuner::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-08-21 13:15:18 +04: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 17:13:33 +03:00
|
|
|
return TVTunerBinding::Wrap(aCx, this, aGivenProto);
|
2014-08-21 13:15:18 +04:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
nsresult
|
|
|
|
TVTuner::SetCurrentSource(TVSourceType aSourceType)
|
|
|
|
{
|
|
|
|
ErrorResult error;
|
|
|
|
if (mCurrentSource) {
|
|
|
|
if (aSourceType == mCurrentSource->Type()) {
|
|
|
|
// No actual change.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to stay tuned for non-current sources.
|
|
|
|
nsresult rv = mCurrentSource->UnsetCurrentChannel();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mSources.Length(); i++) {
|
|
|
|
if (aSourceType == mSources[i]->Type()) {
|
|
|
|
mCurrentSource = mSources[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = InitMediaStream();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DispatchCurrentSourceChangedEvent(mCurrentSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TVTuner::DispatchTVEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
return DispatchTrustedEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:15:18 +04:00
|
|
|
void
|
|
|
|
TVTuner::GetSupportedSourceTypes(nsTArray<TVSourceType>& aSourceTypes,
|
|
|
|
ErrorResult& aRv) const
|
|
|
|
{
|
2014-08-22 08:08:34 +04:00
|
|
|
aSourceTypes = mSupportedSourceTypes;
|
2014-08-21 13:15:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
TVTuner::GetSources(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
|
2014-08-22 08:08:34 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-08-21 13:15:18 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
promise->MaybeResolve(mSources);
|
2014-08-21 13:15:18 +04:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
TVTuner::SetCurrentSource(const TVSourceType aSourceType, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
|
2014-08-22 08:08:34 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-08-21 13:15:18 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:08:34 +04:00
|
|
|
// |SetCurrentSource(const TVSourceType)| will be called once |notifySuccess|
|
|
|
|
// of the callback is invoked.
|
|
|
|
nsCOMPtr<nsITVServiceCallback> callback =
|
|
|
|
new TVServiceSourceSetterCallback(this, promise, aSourceType);
|
|
|
|
nsresult rv = mTVService->SetSource(mId, ToTVSourceTypeStr(aSourceType), callback);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
}
|
2014-08-21 13:15:18 +04:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TVTuner::GetId(nsAString& aId) const
|
|
|
|
{
|
2014-08-22 08:08:34 +04:00
|
|
|
aId = mId;
|
2014-08-21 13:15:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TVSource>
|
|
|
|
TVTuner::GetCurrentSource() const
|
|
|
|
{
|
2014-08-22 08:08:34 +04:00
|
|
|
nsRefPtr<TVSource> currentSource = mCurrentSource;
|
|
|
|
return currentSource.forget();
|
2014-08-21 13:15:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMMediaStream>
|
|
|
|
TVTuner::GetStream() const
|
|
|
|
{
|
2014-08-22 08:08:34 +04:00
|
|
|
nsRefPtr<DOMMediaStream> stream = mStream;
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TVTuner::InitMediaStream()
|
|
|
|
{
|
|
|
|
// TODO Instantiate |mStream| when bug 987498 is done.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TVTuner::DispatchCurrentSourceChangedEvent(TVSource* aSource)
|
|
|
|
{
|
|
|
|
TVCurrentSourceChangedEventInit init;
|
|
|
|
init.mSource = aSource;
|
|
|
|
nsCOMPtr<nsIDOMEvent> event =
|
|
|
|
TVCurrentSourceChangedEvent::Constructor(this,
|
|
|
|
NS_LITERAL_STRING("currentsourcechanged"),
|
|
|
|
init);
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethodWithArg<nsCOMPtr<nsIDOMEvent>>(this,
|
|
|
|
&TVTuner::DispatchTVEvent,
|
|
|
|
event);
|
|
|
|
return NS_DispatchToCurrentThread(runnable);
|
2014-08-21 13:15:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|