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