2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "GeolocationController.h"
29 #if ENABLE(GEOLOCATION)
31 #include "GeolocationClient.h"
32 #include "GeolocationError.h"
33 #include "GeolocationPosition.h"
37 GeolocationController::GeolocationController(Page& page, GeolocationClient& client)
41 m_page.addActivityStateChangeObserver(*this);
44 GeolocationController::~GeolocationController()
46 ASSERT(m_observers.isEmpty());
48 // NOTE: We don't have to remove ourselves from page's ActivityStateChangeObserver set, since
49 // we are supplement of the Page, and our destructor getting called means the page is being
52 m_client.geolocationDestroyed();
55 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
57 // This may be called multiple times with the same observer, though removeObserver()
58 // is called only once with each.
59 bool wasEmpty = m_observers.isEmpty();
60 m_observers.add(observer);
61 if (enableHighAccuracy)
62 m_highAccuracyObservers.add(observer);
64 if (enableHighAccuracy)
65 m_client.setEnableHighAccuracy(true);
66 if (wasEmpty && m_page.isVisible())
67 m_client.startUpdating();
70 void GeolocationController::removeObserver(Geolocation* observer)
72 if (!m_observers.contains(observer))
75 m_observers.remove(observer);
76 m_highAccuracyObservers.remove(observer);
78 if (m_observers.isEmpty())
79 m_client.stopUpdating();
80 else if (m_highAccuracyObservers.isEmpty())
81 m_client.setEnableHighAccuracy(false);
84 void GeolocationController::requestPermission(Geolocation* geolocation)
86 if (!m_page.isVisible()) {
87 m_pendedPermissionRequest.add(geolocation);
91 m_client.requestPermission(geolocation);
94 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
96 if (m_pendedPermissionRequest.remove(geolocation))
99 m_client.cancelPermissionRequest(geolocation);
102 void GeolocationController::positionChanged(GeolocationPosition* position)
104 m_lastPosition = position;
105 Vector<RefPtr<Geolocation>> observersVector;
106 copyToVector(m_observers, observersVector);
107 for (auto& observer : observersVector)
108 observer->positionChanged();
111 void GeolocationController::errorOccurred(GeolocationError* error)
113 Vector<RefPtr<Geolocation>> observersVector;
114 copyToVector(m_observers, observersVector);
115 for (auto& observer : observersVector)
116 observer->setError(error);
119 GeolocationPosition* GeolocationController::lastPosition()
121 if (m_lastPosition.get())
122 return m_lastPosition.get();
124 return m_client.lastPosition();
127 void GeolocationController::activityStateDidChange(ActivityState::Flags oldActivityState, ActivityState::Flags newActivityState)
129 // Toggle GPS based on page visibility to save battery.
130 ActivityState::Flags changed = oldActivityState ^ newActivityState;
131 if (changed & ActivityState::IsVisible && !m_observers.isEmpty()) {
132 if (newActivityState & ActivityState::IsVisible)
133 m_client.startUpdating();
135 m_client.stopUpdating();
138 if (!m_page.isVisible())
141 HashSet<RefPtr<Geolocation>> pendedPermissionRequests = WTFMove(m_pendedPermissionRequest);
142 for (auto& permissionRequest : pendedPermissionRequests)
143 m_client.requestPermission(permissionRequest.get());
146 const char* GeolocationController::supplementName()
148 return "GeolocationController";
151 void provideGeolocationTo(Page* page, GeolocationClient* client)
155 Supplement<Page>::provideTo(page, GeolocationController::supplementName(), std::make_unique<GeolocationController>(*page, *client));
158 } // namespace WebCore
160 #endif // ENABLE(GEOLOCATION)