initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WebGeolocationPermissionRequestManager.cpp
1 /*
2  * Copyright (C) 2010 Google 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebGeolocationPermissionRequestManager.h"
28
29 #include "WebGeolocationPermissionRequest.h"
30 #include <wtf/HashMap.h>
31
32 namespace WebCore {
33 class Geolocation;
34 }
35
36 using namespace WebCore;
37
38 typedef HashMap<Geolocation*, int> GeolocationIdMap;
39 typedef HashMap<int, Geolocation*> IdGeolocationMap;
40
41 namespace WebKit {
42 class WebGeolocationPermissionRequestManagerPrivate {
43 public:
44     GeolocationIdMap m_geolocationIdMap;
45     IdGeolocationMap m_idGeolocationMap;
46 };
47 }
48
49 using namespace WebKit;
50
51 int WebGeolocationPermissionRequestManager::add(const WebKit::WebGeolocationPermissionRequest& permissionRequest)
52 {
53     Geolocation* geolocation = permissionRequest.geolocation();
54     ASSERT(!m_private->m_geolocationIdMap.contains(geolocation));
55     int id = ++m_lastId;
56     m_private->m_geolocationIdMap.add(geolocation, id);
57     m_private->m_idGeolocationMap.add(id, geolocation);
58     return id;
59 }
60
61 bool WebGeolocationPermissionRequestManager::remove(const WebKit::WebGeolocationPermissionRequest& permissionRequest, int& id)
62 {
63     Geolocation* geolocation = permissionRequest.geolocation();
64     GeolocationIdMap::iterator it = m_private->m_geolocationIdMap.find(geolocation);
65     if (it == m_private->m_geolocationIdMap.end())
66         return false;
67     id = it->second;
68     m_private->m_geolocationIdMap.remove(it);
69     m_private->m_idGeolocationMap.remove(id);
70     return true;
71 }
72
73 bool WebGeolocationPermissionRequestManager::remove(int id, WebKit::WebGeolocationPermissionRequest& permissionRequest)
74 {
75     IdGeolocationMap::iterator it = m_private->m_idGeolocationMap.find(id);
76     if (it == m_private->m_idGeolocationMap.end())
77         return false;
78     Geolocation* geolocation = it->second;
79     permissionRequest = WebGeolocationPermissionRequest(geolocation);
80     m_private->m_idGeolocationMap.remove(it);
81     m_private->m_geolocationIdMap.remove(geolocation);
82     return true;
83 }
84
85 void WebGeolocationPermissionRequestManager::init()
86 {
87     m_lastId = 0;
88     m_private.reset(new WebGeolocationPermissionRequestManagerPrivate);
89 }
90
91 void WebGeolocationPermissionRequestManager::reset()
92 {
93     m_private.reset(0);
94 }
95