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