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 | |
29 | #if ENABLE(GEOLOCATION) |
30 | |
31 | #include "GeolocationClient.h" |
32 | #include "GeolocationError.h" |
33 | #include "GeolocationPosition.h" |
34 | |
35 | namespace WebCore { |
36 | |
37 | GeolocationController::GeolocationController(Page& page, GeolocationClient& client) |
38 | : m_page(page) |
39 | , m_client(client) |
40 | { |
41 | m_page.addActivityStateChangeObserver(*this); |
42 | } |
43 | |
44 | GeolocationController::~GeolocationController() |
45 | { |
46 | ASSERT(m_observers.isEmpty()); |
47 | |
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 |
50 | // torn down. |
51 | |
52 | m_client.geolocationDestroyed(); |
53 | } |
54 | |
55 | void GeolocationController::addObserver(Geolocation& observer, bool enableHighAccuracy) |
56 | { |
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); |
63 | |
64 | if (enableHighAccuracy) |
65 | m_client.setEnableHighAccuracy(true); |
66 | if (wasEmpty && m_page.isVisible()) |
67 | m_client.startUpdating(); |
68 | } |
69 | |
70 | void GeolocationController::removeObserver(Geolocation& observer) |
71 | { |
72 | if (!m_observers.contains(observer)) |
73 | return; |
74 | |
75 | m_observers.remove(observer); |
76 | m_highAccuracyObservers.remove(observer); |
77 | |
78 | if (m_observers.isEmpty()) |
79 | m_client.stopUpdating(); |
80 | else if (m_highAccuracyObservers.isEmpty()) |
81 | m_client.setEnableHighAccuracy(false); |
82 | } |
83 | |
84 | void GeolocationController::requestPermission(Geolocation& geolocation) |
85 | { |
86 | if (!m_page.isVisible()) { |
87 | m_pendingPermissionRequest.add(geolocation); |
88 | return; |
89 | } |
90 | |
91 | m_client.requestPermission(geolocation); |
92 | } |
93 | |
94 | void GeolocationController::cancelPermissionRequest(Geolocation& geolocation) |
95 | { |
96 | if (m_pendingPermissionRequest.remove(geolocation)) |
97 | return; |
98 | |
99 | m_client.cancelPermissionRequest(geolocation); |
100 | } |
101 | |
102 | void GeolocationController::positionChanged(const Optional<GeolocationPosition>& position) |
103 | { |
104 | m_lastPosition = position; |
105 | Vector<Ref<Geolocation>> observersVector; |
106 | observersVector.reserveInitialCapacity(m_observers.size()); |
107 | for (auto& observer : m_observers) |
108 | observersVector.uncheckedAppend(observer.copyRef()); |
109 | for (auto& observer : observersVector) |
110 | observer->positionChanged(); |
111 | } |
112 | |
113 | void GeolocationController::errorOccurred(GeolocationError& error) |
114 | { |
115 | Vector<Ref<Geolocation>> observersVector; |
116 | observersVector.reserveInitialCapacity(m_observers.size()); |
117 | for (auto& observer : m_observers) |
118 | observersVector.uncheckedAppend(observer.copyRef()); |
119 | for (auto& observer : observersVector) |
120 | observer->setError(error); |
121 | } |
122 | |
123 | Optional<GeolocationPosition> GeolocationController::lastPosition() |
124 | { |
125 | if (m_lastPosition) |
126 | return m_lastPosition.value(); |
127 | |
128 | return m_client.lastPosition(); |
129 | } |
130 | |
131 | void GeolocationController::activityStateDidChange(OptionSet<ActivityState::Flag> oldActivityState, OptionSet<ActivityState::Flag> newActivityState) |
132 | { |
133 | // Toggle GPS based on page visibility to save battery. |
134 | auto changed = oldActivityState ^ newActivityState; |
135 | if (changed & ActivityState::IsVisible && !m_observers.isEmpty()) { |
136 | if (newActivityState & ActivityState::IsVisible) |
137 | m_client.startUpdating(); |
138 | else |
139 | m_client.stopUpdating(); |
140 | } |
141 | |
142 | if (!m_page.isVisible()) |
143 | return; |
144 | |
145 | auto pendedPermissionRequests = WTFMove(m_pendingPermissionRequest); |
146 | for (auto& permissionRequest : pendedPermissionRequests) |
147 | m_client.requestPermission(permissionRequest.get()); |
148 | } |
149 | |
150 | const char* GeolocationController::supplementName() |
151 | { |
152 | return "GeolocationController" ; |
153 | } |
154 | |
155 | void provideGeolocationTo(Page* page, GeolocationClient& client) |
156 | { |
157 | ASSERT(page); |
158 | Supplement<Page>::provideTo(page, GeolocationController::supplementName(), std::make_unique<GeolocationController>(*page, client)); |
159 | } |
160 | |
161 | } // namespace WebCore |
162 | |
163 | #endif // ENABLE(GEOLOCATION) |
164 | |