1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/browser/aw_permission_manager.h"
9 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/permission_type.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
19 using content::PermissionStatus
;
20 using content::PermissionType
;
22 namespace android_webview
{
24 class LastRequestResultCache
{
26 LastRequestResultCache() : weak_factory_(this) {}
28 void SetResult(PermissionType permission
,
29 const GURL
& requesting_origin
,
30 const GURL
& embedding_origin
,
31 PermissionStatus status
) {
32 DCHECK(status
== content::PERMISSION_STATUS_GRANTED
||
33 status
== content::PERMISSION_STATUS_DENIED
);
35 // TODO(ddorwin): We should be denying empty origins at a higher level.
36 if (requesting_origin
.is_empty() || embedding_origin
.is_empty()) {
37 DLOG(WARNING
) << "Not caching result because of empty origin.";
41 if (!requesting_origin
.is_valid()) {
42 NOTREACHED() << requesting_origin
.possibly_invalid_spec();
45 if (!embedding_origin
.is_valid()) {
46 NOTREACHED() << embedding_origin
.possibly_invalid_spec();
50 if (permission
!= PermissionType::PROTECTED_MEDIA_IDENTIFIER
) {
51 // Other permissions are not cached.
55 std::string key
= GetCacheKey(requesting_origin
, embedding_origin
);
58 // Never store an empty key because it could inadvertently be used for
59 // another combination.
62 pmi_result_cache_
[key
] = status
;
65 PermissionStatus
GetResult(PermissionType permission
,
66 const GURL
& requesting_origin
,
67 const GURL
& embedding_origin
) const {
68 // TODO(ddorwin): We should be denying empty origins at a higher level.
69 if (requesting_origin
.is_empty() || embedding_origin
.is_empty()) {
70 return content::PERMISSION_STATUS_ASK
;
73 DCHECK(requesting_origin
.is_valid())
74 << requesting_origin
.possibly_invalid_spec();
75 DCHECK(embedding_origin
.is_valid())
76 << embedding_origin
.possibly_invalid_spec();
78 if (permission
!= PermissionType::PROTECTED_MEDIA_IDENTIFIER
) {
79 NOTREACHED() << "Results are only cached for PROTECTED_MEDIA_IDENTIFIER";
80 return content::PERMISSION_STATUS_ASK
;
83 std::string key
= GetCacheKey(requesting_origin
, embedding_origin
);
84 StatusMap::const_iterator it
= pmi_result_cache_
.find(key
);
85 if (it
== pmi_result_cache_
.end()) {
86 DLOG(WARNING
) << "GetResult() called for uncached origins: " << key
;
87 return content::PERMISSION_STATUS_ASK
;
94 void ClearResult(PermissionType permission
,
95 const GURL
& requesting_origin
,
96 const GURL
& embedding_origin
) {
97 // TODO(ddorwin): We should be denying empty origins at a higher level.
98 if (requesting_origin
.is_empty() || embedding_origin
.is_empty()) {
102 DCHECK(requesting_origin
.is_valid())
103 << requesting_origin
.possibly_invalid_spec();
104 DCHECK(embedding_origin
.is_valid())
105 << embedding_origin
.possibly_invalid_spec();
108 if (permission
!= PermissionType::PROTECTED_MEDIA_IDENTIFIER
) {
109 // Other permissions are not cached, so nothing to clear.
113 std::string key
= GetCacheKey(requesting_origin
, embedding_origin
);
114 pmi_result_cache_
.erase(key
);
117 base::WeakPtr
<LastRequestResultCache
> GetWeakPtr() {
118 return weak_factory_
.GetWeakPtr();
122 // Returns a concatenation of the origins to be used as the index.
123 // Returns the empty string if either origin is invalid or empty.
124 static std::string
GetCacheKey(const GURL
& requesting_origin
,
125 const GURL
& embedding_origin
) {
126 const std::string
& requesting
= requesting_origin
.spec();
127 const std::string
& embedding
= embedding_origin
.spec();
128 if (requesting
.empty() || embedding
.empty())
129 return std::string();
130 return requesting
+ "," + embedding
;
133 using StatusMap
= base::hash_map
<std::string
, PermissionStatus
>;
134 StatusMap pmi_result_cache_
;
136 base::WeakPtrFactory
<LastRequestResultCache
> weak_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache
);
143 void CallbackPermisisonStatusWrapper(
144 const base::WeakPtr
<LastRequestResultCache
>& result_cache
,
145 const base::Callback
<void(PermissionStatus
)>& callback
,
146 PermissionType permission
,
147 const GURL
& requesting_origin
,
148 const GURL
& embedding_origin
,
150 PermissionStatus status
= allowed
? content::PERMISSION_STATUS_GRANTED
151 : content::PERMISSION_STATUS_DENIED
;
152 if (result_cache
.get()) {
153 result_cache
->SetResult(permission
, requesting_origin
, embedding_origin
,
157 callback
.Run(status
);
160 } // anonymous namespace
162 AwPermissionManager::AwPermissionManager()
163 : content::PermissionManager(), result_cache_(new LastRequestResultCache
) {
166 AwPermissionManager::~AwPermissionManager() {
169 void AwPermissionManager::RequestPermission(
170 PermissionType permission
,
171 content::WebContents
* web_contents
,
175 const base::Callback
<void(PermissionStatus
)>& callback
) {
176 int render_process_id
= web_contents
->GetRenderProcessHost()->GetID();
177 int render_view_id
= web_contents
->GetRenderViewHost()->GetRoutingID();
178 AwBrowserPermissionRequestDelegate
* delegate
=
179 AwBrowserPermissionRequestDelegate::FromID(render_process_id
,
182 DVLOG(0) << "Dropping permission request for "
183 << static_cast<int>(permission
);
184 callback
.Run(content::PERMISSION_STATUS_DENIED
);
188 const GURL
& embedding_origin
=
189 web_contents
->GetLastCommittedURL().GetOrigin();
191 switch (permission
) {
192 case PermissionType::GEOLOCATION
:
193 delegate
->RequestGeolocationPermission(
194 origin
, base::Bind(&CallbackPermisisonStatusWrapper
,
195 result_cache_
->GetWeakPtr(), callback
, permission
,
196 origin
, embedding_origin
));
198 case PermissionType::PROTECTED_MEDIA_IDENTIFIER
:
199 delegate
->RequestProtectedMediaIdentifierPermission(
200 origin
, base::Bind(&CallbackPermisisonStatusWrapper
,
201 result_cache_
->GetWeakPtr(), callback
, permission
,
202 origin
, embedding_origin
));
204 case PermissionType::MIDI_SYSEX
:
205 case PermissionType::NOTIFICATIONS
:
206 case PermissionType::PUSH_MESSAGING
:
207 NOTIMPLEMENTED() << "RequestPermission is not implemented for "
208 << static_cast<int>(permission
);
209 callback
.Run(content::PERMISSION_STATUS_DENIED
);
211 case PermissionType::NUM
:
212 NOTREACHED() << "PermissionType::NUM was not expected here.";
213 callback
.Run(content::PERMISSION_STATUS_DENIED
);
218 void AwPermissionManager::CancelPermissionRequest(
219 PermissionType permission
,
220 content::WebContents
* web_contents
,
222 const GURL
& origin
) {
223 // The caller is canceling (presumably) the most recent request. Assuming the
224 // request did not complete, the user did not respond to the requset.
225 // Thus, assume we do not know the result.
226 const GURL
& embedding_origin
=
227 web_contents
->GetLastCommittedURL().GetOrigin();
228 result_cache_
->ClearResult(permission
, origin
, embedding_origin
);
230 int render_process_id
= web_contents
->GetRenderProcessHost()->GetID();
231 int render_view_id
= web_contents
->GetRenderViewHost()->GetRoutingID();
232 AwBrowserPermissionRequestDelegate
* delegate
=
233 AwBrowserPermissionRequestDelegate::FromID(render_process_id
,
238 switch (permission
) {
239 case PermissionType::GEOLOCATION
:
240 delegate
->CancelGeolocationPermissionRequests(origin
);
242 case PermissionType::PROTECTED_MEDIA_IDENTIFIER
:
243 delegate
->CancelProtectedMediaIdentifierPermissionRequests(origin
);
245 case PermissionType::MIDI_SYSEX
:
246 case PermissionType::NOTIFICATIONS
:
247 case PermissionType::PUSH_MESSAGING
:
248 NOTIMPLEMENTED() << "CancelPermission not implemented for "
249 << static_cast<int>(permission
);
251 case PermissionType::NUM
:
252 NOTREACHED() << "PermissionType::NUM was not expected here.";
257 void AwPermissionManager::ResetPermission(PermissionType permission
,
258 const GURL
& requesting_origin
,
259 const GURL
& embedding_origin
) {
260 result_cache_
->ClearResult(permission
, requesting_origin
, embedding_origin
);
263 PermissionStatus
AwPermissionManager::GetPermissionStatus(
264 PermissionType permission
,
265 const GURL
& requesting_origin
,
266 const GURL
& embedding_origin
) {
267 // Method is called outside the Permissions API only for this permission.
268 if (permission
== PermissionType::PROTECTED_MEDIA_IDENTIFIER
) {
269 return result_cache_
->GetResult(permission
, requesting_origin
,
273 return content::PERMISSION_STATUS_DENIED
;
276 void AwPermissionManager::RegisterPermissionUsage(
277 PermissionType permission
,
278 const GURL
& requesting_origin
,
279 const GURL
& embedding_origin
) {
282 int AwPermissionManager::SubscribePermissionStatusChange(
283 PermissionType permission
,
284 const GURL
& requesting_origin
,
285 const GURL
& embedding_origin
,
286 const base::Callback
<void(PermissionStatus
)>& callback
) {
290 void AwPermissionManager::UnsubscribePermissionStatusChange(
291 int subscription_id
) {
294 } // namespace android_webview