No Bug - Cleanup style in nsGelocation.cpp because jdm told me it sucks. r=me

This commit is contained in:
Doug Turner 2012-07-26 20:33:04 -07:00
Родитель da01a118d6
Коммит 22d8b8370e
1 изменённых файлов: 146 добавлений и 104 удалений

Просмотреть файл

@ -100,10 +100,11 @@ public:
} }
NS_IMETHOD Run() { NS_IMETHOD Run() {
if (mAllow) if (mAllow) {
mRequest->Allow(); mRequest->Allow();
else } else {
mRequest->Cancel(); mRequest->Cancel();
}
return NS_OK; return NS_OK;
} }
@ -129,8 +130,9 @@ public:
NS_IMETHOD Run() { NS_IMETHOD Run() {
mRequest->SendLocation(mPosition); mRequest->SendLocation(mPosition);
if (mLocator) if (mLocator) {
mLocator->RemoveRequest(mRequest); mLocator->RemoveRequest(mRequest);
}
return NS_OK; return NS_OK;
} }
@ -189,13 +191,15 @@ nsDOMGeoPositionError::GetCode(PRInt16 *aCode)
void void
nsDOMGeoPositionError::NotifyCallback(nsIDOMGeoPositionErrorCallback* aCallback) nsDOMGeoPositionError::NotifyCallback(nsIDOMGeoPositionErrorCallback* aCallback)
{ {
if (!aCallback) if (!aCallback) {
return; return;
}
// Ensure that the proper context is on the stack (bug 452762) // Ensure that the proper context is on the stack (bug 452762)
nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1")); nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1"));
if (!stack || NS_FAILED(stack->Push(nsnull))) if (!stack || NS_FAILED(stack->Push(nsnull))) {
return; return;
}
nsAutoMicroTask mt; nsAutoMicroTask mt;
aCallback->HandleEvent(this); aCallback->HandleEvent(this);
@ -252,8 +256,9 @@ void
nsGeolocationRequest::NotifyError(PRInt16 errorCode) nsGeolocationRequest::NotifyError(PRInt16 errorCode)
{ {
nsRefPtr<nsDOMGeoPositionError> positionError = new nsDOMGeoPositionError(errorCode); nsRefPtr<nsDOMGeoPositionError> positionError = new nsDOMGeoPositionError(errorCode);
if (!positionError) if (!positionError) {
return; return;
}
positionError->NotifyCallback(mErrorCallback); positionError->NotifyCallback(mErrorCallback);
} }
@ -337,8 +342,9 @@ nsGeolocationRequest::Allow()
nsCOMPtr<nsIDOMGeoPosition> lastPosition = geoService->GetCachedPosition(); nsCOMPtr<nsIDOMGeoPosition> lastPosition = geoService->GetCachedPosition();
DOMTimeStamp cachedPositionTime; DOMTimeStamp cachedPositionTime;
if (lastPosition) if (lastPosition) {
lastPosition->GetTimestamp(&cachedPositionTime); lastPosition->GetTimestamp(&cachedPositionTime);
}
// check to see if we can use a cached value // check to see if we can use a cached value
// //
@ -362,10 +368,10 @@ nsGeolocationRequest::Allow()
PRTime(cachedPositionTime) )) { PRTime(cachedPositionTime) )) {
// okay, we can return a cached position // okay, we can return a cached position
mAllowed = true; mAllowed = true;
nsCOMPtr<nsIRunnable> ev = nsCOMPtr<nsIRunnable> ev = new RequestSendLocationEvent(lastPosition,
new RequestSendLocationEvent(lastPosition, this, this,
mIsWatchPositionRequest ? nsnull : mLocator); mIsWatchPositionRequest ? nsnull : mLocator);
NS_DispatchToMainThread(ev); NS_DispatchToMainThread(ev);
} }
@ -382,13 +388,15 @@ nsGeolocationRequest::SetTimeoutTimer()
mTimeoutTimer->Cancel(); mTimeoutTimer->Cancel();
mTimeoutTimer = nsnull; mTimeoutTimer = nsnull;
} }
PRInt32 timeout; PRInt32 timeout;
if (mOptions && (timeout = mOptions->timeout) != 0) { if (mOptions && (timeout = mOptions->timeout) != 0) {
if (timeout < 0) if (timeout < 0) {
timeout = 0; timeout = 0;
else if (timeout < 10) } else if (timeout < 10) {
timeout = 10; timeout = 10;
}
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1"); mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT); mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
@ -408,8 +416,9 @@ nsGeolocationRequest::MarkCleared()
void void
nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition) nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
{ {
if (mCleared || !mAllowed) if (mCleared || !mAllowed) {
return; return;
}
if (mTimeoutTimer) { if (mTimeoutTimer) {
mTimeoutTimer->Cancel(); mTimeoutTimer->Cancel();
@ -424,8 +433,9 @@ nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
// Ensure that the proper context is on the stack (bug 452762) // Ensure that the proper context is on the stack (bug 452762)
nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1")); nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1"));
if (!stack || NS_FAILED(stack->Push(nsnull))) if (!stack || NS_FAILED(stack->Push(nsnull))) {
return; // silently fail return; // silently fail
}
nsAutoMicroTask mt; nsAutoMicroTask mt;
mCallback->HandleEvent(aPosition); mCallback->HandleEvent(aPosition);
@ -434,18 +444,21 @@ nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
JSContext* cx; JSContext* cx;
stack->Pop(&cx); stack->Pop(&cx);
if (mIsWatchPositionRequest) if (mIsWatchPositionRequest) {
SetTimeoutTimer(); SetTimeoutTimer();
}
} }
bool bool
nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition) nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition)
{ {
if (!mAllowed) if (!mAllowed) {
return false; return false;
nsCOMPtr<nsIRunnable> ev = }
new RequestSendLocationEvent(aPosition, this,
mIsWatchPositionRequest ? nsnull : mLocator); nsCOMPtr<nsIRunnable> ev = new RequestSendLocationEvent(aPosition,
this,
mIsWatchPositionRequest ? nsnull : mLocator);
NS_DispatchToMainThread(ev); NS_DispatchToMainThread(ev);
return true; return true;
} }
@ -471,10 +484,11 @@ nsGeolocationRequest::Shutdown()
bool nsGeolocationRequest::Recv__delete__(const bool& allow) bool nsGeolocationRequest::Recv__delete__(const bool& allow)
{ {
if (allow) if (allow) {
(void) Allow(); (void) Allow();
else } else {
(void) Cancel(); (void) Cancel();
}
return true; return true;
} }
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
@ -500,22 +514,26 @@ nsresult nsGeolocationService::Init()
Preferences::AddBoolVarCache(&sGeoEnabled, "geo.enabled", sGeoEnabled); Preferences::AddBoolVarCache(&sGeoEnabled, "geo.enabled", sGeoEnabled);
Preferences::AddBoolVarCache(&sGeoIgnoreLocationFilter, "geo.ignore.location_filter", sGeoIgnoreLocationFilter); Preferences::AddBoolVarCache(&sGeoIgnoreLocationFilter, "geo.ignore.location_filter", sGeoIgnoreLocationFilter);
if (!sGeoEnabled) if (!sGeoEnabled) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIGeolocationProvider> provider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID); nsCOMPtr<nsIGeolocationProvider> provider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
// look up any providers that were registered via the category manager // look up any providers that were registered via the category manager
nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1")); nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
if (!catMan) if (!catMan) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
// geolocation service can be enabled -> now register observer // geolocation service can be enabled -> now register observer
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (!obs) if (!obs) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
obs->AddObserver(this, "quit-application", false); obs->AddObserver(this, "quit-application", false);
obs->AddObserver(this, "mozsettings-changed", false); obs->AddObserver(this, "mozsettings-changed", false);
@ -538,8 +556,9 @@ nsresult nsGeolocationService::Init()
catMan->GetCategoryEntry("geolocation-provider", name.get(), getter_Copies(spec)); catMan->GetCategoryEntry("geolocation-provider", name.get(), getter_Copies(spec));
provider = do_GetService(spec); provider = do_GetService(spec);
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
} }
} }
@ -547,26 +566,30 @@ nsresult nsGeolocationService::Init()
#ifdef MOZ_MAEMO_LIBLOCATION #ifdef MOZ_MAEMO_LIBLOCATION
provider = new MaemoLocationProvider(); provider = new MaemoLocationProvider();
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
#endif #endif
#ifdef MOZ_ENABLE_QTMOBILITY #ifdef MOZ_ENABLE_QTMOBILITY
provider = new QTMLocationProvider(); provider = new QTMLocationProvider();
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
#endif #endif
#ifdef MOZ_WIDGET_ANDROID #ifdef MOZ_WIDGET_ANDROID
provider = new AndroidLocationProvider(); provider = new AndroidLocationProvider();
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
#endif #endif
#ifdef MOZ_WIDGET_GONK #ifdef MOZ_WIDGET_GONK
provider = GonkGPSGeolocationProvider::GetSingleton(); provider = GonkGPSGeolocationProvider::GetSingleton();
if (provider) if (provider) {
mProviders.AppendObject(provider); mProviders.AppendObject(provider);
}
#endif #endif
return NS_OK; return NS_OK;
@ -614,19 +637,16 @@ nsGeolocationService::HandleMozsettingChanged(const PRUnichar* aData)
return; return;
} }
if (value.toBoolean() == false) if (value.toBoolean() == false) {
{
// turn things off // turn things off
for (PRUint32 i = 0; i< mGeolocators.Length(); i++) for (PRUint32 i = 0; i< mGeolocators.Length(); i++) {
{ mGeolocators[i]->Shutdown();
mGeolocators[i]->Shutdown();
} }
StopDevice(); StopDevice();
Update(nsnull); Update(nsnull);
mLastPosition = nsnull; mLastPosition = nsnull;
sGeoEnabled = false; sGeoEnabled = false;
} } else {
else {
sGeoEnabled = true; sGeoEnabled = true;
} }
} }
@ -636,34 +656,30 @@ nsGeolocationService::Observe(nsISupports* aSubject,
const char* aTopic, const char* aTopic,
const PRUnichar* aData) const PRUnichar* aData)
{ {
if (!strcmp("quit-application", aTopic)) if (!strcmp("quit-application", aTopic)) {
{
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) { if (obs) {
obs->RemoveObserver(this, "quit-application"); obs->RemoveObserver(this, "quit-application");
obs->RemoveObserver(this, "mozsettings-changed"); obs->RemoveObserver(this, "mozsettings-changed");
} }
for (PRUint32 i = 0; i< mGeolocators.Length(); i++) for (PRUint32 i = 0; i< mGeolocators.Length(); i++) {
mGeolocators[i]->Shutdown(); mGeolocators[i]->Shutdown();
}
StopDevice(); StopDevice();
return NS_OK; return NS_OK;
} }
if (!strcmp("mozsettings-changed", aTopic)) if (!strcmp("mozsettings-changed", aTopic)) {
{
HandleMozsettingChanged(aData); HandleMozsettingChanged(aData);
return NS_OK; return NS_OK;
} }
if (!strcmp("timer-callback", aTopic)) if (!strcmp("timer-callback", aTopic)) {
{
// decide if we can close down the service. // decide if we can close down the service.
for (PRUint32 i = 0; i< mGeolocators.Length(); i++) for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
if (mGeolocators[i]->HasActiveCallbacks()) if (mGeolocators[i]->HasActiveCallbacks()) {
{
SetDisconnectTimer(); SetDisconnectTimer();
return NS_OK; return NS_OK;
} }
@ -682,8 +698,9 @@ nsGeolocationService::Update(nsIDOMGeoPosition *aSomewhere)
{ {
SetCachedPosition(aSomewhere); SetCachedPosition(aSomewhere);
for (PRUint32 i = 0; i< mGeolocators.Length(); i++) for (PRUint32 i = 0; i< mGeolocators.Length(); i++) {
mGeolocators[i]->Update(aSomewhere); mGeolocators[i]->Update(aSomewhere);
}
return NS_OK; return NS_OK;
} }
@ -702,8 +719,9 @@ nsGeolocationService::GetCachedPosition()
nsresult nsresult
nsGeolocationService::StartDevice() nsGeolocationService::StartDevice()
{ {
if (!sGeoEnabled) if (!sGeoEnabled) {
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
}
// we do not want to keep the geolocation devices online // we do not want to keep the geolocation devices online
// indefinitely. Close them down after a reasonable period of // indefinitely. Close them down after a reasonable period of
@ -718,8 +736,9 @@ nsGeolocationService::StartDevice()
// Start them up! // Start them up!
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (!obs) if (!obs) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
for (PRInt32 i = 0; i < mProviders.Count(); i++) { for (PRInt32 i = 0; i < mProviders.Count(); i++) {
mProviders[i]->Startup(); mProviders[i]->Startup();
@ -735,10 +754,11 @@ nsGeolocationService::StartDevice()
void void
nsGeolocationService::SetDisconnectTimer() nsGeolocationService::SetDisconnectTimer()
{ {
if (!mDisconnectTimer) if (!mDisconnectTimer) {
mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1"); mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1");
else } else {
mDisconnectTimer->Cancel(); mDisconnectTimer->Cancel();
}
mDisconnectTimer->Init(this, mDisconnectTimer->Init(this,
sProviderTimeout, sProviderTimeout,
@ -748,19 +768,19 @@ nsGeolocationService::SetDisconnectTimer()
void void
nsGeolocationService::SetHigherAccuracy(bool aEnable) nsGeolocationService::SetHigherAccuracy(bool aEnable)
{ {
if (!mHigherAccuracy && aEnable) { if (!mHigherAccuracy && aEnable) {
for (PRInt32 i = 0; i < mProviders.Count(); i++) { for (PRInt32 i = 0; i < mProviders.Count(); i++) {
mProviders[i]->SetHighAccuracy(true); mProviders[i]->SetHighAccuracy(true);
}
} }
}
if (mHigherAccuracy && !aEnable) {
for (PRInt32 i = 0; i < mProviders.Count(); i++) { if (mHigherAccuracy && !aEnable) {
mProviders[i]->SetHighAccuracy(false); for (PRInt32 i = 0; i < mProviders.Count(); i++) {
} mProviders[i]->SetHighAccuracy(false);
} }
}
mHigherAccuracy = aEnable; mHigherAccuracy = aEnable;
} }
void void
@ -778,8 +798,9 @@ nsGeolocationService::StopDevice()
} }
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (!obs) if (!obs) {
return; return;
}
for (PRInt32 i = 0; i < mProviders.Count(); i++) { for (PRInt32 i = 0; i < mProviders.Count(); i++) {
mProviders[i]->Shutdown(); mProviders[i]->Shutdown();
@ -864,8 +885,9 @@ nsGeolocation::nsGeolocation()
nsGeolocation::~nsGeolocation() nsGeolocation::~nsGeolocation()
{ {
if (mService) if (mService) {
Shutdown(); Shutdown();
}
} }
nsresult nsresult
@ -874,33 +896,37 @@ nsGeolocation::Init(nsIDOMWindow* aContentDom)
// Remember the window // Remember the window
if (aContentDom) { if (aContentDom) {
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContentDom); nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContentDom);
if (!window) if (!window) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
mOwner = do_GetWeakReference(window->GetCurrentInnerWindow()); mOwner = do_GetWeakReference(window->GetCurrentInnerWindow());
if (!mOwner) if (!mOwner) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
// Grab the uri of the document // Grab the uri of the document
nsCOMPtr<nsIDOMDocument> domdoc; nsCOMPtr<nsIDOMDocument> domdoc;
aContentDom->GetDocument(getter_AddRefs(domdoc)); aContentDom->GetDocument(getter_AddRefs(domdoc));
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc); nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
if (!doc) if (!doc) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
doc->NodePrincipal()->GetURI(getter_AddRefs(mURI)); doc->NodePrincipal()->GetURI(getter_AddRefs(mURI));
if (!mURI) if (!mURI) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
} }
// If no aContentDom was passed into us, we are being used // If no aContentDom was passed into us, we are being used
// by chrome/c++ and have no mOwner, no mURI, and no need // by chrome/c++ and have no mOwner, no mURI, and no need
// to prompt. // to prompt.
mService = nsGeolocationService::GetInstance(); mService = nsGeolocationService::GetInstance();
if (mService) if (mService) {
mService->AddLocator(this); mService->AddLocator(this);
}
return NS_OK; return NS_OK;
} }
@ -916,8 +942,9 @@ nsGeolocation::Shutdown()
mWatchingCallbacks[i]->Shutdown(); mWatchingCallbacks[i]->Shutdown();
mWatchingCallbacks.Clear(); mWatchingCallbacks.Clear();
if (mService) if (mService) {
mService->RemoveLocator(this); mService->RemoveLocator(this);
}
mService = nsnull; mService = nsnull;
mURI = nsnull; mURI = nsnull;
@ -926,9 +953,11 @@ nsGeolocation::Shutdown()
bool bool
nsGeolocation::HasActiveCallbacks() nsGeolocation::HasActiveCallbacks()
{ {
for (PRUint32 i = 0; i < mWatchingCallbacks.Length(); i++) for (PRUint32 i = 0; i < mWatchingCallbacks.Length(); i++) {
if (mWatchingCallbacks[i]->IsActive()) if (mWatchingCallbacks[i]->IsActive()) {
return true; return true;
}
}
return mPendingCallbacks.Length() != 0; return mPendingCallbacks.Length() != 0;
} }
@ -950,12 +979,14 @@ nsGeolocation::RemoveRequest(nsGeolocationRequest* aRequest)
void void
nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere) nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere)
{ {
if (!WindowOwnerStillExists()) if (!WindowOwnerStillExists()) {
return Shutdown(); return Shutdown();
}
for (PRUint32 i = mPendingCallbacks.Length(); i> 0; i--) { for (PRUint32 i = mPendingCallbacks.Length(); i> 0; i--) {
if (mPendingCallbacks[i-1]->Update(aSomewhere)) if (mPendingCallbacks[i-1]->Update(aSomewhere)) {
mPendingCallbacks.RemoveElementAt(i-1); mPendingCallbacks.RemoveElementAt(i-1);
}
} }
// notify everyone that is watching // notify everyone that is watching
@ -972,15 +1003,17 @@ nsGeolocation::GetCurrentPosition(nsIDOMGeoPositionCallback *callback,
{ {
NS_ENSURE_ARG_POINTER(callback); NS_ENSURE_ARG_POINTER(callback);
if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) {
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
}
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
callback, callback,
errorCallback, errorCallback,
false); false);
if (!request) if (!request) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = request->Init(cx, options); nsresult rv = request->Init(cx, options);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
@ -992,15 +1025,17 @@ nsGeolocation::GetCurrentPosition(nsIDOMGeoPositionCallback *callback,
} }
if (mOwner) { if (mOwner) {
if (!RegisterRequestWithPrompt(request)) if (!RegisterRequestWithPrompt(request)) {
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
}
mPendingCallbacks.AppendElement(request); mPendingCallbacks.AppendElement(request);
return NS_OK; return NS_OK;
} }
if (!nsContentUtils::IsCallerChrome()) if (!nsContentUtils::IsCallerChrome()) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
mPendingCallbacks.AppendElement(request); mPendingCallbacks.AppendElement(request);
@ -1020,15 +1055,17 @@ nsGeolocation::WatchPosition(nsIDOMGeoPositionCallback *callback,
NS_ENSURE_ARG_POINTER(callback); NS_ENSURE_ARG_POINTER(callback);
if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) {
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
}
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
callback, callback,
errorCallback, errorCallback,
true); true);
if (!request) if (!request) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = request->Init(cx, options); nsresult rv = request->Init(cx, options);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
@ -1044,7 +1081,7 @@ nsGeolocation::WatchPosition(nsIDOMGeoPositionCallback *callback,
return NS_OK; return NS_OK;
} }
if (mOwner) { if (mOwner) {
if (!RegisterRequestWithPrompt(request)) if (!RegisterRequestWithPrompt(request))
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
@ -1055,10 +1092,11 @@ nsGeolocation::WatchPosition(nsIDOMGeoPositionCallback *callback,
return NS_OK; return NS_OK;
} }
if (!nsContentUtils::IsCallerChrome()) if (!nsContentUtils::IsCallerChrome()) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
request->Allow(); request->Allow();
// need to hand back an index/reference. // need to hand back an index/reference.
mWatchingCallbacks.AppendElement(request); mWatchingCallbacks.AppendElement(request);
@ -1071,8 +1109,9 @@ NS_IMETHODIMP
nsGeolocation::ClearWatch(PRInt32 aWatchId) nsGeolocation::ClearWatch(PRInt32 aWatchId)
{ {
PRUint32 count = mWatchingCallbacks.Length(); PRUint32 count = mWatchingCallbacks.Length();
if (aWatchId < 0 || count == 0 || PRUint32(aWatchId) >= count) if (aWatchId < 0 || count == 0 || PRUint32(aWatchId) >= count) {
return NS_OK; return NS_OK;
}
mWatchingCallbacks[aWatchId]->MarkCleared(); mWatchingCallbacks[aWatchId]->MarkCleared();
return NS_OK; return NS_OK;
@ -1084,21 +1123,23 @@ nsGeolocation::WindowOwnerStillExists()
// an owner was never set when nsGeolocation // an owner was never set when nsGeolocation
// was created, which means that this object // was created, which means that this object
// is being used without a window. // is being used without a window.
if (mOwner == nsnull) if (mOwner == nsnull) {
return true; return true;
}
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner); nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
if (window) if (window) {
{
bool closed = false; bool closed = false;
window->GetClosed(&closed); window->GetClosed(&closed);
if (closed) if (closed) {
return false; return false;
}
nsPIDOMWindow* outer = window->GetOuterWindow(); nsPIDOMWindow* outer = window->GetOuterWindow();
if (!outer || outer->GetCurrentInnerWindow() != window) if (!outer || outer->GetCurrentInnerWindow() != window) {
return false; return false;
}
} }
return true; return true;
@ -1108,24 +1149,26 @@ bool
nsGeolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request) nsGeolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request)
{ {
if (Preferences::GetBool("geo.prompt.testing", false)) { if (Preferences::GetBool("geo.prompt.testing", false)) {
nsCOMPtr<nsIRunnable> ev = bool allow = Preferences::GetBool("geo.prompt.testing.allow", false);
new RequestAllowEvent(Preferences::GetBool("geo.prompt.testing.allow", nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(allow,
false), request); request);
NS_DispatchToMainThread(ev); NS_DispatchToMainThread(ev);
return true; return true;
} }
if (XRE_GetProcessType() == GeckoProcessType_Content) { if (XRE_GetProcessType() == GeckoProcessType_Content) {
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner); nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
if (!window) if (!window) {
return true; return true;
}
// because owner implements nsITabChild, we can assume that it is // because owner implements nsITabChild, we can assume that it is
// the one and only TabChild. // the one and only TabChild.
TabChild* child = GetTabChildFrom(window->GetDocShell()); TabChild* child = GetTabChildFrom(window->GetDocShell());
if (!child) if (!child) {
return false; return false;
}
// Retain a reference so the object isn't deleted without IPDL's knowledge. // Retain a reference so the object isn't deleted without IPDL's knowledge.
// Corresponding release occurs in DeallocPContentPermissionRequest. // Corresponding release occurs in DeallocPContentPermissionRequest.
request->AddRef(); request->AddRef();
@ -1141,4 +1184,3 @@ nsGeolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request)
NS_DispatchToMainThread(ev); NS_DispatchToMainThread(ev);
return true; return true;
} }