1/*
2 * Copyright (C) 2010 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 "WebKitDLL.h"
28#include "WebGeolocationClient.h"
29
30#include "WebFrame.h"
31#include "WebGeolocationPolicyListener.h"
32#include "WebGeolocationPosition.h"
33#include "WebSecurityOrigin.h"
34#include "WebView.h"
35#include <WebCore/Document.h>
36#include <WebCore/Frame.h>
37#include <WebCore/Geolocation.h>
38#include <WebCore/SecurityOrigin.h>
39
40using namespace WebCore;
41
42WebGeolocationClient::WebGeolocationClient(WebView* webView)
43    : m_webView(webView)
44{
45}
46
47void WebGeolocationClient::geolocationDestroyed()
48{
49    delete this;
50}
51
52void WebGeolocationClient::startUpdating()
53{
54    COMPtr<IWebGeolocationProvider> provider;
55    if (FAILED(m_webView->geolocationProvider(&provider)))
56        return;
57    provider->registerWebView(m_webView.get());
58}
59
60void WebGeolocationClient::stopUpdating()
61{
62    COMPtr<IWebGeolocationProvider> provider;
63    if (FAILED(m_webView->geolocationProvider(&provider)))
64        return;
65    provider->unregisterWebView(m_webView.get());
66}
67
68GeolocationPosition* WebGeolocationClient::lastPosition()
69{
70    COMPtr<IWebGeolocationProvider> provider;
71    if (FAILED(m_webView->geolocationProvider(&provider)))
72        return 0;
73    COMPtr<IWebGeolocationPosition> position;
74    if (FAILED(provider->lastPosition(&position)))
75        return 0;
76    return core(position.get());
77}
78
79void WebGeolocationClient::requestPermission(Geolocation* geolocation)
80{
81    COMPtr<IWebUIDelegate> uiDelegate;
82    if (FAILED(m_webView->uiDelegate(&uiDelegate))) {
83        geolocation->setIsAllowed(false);
84        return;
85    }
86
87    COMPtr<IWebUIDelegatePrivate2> uiDelegatePrivate2(Query, uiDelegate);
88    if (!uiDelegatePrivate2) {
89        geolocation->setIsAllowed(false);
90        return;
91    }
92
93    Frame* frame = geolocation->frame();
94    COMPtr<WebSecurityOrigin> origin(AdoptCOM, WebSecurityOrigin::createInstance(frame->document()->securityOrigin()));
95    COMPtr<WebGeolocationPolicyListener> listener = WebGeolocationPolicyListener::createInstance(geolocation);
96    HRESULT hr = uiDelegatePrivate2->decidePolicyForGeolocationRequest(m_webView.get(), kit(frame), origin.get(), listener.get());
97    if (hr != E_NOTIMPL)
98        return;
99
100    geolocation->setIsAllowed(false);
101}
102