Bug 838146 part 6. Implement gamepad, bluetooth, time, audiochannel WebIDL APIs on Navigator. r=smaug, sr=sicking

This commit is contained in:
Boris Zbarsky 2013-07-12 10:36:32 -04:00
parent 8145ea330b
commit 83f2124baf
6 changed files with 156 additions and 43 deletions

View File

@ -1603,15 +1603,12 @@ Navigator::GetMozIccManager(ErrorResult& aRv)
NS_IMETHODIMP
Navigator::GetGamepads(nsIVariant** aRetVal)
{
NS_ENSURE_ARG_POINTER(aRetVal);
*aRetVal = nullptr;
NS_ENSURE_STATE(mWindow);
NS_ENSURE_TRUE(mWindow->GetDocShell(), NS_OK);
nsGlobalWindow* win = static_cast<nsGlobalWindow*>(mWindow.get());
ErrorResult rv;
nsAutoTArray<nsRefPtr<Gamepad>, 2> gamepads;
win->GetGamepads(gamepads);
GetGamepads(gamepads, rv);
if (rv.Failed()) {
return rv.ErrorCode();
}
nsRefPtr<nsVariant> out = new nsVariant();
NS_ENSURE_STATE(out);
@ -1629,6 +1626,19 @@ Navigator::GetGamepads(nsIVariant** aRetVal)
return NS_OK;
}
void
Navigator::GetGamepads(nsTArray<nsRefPtr<Gamepad> >& aGamepads,
ErrorResult& aRv)
{
if (!mWindow) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
NS_ENSURE_TRUE_VOID(mWindow->GetDocShell());
nsGlobalWindow* win = static_cast<nsGlobalWindow*>(mWindow.get());
win->GetGamepads(aGamepads);
}
#endif
//*****************************************************************************
@ -1700,18 +1710,33 @@ Navigator::GetMozMobileConnection(ErrorResult& aRv)
NS_IMETHODIMP
Navigator::GetMozBluetooth(nsIDOMBluetoothManager** aBluetooth)
{
nsCOMPtr<nsIDOMBluetoothManager> bluetooth = mBluetooth;
if (!bluetooth) {
if (!mBluetooth) {
NS_ENSURE_STATE(mWindow);
nsresult rv = NS_NewBluetoothManager(mWindow, getter_AddRefs(mBluetooth));
NS_ENSURE_SUCCESS(rv, rv);
bluetooth = mBluetooth;
if (!bluetooth::BluetoothManager::CheckPermission(mWindow)) {
*aBluetooth = nullptr;
return NS_OK;
}
}
bluetooth.forget(aBluetooth);
return NS_OK;
ErrorResult rv;
NS_IF_ADDREF(*aBluetooth = GetMozBluetooth(rv));
return rv.ErrorCode();
}
nsIDOMBluetoothManager*
Navigator::GetMozBluetooth(ErrorResult& aRv)
{
// Callers (either the XPCOM method or the WebIDL binding) are responsible for
// the permission check here.
if (!mBluetooth) {
if (!mWindow) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
mBluetooth = bluetooth::BluetoothManager::Create(mWindow);
}
return mBluetooth;
}
#endif //MOZ_B2G_BT
@ -1820,19 +1845,30 @@ Navigator::MozSetMessageHandler(const nsAString& aType,
NS_IMETHODIMP
Navigator::GetMozTime(nsISupports** aTime)
{
*aTime = nullptr;
NS_ENSURE_STATE(mWindow);
if (!CheckPermission("time")) {
return NS_ERROR_DOM_SECURITY_ERR;
}
ErrorResult rv;
NS_IF_ADDREF(*aTime = GetMozTime(rv));
return rv.ErrorCode();
}
time::TimeManager*
Navigator::GetMozTime(ErrorResult& aRv)
{
// Callers (either the XPCOM method or the WebIDL binding) are responsible for
// the permission check here.
if (!mWindow) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
if (!mTimeManager) {
mTimeManager = new time::TimeManager(mWindow);
}
NS_ADDREF(*aTime = mTimeManager);
return NS_OK;
return mTimeManager;
}
#endif
@ -1945,16 +1981,24 @@ Navigator::CheckPermission(nsPIDOMWindow* aWindow, const char* aType)
NS_IMETHODIMP
Navigator::GetMozAudioChannelManager(nsISupports** aAudioChannelManager)
{
*aAudioChannelManager = nullptr;
ErrorResult rv;
NS_IF_ADDREF(*aAudioChannelManager = GetMozAudioChannelManager(rv));
return rv.ErrorCode();
}
system::AudioChannelManager*
Navigator::GetMozAudioChannelManager(ErrorResult& aRv)
{
if (!mAudioChannelManager) {
NS_ENSURE_STATE(mWindow);
if (!mWindow) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
mAudioChannelManager = new system::AudioChannelManager();
mAudioChannelManager->Init(mWindow);
}
NS_ADDREF(*aAudioChannelManager = mAudioChannelManager);
return NS_OK;
return mAudioChannelManager;
}
#endif
@ -2089,6 +2133,26 @@ Navigator::HasIccManagerSupport(JSContext* /* unused */,
}
#endif // MOZ_B2G_RIL
#ifdef MOZ_B2G_BT
/* static */
bool
Navigator::HasBluetoothSupport(JSContext* /* unused */, JSObject* aGlobal)
{
nsCOMPtr<nsPIDOMWindow> win = GetWindowFromGlobal(aGlobal);
return win && bluetooth::BluetoothManager::CheckPermission(win);
}
#endif // MOZ_B2G_BT
#ifdef MOZ_TIME_MANAGER
/* static */
bool
Navigator::HasTimeSupport(JSContext* /* unused */, JSObject* aGlobal)
{
nsCOMPtr<nsPIDOMWindow> win = GetWindowFromGlobal(aGlobal);
return win && CheckPermission(win, "time");
}
#endif // MOZ_TIME_MANAGER
/* static */
already_AddRefed<nsPIDOMWindow>
Navigator::GetWindowFromGlobal(JSObject* aGlobal)

View File

@ -83,6 +83,9 @@ class DesktopNotificationCenter;
class SmsManager;
class MobileMessageManager;
class MozIdleObserver;
#ifdef MOZ_GAMEPAD
class Gamepad;
#endif // MOZ_GAMEPAD
namespace icc {
#ifdef MOZ_B2G_RIL
@ -313,6 +316,18 @@ public:
nsIDOMMozVoicemail* GetMozVoicemail(ErrorResult& aRv);
nsIDOMMozIccManager* GetMozIccManager(ErrorResult& aRv);
#endif // MOZ_B2G_RIL
#ifdef MOZ_GAMEPAD
void GetGamepads(nsTArray<nsRefPtr<Gamepad> >& aGamepads, ErrorResult& aRv);
#endif // MOZ_GAMEPAD
#ifdef MOZ_B2G_BT
nsIDOMBluetoothManager* GetMozBluetooth(ErrorResult& aRv);
#endif // MOZ_B2G_BT
#ifdef MOZ_TIME_MANAGER
time::TimeManager* GetMozTime(ErrorResult& aRv);
#endif // MOZ_TIME_MANAGER
#ifdef MOZ_AUDIO_CHANNEL_MANAGER
system::AudioChannelManager* GetMozAudioChannelManager(ErrorResult& aRv);
#endif // MOZ_AUDIO_CHANNEL_MANAGER
// WebIDL helper methods
@ -342,6 +357,13 @@ public:
static bool HasIccManagerSupport(JSContext* /* unused */,
JSObject* aGlobal);
#endif // MOZ_B2G_RIL
#ifdef MOZ_B2G_BT
static bool HasBluetoothSupport(JSContext* /* unused */, JSObject* aGlobal);
#endif // MOZ_B2G_BT
#ifdef MOZ_TIME_MANAGER
static bool HasTimeSupport(JSContext* /* unused */, JSObject* aGlobal);
#endif // MOZ_TIME_MANAGER
nsPIDOMWindow* GetParentObject() const
{
return GetWindow();

View File

@ -1706,6 +1706,7 @@ addExternalIface('imgIRequest', nativeType='imgIRequest', notflattened=True)
addExternalIface('LockedFile')
addExternalIface('MediaList')
addExternalIface('MenuBuilder', nativeType='nsIMenuBuilder', notflattened=True)
addExternalIface('MozBluetoothManager', nativeType='nsIDOMBluetoothManager')
addExternalIface('MozBoxObject', nativeType='nsIBoxObject')
addExternalIface('MozCellBroadcast')
addExternalIface('MozConnection', headerFile='nsIDOMConnection.h')

View File

@ -168,30 +168,23 @@ BluetoothManager::Create(nsPIDOMWindow* aWindow)
return manager.forget();
}
nsresult
NS_NewBluetoothManager(nsPIDOMWindow* aWindow,
nsIDOMBluetoothManager** aBluetoothManager)
// static
bool
BluetoothManager::CheckPermission(nsPIDOMWindow* aWindow)
{
NS_ASSERTION(aWindow, "Null pointer!");
nsCOMPtr<nsIPermissionManager> permMgr =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
NS_ENSURE_TRUE(permMgr, NS_ERROR_UNEXPECTED);
NS_ENSURE_TRUE(permMgr, false);
uint32_t permission;
nsresult rv =
permMgr->TestPermissionFromWindow(aWindow, "bluetooth",
&permission);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_SUCCESS(rv, false);
nsRefPtr<BluetoothManager> bluetoothManager;
if (permission == nsIPermissionManager::ALLOW_ACTION) {
bluetoothManager = BluetoothManager::Create(aWindow);
}
bluetoothManager.forget(aBluetoothManager);
return NS_OK;
return permission == nsIPermissionManager::ALLOW_ACTION;
}
void

View File

@ -29,8 +29,10 @@ public:
NS_REALLY_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper)
// Never returns null
static already_AddRefed<BluetoothManager>
Create(nsPIDOMWindow* aWindow);
Create(nsPIDOMWindow* aWindow);
static bool CheckPermission(nsPIDOMWindow* aWindow);
void Notify(const BluetoothSignal& aData);
virtual void SetPropertyByValue(const BluetoothNamedValue& aValue) MOZ_OVERRIDE;
private:
@ -40,7 +42,4 @@ private:
END_BLUETOOTH_NAMESPACE
nsresult NS_NewBluetoothManager(nsPIDOMWindow* aWindow,
nsIDOMBluetoothManager** aBluetoothManager);
#endif

View File

@ -10,6 +10,7 @@
* http://www.w3.org/TR/battery-status/#navigatorbattery-interface
* http://www.w3.org/TR/vibration/#vibration-interface
* http://www.w3.org/2012/sysapps/runtime/#extension-to-the-navigator-interface-1
* https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#navigator-interface-extension
*
* © Copyright 2004-2011 Apple Computer, Inc., Mozilla Foundation, and
* Opera Software ASA. You are granted a license to use, reproduce
@ -296,3 +297,36 @@ partial interface Navigator {
readonly attribute MozIccManager? mozIccManager;
};
#endif // MOZ_B2G_RIL
#ifdef MOZ_GAMEPAD
// https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#navigator-interface-extension
partial interface Navigator {
[Throws, Pref="dom.gamepad.enabled"]
sequence<Gamepad?> getGamepads();
};
#endif // MOZ_GAMEPAD
#ifdef MOZ_B2G_BT
// nsIDOMNavigatorBluetooth
interface MozBluetoothManager;
partial interface Navigator {
[Throws, Func="Navigator::HasBluetoothSupport"]
readonly attribute MozBluetoothManager mozBluetooth;
};
#endif // MOZ_B2G_BT
#ifdef MOZ_TIME_MANAGER
// nsIDOMMozNavigatorTime
partial interface Navigator {
[Throws, Func="Navigator::HasTimeSupport"]
readonly attribute MozTimeManager mozTime;
};
#endif // MOZ_TIME_MANAGER
#ifdef MOZ_AUDIO_CHANNEL_MANAGER
// nsIMozNavigatorAudioChannelManager
partial interface Navigator {
[Throws]
readonly attribute AudioChannelManager mozAudioChannelManager;
};
#endif // MOZ_AUDIO_CHANNEL_MANAGER