WebGeolocationManager.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2011 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 "WebGeolocationManager.h"
27
28#include "WebGeolocationManagerProxyMessages.h"
29#include "WebPage.h"
30#include "WebProcess.h"
31
32#if ENABLE(CLIENT_BASED_GEOLOCATION)
33#include <WebCore/Geolocation.h>
34#include <WebCore/GeolocationController.h>
35#include <WebCore/GeolocationError.h>
36#include <WebCore/GeolocationPosition.h>
37#include <WebCore/Page.h>
38#endif
39
40using namespace WebCore;
41
42namespace WebKit {
43
44WebGeolocationManager::WebGeolocationManager(WebProcess* process)
45    : m_process(process)
46{
47}
48
49WebGeolocationManager::~WebGeolocationManager()
50{
51}
52
53void WebGeolocationManager::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
54{
55    didReceiveWebGeolocationManagerMessage(connection, messageID, arguments);
56}
57
58void WebGeolocationManager::registerWebPage(WebPage* page)
59{
60#if ENABLE(CLIENT_BASED_GEOLOCATION)
61    bool wasEmpty = m_pageSet.isEmpty();
62
63    m_pageSet.add(page);
64
65    if (wasEmpty)
66        m_process->connection()->send(Messages::WebGeolocationManagerProxy::StartUpdating(), 0);
67#endif
68}
69
70void WebGeolocationManager::unregisterWebPage(WebPage* page)
71{
72#if ENABLE(CLIENT_BASED_GEOLOCATION)
73    m_pageSet.remove(page);
74
75    if (m_pageSet.isEmpty())
76        m_process->connection()->send(Messages::WebGeolocationManagerProxy::StopUpdating(), 0);
77#endif
78}
79
80void WebGeolocationManager::didChangePosition(const WebGeolocationPosition::Data& data)
81{
82#if ENABLE(CLIENT_BASED_GEOLOCATION)
83    RefPtr<GeolocationPosition> position = GeolocationPosition::create(data.timestamp, data.latitude, data.longitude, data.accuracy);
84
85    HashSet<WebPage*>::const_iterator it = m_pageSet.begin();
86    HashSet<WebPage*>::const_iterator end = m_pageSet.end();
87    for (; it != end; ++it) {
88        WebPage* page = *it;
89        if (page->corePage())
90            page->corePage()->geolocationController()->positionChanged(position.get());
91    }
92#endif
93}
94
95void WebGeolocationManager::didFailToDeterminePosition()
96{
97#if ENABLE(CLIENT_BASED_GEOLOCATION)
98    // FIXME: Add localized error string.
99    RefPtr<GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, /* Localized error string */ String(""));
100
101    HashSet<WebPage*>::const_iterator it = m_pageSet.begin();
102    HashSet<WebPage*>::const_iterator end = m_pageSet.end();
103    for (; it != end; ++it) {
104        WebPage* page = *it;
105        if (page->corePage())
106            page->corePage()->geolocationController()->errorOccurred(error.get());
107    }
108#endif
109}
110
111} // namespace WebKit
112