Fix crash in ~WebProcessPool when using Geolocation with useNetworkProcess=true
[WebKit-https.git] / Source / WebKit2 / UIProcess / WebGeolocationManagerProxy.cpp
1 /*
2  * Copyright (C) 2011, 2012, 2013 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 "WebGeolocationManagerProxy.h"
28
29 #include "WebGeolocationManagerMessages.h"
30 #include "WebGeolocationManagerProxyMessages.h"
31 #include "WebProcessPool.h"
32
33 namespace WebKit {
34
35 const char* WebGeolocationManagerProxy::supplementName()
36 {
37     return "WebGeolocationManagerProxy";
38 }
39
40 PassRefPtr<WebGeolocationManagerProxy> WebGeolocationManagerProxy::create(WebProcessPool* processPool)
41 {
42     return adoptRef(new WebGeolocationManagerProxy(processPool));
43 }
44
45 WebGeolocationManagerProxy::WebGeolocationManagerProxy(WebProcessPool* processPool)
46     : WebContextSupplement(processPool)
47 {
48     WebContextSupplement::processPool()->addMessageReceiver(Messages::WebGeolocationManagerProxy::messageReceiverName(), *this);
49 }
50
51 void WebGeolocationManagerProxy::initializeProvider(const WKGeolocationProviderBase* provider)
52 {
53     m_provider.initialize(provider);
54 }
55
56 // WebContextSupplement
57
58 void WebGeolocationManagerProxy::processPoolDestroyed()
59 {
60     bool wasUpdating = isUpdating();
61     m_updateRequesters.clear();
62
63     ASSERT(!isUpdating());
64     if (wasUpdating)
65         m_provider.stopUpdating(this);
66 }
67
68 void WebGeolocationManagerProxy::processDidClose(WebProcessProxy* webProcessProxy)
69 {
70     removeRequester(webProcessProxy);
71 }
72
73 void WebGeolocationManagerProxy::refWebContextSupplement()
74 {
75     API::Object::ref();
76 }
77
78 void WebGeolocationManagerProxy::derefWebContextSupplement()
79 {
80     API::Object::deref();
81 }
82
83 void WebGeolocationManagerProxy::providerDidChangePosition(WebGeolocationPosition* position)
84 {
85     if (!processPool())
86         return;
87
88     processPool()->sendToAllProcesses(Messages::WebGeolocationManager::DidChangePosition(position->data()));
89 }
90
91 void WebGeolocationManagerProxy::providerDidFailToDeterminePosition(const String& errorMessage)
92 {
93     if (!processPool())
94         return;
95
96     processPool()->sendToAllProcesses(Messages::WebGeolocationManager::DidFailToDeterminePosition(errorMessage));
97 }
98
99 #if PLATFORM(IOS)
100 void WebGeolocationManagerProxy::resetPermissions()
101 {
102     processPool()->sendToAllProcesses(Messages::WebGeolocationManager::ResetPermissions());
103 }
104 #endif
105
106 void WebGeolocationManagerProxy::startUpdating(IPC::Connection& connection)
107 {
108     bool wasUpdating = isUpdating();
109     m_updateRequesters.add(connection.client());
110     if (!wasUpdating) {
111         m_provider.setEnableHighAccuracy(this, isHighAccuracyEnabled());
112         m_provider.startUpdating(this);
113     }
114 }
115
116 void WebGeolocationManagerProxy::stopUpdating(IPC::Connection& connection)
117 {
118     removeRequester(connection.client());
119 }
120
121 void WebGeolocationManagerProxy::removeRequester(const IPC::Connection::Client* client)
122 {
123     bool wasUpdating = isUpdating();
124     bool highAccuracyWasEnabled = isHighAccuracyEnabled();
125
126     m_highAccuracyRequesters.remove(client);
127     m_updateRequesters.remove(client);
128
129     if (wasUpdating && !isUpdating())
130         m_provider.stopUpdating(this);
131     else {
132         bool highAccuracyShouldBeEnabled = isHighAccuracyEnabled();
133         if (highAccuracyShouldBeEnabled != highAccuracyWasEnabled)
134             m_provider.setEnableHighAccuracy(this, highAccuracyShouldBeEnabled);
135     }
136 }
137
138 void WebGeolocationManagerProxy::setEnableHighAccuracy(IPC::Connection& connection, bool enabled)
139 {
140     bool highAccuracyWasEnabled = isHighAccuracyEnabled();
141
142     if (enabled)
143         m_highAccuracyRequesters.add(connection.client());
144     else
145         m_highAccuracyRequesters.remove(connection.client());
146
147     bool highAccuracyShouldBeEnabled = isHighAccuracyEnabled();
148     if (isUpdating() && highAccuracyWasEnabled != highAccuracyShouldBeEnabled)
149         m_provider.setEnableHighAccuracy(this, highAccuracyShouldBeEnabled);
150 }
151
152 } // namespace WebKit