gecko-dev/dom/voicemail/Voicemail.cpp

261 lines
6.5 KiB
C++
Raw Normal View History

/* -*- 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/. */
#include "mozilla/dom/Voicemail.h"
#include "mozilla/dom/MozVoicemailBinding.h"
#include "mozilla/dom/MozVoicemailEvent.h"
#include "mozilla/dom/MozVoicemailStatusBinding.h"
2013-10-29 01:58:12 +00:00
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "nsContentUtils.h"
#include "nsServiceManagerUtils.h"
// Service instantiation
#include "ipc/VoicemailIPCService.h"
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
#include "nsIGonkVoicemailService.h"
#endif
#include "nsXULAppAPI.h" // For XRE_GetProcessType()
using namespace mozilla::dom;
using mozilla::ErrorResult;
class Voicemail::Listener final : public nsIVoicemailListener
{
Voicemail* mVoicemail;
public:
NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSIVOICEMAILLISTENER(mVoicemail)
explicit Listener(Voicemail* aVoicemail)
: mVoicemail(aVoicemail)
{
MOZ_ASSERT(mVoicemail);
}
void Disconnect()
{
MOZ_ASSERT(mVoicemail);
mVoicemail = nullptr;
}
private:
~Listener()
{
MOZ_ASSERT(!mVoicemail);
}
};
NS_IMPL_ISUPPORTS(Voicemail::Listener, nsIVoicemailListener)
NS_IMPL_CYCLE_COLLECTION_INHERITED(Voicemail, DOMEventTargetHelper,
mStatuses)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Voicemail)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(Voicemail, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(Voicemail, DOMEventTargetHelper)
/* static */ already_AddRefed<Voicemail>
Voicemail::Create(nsPIDOMWindowInner* aWindow,
ErrorResult& aRv)
{
nsCOMPtr<nsIVoicemailService> service =
do_GetService(NS_VOICEMAIL_SERVICE_CONTRACTID);
if (!service) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
RefPtr<Voicemail> voicemail = new Voicemail(aWindow, service);
return voicemail.forget();
}
Voicemail::Voicemail(nsPIDOMWindowInner* aWindow,
nsIVoicemailService* aService)
: DOMEventTargetHelper(aWindow)
, mService(aService)
{
MOZ_ASSERT(mService);
mListener = new Listener(this);
DebugOnly<nsresult> rv = mService->RegisterListener(mListener);
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed registering voicemail messages with service");
uint32_t length = 0;
if (NS_SUCCEEDED(mService->GetNumItems(&length)) && length != 0) {
mStatuses.SetLength(length);
}
}
Voicemail::~Voicemail()
{
MOZ_ASSERT(!mService && !mListener);
}
void
Voicemail::Shutdown()
{
mListener->Disconnect();
mService->UnregisterListener(mListener);
mListener = nullptr;
mService = nullptr;
mStatuses.Clear();
}
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 14:13:33 +00:00
Voicemail::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
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 14:13:33 +00:00
return MozVoicemailBinding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<nsIVoicemailProvider>
Voicemail::GetItemByServiceId(const Optional<uint32_t>& aOptionalServiceId,
uint32_t& aActualServiceId) const
2013-10-29 01:58:12 +00:00
{
if (!mService) {
return nullptr;
}
2013-10-29 01:58:12 +00:00
nsCOMPtr<nsIVoicemailProvider> provider;
if (aOptionalServiceId.WasPassed()) {
aActualServiceId = aOptionalServiceId.Value();
mService->GetItemByServiceId(aActualServiceId,
getter_AddRefs(provider));
} else {
mService->GetDefaultItem(getter_AddRefs(provider));
if (provider) {
NS_ENSURE_SUCCESS(provider->GetServiceId(&aActualServiceId), nullptr);
}
}
// For all retrieved providers, they should have service id
// < mStatuses.Length().
MOZ_ASSERT(!provider || aActualServiceId < mStatuses.Length());
return provider.forget();
2013-10-29 01:58:12 +00:00
}
already_AddRefed<VoicemailStatus>
Voicemail::GetOrCreateStatus(uint32_t aServiceId,
nsIVoicemailProvider* aProvider)
2013-10-29 01:58:12 +00:00
{
MOZ_ASSERT(aServiceId < mStatuses.Length());
MOZ_ASSERT(aProvider);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<VoicemailStatus> res = mStatuses[aServiceId];
if (!res) {
mStatuses[aServiceId] = res = new VoicemailStatus(GetOwner(), aProvider);
2013-10-29 01:58:12 +00:00
}
return res.forget();
2013-10-29 01:58:12 +00:00
}
// MozVoicemail WebIDL
already_AddRefed<VoicemailStatus>
2013-10-29 01:58:12 +00:00
Voicemail::GetStatus(const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t actualServiceId = 0;
nsCOMPtr<nsIVoicemailProvider> provider =
GetItemByServiceId(aServiceId, actualServiceId);
if (!provider) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
return GetOrCreateStatus(actualServiceId, provider);
}
void
Voicemail::GetNumber(const Optional<uint32_t>& aServiceId,
nsString& aNumber,
2013-10-29 01:58:12 +00:00
ErrorResult& aRv) const
{
aNumber.SetIsVoid(true);
uint32_t unused = 0;
nsCOMPtr<nsIVoicemailProvider> provider =
GetItemByServiceId(aServiceId, unused);
if (!provider) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
aRv = provider->GetNumber(aNumber);
}
void
Voicemail::GetDisplayName(const Optional<uint32_t>& aServiceId,
nsString& aDisplayName,
2013-10-29 01:58:12 +00:00
ErrorResult& aRv) const
{
aDisplayName.SetIsVoid(true);
uint32_t unused = 0;
nsCOMPtr<nsIVoicemailProvider> provider =
GetItemByServiceId(aServiceId, unused);
if (!provider) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
aRv = provider->GetDisplayName(aDisplayName);
}
// nsIVoicemailListener
NS_IMETHODIMP
Voicemail::NotifyInfoChanged(nsIVoicemailProvider* aProvider)
{
// Ignored.
return NS_OK;
}
NS_IMETHODIMP
Voicemail::NotifyStatusChanged(nsIVoicemailProvider* aProvider)
{
NS_ENSURE_ARG_POINTER(aProvider);
uint32_t serviceId = 0;
if (NS_FAILED(aProvider->GetServiceId(&serviceId))) {
return NS_ERROR_UNEXPECTED;
}
MozVoicemailEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mStatus = GetOrCreateStatus(serviceId, aProvider);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<MozVoicemailEvent> event =
MozVoicemailEvent::Constructor(this, NS_LITERAL_STRING("statuschanged"), init);
return DispatchTrustedEvent(event);
}
already_AddRefed<nsIVoicemailService>
NS_CreateVoicemailService()
{
nsCOMPtr<nsIVoicemailService> service;
if (XRE_IsContentProcess()) {
service = new mozilla::dom::voicemail::VoicemailIPCService();
} else {
#if defined(MOZ_B2G_RIL)
#if defined(MOZ_WIDGET_GONK)
service = do_GetService(GONK_VOICEMAIL_SERVICE_CONTRACTID);
#endif // MOZ_WIDGET_GONK
#endif // MOZ_B2G_RIL
}
return service.forget();
}