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
android_webview / browser / permission / permission_request_handler.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/permission/permission_request_handler.h"
#include <utility>
#include "android_webview/browser/permission/aw_permission_request.h"
#include "android_webview/browser/permission/aw_permission_request_delegate.h"
#include "android_webview/browser/permission/permission_request_handler_client.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/bind.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
using base::android::ScopedJavaLocalRef;
namespace android_webview {
namespace {
int GetLastCommittedEntryID(content::WebContents* web_contents) {
if (!web_contents)
return 0;
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
return entry ? entry->GetUniqueID() : 0;
}
} // namespace
PermissionRequestHandler::PermissionRequestHandler(
PermissionRequestHandlerClient* client,
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
client_(client),
contents_unique_id_(GetLastCommittedEntryID(web_contents)) {}
PermissionRequestHandler::~PermissionRequestHandler() {
CancelAllRequests();
}
void PermissionRequestHandler::SendRequest(
std::unique_ptr<AwPermissionRequestDelegate> request) {
if (Preauthorized(request->GetOrigin(), request->GetResources())) {
request->NotifyRequestResult(true);
return;
}
base::WeakPtr<AwPermissionRequest> weak_request;
base::android::ScopedJavaLocalRef<jobject> java_peer =
AwPermissionRequest::Create(std::move(request), &weak_request);
requests_.push_back(weak_request);
client_->OnPermissionRequest(java_peer, weak_request.get());
PruneRequests();
}
void PermissionRequestHandler::CancelRequest(const GURL& origin,
int64_t resources) {
// The request list might have multiple requests with same origin and
// resources.
RequestIterator i = FindRequest(origin, resources);
while (i != requests_.end()) {
CancelRequestInternal(i);
requests_.erase(i);
i = FindRequest(origin, resources);
}
}
void PermissionRequestHandler::PreauthorizePermission(const GURL& origin,
int64_t resources) {
if (!resources)
return;
std::string key = origin.DeprecatedGetOriginAsURL().spec();
if (key.empty()) {
LOG(ERROR) << "The origin of preauthorization is empty, ignore it.";
return;
}
preauthorized_permission_[key] |= resources;
}
void PermissionRequestHandler::NavigationEntryCommitted(
const content::LoadCommittedDetails& details) {
const ui::PageTransition transition = details.entry->GetTransitionType();
if (details.is_navigation_to_different_page() ||
ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) ||
contents_unique_id_ != details.entry->GetUniqueID()) {
CancelAllRequests();
contents_unique_id_ = details.entry->GetUniqueID();
}
}
PermissionRequestHandler::RequestIterator PermissionRequestHandler::FindRequest(
const GURL& origin,
int64_t resources) {
RequestIterator i;
for (i = requests_.begin(); i != requests_.end(); ++i) {
if (i->get() && i->get()->GetOrigin() == origin &&
i->get()->GetResources() == resources) {
break;
}
}
return i;
}
void PermissionRequestHandler::CancelRequestInternal(RequestIterator i) {
AwPermissionRequest* request = i->get();
if (request) {
client_->OnPermissionRequestCanceled(request);
request->CancelAndDelete();
}
}
void PermissionRequestHandler::CancelAllRequests() {
for (RequestIterator i = requests_.begin(); i != requests_.end(); ++i)
CancelRequestInternal(i);
}
void PermissionRequestHandler::PruneRequests() {
for (RequestIterator i = requests_.begin(); i != requests_.end();) {
if (!i->get())
i = requests_.erase(i);
else
++i;
}
}
bool PermissionRequestHandler::Preauthorized(const GURL& origin,
int64_t resources) {
std::map<std::string, int64_t>::iterator i =
preauthorized_permission_.find(origin.DeprecatedGetOriginAsURL().spec());
return i != preauthorized_permission_.end() &&
(resources & i->second) == resources;
}
} // namespace android_webview