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