Bug 707659 - Part 1: DOM implementation, r=jlebar

This commit is contained in:
Vicamo Yang 2012-08-23 01:30:27 +08:00
parent 6091137343
commit be454322bf
5 changed files with 48 additions and 60 deletions

View File

@ -56,6 +56,7 @@
#include "nsIDOMGlobalPropertyInitializer.h"
using namespace mozilla::dom::power;
using namespace mozilla::dom::sms;
// This should not be in the namespace.
DOMCI_DATA(Navigator, mozilla::dom::Navigator)
@ -1064,61 +1065,6 @@ Navigator::RequestWakeLock(const nsAString &aTopic, nsIDOMMozWakeLock **aWakeLoc
// Navigator::nsIDOMNavigatorSms
//*****************************************************************************
bool
Navigator::IsSmsAllowed() const
{
static const bool defaultSmsPermission = false;
// First of all, the general pref has to be turned on.
if (!Preferences::GetBool("dom.sms.enabled", defaultSmsPermission)) {
return false;
}
// In addition of having 'dom.sms.enabled' set to true, we require the
// website to be whitelisted. This is a temporary 'security model'.
// 'dom.sms.whitelist' has to contain comma-separated values of URI prepath.
// For local files, "file://" must be listed.
// For data-urls: "moz-nullprincipal:".
// Chrome files also have to be whitelisted for the moment.
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mWindow));
if (!win || !win->GetDocShell()) {
return defaultSmsPermission;
}
nsCOMPtr<nsIDocument> doc = do_QueryInterface(win->GetExtantDocument());
if (!doc) {
return defaultSmsPermission;
}
nsCOMPtr<nsIURI> uri;
doc->NodePrincipal()->GetURI(getter_AddRefs(uri));
if (!uri) {
return defaultSmsPermission;
}
nsCAutoString uriPrePath;
uri->GetPrePath(uriPrePath);
const nsAdoptingString& whitelist =
Preferences::GetString("dom.sms.whitelist");
nsCharSeparatedTokenizer tokenizer(whitelist, ',',
nsCharSeparatedTokenizerTemplate<>::SEPARATOR_OPTIONAL);
while (tokenizer.hasMoreTokens()) {
const nsSubstring& whitelistItem = tokenizer.nextToken();
if (NS_ConvertUTF16toUTF8(whitelistItem).Equals(uriPrePath)) {
return true;
}
}
// The current page hasn't been whitelisted.
return false;
}
bool
Navigator::IsSmsSupported() const
{
@ -1141,15 +1087,15 @@ Navigator::GetMozSms(nsIDOMMozSmsManager** aSmsManager)
*aSmsManager = nullptr;
if (!mSmsManager) {
if (!IsSmsSupported() || !IsSmsAllowed()) {
if (!IsSmsSupported()) {
return NS_OK;
}
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
NS_ENSURE_TRUE(window && window->GetDocShell(), NS_OK);
mSmsManager = new sms::SmsManager();
mSmsManager->Init(window);
mSmsManager = SmsManager::CheckPermissionAndCreateInstance(window);
NS_ENSURE_TRUE(mSmsManager, NS_OK);
}
NS_ADDREF(*aSmsManager = mSmsManager);

View File

@ -142,7 +142,6 @@ public:
NS_DECL_NSIDOMNAVIGATORCAMERA
private:
bool IsSmsAllowed() const;
bool IsSmsSupported() const;
nsRefPtr<nsMimeTypeArray> mMimeTypes;

View File

@ -8,6 +8,7 @@
#include "nsIDOMClassInfo.h"
#include "nsISmsService.h"
#include "nsIObserverService.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "Constants.h"
#include "SmsEvent.h"
@ -18,6 +19,7 @@
#include "nsContentUtils.h"
#include "nsISmsDatabaseService.h"
#include "nsIXPConnect.h"
#include "nsIPermissionManager.h"
/**
* We have to use macros here because our leak analysis tool things we are
@ -59,6 +61,45 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(SmsManager, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(SmsManager, nsDOMEventTargetHelper)
/* static */already_AddRefed<SmsManager>
SmsManager::CheckPermissionAndCreateInstance(nsPIDOMWindow* aWindow)
{
NS_ASSERTION(aWindow, "Null pointer!");
// First of all, the general pref has to be turned on.
bool enabled = false;
Preferences::GetBool("dom.sms.enabled", &enabled);
NS_ENSURE_TRUE(enabled, nullptr);
nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
aWindow :
aWindow->GetCurrentInnerWindow();
// Need the document for security check.
nsCOMPtr<nsIDocument> document =
do_QueryInterface(innerWindow->GetExtantDocument());
NS_ENSURE_TRUE(document, nullptr);
nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
NS_ENSURE_TRUE(principal, nullptr);
nsCOMPtr<nsIPermissionManager> permMgr =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
NS_ENSURE_TRUE(permMgr, nullptr);
PRUint32 permission = nsIPermissionManager::DENY_ACTION;
permMgr->TestPermissionFromPrincipal(principal, "sms", &permission);
if (permission != nsIPermissionManager::ALLOW_ACTION) {
return nullptr;
}
nsRefPtr<SmsManager> smsMgr = new SmsManager();
smsMgr->Init(aWindow);
return smsMgr.forget();
}
void
SmsManager::Init(nsPIDOMWindow *aWindow)
{

View File

@ -30,6 +30,9 @@ public:
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SmsManager,
nsDOMEventTargetHelper)
static already_AddRefed<SmsManager>
CheckPermissionAndCreateInstance(nsPIDOMWindow *aWindow);
void Init(nsPIDOMWindow *aWindow);
void Shutdown();

View File

@ -3635,7 +3635,6 @@ pref("dom.battery.enabled", true);
// WebSMS
pref("dom.sms.enabled", false);
pref("dom.sms.whitelist", "");
// WebContacts
pref("dom.mozContacts.enabled", false);