Initial implementation of Gelocation. Bug 437948. Relanded after fixing memory leaks

This commit is contained in:
Doug Turner 2008-07-11 18:35:03 -07:00
Родитель 72b54a1bcb
Коммит c2e79695dd
26 изменённых файлов: 1841 добавлений и 3 удалений

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

@ -59,7 +59,8 @@ DIRS = \
xul \
storage \
json \
offline
offline \
geolocation
ifdef MOZ_SVG
DIRS += svg

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

@ -0,0 +1,57 @@
# ***** 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 build system.
#
# The Initial Developer of the Original Code is Mozilla Corporation
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Doug Turner <dougt@meer.net>
#
# 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 *****
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = dom
XPIDL_MODULE = dom_geolocation
GRE_MODULE = 1
XPIDLSRCS = \
nsIDOMGeolocation.idl \
nsIDOMGeolocationCallback.idl \
nsIDOMGeolocator.idl \
nsIDOMNavigatorGeolocator.idl \
nsIGeolocationProvider.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

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

@ -0,0 +1,51 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "domstubs.idl"
[scriptable, uuid(CEEBF85D-F5E2-4E27-981A-0BF8705ABF0A)]
interface nsIDOMGeolocation : nsISupports
{
readonly attribute double latitude;
readonly attribute double longitude;
readonly attribute double altitude;
readonly attribute double horizontalAccuracy;
readonly attribute double verticalAccuracy;
readonly attribute DOMTimeStamp timestamp;
};

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

@ -0,0 +1,45 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "domstubs.idl"
interface nsIDOMGeolocation;
[scriptable, function, uuid(679C8833-5D8C-430D-A7A3-FF13FE082098)]
interface nsIDOMGeolocationCallback : nsISupports {
void onRequest(in nsIDOMGeolocation position);
};

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

@ -0,0 +1,54 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "domstubs.idl"
interface nsIDOMGeolocation;
interface nsIDOMGeolocationCallback;
[scriptable, uuid(3C3B69FF-3AFA-4D4C-BA3A-F03940E74E14)]
interface nsIDOMGeolocator : nsISupports
{
readonly attribute nsIDOMGeolocation lastPosition;
void getCurrentPosition(in nsIDOMGeolocationCallback callback);
unsigned short watchPosition(in nsIDOMGeolocationCallback callback);
void clearWatch(in unsigned short watchId);
};

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

@ -0,0 +1,50 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "domstubs.idl"
interface nsIDOMGeolocator;
/**
* Property that extends the navigator object.
*/
[scriptable, uuid(1470B9BC-F17C-4419-85A4-E6C1742977A3)]
interface nsIDOMNavigatorGeolocator : nsISupports
{
readonly attribute nsIDOMGeolocator geolocator;
};

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

@ -0,0 +1,141 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "nsISupports.idl"
interface nsIURI;
interface nsIDOMWindow;
interface nsIDOMGeolocation;
interface nsIGeolocationPrompt;
/**
* nsIGeolocationService
*/
[scriptable, uuid(68300FFD-802C-431B-BF92-0A657696B853)]
interface nsIGeolocationService : nsISupports {
attribute nsIGeolocationPrompt prompt;
};
/**
* Interface allows access to a geolocation and is passed to
* the nsIGeolocationPrompt so that the application can approve
* or deny the request.
*/
[scriptable, function, uuid(D681C322-C075-4C6E-9765-C22711A4A60E)]
interface nsIGeolocationRequest : nsISupports {
readonly attribute nsIURI requestingURI;
readonly attribute nsIDOMWindow requestingWindow;
void cancel();
void allow();
void allowButFuzz();
};
/**
* Interface provides a way for the application to handle
* the UI prompts associated with geolocation.
*/
[scriptable, function, uuid(2300C895-1BEE-4297-912C-A57082F3E936)]
interface nsIGeolocationPrompt : nsISupports {
/**
* Called when a request has been made to access geolocation data
*/
void prompt(in nsIGeolocationRequest request);
};
/**
* Interface provides a way for a geolocation provider to
* notify the system that a new location is available.
*/
[scriptable, uuid(B89D7227-9F04-4236-A582-25A3F2779D72)]
interface nsIGeolocationUpdate : nsISupports {
/**
* Notify the geolocation service that a new geolocation
* has been discovered.
* This must be called on the main thread
*/
void update(in nsIDOMGeolocation location);
};
/**
* Interface provides location information to the nsGeolocator
* via the nsIDOMGeolocationCallback interface. After
* startup is called, any geo location change should call
* callback.onRequest().
*/
[scriptable, uuid(E319BE2D-B1D1-4CA6-AEF4-66178589B63D)]
interface nsIGeolocationProvider : nsISupports {
/**
* Start up the provider. This is called before any other
* method. may be called multiple times.
*/
void startup();
/**
* Returns true when the devices is ready and has a
* postion, otherwise false.
*/
boolean isReady();
/**
* watch
* When a location change is observed, notify the callback
*/
void watch(in nsIGeolocationUpdate callback);
/**
* currentLocation return the current location as seen by
* the provider. may be null.
*/
readonly attribute nsIDOMGeolocation currentLocation;
/**
* shutdown
* Shuts down the location devices.
*/
void shutdown();
};
%{C++
#define NS_GEOLOCATION_SERVICE_CONTRACTID "@mozilla.org/geolocation/service;1"
#define NS_GEOLOCATION_PROVIDER_CONTRACTID "@mozilla.org/geolocation/provider;1"
%}

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

@ -424,6 +424,10 @@ enum nsDOMClassInfoID {
// HTML5
eDOMClassInfo_MessageEvent_id,
// Geolocation
eDOMClassInfo_Geolocation_id,
eDOMClassInfo_Geolocator_id,
// WhatWG Video Element
#if defined(MOZ_MEDIA)
eDOMClassInfo_HTMLVideoElement_id,

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

@ -42,7 +42,7 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
DIRS = base jsurl events storage offline json
DIRS = base jsurl events storage offline json geolocation
include $(topsrcdir)/config/rules.mk

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

@ -124,6 +124,7 @@ LOCAL_INCLUDES = \
-I$(srcdir)/../events \
-I$(srcdir)/../storage \
-I$(srcdir)/../offline \
-I$(srcdir)/../geolocation \
-I$(topsrcdir)/content/xbl/src \
-I$(topsrcdir)/content/xul/document/src \
-I$(topsrcdir)/content/events/src \

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

@ -453,6 +453,10 @@
#include "nsIDOMLoadStatus.h"
#include "nsIDOMLoadStatusEvent.h"
// Geolocation
#include "nsIDOMGeolocation.h"
#include "nsIDOMGeolocator.h"
#include "nsIDOMFileList.h"
#include "nsIDOMFile.h"
#include "nsIDOMFileException.h"
@ -1242,6 +1246,12 @@ static nsDOMClassInfoData sClassInfoData[] = {
NS_DEFINE_CLASSINFO_DATA(MessageEvent, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(Geolocation, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(Geolocator, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
#if defined(MOZ_MEDIA)
NS_DEFINE_CLASSINFO_DATA(HTMLVideoElement, nsHTMLElementSH,
ELEMENT_SCRIPTABLE_FLAGS)
@ -1923,6 +1933,7 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_MAP_BEGIN(Navigator, nsIDOMNavigator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMNavigator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMJSNavigator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMNavigatorGeolocator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMClientInformation)
DOM_CLASSINFO_MAP_END
@ -3405,6 +3416,14 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_EVENT_MAP_ENTRIES
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(Geolocation, nsIDOMGeolocation)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeolocation)
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(Geolocator, nsIDOMGeolocator)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeolocator)
DOM_CLASSINFO_MAP_END
#if defined(MOZ_MEDIA)
DOM_CLASSINFO_MAP_BEGIN(HTMLVideoElement, nsIDOMHTMLVideoElement)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMHTMLVideoElement)

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

@ -73,6 +73,7 @@
#include "nsPluginArray.h"
#include "nsIPluginHost.h"
#include "nsPIPluginHost.h"
#include "nsGeolocation.h"
#ifdef OJI
#include "nsIJVMManager.h"
#include "nsILiveConnectManager.h"
@ -111,6 +112,7 @@
#include "nsIDOMPopupBlockedEvent.h"
#include "nsIDOMPkcs11.h"
#include "nsIDOMOfflineResourceList.h"
#include "nsIDOMGeolocation.h"
#include "nsDOMString.h"
#include "nsIEmbeddingSiteWindow2.h"
#include "nsThreadUtils.h"
@ -9008,6 +9010,7 @@ NS_INTERFACE_MAP_BEGIN(nsNavigator)
NS_INTERFACE_MAP_ENTRY(nsIDOMNavigator)
NS_INTERFACE_MAP_ENTRY(nsIDOMJSNavigator)
NS_INTERFACE_MAP_ENTRY(nsIDOMClientInformation)
NS_INTERFACE_MAP_ENTRY(nsIDOMNavigatorGeolocator)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Navigator)
NS_INTERFACE_MAP_END
@ -9542,6 +9545,8 @@ nsNavigator::LoadingNewDocument()
// arrays may have changed. See bug 150087.
mMimeTypes = nsnull;
mPlugins = nsnull;
mGeolocator = nsnull;
}
nsresult
@ -9662,3 +9667,18 @@ nsNavigator::MozIsLocallyAvailable(const nsAString &aURI,
return NS_OK;
}
//*****************************************************************************
// nsNavigator::nsIDOMNavigatorGeolocator
//*****************************************************************************
NS_IMETHODIMP nsNavigator::GetGeolocator(nsIDOMGeolocator **_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
if (!mGeolocator)
mGeolocator = new nsGeolocator();
NS_IF_ADDREF(*_retval = mGeolocator);
return NS_OK;
}

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

@ -65,6 +65,7 @@
#include "nsIDOM3EventTarget.h"
#include "nsIDOMNSEventTarget.h"
#include "nsIDOMNavigator.h"
#include "nsIDOMNavigatorGeolocator.h"
#include "nsIDOMNSLocation.h"
#include "nsIDOMWindowInternal.h"
#include "nsIInterfaceRequestor.h"
@ -124,6 +125,7 @@ class PostMessageEvent;
class nsDOMOfflineResourceList;
class nsDOMOfflineLoadStatusList;
class nsGeolocator;
// permissible values for CheckOpenAllow
enum OpenAllowValue {
@ -804,7 +806,8 @@ protected:
class nsNavigator : public nsIDOMNavigator,
public nsIDOMJSNavigator,
public nsIDOMClientInformation
public nsIDOMClientInformation,
public nsIDOMNavigatorGeolocator
{
public:
nsNavigator(nsIDocShell *aDocShell);
@ -814,6 +817,7 @@ public:
NS_DECL_NSIDOMNAVIGATOR
NS_DECL_NSIDOMJSNAVIGATOR
NS_DECL_NSIDOMCLIENTINFORMATION
NS_DECL_NSIDOMNAVIGATORGEOLOCATOR
void SetDocShell(nsIDocShell *aDocShell);
nsIDocShell *GetDocShell()
@ -827,6 +831,7 @@ public:
protected:
nsRefPtr<nsMimeTypeArray> mMimeTypes;
nsRefPtr<nsPluginArray> mPlugins;
nsRefPtr<nsGeolocator> mGeolocator;
nsIDocShell* mDocShell; // weak reference
static jsval sPrefInternal_id;

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

@ -0,0 +1,119 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "MaemoLocationProvider.h"
#include "nsGeolocation.h"
NS_IMPL_ISUPPORTS1(MaemoLocationProvider, nsIGeolocationProvider)
MaemoLocationProvider::MaemoLocationProvider()
: mGPSDevice(nsnull), mLocationCallbackHandle(0), mHasSeenLocation(PR_FALSE)
{
}
MaemoLocationProvider::~MaemoLocationProvider()
{
}
void location_changed (LocationGPSDevice *device, gpointer userdata)
{
MaemoLocationProvider* provider = (MaemoLocationProvider*) userdata;
nsRefCnt<nsGeolocation*> somewhere = new nsGeolocation(device->fix->latitude,
device->fix->longitude,
device->fix->altitude,
device->fix->eph,
device->fix->epv,
device->fix->time);
provider->Update(somewhere);
}
NS_IMETHODIMP MaemoLocationProvider::Startup()
{
if (!mGPSDevice)
{
// if we are already started, don't do anything
memset(&mGPSBT, 0, sizeof(gpsbt_t));
int result = gpsbt_start(NULL, 0, 0, 0, NULL, 0, 0, &mGPSBT);
if (result <0)
return NS_ERROR_NOT_AVAILABLE;
mGPSDevice = (LocationGPSDevice*) g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
mLocationCallbackHandle = g_signal_connect (mGPSDevice, "changed", G_CALLBACK (location_changed), this->mCallback);
}
return NS_OK;
}
NS_IMETHODIMP MaemoLocationProvider::IsReady(PRBool *_retval NS_OUTPARAM)
{
*_retval = mHasSeenLocation;
return NS_OK;
}
NS_IMETHODIMP MaemoLocationProvider::Watch(nsIGeolocationUpdate *callback)
{
mCallback = callback; // weak ref
return NS_OK;
}
/* readonly attribute nsIDOMGeolocation currentLocation; */
NS_IMETHODIMP MaemoLocationProvider::GetCurrentLocation(nsIDOMGeolocation * *aCurrentLocation)
{
NS_IF_ADDREF(*aCurrentLocation = mLastLocation);
return NS_OK;
}
NS_IMETHODIMP MaemoLocationProvider::Shutdown()
{
if (mGPSDevice && mLocationCallbackHandle) {
g_signal_handler_disconnect(mGPSDevice, mLocationCallbackHandle);
g_object_unref(mGPSDevice);
gpsbt_stop(&mGPSBT);
mLocationCallbackHandle = 0;
mGPSDevice = nsnull;
mHasSeenLocation = PR_FALSE;
}
return NS_OK;
}
void MaemoLocationProvider::Update(nsIDOMGeolocation* aLocation)
{
mHasSeenLocation = PR_TRUE;
mLastLocation = aLocation;
if (mCallback)
mCallback->Update(aLocation);
}

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

@ -0,0 +1,75 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "nsIGeolocationProvider.h"
#include "nsIDOMGeolocation.h"
#include "nsCOMPtr.h"
#include <glib.h>
#include <errno.h>
#include <gpsbt.h>
#include <gpsmgr.h>
extern "C" {
// need to extern these because of:
// https://bugs.maemo.org/show_bug.cgi?id=3226
#include <location/location-gps-device.h>
#include <location/location-gpsd-control.h>
}
class MaemoLocationProvider : public nsIGeolocationProvider
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIGEOLOCATIONPROVIDER
MaemoLocationProvider();
void Update(nsIDOMGeolocation* aLocation);
private:
~MaemoLocationProvider();
nsCOMPtr<nsIDOMGeolocation> mLastLocation;
nsIGeolocationUpdate* mCallback; // weak reference by contract.
gpsbt_t mGPSBT;
LocationGPSDevice *mGPSDevice;
gulong mLocationCallbackHandle;
PRBool mHasSeenLocation;
};

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

@ -0,0 +1,84 @@
# ***** 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 build system.
#
# The Initial Developer of the Original Code is Mozilla Corporation
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Doug Turner <dougt@meer.net>
#
# 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 *****
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = dom
LIBRARY_NAME = jsdomgeolocation_s
LIBXUL_LIBRARY = 1
# we don't want the shared lib, but we want to force the creation of a static lib.
FORCE_STATIC_LIB = 1
REQUIRES = \
xpcom \
content \
layout \
pref \
widget \
string \
dom \
js \
caps \
xpconnect\
intl \
necko \
windowwatcher \
$(NULL)
CPPSRCS = \
nsGeolocation.cpp \
$(NULL)
EXTRA_DSO_LDOPTS = \
$(MOZ_COMPONENT_LIBS) \
$(NULL)
LOCAL_INCLUDES = \
-I$(srcdir)/../base \
$(NULL)
ifdef NS_OSSO
CPPSRCS += MaemoLocationProvider.cpp
LOCAL_INCLUDES += $(MOZ_GTK2_CFLAGS)
endif
include $(topsrcdir)/config/rules.mk

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

@ -0,0 +1,644 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "nsGeolocation.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsIDOMWindow.h"
#include "nsDOMClassInfo.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsIJSContextStack.h"
#include "nsIScriptGlobalObject.h"
#include "nsIScriptSecurityManager.h"
#include "nsContentUtils.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsIPermissionManager.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch2.h"
#ifdef NS_OSSO
#include "MaemoLocationProvider.h"
#endif
////////////////////////////////////////////////////
// nsGeolocationRequest
////////////////////////////////////////////////////
nsGeolocationRequest::nsGeolocationRequest(nsGeolocator* locator, nsIDOMGeolocationCallback* callback)
: mAllowed(PR_FALSE), mCleared(PR_FALSE), mFuzzLocation(PR_FALSE), mCallback(callback), mLocator(locator)
{
SetOwner();
SetURI();
}
nsGeolocationRequest::~nsGeolocationRequest()
{
}
NS_INTERFACE_MAP_BEGIN(nsGeolocationRequest)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGeolocationRequest)
NS_INTERFACE_MAP_ENTRY(nsIGeolocationRequest)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsGeolocationRequest)
NS_IMPL_RELEASE(nsGeolocationRequest)
NS_IMETHODIMP
nsGeolocationRequest::GetRequestingURI(nsIURI * *aRequestingURI)
{
NS_ENSURE_ARG_POINTER(aRequestingURI);
*aRequestingURI = mURI;
NS_ADDREF(*aRequestingURI);
return NS_OK;
}
NS_IMETHODIMP
nsGeolocationRequest::GetRequestingWindow(nsIDOMWindow * *aRequestingWindow)
{
NS_ENSURE_ARG_POINTER(aRequestingWindow);
*aRequestingWindow = mOwner;
NS_ADDREF(*aRequestingWindow);
return NS_OK;
}
NS_IMETHODIMP
nsGeolocationRequest::Cancel()
{
// pass a null back.
mCallback->OnRequest(nsnull);
// remove ourselves from the locators callback lists.
mLocator->RemoveRequest(this);
return NS_OK;
}
NS_IMETHODIMP
nsGeolocationRequest::Allow()
{
// Kick off the geo device, if it isn't already running
nsRefPtr<nsGeolocatorService> geoService = nsGeolocatorService::GetInstance();
geoService->StartDevice();
mAllowed = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocationRequest::AllowButFuzz()
{
mFuzzLocation = PR_TRUE;
return Allow();
}
void
nsGeolocationRequest::MarkCleared()
{
mCleared = PR_TRUE;
}
void
nsGeolocationRequest::SendLocation(nsIDOMGeolocation* location)
{
if (!OwnerStillExists() || mCleared)
return;
//TODO mFuzzLocation. Needs to be defined what we do here.
if (mFuzzLocation)
{
// need to make a copy because nsIDOMGeolocation is
// readonly, and we are not sure of its implementation.
double lat, lon, alt, herror, verror;
DOMTimeStamp time;
location->GetLatitude(&lat);
location->GetLongitude(&lon);
location->GetAltitude(&alt);
location->GetHorizontalAccuracy(&herror);
location->GetVerticalAccuracy(&verror);
location->GetTimestamp(&time);
// do something to the numbers... TODO.
// mask out any location until we figure out how to fuzz locations;
lat = 0; lon = 0; alt = 0; herror = 100000; verror = 100000;
nsRefPtr<nsGeolocation> somewhere = new nsGeolocation(lat,
lon,
alt,
herror,
verror,
time);
mCallback->OnRequest(somewhere);
return;
}
mCallback->OnRequest(location);
}
void
nsGeolocationRequest::SetURI()
{
nsIScriptSecurityManager* secman = nsContentUtils::GetSecurityManager();
if (!secman)
return;
nsCOMPtr<nsIPrincipal> principal;
secman->GetSubjectPrincipal(getter_AddRefs(principal));
if (!principal)
return;
principal->GetURI(getter_AddRefs(mURI));
}
void
nsGeolocationRequest::SetOwner()
{
nsCOMPtr<nsIJSContextStack> stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
if (!stack)
{
return;
}
JSContext *cx;
if (NS_FAILED(stack->Peek(&cx)) || !cx)
{
return;
}
nsIScriptContext* context = GetScriptContextFromJSContext(cx);
if (context)
{
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(context->GetGlobalObject());
if (window)
{
mOwner = window->GetCurrentInnerWindow();
}
}
}
void
nsGeolocationRequest::Shutdown()
{
mCleared = PR_TRUE;
mCallback = nsnull;
mOwner = nsnull;
mURI = nsnull;
}
PRBool
nsGeolocationRequest::OwnerStillExists()
{
if (!mOwner)
return PR_FALSE;
nsCOMPtr<nsIDOMWindowInternal> domWindow(mOwner);
if (domWindow)
{
PRBool closed = PR_FALSE;
domWindow->GetClosed(&closed);
if (closed)
return PR_FALSE;
}
nsPIDOMWindow* outer = mOwner->GetOuterWindow();
if (!outer || outer->GetCurrentInnerWindow() != mOwner)
{
return PR_FALSE;
}
return PR_TRUE;
}
////////////////////////////////////////////////////
// nsGeolocation
////////////////////////////////////////////////////
NS_INTERFACE_MAP_BEGIN(nsGeolocation)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeolocation)
NS_INTERFACE_MAP_ENTRY(nsIDOMGeolocation)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Geolocation)
NS_INTERFACE_MAP_END
NS_IMPL_THREADSAFE_ADDREF(nsGeolocation)
NS_IMPL_THREADSAFE_RELEASE(nsGeolocation)
NS_IMETHODIMP
nsGeolocation::GetLatitude(double *aLatitude)
{
*aLatitude = mLat;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocation::GetLongitude(double *aLongitude)
{
*aLongitude = mLong;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocation::GetAltitude(double *aAltitude)
{
*aAltitude = mAlt;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocation::GetHorizontalAccuracy(double *aHorizontalAccuracy)
{
*aHorizontalAccuracy = mHError;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocation::GetVerticalAccuracy(double *aVerticalAccuracy)
{
*aVerticalAccuracy = mVError;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocation::GetTimestamp(DOMTimeStamp* aTimestamp)
{
*aTimestamp = mTimestamp;
return NS_OK;
}
////////////////////////////////////////////////////
// nsGeolocatorService
////////////////////////////////////////////////////
NS_INTERFACE_MAP_BEGIN(nsGeolocatorService)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGeolocationUpdate)
NS_INTERFACE_MAP_ENTRY(nsIGeolocationUpdate)
NS_INTERFACE_MAP_ENTRY(nsIGeolocationService)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END
NS_IMPL_THREADSAFE_ADDREF(nsGeolocatorService)
NS_IMPL_THREADSAFE_RELEASE(nsGeolocatorService)
nsGeolocatorService::nsGeolocatorService()
{
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
if (obs) {
obs->AddObserver(this, "quit-application", false);
}
mTimeout = nsContentUtils::GetIntPref("geo.timeout", 6000);
}
nsGeolocatorService::~nsGeolocatorService()
{
}
NS_IMETHODIMP
nsGeolocatorService::Observe(nsISupports* aSubject, const char* aTopic,
const PRUnichar* aData)
{
if (!strcmp("quit-application", aTopic))
{
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
if (obs) {
obs->RemoveObserver(this, "quit-application");
}
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
mGeolocators[i]->Shutdown();
StopDevice();
// Remove our reference to any prompt that may have been set.
mPrompt = nsnull;
return NS_OK;
}
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())
{
SetDisconnectTimer();
return NS_OK;
}
// okay to close up.
StopDevice();
Update(nsnull);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsGeolocatorService::GetPrompt(nsIGeolocationPrompt * *aPrompt)
{
NS_ENSURE_ARG_POINTER(aPrompt);
*aPrompt = mPrompt;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocatorService::SetPrompt(nsIGeolocationPrompt * aPrompt)
{
mPrompt = aPrompt;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocatorService::Update(nsIDOMGeolocation *somewhere)
{
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
mGeolocators[i]->Update(somewhere);
return NS_OK;
}
already_AddRefed<nsIDOMGeolocation>
nsGeolocatorService::GetLastKnownPosition()
{
nsIDOMGeolocation* location = nsnull;
if (mProvider)
mProvider->GetCurrentLocation(&location);
return location;
}
PRBool
nsGeolocatorService::IsDeviceReady()
{
PRBool ready = PR_FALSE;
if (mProvider)
mProvider->IsReady(&ready);
return ready;
}
nsresult
nsGeolocatorService::StartDevice()
{
if (!mProvider)
{
// Check to see if there is an override in place. if so, use it.
mProvider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
// if NS_OSSO, see if we should try the MAEMO location provider
#ifdef NS_OSSO
if (!mProvider)
{
// guess not, lets try a default one:
mProvider = new MaemoLocationProvider();
}
#endif
if (!mProvider)
return NS_ERROR_NOT_AVAILABLE;
// if we have one, start it up.
nsresult rv = mProvider->Startup();
if (NS_FAILED(rv))
return NS_ERROR_NOT_AVAILABLE;;
// lets monitor it for any changes.
mProvider->Watch(this);
// we do not want to keep the geolocation devices online
// indefinitely. Close them down after a reasonable period of
// inactivivity
SetDisconnectTimer();
}
return NS_OK;
}
void
nsGeolocatorService::SetDisconnectTimer()
{
if (!mDisconnectTimer)
mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1");
else
mDisconnectTimer->Cancel();
mDisconnectTimer->Init(this,
mTimeout,
nsITimer::TYPE_ONE_SHOT);
}
void
nsGeolocatorService::StopDevice()
{
if (mProvider) {
mProvider->Shutdown();
mProvider = nsnull;
}
if(mDisconnectTimer) {
mDisconnectTimer->Cancel();
mDisconnectTimer = nsnull;
}
}
nsGeolocatorService* nsGeolocatorService::gService = nsnull;
nsGeolocatorService*
nsGeolocatorService::GetInstance()
{
if (!nsGeolocatorService::gService) {
nsGeolocatorService::gService = new nsGeolocatorService();
}
return nsGeolocatorService::gService;
}
nsGeolocatorService*
nsGeolocatorService::GetGeolocationService()
{
nsGeolocatorService* inst = nsGeolocatorService::GetInstance();
NS_ADDREF(inst);
return inst;
}
void
nsGeolocatorService::AddLocator(nsGeolocator* locator)
{
mGeolocators.AppendElement(locator);
}
void
nsGeolocatorService::RemoveLocator(nsGeolocator* locator)
{
mGeolocators.RemoveElement(locator);
}
////////////////////////////////////////////////////
// nsGeolocator
////////////////////////////////////////////////////
NS_INTERFACE_MAP_BEGIN(nsGeolocator)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeolocator)
NS_INTERFACE_MAP_ENTRY(nsIDOMGeolocator)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Geolocator)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsGeolocator)
NS_IMPL_RELEASE(nsGeolocator)
nsGeolocator::nsGeolocator()
: mUpdateInProgress(PR_FALSE)
{
mService = nsGeolocatorService::GetInstance();
if (mService)
mService->AddLocator(this);
}
nsGeolocator::~nsGeolocator()
{
}
void
nsGeolocator::Shutdown()
{
// Shutdown and release all callbacks
for (PRInt32 i = 0; i< mPendingCallbacks.Count(); i++)
mPendingCallbacks[i]->Shutdown();
mPendingCallbacks.Clear();
for (PRInt32 i = 0; i< mWatchingCallbacks.Count(); i++)
mWatchingCallbacks[i]->Shutdown();
mWatchingCallbacks.Clear();
if (mService)
mService->RemoveLocator(this);
mService = nsnull;
}
PRBool
nsGeolocator::HasActiveCallbacks()
{
return (PRBool) mWatchingCallbacks.Count();
}
void
nsGeolocator::RemoveRequest(nsGeolocationRequest* request)
{
mPendingCallbacks.RemoveObject(request);
// if it is in the mWatchingCallbacks, we can't do much
// since we passed back the position in the array to who
// ever called WatchPosition() and we do not want to mess
// around with the ordering of the array. Instead, just
// mark the request as "cleared".
request->MarkCleared();
}
void
nsGeolocator::Update(nsIDOMGeolocation *somewhere)
{
// This method calls out to objects which may spin and
// event loop which may add new location objects into
// mPendingCallbacks, and mWatchingCallbacks. Since this
// function can only be called on the primary thread, we
// can lock this method with a member var.
if (mUpdateInProgress)
return;
mUpdateInProgress = PR_TRUE;
// notify anyone that has been waiting
for (PRInt32 i = 0; i< mPendingCallbacks.Count(); i++)
mPendingCallbacks[i]->SendLocation(somewhere);
mPendingCallbacks.Clear();
// notify everyone that is watching
for (PRInt32 i = 0; i< mWatchingCallbacks.Count(); i++)
mWatchingCallbacks[i]->SendLocation(somewhere);
mUpdateInProgress = PR_FALSE;
}
NS_IMETHODIMP
nsGeolocator::GetLastPosition(nsIDOMGeolocation * *aLastPosition)
{
// we are advocating that this method be removed.
NS_ENSURE_ARG_POINTER(aLastPosition);
*aLastPosition = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocator::GetCurrentPosition(nsIDOMGeolocationCallback *callback)
{
nsIGeolocationPrompt* prompt = mService->GetPrompt();
if (prompt == nsnull)
return NS_ERROR_NOT_AVAILABLE;
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, callback);
prompt->Prompt(request);
// What if you have a location provider that only sends a location once, then stops.? fix.
mPendingCallbacks.AppendObject(request);
return NS_OK;
}
NS_IMETHODIMP
nsGeolocator::WatchPosition(nsIDOMGeolocationCallback *callback, PRUint16 *_retval NS_OUTPARAM)
{
nsIGeolocationPrompt* prompt = mService->GetPrompt();
if (prompt == nsnull)
return NS_ERROR_NOT_AVAILABLE;
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, callback);
prompt->Prompt(request);
// need to hand back an index/reference.
mWatchingCallbacks.AppendObject(request);
*_retval = mWatchingCallbacks.Count() - 1;
return NS_OK;
}
NS_IMETHODIMP
nsGeolocator::ClearWatch(PRUint16 watchId)
{
mWatchingCallbacks[watchId]->MarkCleared();
return NS_OK;
}

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

@ -0,0 +1,215 @@
/* ***** 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.
*
* The Initial Developer of the Original Code is Mozilla Corporation
* 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 ***** */
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "nsTArray.h"
#include "nsITimer.h"
#include "nsIObserver.h"
#include "nsIDOMGeolocation.h"
#include "nsIDOMGeolocation.h"
#include "nsIDOMGeolocationCallback.h"
#include "nsIDOMGeolocator.h"
#include "nsIDOMNavigatorGeolocator.h"
#include "nsPIDOMWindow.h"
#include "nsIGeolocationProvider.h"
class nsGeolocator;
class nsGeolocatorService;
class nsGeolocationRequest : public nsIGeolocationRequest
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIGEOLOCATIONREQUEST
nsGeolocationRequest(nsGeolocator* locator, nsIDOMGeolocationCallback* callback);
void SetURI();
void SetOwner();
PRBool OwnerStillExists();
void Shutdown();
void SendLocation(nsIDOMGeolocation* location);
void MarkCleared();
PRBool Allowed() {return mAllowed;}
~nsGeolocationRequest();
private:
PRBool mAllowed;
PRBool mCleared;
PRBool mFuzzLocation;
nsCOMPtr<nsIDOMGeolocationCallback> mCallback;
nsCOMPtr<nsPIDOMWindow> mOwner;
nsCOMPtr<nsIURI> mURI;
nsGeolocator* mLocator; // The locator exists alonger than this object.
};
/**
* Simple object that holds a single point in space.
*/
class nsGeolocation : public nsIDOMGeolocation
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMGEOLOCATION
nsGeolocation(double aLat, double aLong, double aAlt, double aHError, double aVError, long long aTimestamp)
: mLat(aLat), mLong(aLong), mAlt(aAlt), mHError(aHError), mVError(aVError), mTimestamp(aTimestamp){};
private:
~nsGeolocation(){}
double mLat, mLong, mAlt, mHError, mVError;
long long mTimestamp;
};
/**
* Singleton that manages the geolocation provider
*/
class nsGeolocatorService : public nsIGeolocationService, public nsIGeolocationUpdate, public nsIObserver
{
public:
static nsGeolocatorService* GetGeolocationService();
static nsGeolocatorService* GetInstance(); // Non-Addref'ing
static nsGeolocatorService* gService;
NS_DECL_ISUPPORTS
NS_DECL_NSIGEOLOCATIONUPDATE
NS_DECL_NSIOBSERVER
NS_DECL_NSIGEOLOCATIONSERVICE
nsGeolocatorService();
// Management of the nsGeolocator objects
void AddLocator(nsGeolocator* locator);
void RemoveLocator(nsGeolocator* locator);
// Returns the last geolocation we have seen since calling StartDevice()
already_AddRefed<nsIDOMGeolocation> GetLastKnownPosition();
// Returns the application defined UI prompt
nsIGeolocationPrompt* GetPrompt() { return mPrompt; };
// Returns true if the we have successfully found and started a
// geolocation device
PRBool IsDeviceReady();
// 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:
~nsGeolocatorService();
// 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.
nsCOMPtr<nsIGeolocationProvider> mProvider;
// mGeolocators are not owned here. Their constructor
// addes them to this list, and their destructor removes
// them from this list.
nsTArray<nsGeolocator*> mGeolocators;
// prompt callback, if any
nsCOMPtr<nsIGeolocationPrompt> mPrompt;
};
/**
* Can return a geolocation info
*/
class nsGeolocator : public nsIDOMGeolocator
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMGEOLOCATOR
nsGeolocator();
// Called by the geolocation device to notify that a location has changed.
void Update(nsIDOMGeolocation* aLocation);
// Returns true if any of the callbacks are repeating
PRBool HasActiveCallbacks();
// Remove request from all callbacks arrays
void RemoveRequest(nsGeolocationRequest* request);
// Shutting down.
void Shutdown();
private:
~nsGeolocator();
// 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.
nsCOMArray<nsGeolocationRequest> mPendingCallbacks;
nsCOMArray<nsGeolocationRequest> mWatchingCallbacks;
PRBool mUpdateInProgress;
// owning back pointer.
nsRefPtr<nsGeolocatorService> mService;
};

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

@ -50,6 +50,7 @@ DIRS += \
bugs \
chrome \
whatwg \
geolocation \
$(NULL)
# dom-level2-html disabled due to failures on multiple platforms

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

@ -0,0 +1,56 @@
# ***** 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 build system.
#
# The Initial Developer of the Original Code is Mozilla Corporation
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Doug Turner <dougt@meer.net>
#
# 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 *****
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = dom/tests/mochitest/geolocation
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_TEST_FILES = \
test_lastPosition.html \
prompt_common.js \
geolocation_common.js \
$(NULL)
libs:: $(_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)

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

@ -0,0 +1,12 @@
function check_geolocation(location) {
ok(location, "Check to see if this location is non-null");
ok(location.latitude, "Check to see if there is a latitude");
ok(location.longitude, "Check to see if there is a longitude");
ok(location.altitude, "Check to see if there is a altitude");
ok(location.horizontalAccuracy, "Check to see if there is a horizontalAccuracy");
ok(location.verticalAccuracy, "Check to see if there is a verticalAccuracy");
ok(location.timestamp, "Check to see if there is a timestamp");
}

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

@ -0,0 +1,75 @@
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
const Ci = Components.interfaces;
ok(Ci != null, "Access Ci");
const Cc = Components.classes;
ok(Cc != null, "Access Cc");
var didDialog;
var timer; // keep in outer scope so it's not GC'd before firing
function startCallbackTimer() {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
didDialog = false;
// Delay before the callback twiddles the prompt.
const dialogDelay = 10;
// Use a timer to invoke a callback to twiddle the authentication dialog
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT);
}
var observer = {
QueryInterface : function (iid) {
const interfaces = [Ci.nsIObserver,
Ci.nsISupports, Ci.nsISupportsWeakReference];
if (!interfaces.some( function(v) { return iid.equals(v) } ))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
},
observe : function (subject, topic, data) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var doc = getDialogDoc();
if (doc)
handleDialog(doc);
else
startCallbackTimer(); // try again in a bit
}
};
function getDialogDoc() {
// Find the <browser> which contains notifyWindow, by looking
// through all the open windows and all the <browsers> in each.
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
//var enumerator = wm.getEnumerator("navigator:browser");
var enumerator = wm.getXULWindowEnumerator(null);
while (enumerator.hasMoreElements()) {
var win = enumerator.getNext();
var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell;
var containedDocShells = windowDocShell.getDocShellEnumerator(
Ci.nsIDocShellTreeItem.typeChrome,
Ci.nsIDocShell.ENUMERATE_FORWARDS);
while (containedDocShells.hasMoreElements()) {
// Get the corresponding document for this docshell
var childDocShell = containedDocShells.getNext();
// We don't want it if it's not done loading.
if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE)
continue;
var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell).
contentViewer.DOMDocument;
//ok(true, "Got window: " + childDoc.location.href);
if (childDoc.location.href == "chrome://global/content/commonDialog.xul")
return childDoc;
}
}
return null;
}

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

@ -0,0 +1,89 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
<title>Test for getCurrentPosition </title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="prompt_common.js"></script>
<script type="text/javascript" src="geolocation_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug **/
function handleDialog(doc) {
// Verify the error message is correct - the string (places) is not
// translated
var dialog = doc.getElementById("commonDialog");
var desc = doc.getElementById("info.body");
var errmsg = desc.childNodes[0].data;
ok(errmsg.indexOf("located") > 0, "Check for the correct message");
// Clear the dialog
if (allow_dialog)
dialog.acceptDialog();
else
dialog.cancelDialog();
}
ok(navigator.geolocator, "Ensure that the geolocation object is present");
//first we want to prevent this proprerty from loading.
/*
Moving to the info bar very soon. right now, there is no
dialog in FF.
allow_dialog = 0;
startCallbackTimer();
*/
last = navigator.geolocator.lastPosition;
ok(last == null, "Check to ensure the lastPosition is null");
// Try again, but this time allow the lookup.
/*
Moving to the info bar very soon. right now, there is no
dialog in FF
allow_dialog = 1;
startCallbackTimer();
*/
last = navigator.geolocator.lastPosition;
if (last != null) {
check_geolocation(last);
}
else
{
ok(1, "No geo location available");
}
SimpleTest.finish();
</script>
</pre>
</body>
</html>

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

@ -138,6 +138,7 @@ SHARED_LIBRARY_LIBS = \
$(DEPTH)/dom/src/jsurl/$(LIB_PREFIX)jsurl_s.$(LIB_SUFFIX) \
$(DEPTH)/dom/src/storage/$(LIB_PREFIX)jsdomstorage_s.$(LIB_SUFFIX) \
$(DEPTH)/dom/src/offline/$(LIB_PREFIX)jsdomoffline_s.$(LIB_SUFFIX) \
$(DEPTH)/dom/src/geolocation/$(LIB_PREFIX)jsdomgeolocation_s.$(LIB_SUFFIX) \
$(DEPTH)/editor/libeditor/text/$(LIB_PREFIX)texteditor_s.$(LIB_SUFFIX) \
$(DEPTH)/editor/libeditor/base/$(LIB_PREFIX)editorbase_s.$(LIB_SUFFIX) \
$(NULL)
@ -267,6 +268,7 @@ LOCAL_INCLUDES += -I$(srcdir)/../base \
-I$(topsrcdir)/dom/src/jsurl \
-I$(topsrcdir)/dom/src/storage \
-I$(topsrcdir)/dom/src/offline \
-I$(topsrcdir)/dom/src/geolocation \
-I. \
-I$(topsrcdir)/editor/libeditor/base \
-I$(topsrcdir)/editor/libeditor/text \

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

@ -266,6 +266,8 @@ static void Shutdown();
#include "nsIXMLContentBuilder.h"
#endif
#include "nsGeolocation.h"
// Transformiix
/* {0C351177-0159-4500-86B0-A219DFDE4258} */
#define TRANSFORMIIX_XPATH1_SCHEME_CID \
@ -786,6 +788,11 @@ CreateWindowControllerWithSingletonCommandTable(nsISupports *aOuter,
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMScriptObjectFactory)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsBaseDOMException)
#define NS_GEOLOCATION_SERVICE_CID \
{ 0x404d02a, 0x1CA, 0xAAAB, { 0x47, 0x62, 0x94, 0x4b, 0x1b, 0xf2, 0xf7, 0xb5 } }
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsGeolocatorService, nsGeolocatorService::GetGeolocationService)
// The list of components we register
static const nsModuleComponentInfo gComponents[] = {
#ifdef DEBUG
@ -1373,6 +1380,13 @@ static const nsModuleComponentInfo gComponents[] = {
"@mozilla.org/textservices/textservicesdocument;1",
nsTextServicesDocumentConstructor },
#endif
{ "Geolocation Service",
NS_GEOLOCATION_SERVICE_CID,
"@mozilla.org/geolocation/service;1",
nsGeolocatorServiceConstructor },
};
NS_IMPL_NSGETMODULE_WITH_CTOR(nsLayoutModule, gComponents, Initialize)

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

@ -212,6 +212,10 @@ EXTRA_DSO_LDOPTS += $(XLDFLAGS) $(XLIBS) $(MOZ_XFT_LIBS) $(MOZ_GTK2_LIBS) $(XT_L
EXTRA_DSO_LDOPTS += $(FT2_LIBS)
endif
ifdef NS_OSSO
EXTRA_DSO_LDOPTS += -llocation -lgpsbt
endif
ifdef MOZ_ENABLE_STARTUP_NOTIFICATION
EXTRA_DSO_LDOPTS += $(MOZ_STARTUP_NOTIFICATION_LIBS)
endif