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