| 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 class 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 : permission(permission), |
| 148 const GURL& embedding_origin, | 141 requesting_origin(requesting_origin), |
| 149 bool allowed) { | 142 embedding_origin(embedding_origin), |
| 150 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED | 143 render_process_id(render_frame_host->GetProcess()->GetID()), |
| 151 : content::PERMISSION_STATUS_DENIED; | 144 render_frame_id(render_frame_host->GetRoutingID()) { |
| 152 if (result_cache.get()) { | |
| 153 result_cache->SetResult(permission, requesting_origin, embedding_origin, | |
| 154 status); | |
| 155 } | 145 } |
| 156 | 146 |
| 157 callback.Run(status); | 147 PermissionType permission; |
| 158 } | 148 GURL requesting_origin; |
| 159 | 149 GURL embedding_origin; |
| 160 } // anonymous namespace | 150 int render_process_id; |
| 151 int render_frame_id; |
| 152 }; |
| 161 | 153 |
| 162 AwPermissionManager::AwPermissionManager() | 154 AwPermissionManager::AwPermissionManager() |
| 163 : content::PermissionManager(), result_cache_(new LastRequestResultCache) { | 155 : content::PermissionManager(), |
| 156 result_cache_(new LastRequestResultCache), |
| 157 weak_ptr_factory_(this) { |
| 164 } | 158 } |
| 165 | 159 |
| 166 AwPermissionManager::~AwPermissionManager() { | 160 AwPermissionManager::~AwPermissionManager() { |
| 167 } | 161 } |
| 168 | 162 |
| 169 void AwPermissionManager::RequestPermission( | 163 int AwPermissionManager::RequestPermission( |
| 170 PermissionType permission, | 164 PermissionType permission, |
| 171 content::RenderFrameHost* render_frame_host, | 165 content::RenderFrameHost* render_frame_host, |
| 172 int request_id, | 166 const GURL& requesting_origin, |
| 173 const GURL& origin, | |
| 174 bool user_gesture, | 167 bool user_gesture, |
| 175 const base::Callback<void(PermissionStatus)>& callback) { | 168 const base::Callback<void(PermissionStatus)>& callback) { |
| 176 int render_process_id = render_frame_host->GetProcess()->GetID(); | 169 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 177 int render_frame_id = render_frame_host->GetRoutingID(); | 170 int render_frame_id = render_frame_host->GetRoutingID(); |
| 178 AwBrowserPermissionRequestDelegate* delegate = | 171 AwBrowserPermissionRequestDelegate* delegate = |
| 179 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 172 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 180 render_frame_id); | 173 render_frame_id); |
| 181 if (!delegate) { | 174 if (!delegate) { |
| 182 DVLOG(0) << "Dropping permission request for " | 175 DVLOG(0) << "Dropping permission request for " |
| 183 << static_cast<int>(permission); | 176 << static_cast<int>(permission); |
| 184 callback.Run(content::PERMISSION_STATUS_DENIED); | 177 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 185 return; | 178 return kNoPendingOperation; |
| 186 } | 179 } |
| 187 | 180 |
| 188 const GURL& embedding_origin = | 181 const GURL& embedding_origin = |
| 189 content::WebContents::FromRenderFrameHost(render_frame_host) | 182 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 190 ->GetLastCommittedURL().GetOrigin(); | 183 ->GetLastCommittedURL().GetOrigin(); |
| 191 | 184 |
| 185 int request_id = kNoPendingOperation; |
| 192 switch (permission) { | 186 switch (permission) { |
| 193 case PermissionType::GEOLOCATION: | 187 case PermissionType::GEOLOCATION: |
| 188 request_id = pending_requests_.Add(new PendingRequest( |
| 189 permission, requesting_origin, |
| 190 embedding_origin, render_frame_host)); |
| 194 delegate->RequestGeolocationPermission( | 191 delegate->RequestGeolocationPermission( |
| 195 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 192 requesting_origin, |
| 196 result_cache_->GetWeakPtr(), callback, permission, | 193 base::Bind(&AwPermissionManager::OnRequestResponse, |
| 197 origin, embedding_origin)); | 194 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 195 callback)); |
| 198 break; | 196 break; |
| 199 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 197 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 198 request_id = pending_requests_.Add(new PendingRequest( |
| 199 permission, requesting_origin, |
| 200 embedding_origin, render_frame_host)); |
| 200 delegate->RequestProtectedMediaIdentifierPermission( | 201 delegate->RequestProtectedMediaIdentifierPermission( |
| 201 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 202 requesting_origin, |
| 202 result_cache_->GetWeakPtr(), callback, permission, | 203 base::Bind(&AwPermissionManager::OnRequestResponse, |
| 203 origin, embedding_origin)); | 204 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 205 callback)); |
| 204 break; | 206 break; |
| 205 case PermissionType::MIDI_SYSEX: | 207 case PermissionType::MIDI_SYSEX: |
| 208 request_id = pending_requests_.Add(new PendingRequest( |
| 209 permission, requesting_origin, |
| 210 embedding_origin, render_frame_host)); |
| 206 delegate->RequestMIDISysexPermission( | 211 delegate->RequestMIDISysexPermission( |
| 207 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 212 requesting_origin, |
| 208 result_cache_->GetWeakPtr(), callback, permission, | 213 base::Bind(&AwPermissionManager::OnRequestResponse, |
| 209 origin, embedding_origin)); | 214 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 215 callback)); |
| 210 break; | 216 break; |
| 211 case PermissionType::AUDIO_CAPTURE: | 217 case PermissionType::AUDIO_CAPTURE: |
| 212 case PermissionType::VIDEO_CAPTURE: | 218 case PermissionType::VIDEO_CAPTURE: |
| 213 case PermissionType::NOTIFICATIONS: | 219 case PermissionType::NOTIFICATIONS: |
| 214 case PermissionType::PUSH_MESSAGING: | 220 case PermissionType::PUSH_MESSAGING: |
| 215 case PermissionType::DURABLE_STORAGE: | 221 case PermissionType::DURABLE_STORAGE: |
| 216 NOTIMPLEMENTED() << "RequestPermission is not implemented for " | 222 NOTIMPLEMENTED() << "RequestPermission is not implemented for " |
| 217 << static_cast<int>(permission); | 223 << static_cast<int>(permission); |
| 218 callback.Run(content::PERMISSION_STATUS_DENIED); | 224 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 219 break; | 225 break; |
| 220 case PermissionType::MIDI: | 226 case PermissionType::MIDI: |
| 221 callback.Run(content::PERMISSION_STATUS_GRANTED); | 227 callback.Run(content::PERMISSION_STATUS_GRANTED); |
| 222 break; | 228 break; |
| 223 case PermissionType::NUM: | 229 case PermissionType::NUM: |
| 224 NOTREACHED() << "PermissionType::NUM was not expected here."; | 230 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 225 callback.Run(content::PERMISSION_STATUS_DENIED); | 231 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 226 break; | 232 break; |
| 227 } | 233 } |
| 234 return request_id; |
| 235 } |
| 236 |
| 237 // static |
| 238 void AwPermissionManager::OnRequestResponse( |
| 239 const base::WeakPtr<AwPermissionManager>& manager, |
| 240 int request_id, |
| 241 const base::Callback<void(PermissionStatus)>& callback, |
| 242 bool allowed) { |
| 243 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED |
| 244 : content::PERMISSION_STATUS_DENIED; |
| 245 if (manager.get()) { |
| 246 PendingRequest* pending_request = |
| 247 manager->pending_requests_.Lookup(request_id); |
| 248 manager->result_cache_->SetResult( |
| 249 pending_request->permission, |
| 250 pending_request->requesting_origin, |
| 251 pending_request->embedding_origin, |
| 252 status); |
| 253 manager->pending_requests_.Remove(request_id); |
| 254 } |
| 255 callback.Run(status); |
| 228 } | 256 } |
| 229 | 257 |
| 230 void AwPermissionManager::CancelPermissionRequest( | 258 void AwPermissionManager::CancelPermissionRequest( |
| 231 PermissionType permission, | 259 PermissionType permission, |
| 232 content::RenderFrameHost* render_frame_host, | 260 content::RenderFrameHost* render_frame_host, |
| 233 int request_id, | 261 int request_id, |
| 234 const GURL& origin) { | 262 const GURL& origin) { |
| 263 PendingRequest* pending_request = pending_requests_.Lookup(request_id); |
| 264 if (!pending_request) |
| 265 return; |
| 266 |
| 235 // The caller is canceling (presumably) the most recent request. Assuming the | 267 // The caller is canceling (presumably) the most recent request. Assuming the |
| 236 // request did not complete, the user did not respond to the requset. | 268 // request did not complete, the user did not respond to the requset. |
| 237 // Thus, assume we do not know the result. | 269 // Thus, assume we do not know the result. |
| 238 const GURL& embedding_origin = | 270 const GURL& embedding_origin = |
| 239 content::WebContents::FromRenderFrameHost(render_frame_host) | 271 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 240 ->GetLastCommittedURL().GetOrigin(); | 272 ->GetLastCommittedURL().GetOrigin(); |
| 241 result_cache_->ClearResult(permission, origin, embedding_origin); | 273 result_cache_->ClearResult(permission, origin, embedding_origin); |
| 242 | 274 |
| 243 int render_process_id = render_frame_host->GetProcess()->GetID(); | 275 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 244 int render_frame_id = render_frame_host->GetRoutingID(); | 276 int render_frame_id = render_frame_host->GetRoutingID(); |
| 245 AwBrowserPermissionRequestDelegate* delegate = | 277 AwBrowserPermissionRequestDelegate* delegate = |
| 246 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 278 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 247 render_frame_id); | 279 render_frame_id); |
| 248 if (!delegate) | 280 if (!delegate) { |
| 281 pending_requests_.Remove(request_id); |
| 249 return; | 282 return; |
| 283 } |
| 250 | 284 |
| 251 switch (permission) { | 285 switch (permission) { |
| 252 case PermissionType::GEOLOCATION: | 286 case PermissionType::GEOLOCATION: |
| 253 delegate->CancelGeolocationPermissionRequests(origin); | 287 delegate->CancelGeolocationPermissionRequests(origin); |
| 254 break; | 288 break; |
| 255 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 289 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 256 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); | 290 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); |
| 257 break; | 291 break; |
| 258 case PermissionType::MIDI_SYSEX: | 292 case PermissionType::MIDI_SYSEX: |
| 259 delegate->CancelMIDISysexPermissionRequests(origin); | 293 delegate->CancelMIDISysexPermissionRequests(origin); |
| 260 break; | 294 break; |
| 261 case PermissionType::NOTIFICATIONS: | 295 case PermissionType::NOTIFICATIONS: |
| 262 case PermissionType::PUSH_MESSAGING: | 296 case PermissionType::PUSH_MESSAGING: |
| 263 case PermissionType::DURABLE_STORAGE: | 297 case PermissionType::DURABLE_STORAGE: |
| 264 case PermissionType::AUDIO_CAPTURE: | 298 case PermissionType::AUDIO_CAPTURE: |
| 265 case PermissionType::VIDEO_CAPTURE: | 299 case PermissionType::VIDEO_CAPTURE: |
| 266 NOTIMPLEMENTED() << "CancelPermission not implemented for " | 300 NOTIMPLEMENTED() << "CancelPermission not implemented for " |
| 267 << static_cast<int>(permission); | 301 << static_cast<int>(permission); |
| 268 break; | 302 break; |
| 269 case PermissionType::MIDI: | 303 case PermissionType::MIDI: |
| 270 // There is nothing to cancel so this is simply ignored. | 304 // There is nothing to cancel so this is simply ignored. |
| 271 break; | 305 break; |
| 272 case PermissionType::NUM: | 306 case PermissionType::NUM: |
| 273 NOTREACHED() << "PermissionType::NUM was not expected here."; | 307 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 274 break; | 308 break; |
| 275 } | 309 } |
| 310 |
| 311 pending_requests_.Remove(request_id); |
| 276 } | 312 } |
| 277 | 313 |
| 278 void AwPermissionManager::ResetPermission(PermissionType permission, | 314 void AwPermissionManager::ResetPermission(PermissionType permission, |
| 279 const GURL& requesting_origin, | 315 const GURL& requesting_origin, |
| 280 const GURL& embedding_origin) { | 316 const GURL& embedding_origin) { |
| 281 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); | 317 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); |
| 282 } | 318 } |
| 283 | 319 |
| 284 PermissionStatus AwPermissionManager::GetPermissionStatus( | 320 PermissionStatus AwPermissionManager::GetPermissionStatus( |
| 285 PermissionType permission, | 321 PermissionType permission, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 300 PermissionType permission, | 336 PermissionType permission, |
| 301 const GURL& requesting_origin, | 337 const GURL& requesting_origin, |
| 302 const GURL& embedding_origin) { | 338 const GURL& embedding_origin) { |
| 303 } | 339 } |
| 304 | 340 |
| 305 int AwPermissionManager::SubscribePermissionStatusChange( | 341 int AwPermissionManager::SubscribePermissionStatusChange( |
| 306 PermissionType permission, | 342 PermissionType permission, |
| 307 const GURL& requesting_origin, | 343 const GURL& requesting_origin, |
| 308 const GURL& embedding_origin, | 344 const GURL& embedding_origin, |
| 309 const base::Callback<void(PermissionStatus)>& callback) { | 345 const base::Callback<void(PermissionStatus)>& callback) { |
| 310 return -1; | 346 return kNoPendingOperation; |
| 311 } | 347 } |
| 312 | 348 |
| 313 void AwPermissionManager::UnsubscribePermissionStatusChange( | 349 void AwPermissionManager::UnsubscribePermissionStatusChange( |
| 314 int subscription_id) { | 350 int subscription_id) { |
| 315 } | 351 } |
| 316 | 352 |
| 317 } // namespace android_webview | 353 } // namespace android_webview |
| OLD | NEW |