2008-07-15 16:37:48 -07:00
|
|
|
/* ***** 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 Geolocation.
|
|
|
|
*
|
2009-12-21 13:50:30 -08:00
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation
|
2008-07-15 16:37:48 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@meer.net> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either 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 ***** */
|
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#ifndef nsGeoLocation_h
|
|
|
|
#define nsGeoLocation_h
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
#ifdef MOZ_IPC
|
2010-09-09 22:00:08 -07:00
|
|
|
#include "mozilla/dom/PContentPermissionRequestChild.h"
|
2010-05-14 17:45:53 -07:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
2010-05-13 10:44:51 -07:00
|
|
|
#endif
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
#include "nsWeakPtr.h"
|
2009-01-14 17:10:46 -08:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
#include "nsIDOMGeoGeolocation.h"
|
|
|
|
#include "nsIDOMGeoPosition.h"
|
|
|
|
#include "nsIDOMGeoPositionError.h"
|
|
|
|
#include "nsIDOMGeoPositionCallback.h"
|
|
|
|
#include "nsIDOMGeoPositionErrorCallback.h"
|
|
|
|
#include "nsIDOMGeoPositionOptions.h"
|
|
|
|
#include "nsIDOMNavigatorGeolocation.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
#include "nsIGeolocationProvider.h"
|
2010-09-09 21:59:51 -07:00
|
|
|
#include "nsIContentPermissionPrompt.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-09-09 22:00:08 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
#include "PCOMContentPermissionRequestChild.h"
|
|
|
|
#endif
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
class nsGeolocationService;
|
|
|
|
class nsGeolocation;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
class nsGeolocationRequest
|
2010-09-09 21:59:51 -07:00
|
|
|
: public nsIContentPermissionRequest
|
2010-05-13 10:44:51 -07:00
|
|
|
, public nsITimerCallback
|
|
|
|
#ifdef MOZ_IPC
|
2010-09-09 22:00:08 -07:00
|
|
|
, public PCOMContentPermissionRequestChild
|
2010-05-13 10:44:51 -07:00
|
|
|
#endif
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_DECL_NSICONTENTPERMISSIONREQUEST
|
2008-10-20 12:37:10 -07:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2009-01-14 17:10:46 -08:00
|
|
|
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsGeolocationRequest, nsIContentPermissionRequest)
|
2009-01-14 17:10:46 -08:00
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationRequest(nsGeolocation* locator,
|
|
|
|
nsIDOMGeoPositionCallback* callback,
|
2008-10-20 12:37:10 -07:00
|
|
|
nsIDOMGeoPositionErrorCallback* errorCallback,
|
|
|
|
nsIDOMGeoPositionOptions* options);
|
2008-11-12 07:59:38 -08:00
|
|
|
nsresult Init();
|
2008-07-15 16:37:48 -07:00
|
|
|
void Shutdown();
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
void SendLocation(nsIDOMGeoPosition* location);
|
2008-07-15 16:37:48 -07:00
|
|
|
void MarkCleared();
|
2010-09-27 14:23:35 -07:00
|
|
|
PRBool IsActive() {return !mCleared;}
|
2008-07-15 16:37:48 -07:00
|
|
|
PRBool Allowed() {return mAllowed;}
|
2010-08-31 09:15:52 -07:00
|
|
|
void SetTimeoutTimer();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
~nsGeolocationRequest();
|
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
bool Recv__delete__(const bool& allow);
|
2010-09-09 22:00:08 -07:00
|
|
|
void IPDLRelease() { Release(); }
|
2010-05-13 10:44:51 -07:00
|
|
|
#endif
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
private:
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
void NotifyError(PRInt16 errorCode);
|
|
|
|
PRPackedBool mAllowed;
|
|
|
|
PRPackedBool mCleared;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimeoutTimer;
|
2008-08-14 09:35:49 -07:00
|
|
|
nsCOMPtr<nsIDOMGeoPositionCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIDOMGeoPositionErrorCallback> mErrorCallback;
|
2008-10-20 12:37:10 -07:00
|
|
|
nsCOMPtr<nsIDOMGeoPositionOptions> mOptions;
|
|
|
|
|
2009-06-11 17:46:46 -07:00
|
|
|
nsRefPtr<nsGeolocation> mLocator;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton that manages the geolocation provider
|
|
|
|
*/
|
2008-10-23 11:01:32 -07:00
|
|
|
class nsGeolocationService : public nsIGeolocationUpdate, public nsIObserver
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
static nsGeolocationService* GetGeolocationService();
|
|
|
|
static nsGeolocationService* GetInstance(); // Non-Addref'ing
|
|
|
|
static nsGeolocationService* gService;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIGEOLOCATIONUPDATE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2009-08-28 10:40:27 -07:00
|
|
|
nsGeolocationService() {mTimeout = 6000;};
|
|
|
|
|
|
|
|
nsresult Init();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
// Management of the nsGeolocation objects
|
|
|
|
void AddLocator(nsGeolocation* locator);
|
|
|
|
void RemoveLocator(nsGeolocation* locator);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
PRBool IsBetterPosition(nsIDOMGeoPosition* aPosition);
|
|
|
|
|
2008-12-05 08:29:43 -08:00
|
|
|
void SetCachedPosition(nsIDOMGeoPosition* aPosition);
|
|
|
|
nsIDOMGeoPosition* GetCachedPosition();
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Find and startup a geolocation device (gps, nmea, etc.)
|
|
|
|
nsresult StartDevice();
|
|
|
|
|
|
|
|
// Stop the started geolocation device (gps, nmea, etc.)
|
|
|
|
void StopDevice();
|
|
|
|
|
|
|
|
// create, or reinitalize the callback timer
|
|
|
|
void SetDisconnectTimer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
~nsGeolocationService();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Disconnect timer. When this timer expires, it clears all pending callbacks
|
|
|
|
// and closes down the provider, unless we are watching a point, and in that
|
|
|
|
// case, we disable the disconnect timer.
|
|
|
|
nsCOMPtr<nsITimer> mDisconnectTimer;
|
|
|
|
|
|
|
|
// Time, in milliseconds, to wait for the location provider to spin up.
|
|
|
|
PRInt32 mTimeout;
|
|
|
|
|
|
|
|
// The object providing geo location information to us.
|
2009-07-10 17:04:39 -07:00
|
|
|
nsCOMArray<nsIGeolocationProvider> mProviders;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// mGeolocators are not owned here. Their constructor
|
2009-07-10 17:04:39 -07:00
|
|
|
// adds them to this list, and their destructor removes
|
2008-07-15 16:37:48 -07:00
|
|
|
// them from this list.
|
2008-08-14 09:35:49 -07:00
|
|
|
nsTArray<nsGeolocation*> mGeolocators;
|
2008-12-05 08:29:43 -08:00
|
|
|
|
|
|
|
// This is the last geo position that we have seen.
|
|
|
|
nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can return a geolocation info
|
|
|
|
*/
|
2008-08-14 09:35:49 -07:00
|
|
|
class nsGeolocation : public nsIDOMGeoGeolocation
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_DECL_NSIDOMGEOGEOLOCATION
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(nsGeolocation)
|
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsGeolocation();
|
|
|
|
|
|
|
|
nsresult Init(nsIDOMWindow* contentDom=nsnull);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Called by the geolocation device to notify that a location has changed.
|
2008-08-14 09:35:49 -07:00
|
|
|
void Update(nsIDOMGeoPosition* aPosition);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Returns true if any of the callbacks are repeating
|
2008-10-20 12:37:10 -07:00
|
|
|
PRBool HasActiveCallbacks();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Remove request from all callbacks arrays
|
2008-10-20 12:37:10 -07:00
|
|
|
void RemoveRequest(nsGeolocationRequest* request);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Shutting down.
|
2008-10-20 12:37:10 -07:00
|
|
|
void Shutdown();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
// Getter for the URI that this nsGeolocation was loaded from
|
2008-07-15 16:37:48 -07:00
|
|
|
nsIURI* GetURI() { return mURI; }
|
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
// Getter for the window that this nsGeolocation is owned by
|
|
|
|
nsIWeakReference* GetOwner() { return mOwner; }
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Check to see if the widnow still exists
|
2009-11-12 07:14:45 -08:00
|
|
|
PRBool WindowOwnerStillExists();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
~nsGeolocation();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
void RegisterRequestWithPrompt(nsGeolocationRequest* request);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Two callback arrays. The first |mPendingCallbacks| holds objects for only
|
|
|
|
// one callback and then they are released/removed from the array. The second
|
|
|
|
// |mWatchingCallbacks| holds objects until the object is explictly removed or
|
|
|
|
// there is a page change.
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
|
|
|
|
nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// window that this was created for. Weak reference.
|
2009-07-10 17:02:32 -07:00
|
|
|
nsWeakPtr mOwner;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// where the content was loaded from
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
|
|
|
|
// owning back pointer.
|
2008-08-14 09:35:49 -07:00
|
|
|
nsRefPtr<nsGeolocationService> mService;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#endif /* nsGeoLocation_h */
|