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/Frame.h>
36#include <WebCore/Geolocation.h>
37#include <WebCore/SecurityOrigin.h>
38
39using namespace WebCore;
40
41WebGeolocationClient::WebGeolocationClient(WebView* webView)
42    : m_webView(webView)
43{
44}
45
46void WebGeolocationClient::geolocationDestroyed()
47{
48    delete this;
49}
50
51void WebGeolocationClient::startUpdating()
52{
53    COMPtr<IWebGeolocationProvider> provider;
54    if (FAILED(m_webView->geolocationProvider(&provider)))
55        return;
56    provider->registerWebView(m_webView.get());
57}
58
59void WebGeolocationClient::stopUpdating()
60{
61    COMPtr<IWebGeolocationProvider> provider;
62    if (FAILED(m_webView->geolocationProvider(&provider)))
63        return;
64    provider->unregisterWebView(m_webView.get());
65}
66
67GeolocationPosition* WebGeolocationClient::lastPosition()
68{
69#if ENABLE(CLIENT_BASED_GEOLOCATION)
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#else
78    return 0;
79#endif
80}
81
82void WebGeolocationClient::requestPermission(Geolocation* geolocation)
83{
84    COMPtr<IWebUIDelegate> uiDelegate;
85    if (FAILED(m_webView->uiDelegate(&uiDelegate))) {
86        geolocation->setIsAllowed(false);
87        return;
88    }
89
90    COMPtr<IWebUIDelegatePrivate2> uiDelegatePrivate2(Query, uiDelegate);
91    if (!uiDelegatePrivate2) {
92        geolocation->setIsAllowed(false);
93        return;
94    }
95
96    Frame* frame = geolocation->frame();
97    COMPtr<WebSecurityOrigin> origin(AdoptCOM, WebSecurityOrigin::createInstance(frame->document()->securityOrigin()));
98    COMPtr<WebGeolocationPolicyListener> listener = WebGeolocationPolicyListener::createInstance(geolocation);
99    HRESULT hr = uiDelegatePrivate2->decidePolicyForGeolocationRequest(m_webView.get(), kit(frame), origin.get(), listener.get());
100    if (hr != E_NOTIMPL)
101        return;
102
103    geolocation->setIsAllowed(false);
104}
105