Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / geolocation / GeolocationController.cpp
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
28 #include "modules/geolocation/GeolocationController.h"
29
30 #include "core/inspector/InspectorController.h"
31 #include "modules/geolocation/GeolocationClient.h"
32 #include "modules/geolocation/GeolocationError.h"
33 #include "modules/geolocation/GeolocationInspectorAgent.h"
34 #include "modules/geolocation/GeolocationPosition.h"
35
36 namespace WebCore {
37
38 GeolocationController::GeolocationController(Page& page, GeolocationClient* client)
39     : PageLifecycleObserver(&page)
40     , m_client(client)
41     , m_hasClientForTest(false)
42     , m_isClientUpdating(false)
43     , m_inspectorAgent()
44 {
45     OwnPtr<GeolocationInspectorAgent> geolocationAgent(GeolocationInspectorAgent::create(this));
46     m_inspectorAgent = geolocationAgent.get();
47     page.inspectorController().registerModuleAgent(geolocationAgent.release());
48 }
49
50 void GeolocationController::startUpdatingIfNeeded()
51 {
52     if (m_isClientUpdating)
53         return;
54     m_isClientUpdating = true;
55     m_client->startUpdating();
56 }
57
58 void GeolocationController::stopUpdatingIfNeeded()
59 {
60     if (!m_isClientUpdating)
61         return;
62     m_isClientUpdating = false;
63     m_client->stopUpdating();
64 }
65
66 GeolocationController::~GeolocationController()
67 {
68     ASSERT(m_observers.isEmpty());
69 }
70
71 void GeolocationController::willBeDestroyed()
72 {
73     if (m_client)
74         m_client->geolocationDestroyed();
75 }
76
77 PassOwnPtrWillBeRawPtr<GeolocationController> GeolocationController::create(Page& page, GeolocationClient* client)
78 {
79     return adoptPtrWillBeNoop(new GeolocationController(page, client));
80 }
81
82 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
83 {
84     // This may be called multiple times with the same observer, though removeObserver()
85     // is called only once with each.
86     bool wasEmpty = m_observers.isEmpty();
87     m_observers.add(observer);
88     if (enableHighAccuracy)
89         m_highAccuracyObservers.add(observer);
90
91     if (m_client) {
92         if (enableHighAccuracy)
93             m_client->setEnableHighAccuracy(true);
94         if (wasEmpty && page() && page()->visibilityState() == PageVisibilityStateVisible)
95             startUpdatingIfNeeded();
96     }
97 }
98
99 void GeolocationController::removeObserver(Geolocation* observer)
100 {
101     if (!m_observers.contains(observer))
102         return;
103
104     m_observers.remove(observer);
105     m_highAccuracyObservers.remove(observer);
106
107     if (m_client) {
108         if (m_observers.isEmpty())
109             stopUpdatingIfNeeded();
110         else if (m_highAccuracyObservers.isEmpty())
111             m_client->setEnableHighAccuracy(false);
112     }
113 }
114
115 void GeolocationController::requestPermission(Geolocation* geolocation)
116 {
117     if (m_client)
118         m_client->requestPermission(geolocation);
119 }
120
121 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
122 {
123     if (m_client)
124         m_client->cancelPermissionRequest(geolocation);
125 }
126
127 void GeolocationController::positionChanged(GeolocationPosition* position)
128 {
129     position = m_inspectorAgent->overrideGeolocationPosition(position);
130     if (!position) {
131         errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, "PositionUnavailable").get());
132         return;
133     }
134     m_lastPosition = position;
135     WillBeHeapVector<RefPtrWillBeMember<Geolocation> > observersVector;
136     copyToVector(m_observers, observersVector);
137     for (size_t i = 0; i < observersVector.size(); ++i)
138         observersVector[i]->positionChanged();
139 }
140
141 void GeolocationController::errorOccurred(GeolocationError* error)
142 {
143     WillBeHeapVector<RefPtrWillBeMember<Geolocation> > observersVector;
144     copyToVector(m_observers, observersVector);
145     for (size_t i = 0; i < observersVector.size(); ++i)
146         observersVector[i]->setError(error);
147 }
148
149 GeolocationPosition* GeolocationController::lastPosition()
150 {
151     if (m_lastPosition.get())
152         return m_lastPosition.get();
153
154     if (!m_client)
155         return 0;
156
157     return m_client->lastPosition();
158 }
159
160 void GeolocationController::setClientForTest(GeolocationClient* client)
161 {
162     m_client = client;
163     m_hasClientForTest = true;
164 }
165
166 void GeolocationController::pageVisibilityChanged()
167 {
168     if (m_observers.isEmpty() || !m_client)
169         return;
170
171     if (page() && page()->visibilityState() == PageVisibilityStateVisible)
172         startUpdatingIfNeeded();
173     else
174         stopUpdatingIfNeeded();
175 }
176
177 const char* GeolocationController::supplementName()
178 {
179     return "GeolocationController";
180 }
181
182 void GeolocationController::trace(Visitor* visitor)
183 {
184     visitor->trace(m_lastPosition);
185     visitor->trace(m_observers);
186     visitor->trace(m_highAccuracyObservers);
187     WillBeHeapSupplement<Page>::trace(visitor);
188 }
189
190 void provideGeolocationTo(Page& page, GeolocationClient* client)
191 {
192     WillBeHeapSupplement<Page>::provideTo(page, GeolocationController::supplementName(), GeolocationController::create(page, client));
193 }
194
195 } // namespace WebCore