1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* Copyright 2009, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GeolocationPermissions.h"
#include "DOMWindow.h"
#include "Frame.h"
#include "Geolocation.h"
#include "Navigator.h"
#include "SQLiteDatabase.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include "WebViewCore.h"
using namespace WebCore;
namespace android {
GeolocationPermissions::PermissionsMap GeolocationPermissions::s_permanentPermissions;
GeolocationPermissions::GeolocationPermissionsVector GeolocationPermissions::s_instances;
bool GeolocationPermissions::s_alwaysDeny = false;
bool GeolocationPermissions::s_permanentPermissionsLoaded = false;
bool GeolocationPermissions::s_permanentPermissionsModified = false;
String GeolocationPermissions::s_databasePath;
static const char* databaseName = "/GeolocationPermissions.db";
GeolocationPermissions::GeolocationPermissions(WebViewCore* webViewCore, Frame* mainFrame)
: m_webViewCore(webViewCore)
, m_mainFrame(mainFrame)
, m_timer(this, &GeolocationPermissions::timerFired)
{
ASSERT(m_webViewCore);
maybeLoadPermanentPermissions();
s_instances.append(this);
}
GeolocationPermissions::~GeolocationPermissions()
{
size_t index = s_instances.find(this);
s_instances.remove(index);
}
void GeolocationPermissions::queryPermissionState(Frame* frame)
{
ASSERT(s_permanentPermissionsLoaded);
// We use SecurityOrigin::toString to key the map. Note that testing
// the SecurityOrigin pointer for equality is insufficient.
String originString = frame->document()->securityOrigin()->toString();
// If we've been told to always deny requests, do so.
if (s_alwaysDeny) {
makeAsynchronousCallbackToGeolocation(originString, false);
return;
}
// See if we have a record for this origin in the permanent permissions.
// These take precedence over temporary permissions so that changes made
// from the browser settings work as intended.
PermissionsMap::const_iterator iter = s_permanentPermissions.find(originString);
PermissionsMap::const_iterator end = s_permanentPermissions.end();
if (iter != end) {
bool allow = iter->second;
makeAsynchronousCallbackToGeolocation(originString, allow);
return;
}
// Check the temporary permisions.
iter = m_temporaryPermissions.find(originString);
end = m_temporaryPermissions.end();
if (iter != end) {
bool allow = iter->second;
makeAsynchronousCallbackToGeolocation(originString, allow);
return;
}
// If there's no pending request, prompt the user.
if (m_originInProgress.isEmpty()) {
m_originInProgress = originString;
// Although multiple tabs may request permissions for the same origin
// simultaneously, the routing in WebViewCore/CallbackProxy ensures that
// the result of the request will make it back to this object, so
// there's no need for a globally unique ID for the request.
m_webViewCore->geolocationPermissionsShowPrompt(m_originInProgress);
return;
}
// If the request in progress is not for this origin, queue this request.
if ((m_originInProgress != originString)
&& (m_queuedOrigins.find(originString) == WTF::notFound))
m_queuedOrigins.append(originString);
}
void GeolocationPermissions::makeAsynchronousCallbackToGeolocation(String origin, bool allow)
{
m_callbackData.origin = origin;
m_callbackData.allow = allow;
m_timer.startOneShot(0);
}
void GeolocationPermissions::providePermissionState(String origin, bool allow, bool remember)
{
ASSERT(s_permanentPermissionsLoaded);
// It's possible that this method is called with an origin that doesn't
// match m_originInProgress. This can occur if this object is reset
// while a permission result is in the process of being marshalled back to
// the WebCore thread from the browser. In this case, we simply ignore the
// call.
if (origin != m_originInProgress)
return;
maybeCallbackFrames(m_originInProgress, allow);
recordPermissionState(origin, allow, remember);
// If the permissions are set to be remembered, cancel any queued requests
// for this domain in other tabs.
if (remember)
cancelPendingRequestsInOtherTabs(m_originInProgress);
// Clear the origin in progress to signal that this request is done.
m_originInProgress = "";
// If there are other requests queued, start the next one.
if (!m_queuedOrigins.isEmpty()) {
m_originInProgress = m_queuedOrigins.first();
m_queuedOrigins.remove(0);
m_webViewCore->geolocationPermissionsShowPrompt(m_originInProgress);
}
}
void GeolocationPermissions::recordPermissionState(String origin, bool allow, bool remember)
{
if (remember) {
s_permanentPermissions.set(m_originInProgress, allow);
s_permanentPermissionsModified = true;
} else {
// It's possible that another tab recorded a permanent permission for
// this origin while our request was in progress, but we record it
// anyway.
m_temporaryPermissions.set(m_originInProgress, allow);
}
}
void GeolocationPermissions::cancelPendingRequestsInOtherTabs(String origin)
{
for (GeolocationPermissionsVector::const_iterator iter = s_instances.begin();
iter != s_instances.end();
++iter)
(*iter)->cancelPendingRequests(origin);
}
void GeolocationPermissions::cancelPendingRequests(String origin)
{
size_t index = m_queuedOrigins.find(origin);
if (index != WTF::notFound) {
// Get the permission from the permanent list.
PermissionsMap::const_iterator iter = s_permanentPermissions.find(origin);
#ifndef NDEBUG
PermissionsMap::const_iterator end = s_permanentPermissions.end();
ASSERT(iter != end);
#endif
bool allow = iter->second;
maybeCallbackFrames(origin, allow);
m_queuedOrigins.remove(index);
}
}
void GeolocationPermissions::timerFired(Timer<GeolocationPermissions>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
maybeCallbackFrames(m_callbackData.origin, m_callbackData.allow);
}
void GeolocationPermissions::resetTemporaryPermissionStates()
{
ASSERT(s_permanentPermissionsLoaded);
m_originInProgress = "";
m_queuedOrigins.clear();
m_temporaryPermissions.clear();
// If any permission results are being marshalled back to this thread, this
// will render them inefective.
m_timer.stop();
m_webViewCore->geolocationPermissionsHidePrompt();
}
void GeolocationPermissions::maybeCallbackFrames(String origin, bool allow)
{
// We can't track which frame issued the request, as frames can be deleted
// or have their contents replaced. Even uniqueChildName is not unique when
// frames are dynamically deleted and created. Instead, we simply call back
// to the Geolocation object in all frames from the correct origin.
for (Frame* frame = m_mainFrame; frame; frame = frame->tree()->traverseNext()) {
if (origin == frame->document()->securityOrigin()->toString()) {
// If the page has changed, it may no longer have a Geolocation
// object.
Geolocation* geolocation = frame->domWindow()->navigator()->optionalGeolocation();
if (geolocation)
geolocation->setIsAllowed(allow);
}
}
}
GeolocationPermissions::OriginSet GeolocationPermissions::getOrigins()
{
maybeLoadPermanentPermissions();
OriginSet origins;
PermissionsMap::const_iterator end = s_permanentPermissions.end();
for (PermissionsMap::const_iterator iter = s_permanentPermissions.begin(); iter != end; ++iter)
origins.add(iter->first);
return origins;
}
bool GeolocationPermissions::getAllowed(String origin)
{
maybeLoadPermanentPermissions();
bool allowed = false;
PermissionsMap::const_iterator iter = s_permanentPermissions.find(origin);
PermissionsMap::const_iterator end = s_permanentPermissions.end();
if (iter != end)
allowed = iter->second;
return allowed;
}
void GeolocationPermissions::clear(String origin)
{
maybeLoadPermanentPermissions();
PermissionsMap::iterator iter = s_permanentPermissions.find(origin);
if (iter != s_permanentPermissions.end()) {
s_permanentPermissions.remove(iter);
s_permanentPermissionsModified = true;
}
}
void GeolocationPermissions::allow(String origin)
{
maybeLoadPermanentPermissions();
// We replace any existing permanent permission.
s_permanentPermissions.set(origin, true);
s_permanentPermissionsModified = true;
}
void GeolocationPermissions::clearAll()
{
maybeLoadPermanentPermissions();
s_permanentPermissions.clear();
s_permanentPermissionsModified = true;
}
void GeolocationPermissions::maybeLoadPermanentPermissions()
{
if (s_permanentPermissionsLoaded)
return;
s_permanentPermissionsLoaded = true;
SQLiteDatabase database;
if (!database.open(s_databasePath + databaseName))
return;
// Create the table here, such that even if we've just created the DB, the
// commands below should succeed.
if (!database.executeCommand("CREATE TABLE IF NOT EXISTS Permissions (origin TEXT UNIQUE NOT NULL, allow INTEGER NOT NULL)")) {
database.close();
return;
}
SQLiteStatement statement(database, "SELECT * FROM Permissions");
if (statement.prepare() != SQLResultOk) {
database.close();
return;
}
ASSERT(s_permanentPermissions.size() == 0);
while (statement.step() == SQLResultRow)
s_permanentPermissions.set(statement.getColumnText(0), statement.getColumnInt64(1));
database.close();
}
void GeolocationPermissions::maybeStorePermanentPermissions()
{
// If the permanent permissions haven't been modified, there's no need to
// save them to the DB. (If we haven't even loaded them, writing them now
// would overwrite the stored permissions with the empty set.)
if (!s_permanentPermissionsModified)
return;
SQLiteDatabase database;
if (!database.open(s_databasePath + databaseName))
return;
SQLiteTransaction transaction(database);
// The number of entries should be small enough that it's not worth trying
// to perform a diff. Simply clear the table and repopulate it.
if (!database.executeCommand("DELETE FROM Permissions")) {
database.close();
return;
}
PermissionsMap::const_iterator end = s_permanentPermissions.end();
for (PermissionsMap::const_iterator iter = s_permanentPermissions.begin(); iter != end; ++iter) {
SQLiteStatement statement(database, "INSERT INTO Permissions (origin, allow) VALUES (?, ?)");
if (statement.prepare() != SQLResultOk)
continue;
statement.bindText(1, iter->first);
statement.bindInt64(2, iter->second);
statement.executeCommand();
}
transaction.commit();
database.close();
s_permanentPermissionsModified = false;
}
void GeolocationPermissions::setDatabasePath(String path)
{
// Take the first non-empty value.
if (s_databasePath.length() > 0)
return;
s_databasePath = path;
}
void GeolocationPermissions::setAlwaysDeny(bool deny)
{
s_alwaysDeny = deny;
}
} // namespace android
|