OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "android_webview/browser/aw_permission_manager.h" | 5 #include "android_webview/browser/aw_permission_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "android_webview/browser/aw_browser_permission_request_delegate.h" | 9 #include "android_webview/browser/aw_browser_permission_request_delegate.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "content/public/browser/permission_type.h" | 13 #include "content/public/browser/permission_type.h" |
14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
17 | 17 |
18 using content::mojom::PermissionStatus; | 18 using blink::mojom::PermissionStatus; |
19 using content::PermissionType; | 19 using content::PermissionType; |
20 | 20 |
21 namespace android_webview { | 21 namespace android_webview { |
22 | 22 |
23 class LastRequestResultCache { | 23 class LastRequestResultCache { |
24 public: | 24 public: |
25 LastRequestResultCache() = default; | 25 LastRequestResultCache() = default; |
26 | 26 |
27 void SetResult(PermissionType permission, | 27 void SetResult(PermissionType permission, |
28 const GURL& requesting_origin, | 28 const GURL& requesting_origin, |
29 const GURL& embedding_origin, | 29 const GURL& embedding_origin, |
30 PermissionStatus status) { | 30 PermissionStatus status) { |
31 DCHECK(status == content::mojom::PermissionStatus::GRANTED || | 31 DCHECK(status == PermissionStatus::GRANTED || |
32 status == content::mojom::PermissionStatus::DENIED); | 32 status == PermissionStatus::DENIED); |
33 | 33 |
34 // TODO(ddorwin): We should be denying empty origins at a higher level. | 34 // TODO(ddorwin): We should be denying empty origins at a higher level. |
35 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { | 35 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { |
36 DLOG(WARNING) << "Not caching result because of empty origin."; | 36 DLOG(WARNING) << "Not caching result because of empty origin."; |
37 return; | 37 return; |
38 } | 38 } |
39 | 39 |
40 if (!requesting_origin.is_valid()) { | 40 if (!requesting_origin.is_valid()) { |
41 NOTREACHED() << requesting_origin.possibly_invalid_spec(); | 41 NOTREACHED() << requesting_origin.possibly_invalid_spec(); |
42 return; | 42 return; |
(...skipping 16 matching lines...) Expand all Loading... |
59 return; | 59 return; |
60 } | 60 } |
61 pmi_result_cache_[key] = status; | 61 pmi_result_cache_[key] = status; |
62 } | 62 } |
63 | 63 |
64 PermissionStatus GetResult(PermissionType permission, | 64 PermissionStatus GetResult(PermissionType permission, |
65 const GURL& requesting_origin, | 65 const GURL& requesting_origin, |
66 const GURL& embedding_origin) const { | 66 const GURL& embedding_origin) const { |
67 // TODO(ddorwin): We should be denying empty origins at a higher level. | 67 // TODO(ddorwin): We should be denying empty origins at a higher level. |
68 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { | 68 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { |
69 return content::mojom::PermissionStatus::ASK; | 69 return PermissionStatus::ASK; |
70 } | 70 } |
71 | 71 |
72 DCHECK(requesting_origin.is_valid()) | 72 DCHECK(requesting_origin.is_valid()) |
73 << requesting_origin.possibly_invalid_spec(); | 73 << requesting_origin.possibly_invalid_spec(); |
74 DCHECK(embedding_origin.is_valid()) | 74 DCHECK(embedding_origin.is_valid()) |
75 << embedding_origin.possibly_invalid_spec(); | 75 << embedding_origin.possibly_invalid_spec(); |
76 | 76 |
77 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { | 77 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { |
78 NOTREACHED() << "Results are only cached for PROTECTED_MEDIA_IDENTIFIER"; | 78 NOTREACHED() << "Results are only cached for PROTECTED_MEDIA_IDENTIFIER"; |
79 return content::mojom::PermissionStatus::ASK; | 79 return PermissionStatus::ASK; |
80 } | 80 } |
81 | 81 |
82 std::string key = GetCacheKey(requesting_origin, embedding_origin); | 82 std::string key = GetCacheKey(requesting_origin, embedding_origin); |
83 StatusMap::const_iterator it = pmi_result_cache_.find(key); | 83 StatusMap::const_iterator it = pmi_result_cache_.find(key); |
84 if (it == pmi_result_cache_.end()) { | 84 if (it == pmi_result_cache_.end()) { |
85 DLOG(WARNING) << "GetResult() called for uncached origins: " << key; | 85 DLOG(WARNING) << "GetResult() called for uncached origins: " << key; |
86 return content::mojom::PermissionStatus::ASK; | 86 return PermissionStatus::ASK; |
87 } | 87 } |
88 | 88 |
89 DCHECK(!key.empty()); | 89 DCHECK(!key.empty()); |
90 return it->second; | 90 return it->second; |
91 } | 91 } |
92 | 92 |
93 void ClearResult(PermissionType permission, | 93 void ClearResult(PermissionType permission, |
94 const GURL& requesting_origin, | 94 const GURL& requesting_origin, |
95 const GURL& embedding_origin) { | 95 const GURL& embedding_origin) { |
96 // TODO(ddorwin): We should be denying empty origins at a higher level. | 96 // TODO(ddorwin): We should be denying empty origins at a higher level. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 const GURL& requesting_origin, | 171 const GURL& requesting_origin, |
172 const base::Callback<void(PermissionStatus)>& callback) { | 172 const base::Callback<void(PermissionStatus)>& callback) { |
173 int render_process_id = render_frame_host->GetProcess()->GetID(); | 173 int render_process_id = render_frame_host->GetProcess()->GetID(); |
174 int render_frame_id = render_frame_host->GetRoutingID(); | 174 int render_frame_id = render_frame_host->GetRoutingID(); |
175 AwBrowserPermissionRequestDelegate* delegate = | 175 AwBrowserPermissionRequestDelegate* delegate = |
176 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 176 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
177 render_frame_id); | 177 render_frame_id); |
178 if (!delegate) { | 178 if (!delegate) { |
179 DVLOG(0) << "Dropping permission request for " | 179 DVLOG(0) << "Dropping permission request for " |
180 << static_cast<int>(permission); | 180 << static_cast<int>(permission); |
181 callback.Run(content::mojom::PermissionStatus::DENIED); | 181 callback.Run(PermissionStatus::DENIED); |
182 return kNoPendingOperation; | 182 return kNoPendingOperation; |
183 } | 183 } |
184 | 184 |
185 // Do not delegate any requests which are already pending. | 185 // Do not delegate any requests which are already pending. |
186 bool should_delegate_request = true; | 186 bool should_delegate_request = true; |
187 for (PendingRequestsMap::Iterator<PendingRequest> it(&pending_requests_); | 187 for (PendingRequestsMap::Iterator<PendingRequest> it(&pending_requests_); |
188 !it.IsAtEnd(); it.Advance()) { | 188 !it.IsAtEnd(); it.Advance()) { |
189 if (permission == it.GetCurrentValue()->permission) { | 189 if (permission == it.GetCurrentValue()->permission) { |
190 should_delegate_request = false; | 190 should_delegate_request = false; |
191 break; | 191 break; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } | 238 } |
239 break; | 239 break; |
240 case PermissionType::AUDIO_CAPTURE: | 240 case PermissionType::AUDIO_CAPTURE: |
241 case PermissionType::VIDEO_CAPTURE: | 241 case PermissionType::VIDEO_CAPTURE: |
242 case PermissionType::NOTIFICATIONS: | 242 case PermissionType::NOTIFICATIONS: |
243 case PermissionType::PUSH_MESSAGING: | 243 case PermissionType::PUSH_MESSAGING: |
244 case PermissionType::DURABLE_STORAGE: | 244 case PermissionType::DURABLE_STORAGE: |
245 case PermissionType::BACKGROUND_SYNC: | 245 case PermissionType::BACKGROUND_SYNC: |
246 NOTIMPLEMENTED() << "RequestPermission is not implemented for " | 246 NOTIMPLEMENTED() << "RequestPermission is not implemented for " |
247 << static_cast<int>(permission); | 247 << static_cast<int>(permission); |
248 callback.Run(content::mojom::PermissionStatus::DENIED); | 248 callback.Run(PermissionStatus::DENIED); |
249 break; | 249 break; |
250 case PermissionType::MIDI: | 250 case PermissionType::MIDI: |
251 callback.Run(content::mojom::PermissionStatus::GRANTED); | 251 callback.Run(PermissionStatus::GRANTED); |
252 break; | 252 break; |
253 case PermissionType::NUM: | 253 case PermissionType::NUM: |
254 NOTREACHED() << "PermissionType::NUM was not expected here."; | 254 NOTREACHED() << "PermissionType::NUM was not expected here."; |
255 callback.Run(content::mojom::PermissionStatus::DENIED); | 255 callback.Run(PermissionStatus::DENIED); |
256 break; | 256 break; |
257 } | 257 } |
258 return request_id; | 258 return request_id; |
259 } | 259 } |
260 | 260 |
261 int AwPermissionManager::RequestPermissions( | 261 int AwPermissionManager::RequestPermissions( |
262 const std::vector<PermissionType>& permissions, | 262 const std::vector<PermissionType>& permissions, |
263 content::RenderFrameHost* render_frame_host, | 263 content::RenderFrameHost* render_frame_host, |
264 const GURL& requesting_origin, | 264 const GURL& requesting_origin, |
265 const base::Callback<void( | 265 const base::Callback<void( |
(...skipping 13 matching lines...) Expand all Loading... |
279 callback.Run(result); | 279 callback.Run(result); |
280 return kNoPendingOperation; | 280 return kNoPendingOperation; |
281 } | 281 } |
282 | 282 |
283 // static | 283 // static |
284 void AwPermissionManager::OnRequestResponse( | 284 void AwPermissionManager::OnRequestResponse( |
285 const base::WeakPtr<AwPermissionManager>& manager, | 285 const base::WeakPtr<AwPermissionManager>& manager, |
286 int request_id, | 286 int request_id, |
287 const base::Callback<void(PermissionStatus)>& callback, | 287 const base::Callback<void(PermissionStatus)>& callback, |
288 bool allowed) { | 288 bool allowed) { |
289 PermissionStatus status = allowed ? content::mojom::PermissionStatus::GRANTED | 289 PermissionStatus status = |
290 : content::mojom::PermissionStatus::DENIED; | 290 allowed ? PermissionStatus::GRANTED : PermissionStatus::DENIED; |
291 if (manager.get()) { | 291 if (manager.get()) { |
292 PendingRequest* pending_request = | 292 PendingRequest* pending_request = |
293 manager->pending_requests_.Lookup(request_id); | 293 manager->pending_requests_.Lookup(request_id); |
294 | 294 |
295 for (PendingRequestsMap::Iterator<PendingRequest> it( | 295 for (PendingRequestsMap::Iterator<PendingRequest> it( |
296 &manager->pending_requests_); | 296 &manager->pending_requests_); |
297 !it.IsAtEnd(); it.Advance()) { | 297 !it.IsAtEnd(); it.Advance()) { |
298 if (pending_request->permission == it.GetCurrentValue()->permission && | 298 if (pending_request->permission == it.GetCurrentValue()->permission && |
299 it.GetCurrentKey() != request_id) { | 299 it.GetCurrentKey() != request_id) { |
300 it.GetCurrentValue()->callback.Run(status); | 300 it.GetCurrentValue()->callback.Run(status); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 | 384 |
385 PermissionStatus AwPermissionManager::GetPermissionStatus( | 385 PermissionStatus AwPermissionManager::GetPermissionStatus( |
386 PermissionType permission, | 386 PermissionType permission, |
387 const GURL& requesting_origin, | 387 const GURL& requesting_origin, |
388 const GURL& embedding_origin) { | 388 const GURL& embedding_origin) { |
389 // Method is called outside the Permissions API only for this permission. | 389 // Method is called outside the Permissions API only for this permission. |
390 if (permission == PermissionType::PROTECTED_MEDIA_IDENTIFIER) { | 390 if (permission == PermissionType::PROTECTED_MEDIA_IDENTIFIER) { |
391 return result_cache_->GetResult(permission, requesting_origin, | 391 return result_cache_->GetResult(permission, requesting_origin, |
392 embedding_origin); | 392 embedding_origin); |
393 } else if (permission == PermissionType::MIDI) { | 393 } else if (permission == PermissionType::MIDI) { |
394 return content::mojom::PermissionStatus::GRANTED; | 394 return PermissionStatus::GRANTED; |
395 } | 395 } |
396 | 396 |
397 return content::mojom::PermissionStatus::DENIED; | 397 return PermissionStatus::DENIED; |
398 } | 398 } |
399 | 399 |
400 void AwPermissionManager::RegisterPermissionUsage( | 400 void AwPermissionManager::RegisterPermissionUsage( |
401 PermissionType permission, | 401 PermissionType permission, |
402 const GURL& requesting_origin, | 402 const GURL& requesting_origin, |
403 const GURL& embedding_origin) { | 403 const GURL& embedding_origin) { |
404 } | 404 } |
405 | 405 |
406 int AwPermissionManager::SubscribePermissionStatusChange( | 406 int AwPermissionManager::SubscribePermissionStatusChange( |
407 PermissionType permission, | 407 PermissionType permission, |
408 const GURL& requesting_origin, | 408 const GURL& requesting_origin, |
409 const GURL& embedding_origin, | 409 const GURL& embedding_origin, |
410 const base::Callback<void(PermissionStatus)>& callback) { | 410 const base::Callback<void(PermissionStatus)>& callback) { |
411 return kNoPendingOperation; | 411 return kNoPendingOperation; |
412 } | 412 } |
413 | 413 |
414 void AwPermissionManager::UnsubscribePermissionStatusChange( | 414 void AwPermissionManager::UnsubscribePermissionStatusChange( |
415 int subscription_id) { | 415 int subscription_id) { |
416 } | 416 } |
417 | 417 |
418 } // namespace android_webview | 418 } // namespace android_webview |
OLD | NEW |