• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "GeolocationController.h"
28 #include "GeolocationPosition.h"
29 
30 #if ENABLE(CLIENT_BASED_GEOLOCATION)
31 
32 #include "GeolocationClient.h"
33 
34 namespace WebCore {
35 
GeolocationController(Page * page,GeolocationClient * client)36 GeolocationController::GeolocationController(Page* page, GeolocationClient* client)
37     : m_page(page)
38     , m_client(client)
39 {
40 }
41 
~GeolocationController()42 GeolocationController::~GeolocationController()
43 {
44     ASSERT(m_observers.isEmpty());
45 
46     if (m_client)
47         m_client->geolocationDestroyed();
48 }
49 
addObserver(Geolocation * observer,bool enableHighAccuracy)50 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
51 {
52     // This may be called multiple times with the same observer, though removeObserver()
53     // is called only once with each.
54     bool wasEmpty = m_observers.isEmpty();
55     m_observers.add(observer);
56     if (enableHighAccuracy)
57         m_highAccuracyObservers.add(observer);
58 
59     if (m_client) {
60         if (enableHighAccuracy)
61             m_client->setEnableHighAccuracy(true);
62         if (wasEmpty)
63             m_client->startUpdating();
64     }
65 }
66 
removeObserver(Geolocation * observer)67 void GeolocationController::removeObserver(Geolocation* observer)
68 {
69     if (!m_observers.contains(observer))
70         return;
71 
72     m_observers.remove(observer);
73     m_highAccuracyObservers.remove(observer);
74 
75     if (m_client) {
76         if (m_observers.isEmpty())
77             m_client->stopUpdating();
78         else if (m_highAccuracyObservers.isEmpty())
79             m_client->setEnableHighAccuracy(false);
80     }
81 }
82 
requestPermission(Geolocation * geolocation)83 void GeolocationController::requestPermission(Geolocation* geolocation)
84 {
85     if (m_client)
86         m_client->requestPermission(geolocation);
87 }
88 
cancelPermissionRequest(Geolocation * geolocation)89 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
90 {
91     if (m_client)
92         m_client->cancelPermissionRequest(geolocation);
93 }
94 
positionChanged(GeolocationPosition * position)95 void GeolocationController::positionChanged(GeolocationPosition* position)
96 {
97     m_lastPosition = position;
98     Vector<RefPtr<Geolocation> > observersVector;
99     copyToVector(m_observers, observersVector);
100     for (size_t i = 0; i < observersVector.size(); ++i)
101         observersVector[i]->positionChanged();
102 }
103 
errorOccurred(GeolocationError * error)104 void GeolocationController::errorOccurred(GeolocationError* error)
105 {
106     Vector<RefPtr<Geolocation> > observersVector;
107     copyToVector(m_observers, observersVector);
108     for (size_t i = 0; i < observersVector.size(); ++i)
109         observersVector[i]->setError(error);
110 }
111 
lastPosition()112 GeolocationPosition* GeolocationController::lastPosition()
113 {
114     if (m_lastPosition.get())
115         return m_lastPosition.get();
116 
117     if (!m_client)
118         return 0;
119 
120     return m_client->lastPosition();
121 }
122 
123 } // namespace WebCore
124 
125 #endif // ENABLE(CLIENT_BASED_GEOLOCATION)
126