| 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 "base/memory/weak_ptr.h" | |
| 14 #include "content/public/browser/permission_type.h" | 13 #include "content/public/browser/permission_type.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 18 | 17 |
| 19 using content::PermissionStatus; | 18 using content::PermissionStatus; |
| 20 using content::PermissionType; | 19 using content::PermissionType; |
| 21 | 20 |
| 22 namespace android_webview { | 21 namespace android_webview { |
| 23 | 22 |
| 24 class LastRequestResultCache { | 23 class LastRequestResultCache { |
| 25 public: | 24 public: |
| 26 LastRequestResultCache() : weak_factory_(this) {} | 25 LastRequestResultCache() = default; |
| 27 | 26 |
| 28 void SetResult(PermissionType permission, | 27 void SetResult(PermissionType permission, |
| 29 const GURL& requesting_origin, | 28 const GURL& requesting_origin, |
| 30 const GURL& embedding_origin, | 29 const GURL& embedding_origin, |
| 31 PermissionStatus status) { | 30 PermissionStatus status) { |
| 32 DCHECK(status == content::PERMISSION_STATUS_GRANTED || | 31 DCHECK(status == content::PERMISSION_STATUS_GRANTED || |
| 33 status == content::PERMISSION_STATUS_DENIED); | 32 status == content::PERMISSION_STATUS_DENIED); |
| 34 | 33 |
| 35 // 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. |
| 36 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { | 35 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 106 |
| 108 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { | 107 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { |
| 109 // Other permissions are not cached, so nothing to clear. | 108 // Other permissions are not cached, so nothing to clear. |
| 110 return; | 109 return; |
| 111 } | 110 } |
| 112 | 111 |
| 113 std::string key = GetCacheKey(requesting_origin, embedding_origin); | 112 std::string key = GetCacheKey(requesting_origin, embedding_origin); |
| 114 pmi_result_cache_.erase(key); | 113 pmi_result_cache_.erase(key); |
| 115 } | 114 } |
| 116 | 115 |
| 117 base::WeakPtr<LastRequestResultCache> GetWeakPtr() { | |
| 118 return weak_factory_.GetWeakPtr(); | |
| 119 } | |
| 120 | |
| 121 private: | 116 private: |
| 122 // Returns a concatenation of the origins to be used as the index. | 117 // Returns a concatenation of the origins to be used as the index. |
| 123 // Returns the empty string if either origin is invalid or empty. | 118 // Returns the empty string if either origin is invalid or empty. |
| 124 static std::string GetCacheKey(const GURL& requesting_origin, | 119 static std::string GetCacheKey(const GURL& requesting_origin, |
| 125 const GURL& embedding_origin) { | 120 const GURL& embedding_origin) { |
| 126 const std::string& requesting = requesting_origin.spec(); | 121 const std::string& requesting = requesting_origin.spec(); |
| 127 const std::string& embedding = embedding_origin.spec(); | 122 const std::string& embedding = embedding_origin.spec(); |
| 128 if (requesting.empty() || embedding.empty()) | 123 if (requesting.empty() || embedding.empty()) |
| 129 return std::string(); | 124 return std::string(); |
| 130 return requesting + "," + embedding; | 125 return requesting + "," + embedding; |
| 131 } | 126 } |
| 132 | 127 |
| 133 using StatusMap = base::hash_map<std::string, PermissionStatus>; | 128 using StatusMap = base::hash_map<std::string, PermissionStatus>; |
| 134 StatusMap pmi_result_cache_; | 129 StatusMap pmi_result_cache_; |
| 135 | 130 |
| 136 base::WeakPtrFactory<LastRequestResultCache> weak_factory_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache); | 131 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache); |
| 139 }; | 132 }; |
| 140 | 133 |
| 141 namespace { | 134 struct AwPermissionManager::PendingRequest { |
| 142 | 135 public: |
| 143 void CallbackPermisisonStatusWrapper( | 136 PendingRequest(PermissionType permission, |
| 144 const base::WeakPtr<LastRequestResultCache>& result_cache, | 137 GURL requesting_origin, |
| 145 const base::Callback<void(PermissionStatus)>& callback, | 138 GURL embedding_origin, |
| 146 PermissionType permission, | 139 content::RenderFrameHost* render_frame_host, |
| 147 const GURL& requesting_origin, | 140 const base::Callback<void(PermissionStatus)>& callback) |
| 148 const GURL& embedding_origin, | 141 : permission(permission), |
| 149 bool allowed) { | 142 requesting_origin(requesting_origin), |
| 150 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED | 143 embedding_origin(embedding_origin), |
| 151 : content::PERMISSION_STATUS_DENIED; | 144 render_process_id(render_frame_host->GetProcess()->GetID()), |
| 152 if (result_cache.get()) { | 145 render_frame_id(render_frame_host->GetRoutingID()), |
| 153 result_cache->SetResult(permission, requesting_origin, embedding_origin, | 146 callback(callback) { |
| 154 status); | |
| 155 } | 147 } |
| 156 | 148 |
| 157 callback.Run(status); | 149 ~PendingRequest() = default; |
| 158 } | |
| 159 | 150 |
| 160 } // anonymous namespace | 151 PermissionType permission; |
| 152 GURL requesting_origin; |
| 153 GURL embedding_origin; |
| 154 int render_process_id; |
| 155 int render_frame_id; |
| 156 base::Callback<void(PermissionStatus)> callback; |
| 157 }; |
| 161 | 158 |
| 162 AwPermissionManager::AwPermissionManager() | 159 AwPermissionManager::AwPermissionManager() |
| 163 : content::PermissionManager(), result_cache_(new LastRequestResultCache) { | 160 : content::PermissionManager(), |
| 161 result_cache_(new LastRequestResultCache), |
| 162 weak_ptr_factory_(this) { |
| 164 } | 163 } |
| 165 | 164 |
| 166 AwPermissionManager::~AwPermissionManager() { | 165 AwPermissionManager::~AwPermissionManager() { |
| 167 } | 166 } |
| 168 | 167 |
| 169 void AwPermissionManager::RequestPermission( | 168 int AwPermissionManager::RequestPermission( |
| 170 PermissionType permission, | 169 PermissionType permission, |
| 171 content::RenderFrameHost* render_frame_host, | 170 content::RenderFrameHost* render_frame_host, |
| 172 int request_id, | 171 const GURL& requesting_origin, |
| 173 const GURL& origin, | |
| 174 bool user_gesture, | 172 bool user_gesture, |
| 175 const base::Callback<void(PermissionStatus)>& callback) { | 173 const base::Callback<void(PermissionStatus)>& callback) { |
| 176 int render_process_id = render_frame_host->GetProcess()->GetID(); | 174 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 177 int render_frame_id = render_frame_host->GetRoutingID(); | 175 int render_frame_id = render_frame_host->GetRoutingID(); |
| 178 AwBrowserPermissionRequestDelegate* delegate = | 176 AwBrowserPermissionRequestDelegate* delegate = |
| 179 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 177 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 180 render_frame_id); | 178 render_frame_id); |
| 181 if (!delegate) { | 179 if (!delegate) { |
| 182 DVLOG(0) << "Dropping permission request for " | 180 DVLOG(0) << "Dropping permission request for " |
| 183 << static_cast<int>(permission); | 181 << static_cast<int>(permission); |
| 184 callback.Run(content::PERMISSION_STATUS_DENIED); | 182 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 185 return; | 183 return kNoPendingOperation; |
| 184 } |
| 185 |
| 186 // Do not delegate any requests which are already pending. |
| 187 bool should_delegate_request = true; |
| 188 for (PendingRequestsMap::Iterator<PendingRequest> it(&pending_requests_); |
| 189 !it.IsAtEnd(); it.Advance()) { |
| 190 if (permission == it.GetCurrentValue()->permission) { |
| 191 should_delegate_request = false; |
| 192 break; |
| 193 } |
| 186 } | 194 } |
| 187 | 195 |
| 188 const GURL& embedding_origin = | 196 const GURL& embedding_origin = |
| 189 content::WebContents::FromRenderFrameHost(render_frame_host) | 197 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 190 ->GetLastCommittedURL().GetOrigin(); | 198 ->GetLastCommittedURL().GetOrigin(); |
| 191 | 199 |
| 200 int request_id = kNoPendingOperation; |
| 192 switch (permission) { | 201 switch (permission) { |
| 193 case PermissionType::GEOLOCATION: | 202 case PermissionType::GEOLOCATION: |
| 194 delegate->RequestGeolocationPermission( | 203 request_id = pending_requests_.Add(new PendingRequest( |
| 195 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 204 permission, requesting_origin, |
| 196 result_cache_->GetWeakPtr(), callback, permission, | 205 embedding_origin, render_frame_host, |
| 197 origin, embedding_origin)); | 206 callback)); |
| 207 if (should_delegate_request) { |
| 208 delegate->RequestGeolocationPermission( |
| 209 requesting_origin, |
| 210 base::Bind(&OnRequestResponse, |
| 211 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 212 callback)); |
| 213 } |
| 198 break; | 214 break; |
| 199 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 215 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 200 delegate->RequestProtectedMediaIdentifierPermission( | 216 request_id = pending_requests_.Add(new PendingRequest( |
| 201 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 217 permission, requesting_origin, |
| 202 result_cache_->GetWeakPtr(), callback, permission, | 218 embedding_origin, render_frame_host, |
| 203 origin, embedding_origin)); | 219 callback)); |
| 220 if (should_delegate_request) { |
| 221 delegate->RequestProtectedMediaIdentifierPermission( |
| 222 requesting_origin, |
| 223 base::Bind(&OnRequestResponse, |
| 224 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 225 callback)); |
| 226 } |
| 204 break; | 227 break; |
| 205 case PermissionType::MIDI_SYSEX: | 228 case PermissionType::MIDI_SYSEX: |
| 206 delegate->RequestMIDISysexPermission( | 229 request_id = pending_requests_.Add(new PendingRequest( |
| 207 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 230 permission, requesting_origin, |
| 208 result_cache_->GetWeakPtr(), callback, permission, | 231 embedding_origin, render_frame_host, |
| 209 origin, embedding_origin)); | 232 callback)); |
| 233 if (should_delegate_request) { |
| 234 delegate->RequestMIDISysexPermission( |
| 235 requesting_origin, |
| 236 base::Bind(&OnRequestResponse, |
| 237 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 238 callback)); |
| 239 } |
| 210 break; | 240 break; |
| 211 case PermissionType::AUDIO_CAPTURE: | 241 case PermissionType::AUDIO_CAPTURE: |
| 212 case PermissionType::VIDEO_CAPTURE: | 242 case PermissionType::VIDEO_CAPTURE: |
| 213 case PermissionType::NOTIFICATIONS: | 243 case PermissionType::NOTIFICATIONS: |
| 214 case PermissionType::PUSH_MESSAGING: | 244 case PermissionType::PUSH_MESSAGING: |
| 215 case PermissionType::DURABLE_STORAGE: | 245 case PermissionType::DURABLE_STORAGE: |
| 216 NOTIMPLEMENTED() << "RequestPermission is not implemented for " | 246 NOTIMPLEMENTED() << "RequestPermission is not implemented for " |
| 217 << static_cast<int>(permission); | 247 << static_cast<int>(permission); |
| 218 callback.Run(content::PERMISSION_STATUS_DENIED); | 248 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 219 break; | 249 break; |
| 220 case PermissionType::MIDI: | 250 case PermissionType::MIDI: |
| 221 callback.Run(content::PERMISSION_STATUS_GRANTED); | 251 callback.Run(content::PERMISSION_STATUS_GRANTED); |
| 222 break; | 252 break; |
| 223 case PermissionType::NUM: | 253 case PermissionType::NUM: |
| 224 NOTREACHED() << "PermissionType::NUM was not expected here."; | 254 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 225 callback.Run(content::PERMISSION_STATUS_DENIED); | 255 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 226 break; | 256 break; |
| 227 } | 257 } |
| 258 return request_id; |
| 259 } |
| 260 |
| 261 // static |
| 262 void AwPermissionManager::OnRequestResponse( |
| 263 const base::WeakPtr<AwPermissionManager>& manager, |
| 264 int request_id, |
| 265 const base::Callback<void(PermissionStatus)>& callback, |
| 266 bool allowed) { |
| 267 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED |
| 268 : content::PERMISSION_STATUS_DENIED; |
| 269 if (manager.get()) { |
| 270 PendingRequest* pending_request = |
| 271 manager->pending_requests_.Lookup(request_id); |
| 272 |
| 273 for (PendingRequestsMap::Iterator<PendingRequest> it( |
| 274 &manager->pending_requests_); |
| 275 !it.IsAtEnd(); it.Advance()) { |
| 276 if (pending_request->permission == it.GetCurrentValue()->permission && |
| 277 it.GetCurrentKey() != request_id) { |
| 278 it.GetCurrentValue()->callback.Run(status); |
| 279 manager->pending_requests_.Remove(it.GetCurrentKey()); |
| 280 } |
| 281 } |
| 282 |
| 283 manager->result_cache_->SetResult( |
| 284 pending_request->permission, |
| 285 pending_request->requesting_origin, |
| 286 pending_request->embedding_origin, |
| 287 status); |
| 288 manager->pending_requests_.Remove(request_id); |
| 289 } |
| 290 callback.Run(status); |
| 228 } | 291 } |
| 229 | 292 |
| 230 void AwPermissionManager::CancelPermissionRequest( | 293 void AwPermissionManager::CancelPermissionRequest( |
| 231 PermissionType permission, | 294 PermissionType permission, |
| 232 content::RenderFrameHost* render_frame_host, | 295 content::RenderFrameHost* render_frame_host, |
| 233 int request_id, | 296 int request_id, |
| 234 const GURL& origin) { | 297 const GURL& origin) { |
| 298 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 299 if (!pending_request) |
| 300 return; |
| 301 |
| 235 // The caller is canceling (presumably) the most recent request. Assuming the | 302 // The caller is canceling (presumably) the most recent request. Assuming the |
| 236 // request did not complete, the user did not respond to the requset. | 303 // request did not complete, the user did not respond to the requset. |
| 237 // Thus, assume we do not know the result. | 304 // Thus, assume we do not know the result. |
| 238 const GURL& embedding_origin = | 305 const GURL& embedding_origin = |
| 239 content::WebContents::FromRenderFrameHost(render_frame_host) | 306 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 240 ->GetLastCommittedURL().GetOrigin(); | 307 ->GetLastCommittedURL().GetOrigin(); |
| 241 result_cache_->ClearResult(permission, origin, embedding_origin); | 308 result_cache_->ClearResult(permission, origin, embedding_origin); |
| 242 | 309 |
| 243 int render_process_id = render_frame_host->GetProcess()->GetID(); | 310 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 244 int render_frame_id = render_frame_host->GetRoutingID(); | 311 int render_frame_id = render_frame_host->GetRoutingID(); |
| 245 AwBrowserPermissionRequestDelegate* delegate = | 312 AwBrowserPermissionRequestDelegate* delegate = |
| 246 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 313 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 247 render_frame_id); | 314 render_frame_id); |
| 248 if (!delegate) | 315 if (!delegate) { |
| 316 pending_requests_.Remove(request_id); |
| 249 return; | 317 return; |
| 318 } |
| 250 | 319 |
| 251 switch (permission) { | 320 switch (permission) { |
| 252 case PermissionType::GEOLOCATION: | 321 case PermissionType::GEOLOCATION: |
| 253 delegate->CancelGeolocationPermissionRequests(origin); | 322 delegate->CancelGeolocationPermissionRequests(origin); |
| 254 break; | 323 break; |
| 255 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 324 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 256 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); | 325 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); |
| 257 break; | 326 break; |
| 258 case PermissionType::MIDI_SYSEX: | 327 case PermissionType::MIDI_SYSEX: |
| 259 delegate->CancelMIDISysexPermissionRequests(origin); | 328 delegate->CancelMIDISysexPermissionRequests(origin); |
| 260 break; | 329 break; |
| 261 case PermissionType::NOTIFICATIONS: | 330 case PermissionType::NOTIFICATIONS: |
| 262 case PermissionType::PUSH_MESSAGING: | 331 case PermissionType::PUSH_MESSAGING: |
| 263 case PermissionType::DURABLE_STORAGE: | 332 case PermissionType::DURABLE_STORAGE: |
| 264 case PermissionType::AUDIO_CAPTURE: | 333 case PermissionType::AUDIO_CAPTURE: |
| 265 case PermissionType::VIDEO_CAPTURE: | 334 case PermissionType::VIDEO_CAPTURE: |
| 266 NOTIMPLEMENTED() << "CancelPermission not implemented for " | 335 NOTIMPLEMENTED() << "CancelPermission not implemented for " |
| 267 << static_cast<int>(permission); | 336 << static_cast<int>(permission); |
| 268 break; | 337 break; |
| 269 case PermissionType::MIDI: | 338 case PermissionType::MIDI: |
| 270 // There is nothing to cancel so this is simply ignored. | 339 // There is nothing to cancel so this is simply ignored. |
| 271 break; | 340 break; |
| 272 case PermissionType::NUM: | 341 case PermissionType::NUM: |
| 273 NOTREACHED() << "PermissionType::NUM was not expected here."; | 342 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 274 break; | 343 break; |
| 275 } | 344 } |
| 345 |
| 346 pending_requests_.Remove(request_id); |
| 276 } | 347 } |
| 277 | 348 |
| 278 void AwPermissionManager::ResetPermission(PermissionType permission, | 349 void AwPermissionManager::ResetPermission(PermissionType permission, |
| 279 const GURL& requesting_origin, | 350 const GURL& requesting_origin, |
| 280 const GURL& embedding_origin) { | 351 const GURL& embedding_origin) { |
| 281 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); | 352 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); |
| 282 } | 353 } |
| 283 | 354 |
| 284 PermissionStatus AwPermissionManager::GetPermissionStatus( | 355 PermissionStatus AwPermissionManager::GetPermissionStatus( |
| 285 PermissionType permission, | 356 PermissionType permission, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 300 PermissionType permission, | 371 PermissionType permission, |
| 301 const GURL& requesting_origin, | 372 const GURL& requesting_origin, |
| 302 const GURL& embedding_origin) { | 373 const GURL& embedding_origin) { |
| 303 } | 374 } |
| 304 | 375 |
| 305 int AwPermissionManager::SubscribePermissionStatusChange( | 376 int AwPermissionManager::SubscribePermissionStatusChange( |
| 306 PermissionType permission, | 377 PermissionType permission, |
| 307 const GURL& requesting_origin, | 378 const GURL& requesting_origin, |
| 308 const GURL& embedding_origin, | 379 const GURL& embedding_origin, |
| 309 const base::Callback<void(PermissionStatus)>& callback) { | 380 const base::Callback<void(PermissionStatus)>& callback) { |
| 310 return -1; | 381 return kNoPendingOperation; |
| 311 } | 382 } |
| 312 | 383 |
| 313 void AwPermissionManager::UnsubscribePermissionStatusChange( | 384 void AwPermissionManager::UnsubscribePermissionStatusChange( |
| 314 int subscription_id) { | 385 int subscription_id) { |
| 315 } | 386 } |
| 316 | 387 |
| 317 } // namespace android_webview | 388 } // namespace android_webview |
| OLD | NEW |