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 "config.h"
27#include "GeolocationPermissionRequestManager.h"
28
29#include "WebCoreArgumentCoders.h"
30#include "WebFrame.h"
31#include "WebPage.h"
32#include "WebPageProxyMessages.h"
33#include <WebCore/Frame.h>
34#include <WebCore/Geolocation.h>
35#include <WebCore/SecurityOrigin.h>
36
37using namespace WebCore;
38
39namespace WebKit {
40
41static uint64_t generateGeolocationID()
42{
43    static uint64_t uniqueGeolocationID = 1;
44    return uniqueGeolocationID++;
45}
46
47GeolocationPermissionRequestManager::GeolocationPermissionRequestManager(WebPage* page)
48    : m_page(page)
49{
50}
51
52void GeolocationPermissionRequestManager::startRequestForGeolocation(Geolocation* geolocation)
53{
54    uint64_t geolocationID = generateGeolocationID();
55
56    m_geolocationToIDMap.set(geolocation, geolocationID);
57    m_idToGeolocationMap.set(geolocationID, geolocation);
58
59
60    Frame* frame = geolocation->frame();
61
62    WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(frame->loader()->client())->webFrame();
63    SecurityOrigin* origin = frame->document()->securityOrigin();
64
65    m_page->send(Messages::WebPageProxy::RequestGeolocationPermissionForFrame(geolocationID, webFrame->frameID(), origin->databaseIdentifier()));
66}
67
68void GeolocationPermissionRequestManager::cancelRequestForGeolocation(Geolocation* geolocation)
69{
70    GeolocationToIDMap::iterator it = m_geolocationToIDMap.find(geolocation);
71    if (it == m_geolocationToIDMap.end())
72        return;
73
74    m_geolocationToIDMap.remove(it);
75    m_idToGeolocationMap.remove(it->second);
76}
77
78void GeolocationPermissionRequestManager::didReceiveGeolocationPermissionDecision(uint64_t geolocationID, bool allowed)
79{
80    IDToGeolocationMap::iterator it = m_idToGeolocationMap.find(geolocationID);
81    if (it == m_idToGeolocationMap.end())
82        return;
83
84    Geolocation* geolocation = it->second;
85    geolocation->setIsAllowed(allowed);
86
87    m_idToGeolocationMap.remove(it);
88    m_geolocationToIDMap.remove(geolocation);
89}
90
91} // namespace WebKit
92