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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/aw_permission_manager.h"
#include <string>
#include "android_webview/browser/aw_browser_permission_request_delegate.h"
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/logging.h"
#include "content/public/browser/permission_type.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
using content::PermissionStatus;
using content::PermissionType;
namespace android_webview {
class LastRequestResultCache {
public:
LastRequestResultCache() = default;
void SetResult(PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin,
PermissionStatus status) {
DCHECK(status == content::PermissionStatus::GRANTED ||
status == content::PermissionStatus::DENIED);
// TODO(ddorwin): We should be denying empty origins at a higher level.
if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
DLOG(WARNING) << "Not caching result because of empty origin.";
return;
}
if (!requesting_origin.is_valid()) {
NOTREACHED() << requesting_origin.possibly_invalid_spec();
return;
}
if (!embedding_origin.is_valid()) {
NOTREACHED() << embedding_origin.possibly_invalid_spec();
return;
}
if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
// Other permissions are not cached.
return;
}
std::string key = GetCacheKey(requesting_origin, embedding_origin);
if (key.empty()) {
NOTREACHED();
// Never store an empty key because it could inadvertently be used for
// another combination.
return;
}
pmi_result_cache_[key] = status;
}
PermissionStatus GetResult(PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) const {
// TODO(ddorwin): We should be denying empty origins at a higher level.
if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
return content::PermissionStatus::ASK;
}
DCHECK(requesting_origin.is_valid())
<< requesting_origin.possibly_invalid_spec();
DCHECK(embedding_origin.is_valid())
<< embedding_origin.possibly_invalid_spec();
if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
NOTREACHED() << "Results are only cached for PROTECTED_MEDIA_IDENTIFIER";
return content::PermissionStatus::ASK;
}
std::string key = GetCacheKey(requesting_origin, embedding_origin);
StatusMap::const_iterator it = pmi_result_cache_.find(key);
if (it == pmi_result_cache_.end()) {
DLOG(WARNING) << "GetResult() called for uncached origins: " << key;
return content::PermissionStatus::ASK;
}
DCHECK(!key.empty());
return it->second;
}
void ClearResult(PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
// TODO(ddorwin): We should be denying empty origins at a higher level.
if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
return;
}
DCHECK(requesting_origin.is_valid())
<< requesting_origin.possibly_invalid_spec();
DCHECK(embedding_origin.is_valid())
<< embedding_origin.possibly_invalid_spec();
if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
// Other permissions are not cached, so nothing to clear.
return;
}
std::string key = GetCacheKey(requesting_origin, embedding_origin);
pmi_result_cache_.erase(key);
}
private:
// Returns a concatenation of the origins to be used as the index.
// Returns the empty string if either origin is invalid or empty.
static std::string GetCacheKey(const GURL& requesting_origin,
const GURL& embedding_origin) {
const std::string& requesting = requesting_origin.spec();
const std::string& embedding = embedding_origin.spec();
if (requesting.empty() || embedding.empty())
return std::string();
return requesting + "," + embedding;
}
using StatusMap = base::hash_map<std::string, PermissionStatus>;
StatusMap pmi_result_cache_;
DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache);
};
struct AwPermissionManager::PendingRequest {
public:
PendingRequest(PermissionType permission,
GURL requesting_origin,
GURL embedding_origin,
content::RenderFrameHost* render_frame_host,
const base::Callback<void(PermissionStatus)>& callback)
: permission(permission),
requesting_origin(requesting_origin),
embedding_origin(embedding_origin),
render_process_id(render_frame_host->GetProcess()->GetID()),
render_frame_id(render_frame_host->GetRoutingID()),
callback(callback) {
}
~PendingRequest() = default;
PermissionType permission;
GURL requesting_origin;
GURL embedding_origin;
int render_process_id;
int render_frame_id;
base::Callback<void(PermissionStatus)> callback;
};
AwPermissionManager::AwPermissionManager()
: content::PermissionManager(),
result_cache_(new LastRequestResultCache),
weak_ptr_factory_(this) {
}
AwPermissionManager::~AwPermissionManager() {
}
int AwPermissionManager::RequestPermission(
PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(PermissionStatus)>& callback) {
int render_process_id = render_frame_host->GetProcess()->GetID();
int render_frame_id = render_frame_host->GetRoutingID();
AwBrowserPermissionRequestDelegate* delegate =
AwBrowserPermissionRequestDelegate::FromID(render_process_id,
render_frame_id);
if (!delegate) {
DVLOG(0) << "Dropping permission request for "
<< static_cast<int>(permission);
callback.Run(content::PermissionStatus::DENIED);
return kNoPendingOperation;
}
// Do not delegate any requests which are already pending.
bool should_delegate_request = true;
for (PendingRequestsMap::Iterator<PendingRequest> it(&pending_requests_);
!it.IsAtEnd(); it.Advance()) {
if (permission == it.GetCurrentValue()->permission) {
should_delegate_request = false;
break;
}
}
const GURL& embedding_origin =
content::WebContents::FromRenderFrameHost(render_frame_host)
->GetLastCommittedURL().GetOrigin();
int request_id = kNoPendingOperation;
switch (permission) {
case PermissionType::GEOLOCATION:
request_id = pending_requests_.Add(new PendingRequest(
permission, requesting_origin,
embedding_origin, render_frame_host,
callback));
if (should_delegate_request) {
delegate->RequestGeolocationPermission(
requesting_origin,
base::Bind(&OnRequestResponse,
weak_ptr_factory_.GetWeakPtr(), request_id,
callback));
}
break;
case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
request_id = pending_requests_.Add(new PendingRequest(
permission, requesting_origin,
embedding_origin, render_frame_host,
callback));
if (should_delegate_request) {
delegate->RequestProtectedMediaIdentifierPermission(
requesting_origin,
base::Bind(&OnRequestResponse,
weak_ptr_factory_.GetWeakPtr(), request_id,
callback));
}
break;
case PermissionType::MIDI_SYSEX:
request_id = pending_requests_.Add(new PendingRequest(
permission, requesting_origin,
embedding_origin, render_frame_host,
callback));
if (should_delegate_request) {
delegate->RequestMIDISysexPermission(
requesting_origin,
base::Bind(&OnRequestResponse,
weak_ptr_factory_.GetWeakPtr(), request_id,
callback));
}
break;
case PermissionType::AUDIO_CAPTURE:
case PermissionType::VIDEO_CAPTURE:
case PermissionType::NOTIFICATIONS:
case PermissionType::PUSH_MESSAGING:
case PermissionType::DURABLE_STORAGE:
NOTIMPLEMENTED() << "RequestPermission is not implemented for "
<< static_cast<int>(permission);
callback.Run(content::PermissionStatus::DENIED);
break;
case PermissionType::MIDI:
callback.Run(content::PermissionStatus::GRANTED);
break;
case PermissionType::NUM:
NOTREACHED() << "PermissionType::NUM was not expected here.";
callback.Run(content::PermissionStatus::DENIED);
break;
}
return request_id;
}
int AwPermissionManager::RequestPermissions(
const std::vector<PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(
const std::vector<PermissionStatus>&)>& callback) {
NOTIMPLEMENTED() << "RequestPermissions has not been implemented in WebView";
std::vector<PermissionStatus> result(permissions.size());
const GURL& embedding_origin =
content::WebContents::FromRenderFrameHost(render_frame_host)
->GetLastCommittedURL().GetOrigin();
for (PermissionType type : permissions) {
result.push_back(GetPermissionStatus(
type, requesting_origin, embedding_origin));
}
callback.Run(result);
return kNoPendingOperation;
}
// static
void AwPermissionManager::OnRequestResponse(
const base::WeakPtr<AwPermissionManager>& manager,
int request_id,
const base::Callback<void(PermissionStatus)>& callback,
bool allowed) {
PermissionStatus status = allowed ? content::PermissionStatus::GRANTED
: content::PermissionStatus::DENIED;
if (manager.get()) {
PendingRequest* pending_request =
manager->pending_requests_.Lookup(request_id);
for (PendingRequestsMap::Iterator<PendingRequest> it(
&manager->pending_requests_);
!it.IsAtEnd(); it.Advance()) {
if (pending_request->permission == it.GetCurrentValue()->permission &&
it.GetCurrentKey() != request_id) {
it.GetCurrentValue()->callback.Run(status);
manager->pending_requests_.Remove(it.GetCurrentKey());
}
}
manager->result_cache_->SetResult(
pending_request->permission,
pending_request->requesting_origin,
pending_request->embedding_origin,
status);
manager->pending_requests_.Remove(request_id);
}
callback.Run(status);
}
void AwPermissionManager::CancelPermissionRequest(int request_id) {
PendingRequest* pending_request = pending_requests_.Lookup(request_id);
if (!pending_request)
return;
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(pending_request->render_process_id,
pending_request->render_frame_id);
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
DCHECK(web_contents);
// The caller is canceling (presumably) the most recent request. Assuming the
// request did not complete, the user did not respond to the requset.
// Thus, assume we do not know the result.
const GURL& embedding_origin = web_contents
->GetLastCommittedURL().GetOrigin();
result_cache_->ClearResult(
pending_request->permission,
pending_request->requesting_origin,
embedding_origin);
AwBrowserPermissionRequestDelegate* delegate =
AwBrowserPermissionRequestDelegate::FromID(
pending_request->render_process_id,
pending_request->render_frame_id);
if (!delegate) {
pending_requests_.Remove(request_id);
return;
}
switch (pending_request->permission) {
case PermissionType::GEOLOCATION:
delegate->CancelGeolocationPermissionRequests(
pending_request->requesting_origin);
break;
case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
delegate->CancelProtectedMediaIdentifierPermissionRequests(
pending_request->requesting_origin);
break;
case PermissionType::MIDI_SYSEX:
delegate->CancelMIDISysexPermissionRequests(
pending_request->requesting_origin);
break;
case PermissionType::NOTIFICATIONS:
case PermissionType::PUSH_MESSAGING:
case PermissionType::DURABLE_STORAGE:
case PermissionType::AUDIO_CAPTURE:
case PermissionType::VIDEO_CAPTURE:
NOTIMPLEMENTED() << "CancelPermission not implemented for "
<< static_cast<int>(pending_request->permission);
break;
case PermissionType::MIDI:
// There is nothing to cancel so this is simply ignored.
break;
case PermissionType::NUM:
NOTREACHED() << "PermissionType::NUM was not expected here.";
break;
}
pending_requests_.Remove(request_id);
}
void AwPermissionManager::ResetPermission(PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
result_cache_->ClearResult(permission, requesting_origin, embedding_origin);
}
PermissionStatus AwPermissionManager::GetPermissionStatus(
PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
// Method is called outside the Permissions API only for this permission.
if (permission == PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
return result_cache_->GetResult(permission, requesting_origin,
embedding_origin);
} else if (permission == PermissionType::MIDI) {
return content::PermissionStatus::GRANTED;
}
return content::PermissionStatus::DENIED;
}
void AwPermissionManager::RegisterPermissionUsage(
PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
}
int AwPermissionManager::SubscribePermissionStatusChange(
PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin,
const base::Callback<void(PermissionStatus)>& callback) {
return kNoPendingOperation;
}
void AwPermissionManager::UnsubscribePermissionStatusChange(
int subscription_id) {
}
} // namespace android_webview
|