Bug 674725 - Part M - Receiving SMS: DOM part. r=smaug

This commit is contained in:
Mounir Lamouri 2011-11-25 01:48:57 +01:00
parent f77077899f
commit aab48992a8
8 changed files with 247 additions and 13 deletions

View File

@ -108,6 +108,10 @@ Navigator::~Navigator()
if (mBatteryManager) {
mBatteryManager->Shutdown();
}
if (mSmsManager) {
mSmsManager->Shutdown();
}
}
NS_INTERFACE_MAP_BEGIN(Navigator)
@ -150,6 +154,7 @@ Navigator::SetDocShell(nsIDocShell* aDocShell)
}
if (mSmsManager) {
mSmsManager->Shutdown();
mSmsManager = nsnull;
}
}
@ -534,6 +539,7 @@ Navigator::LoadingNewDocument()
}
if (mSmsManager) {
mSmsManager->Shutdown();
mSmsManager = nsnull;
}
}
@ -851,13 +857,27 @@ Navigator::IsSmsSupported() const
NS_IMETHODIMP
Navigator::GetMozSms(nsIDOMMozSmsManager** aSmsManager)
{
*aSmsManager = nsnull;
if (!mSmsManager) {
if (!IsSmsSupported() || !IsSmsAllowed()) {
*aSmsManager = nsnull;
return NS_OK;
}
nsCOMPtr<nsPIDOMWindow> window = do_GetInterface(mDocShell);
NS_ENSURE_TRUE(window, NS_OK);
nsCOMPtr<nsIDocument> document = do_GetInterface(mDocShell);
NS_ENSURE_TRUE(document, NS_OK);
nsIScriptGlobalObject* sgo = document->GetScopeObject();
NS_ENSURE_TRUE(sgo, NS_OK);
nsIScriptContext* scx = sgo->GetContext();
NS_ENSURE_TRUE(scx, NS_OK);
mSmsManager = new sms::SmsManager();
mSmsManager->Init(window->GetCurrentInnerWindow(), scx);
}
NS_ADDREF(*aSmsManager = mSmsManager);

View File

@ -50,7 +50,6 @@
#include "nsIDOMNavigatorBattery.h"
#include "nsIDOMNavigatorSms.h"
#include "nsAutoPtr.h"
#include "nsIDOMSmsManager.h"
class nsPluginArray;
class nsMimeTypeArray;
@ -69,6 +68,10 @@ namespace battery {
class BatteryManager;
} // namespace battery
namespace sms {
class SmsManager;
} // namespace sms
class Navigator : public nsIDOMNavigator,
public nsIDOMClientInformation,
public nsIDOMNavigatorGeolocation,
@ -114,7 +117,7 @@ private:
nsRefPtr<nsGeolocation> mGeolocation;
nsRefPtr<nsDesktopNotificationCenter> mNotification;
nsRefPtr<battery::BatteryManager> mBatteryManager;
nsCOMPtr<nsIDOMMozSmsManager> mSmsManager;
nsRefPtr<sms::SmsManager> mSmsManager;
nsIDocShell* mDocShell; // weak reference
};

View File

@ -34,11 +34,15 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
#include "nsIDOMEventTarget.idl"
[scriptable, function, uuid(473547ee-7a26-46d3-8476-f012985cd0c8)]
interface nsIDOMMozSmsManager : nsISupports
interface nsIDOMEventListener;
[scriptable, function, uuid(807d593c-09cb-4aa3-afa5-aa0a671bd0d3)]
interface nsIDOMMozSmsManager : nsIDOMEventTarget
{
unsigned short getNumberOfMessagesForText(in DOMString text);
void send(in DOMString number, in DOMString message);
attribute nsIDOMEventListener onreceived;
};

46
dom/sms/src/Constants.cpp Normal file
View File

@ -0,0 +1,46 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
namespace mozilla {
namespace dom {
namespace sms {
const char* kSmsReceivedObserverTopic = "sms-received";
} // namespace sms
} // namespace dom
} // namespace mozilla

51
dom/sms/src/Constants.h Normal file
View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozilla_dom_sms_Constants_h
#define mozilla_dom_sms_Constants_h
namespace mozilla {
namespace dom {
namespace sms {
extern const char* kSmsReceivedObserverTopic; // Defined in the .cpp.
} // namespace sms
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_sms_Constants_h

View File

@ -62,6 +62,7 @@ EXPORTS_mozilla/dom/sms = \
SmsChild.h \
SmsParent.h \
SmsServiceFactory.h \
Constants.h
$(NULL)
CPPSRCS = \
@ -72,6 +73,7 @@ CPPSRCS = \
SmsParent.cpp \
SmsMessage.cpp \
SmsEvent.cpp \
Constants.cpp \
$(NULL)
LOCAL_INCLUDES = \

View File

@ -38,6 +38,17 @@
#include "SmsManager.h"
#include "nsIDOMClassInfo.h"
#include "nsISmsService.h"
#include "nsIObserverService.h"
#include "mozilla/Services.h"
#include "Constants.h"
#include "SmsEvent.h"
#include "nsIDOMSmsMessage.h"
/**
* We have to use macros here because our leak analysis tool things we are
* leaking strings when we have |static const nsString|. Sad :(
*/
#define RECEIVED_EVENT_NAME NS_LITERAL_STRING("received")
DOMCI_DATA(MozSmsManager, mozilla::dom::sms::SmsManager)
@ -45,14 +56,54 @@ namespace mozilla {
namespace dom {
namespace sms {
NS_INTERFACE_MAP_BEGIN(SmsManager)
NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsManager)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsManager)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_CLASS(SmsManager)
NS_IMPL_ADDREF(SmsManager)
NS_IMPL_RELEASE(SmsManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SmsManager,
nsDOMEventTargetWrapperCache)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(received)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SmsManager,
nsDOMEventTargetWrapperCache)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(received)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SmsManager)
NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsManager)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozSmsManager)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsManager)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache)
NS_IMPL_ADDREF_INHERITED(SmsManager, nsDOMEventTargetWrapperCache)
NS_IMPL_RELEASE_INHERITED(SmsManager, nsDOMEventTargetWrapperCache)
void
SmsManager::Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext)
{
// Those vars come from nsDOMEventTargetHelper.
mOwner = aWindow;
mScriptContext = aScriptContext;
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
// GetObserverService() can return null is some situations like shutdown.
if (!obs) {
return;
}
obs->AddObserver(this, kSmsReceivedObserverTopic, false);
}
void
SmsManager::Shutdown()
{
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
// GetObserverService() can return null is some situations like shutdown.
if (!obs) {
return;
}
obs->RemoveObserver(this, kSmsReceivedObserverTopic);
}
NS_IMETHODIMP
SmsManager::GetNumberOfMessagesForText(const nsAString& aText, PRUint16* aResult)
@ -76,6 +127,43 @@ SmsManager::Send(const nsAString& aNumber, const nsAString& aMessage)
return NS_OK;
}
NS_IMPL_EVENT_HANDLER(SmsManager, received)
nsresult
SmsManager::DispatchTrustedSmsEventToSelf(const nsAString& aEventName, nsIDOMMozSmsMessage* aMessage)
{
nsRefPtr<nsDOMEvent> event = new SmsEvent(nsnull, nsnull);
nsresult rv = static_cast<SmsEvent*>(event.get())->Init(aEventName, false,
false, aMessage);
NS_ENSURE_SUCCESS(rv, rv);
rv = event->SetTrusted(PR_TRUE);
NS_ENSURE_SUCCESS(rv, rv);
bool dummy;
rv = DispatchEvent(event, &dummy);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
SmsManager::Observe(nsISupports* aSubject, const char* aTopic,
const PRUnichar* aData)
{
if (!strcmp(aTopic, kSmsReceivedObserverTopic)) {
nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
if (!message) {
NS_ERROR("Got a 'sms-received' topic without a valid message!");
return NS_OK;
}
DispatchTrustedSmsEventToSelf(RECEIVED_EVENT_NAME, message);
}
return NS_OK;
}
} // namespace sms
} // namespace dom
} // namespace mozilla

View File

@ -39,16 +39,36 @@
#define mozilla_dom_sms_SmsManager_h
#include "nsIDOMSmsManager.h"
#include "nsIObserver.h"
#include "nsDOMEventTargetWrapperCache.h"
class nsIDOMMozSmsMessage;
namespace mozilla {
namespace dom {
namespace sms {
class SmsManager : public nsIDOMMozSmsManager
, public nsIObserver
, public nsDOMEventTargetWrapperCache
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
NS_DECL_NSIDOMMOZSMSMANAGER
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetWrapperCache::)
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SmsManager,
nsDOMEventTargetWrapperCache)
void Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext);
void Shutdown();
private:
nsresult DispatchTrustedSmsEventToSelf(const nsAString& aEventName,
nsIDOMMozSmsMessage* aMessage);
NS_DECL_EVENT_HANDLER(received)
};
} // namespace sms