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 |
| 23 namespace { | |
| 24 // The weak pointer to this is used to clean up any information which is | |
| 25 // stored in the pending request or result cache maps. However, the callback | |
| 26 // should be run regardless of whether the class is still alive so the method | |
| 27 // is static. | |
| 28 void OnRequestResponse( | |
| 29 const base::WeakPtr<AwPermissionManager>& manager, | |
| 30 int request_id, | |
| 31 const base::Callback<void(PermissionStatus)>& callback, | |
| 32 bool allowed) { | |
| 33 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED | |
| 34 : content::PERMISSION_STATUS_DENIED; | |
| 35 if (manager.get()) { | |
| 36 PendingRequest* pending_request = | |
| 37 manager->pending_requests_.Lookup(request_id); | |
| 38 manager->result_cache_->SetResult( | |
| 39 pending_request->permission, | |
| 40 pending_request->requesting_origin, | |
| 41 pending_request->embedding_origin, | |
| 42 status); | |
| 43 manager->pending_requests_.Remove(request_id); | |
| 44 } | |
| 45 callback.Run(status); | |
| 46 } | |
| 47 } // anonymous namespace. | |
| 48 | |
| 24 class LastRequestResultCache { | 49 class LastRequestResultCache { |
| 25 public: | 50 public: |
| 26 LastRequestResultCache() : weak_factory_(this) {} | 51 LastRequestResultCache() = default; |
| 27 | 52 |
| 28 void SetResult(PermissionType permission, | 53 void SetResult(PermissionType permission, |
| 29 const GURL& requesting_origin, | 54 const GURL& requesting_origin, |
| 30 const GURL& embedding_origin, | 55 const GURL& embedding_origin, |
| 31 PermissionStatus status) { | 56 PermissionStatus status) { |
| 32 DCHECK(status == content::PERMISSION_STATUS_GRANTED || | 57 DCHECK(status == content::PERMISSION_STATUS_GRANTED || |
| 33 status == content::PERMISSION_STATUS_DENIED); | 58 status == content::PERMISSION_STATUS_DENIED); |
| 34 | 59 |
| 35 // TODO(ddorwin): We should be denying empty origins at a higher level. | 60 // TODO(ddorwin): We should be denying empty origins at a higher level. |
| 36 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { | 61 if (requesting_origin.is_empty() || embedding_origin.is_empty()) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 | 132 |
| 108 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { | 133 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) { |
| 109 // Other permissions are not cached, so nothing to clear. | 134 // Other permissions are not cached, so nothing to clear. |
| 110 return; | 135 return; |
| 111 } | 136 } |
| 112 | 137 |
| 113 std::string key = GetCacheKey(requesting_origin, embedding_origin); | 138 std::string key = GetCacheKey(requesting_origin, embedding_origin); |
| 114 pmi_result_cache_.erase(key); | 139 pmi_result_cache_.erase(key); |
| 115 } | 140 } |
| 116 | 141 |
| 117 base::WeakPtr<LastRequestResultCache> GetWeakPtr() { | |
| 118 return weak_factory_.GetWeakPtr(); | |
| 119 } | |
| 120 | |
| 121 private: | 142 private: |
| 122 // Returns a concatenation of the origins to be used as the index. | 143 // Returns a concatenation of the origins to be used as the index. |
| 123 // Returns the empty string if either origin is invalid or empty. | 144 // Returns the empty string if either origin is invalid or empty. |
| 124 static std::string GetCacheKey(const GURL& requesting_origin, | 145 static std::string GetCacheKey(const GURL& requesting_origin, |
| 125 const GURL& embedding_origin) { | 146 const GURL& embedding_origin) { |
| 126 const std::string& requesting = requesting_origin.spec(); | 147 const std::string& requesting = requesting_origin.spec(); |
| 127 const std::string& embedding = embedding_origin.spec(); | 148 const std::string& embedding = embedding_origin.spec(); |
| 128 if (requesting.empty() || embedding.empty()) | 149 if (requesting.empty() || embedding.empty()) |
| 129 return std::string(); | 150 return std::string(); |
| 130 return requesting + "," + embedding; | 151 return requesting + "," + embedding; |
| 131 } | 152 } |
| 132 | 153 |
| 133 using StatusMap = base::hash_map<std::string, PermissionStatus>; | 154 using StatusMap = base::hash_map<std::string, PermissionStatus>; |
| 134 StatusMap pmi_result_cache_; | 155 StatusMap pmi_result_cache_; |
| 135 | 156 |
| 136 base::WeakPtrFactory<LastRequestResultCache> weak_factory_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache); | 157 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache); |
| 139 }; | 158 }; |
| 140 | 159 |
| 141 namespace { | 160 class AwPermissionManager::PendingRequest { |
|
jochen (gone - plz use gerrit)
2015/09/28 17:27:06
should be a struct
Lalit Maganti
2015/09/28 17:59:29
Done.
| |
| 142 | 161 public: |
| 143 void CallbackPermisisonStatusWrapper( | 162 PendingRequest(PermissionType permission, |
|
jochen (gone - plz use gerrit)
2015/09/28 17:27:06
please also add a dtor
Lalit Maganti
2015/09/28 17:59:29
Done.
| |
| 144 const base::WeakPtr<LastRequestResultCache>& result_cache, | 163 GURL requesting_origin, |
| 145 const base::Callback<void(PermissionStatus)>& callback, | 164 GURL embedding_origin, |
| 146 PermissionType permission, | 165 content::RenderFrameHost* render_frame_host) |
| 147 const GURL& requesting_origin, | 166 : permission(permission), |
| 148 const GURL& embedding_origin, | 167 requesting_origin(requesting_origin), |
| 149 bool allowed) { | 168 embedding_origin(embedding_origin), |
| 150 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED | 169 render_process_id(render_frame_host->GetProcess()->GetID()), |
| 151 : content::PERMISSION_STATUS_DENIED; | 170 render_frame_id(render_frame_host->GetRoutingID()) { |
| 152 if (result_cache.get()) { | |
| 153 result_cache->SetResult(permission, requesting_origin, embedding_origin, | |
| 154 status); | |
| 155 } | 171 } |
| 156 | 172 |
| 157 callback.Run(status); | 173 PermissionType permission; |
| 158 } | 174 GURL requesting_origin; |
| 159 | 175 GURL embedding_origin; |
| 160 } // anonymous namespace | 176 int render_process_id; |
| 177 int render_frame_id; | |
| 178 }; | |
| 161 | 179 |
| 162 AwPermissionManager::AwPermissionManager() | 180 AwPermissionManager::AwPermissionManager() |
| 163 : content::PermissionManager(), result_cache_(new LastRequestResultCache) { | 181 : content::PermissionManager(), |
| 182 result_cache_(new LastRequestResultCache), | |
| 183 weak_ptr_factory_(this) { | |
| 164 } | 184 } |
| 165 | 185 |
| 166 AwPermissionManager::~AwPermissionManager() { | 186 AwPermissionManager::~AwPermissionManager() { |
| 167 } | 187 } |
| 168 | 188 |
| 169 void AwPermissionManager::RequestPermission( | 189 int AwPermissionManager::RequestPermission( |
| 170 PermissionType permission, | 190 PermissionType permission, |
| 171 content::RenderFrameHost* render_frame_host, | 191 content::RenderFrameHost* render_frame_host, |
| 172 int request_id, | 192 const GURL& requesting_origin, |
| 173 const GURL& origin, | |
| 174 bool user_gesture, | 193 bool user_gesture, |
| 175 const base::Callback<void(PermissionStatus)>& callback) { | 194 const base::Callback<void(PermissionStatus)>& callback) { |
| 176 int render_process_id = render_frame_host->GetProcess()->GetID(); | 195 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 177 int render_frame_id = render_frame_host->GetRoutingID(); | 196 int render_frame_id = render_frame_host->GetRoutingID(); |
| 178 AwBrowserPermissionRequestDelegate* delegate = | 197 AwBrowserPermissionRequestDelegate* delegate = |
| 179 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 198 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 180 render_frame_id); | 199 render_frame_id); |
| 181 if (!delegate) { | 200 if (!delegate) { |
| 182 DVLOG(0) << "Dropping permission request for " | 201 DVLOG(0) << "Dropping permission request for " |
| 183 << static_cast<int>(permission); | 202 << static_cast<int>(permission); |
| 184 callback.Run(content::PERMISSION_STATUS_DENIED); | 203 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 185 return; | 204 return kNoPendingOperation; |
| 186 } | 205 } |
| 187 | 206 |
| 188 const GURL& embedding_origin = | 207 const GURL& embedding_origin = |
| 189 content::WebContents::FromRenderFrameHost(render_frame_host) | 208 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 190 ->GetLastCommittedURL().GetOrigin(); | 209 ->GetLastCommittedURL().GetOrigin(); |
| 191 | 210 |
| 211 int request_id = kNoPendingOperation; | |
| 192 switch (permission) { | 212 switch (permission) { |
| 193 case PermissionType::GEOLOCATION: | 213 case PermissionType::GEOLOCATION: |
| 214 request_id = pending_requests_.Add(new PendingRequest( | |
| 215 permission, requesting_origin, | |
| 216 embedding_origin, render_frame_host)); | |
| 194 delegate->RequestGeolocationPermission( | 217 delegate->RequestGeolocationPermission( |
| 195 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 218 requesting_origin, |
| 196 result_cache_->GetWeakPtr(), callback, permission, | 219 base::Bind(&OnRequestResponse, |
| 197 origin, embedding_origin)); | 220 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 221 callback)); | |
| 198 break; | 222 break; |
| 199 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 223 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 224 request_id = pending_requests_.Add(new PendingRequest( | |
| 225 permission, requesting_origin, | |
| 226 embedding_origin, render_frame_host)); | |
| 200 delegate->RequestProtectedMediaIdentifierPermission( | 227 delegate->RequestProtectedMediaIdentifierPermission( |
| 201 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 228 requesting_origin, |
| 202 result_cache_->GetWeakPtr(), callback, permission, | 229 base::Bind(&OnRequestResponse, |
| 203 origin, embedding_origin)); | 230 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 231 callback)); | |
| 204 break; | 232 break; |
| 205 case PermissionType::MIDI_SYSEX: | 233 case PermissionType::MIDI_SYSEX: |
| 234 request_id = pending_requests_.Add(new PendingRequest( | |
| 235 permission, requesting_origin, | |
| 236 embedding_origin, render_frame_host)); | |
| 206 delegate->RequestMIDISysexPermission( | 237 delegate->RequestMIDISysexPermission( |
| 207 origin, base::Bind(&CallbackPermisisonStatusWrapper, | 238 requesting_origin, |
| 208 result_cache_->GetWeakPtr(), callback, permission, | 239 base::Bind(&OnRequestResponse, |
| 209 origin, embedding_origin)); | 240 weak_ptr_factory_.GetWeakPtr(), request_id, |
| 241 callback)); | |
| 210 break; | 242 break; |
| 211 case PermissionType::AUDIO_CAPTURE: | 243 case PermissionType::AUDIO_CAPTURE: |
| 212 case PermissionType::VIDEO_CAPTURE: | 244 case PermissionType::VIDEO_CAPTURE: |
| 213 case PermissionType::NOTIFICATIONS: | 245 case PermissionType::NOTIFICATIONS: |
| 214 case PermissionType::PUSH_MESSAGING: | 246 case PermissionType::PUSH_MESSAGING: |
| 215 case PermissionType::DURABLE_STORAGE: | 247 case PermissionType::DURABLE_STORAGE: |
| 216 NOTIMPLEMENTED() << "RequestPermission is not implemented for " | 248 NOTIMPLEMENTED() << "RequestPermission is not implemented for " |
| 217 << static_cast<int>(permission); | 249 << static_cast<int>(permission); |
| 218 callback.Run(content::PERMISSION_STATUS_DENIED); | 250 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 219 break; | 251 break; |
| 220 case PermissionType::MIDI: | 252 case PermissionType::MIDI: |
| 221 callback.Run(content::PERMISSION_STATUS_GRANTED); | 253 callback.Run(content::PERMISSION_STATUS_GRANTED); |
| 222 break; | 254 break; |
| 223 case PermissionType::NUM: | 255 case PermissionType::NUM: |
| 224 NOTREACHED() << "PermissionType::NUM was not expected here."; | 256 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 225 callback.Run(content::PERMISSION_STATUS_DENIED); | 257 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 226 break; | 258 break; |
| 227 } | 259 } |
| 260 return request_id; | |
| 228 } | 261 } |
| 229 | 262 |
| 230 void AwPermissionManager::CancelPermissionRequest( | 263 void AwPermissionManager::CancelPermissionRequest( |
| 231 PermissionType permission, | 264 PermissionType permission, |
| 232 content::RenderFrameHost* render_frame_host, | 265 content::RenderFrameHost* render_frame_host, |
| 233 int request_id, | 266 int request_id, |
| 234 const GURL& origin) { | 267 const GURL& origin) { |
| 268 PendingRequest* pending_request = pending_requests_.Lookup(request_id); | |
| 269 if (!pending_request) | |
| 270 return; | |
| 271 | |
| 235 // The caller is canceling (presumably) the most recent request. Assuming the | 272 // The caller is canceling (presumably) the most recent request. Assuming the |
| 236 // request did not complete, the user did not respond to the requset. | 273 // request did not complete, the user did not respond to the requset. |
| 237 // Thus, assume we do not know the result. | 274 // Thus, assume we do not know the result. |
| 238 const GURL& embedding_origin = | 275 const GURL& embedding_origin = |
| 239 content::WebContents::FromRenderFrameHost(render_frame_host) | 276 content::WebContents::FromRenderFrameHost(render_frame_host) |
| 240 ->GetLastCommittedURL().GetOrigin(); | 277 ->GetLastCommittedURL().GetOrigin(); |
| 241 result_cache_->ClearResult(permission, origin, embedding_origin); | 278 result_cache_->ClearResult(permission, origin, embedding_origin); |
| 242 | 279 |
| 243 int render_process_id = render_frame_host->GetProcess()->GetID(); | 280 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 244 int render_frame_id = render_frame_host->GetRoutingID(); | 281 int render_frame_id = render_frame_host->GetRoutingID(); |
| 245 AwBrowserPermissionRequestDelegate* delegate = | 282 AwBrowserPermissionRequestDelegate* delegate = |
| 246 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 283 AwBrowserPermissionRequestDelegate::FromID(render_process_id, |
| 247 render_frame_id); | 284 render_frame_id); |
| 248 if (!delegate) | 285 if (!delegate) { |
| 286 pending_requests_.Remove(request_id); | |
| 249 return; | 287 return; |
| 288 } | |
| 250 | 289 |
| 251 switch (permission) { | 290 switch (permission) { |
| 252 case PermissionType::GEOLOCATION: | 291 case PermissionType::GEOLOCATION: |
| 253 delegate->CancelGeolocationPermissionRequests(origin); | 292 delegate->CancelGeolocationPermissionRequests(origin); |
| 254 break; | 293 break; |
| 255 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 294 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 256 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); | 295 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); |
| 257 break; | 296 break; |
| 258 case PermissionType::MIDI_SYSEX: | 297 case PermissionType::MIDI_SYSEX: |
| 259 delegate->CancelMIDISysexPermissionRequests(origin); | 298 delegate->CancelMIDISysexPermissionRequests(origin); |
| 260 break; | 299 break; |
| 261 case PermissionType::NOTIFICATIONS: | 300 case PermissionType::NOTIFICATIONS: |
| 262 case PermissionType::PUSH_MESSAGING: | 301 case PermissionType::PUSH_MESSAGING: |
| 263 case PermissionType::DURABLE_STORAGE: | 302 case PermissionType::DURABLE_STORAGE: |
| 264 case PermissionType::AUDIO_CAPTURE: | 303 case PermissionType::AUDIO_CAPTURE: |
| 265 case PermissionType::VIDEO_CAPTURE: | 304 case PermissionType::VIDEO_CAPTURE: |
| 266 NOTIMPLEMENTED() << "CancelPermission not implemented for " | 305 NOTIMPLEMENTED() << "CancelPermission not implemented for " |
| 267 << static_cast<int>(permission); | 306 << static_cast<int>(permission); |
| 268 break; | 307 break; |
| 269 case PermissionType::MIDI: | 308 case PermissionType::MIDI: |
| 270 // There is nothing to cancel so this is simply ignored. | 309 // There is nothing to cancel so this is simply ignored. |
| 271 break; | 310 break; |
| 272 case PermissionType::NUM: | 311 case PermissionType::NUM: |
| 273 NOTREACHED() << "PermissionType::NUM was not expected here."; | 312 NOTREACHED() << "PermissionType::NUM was not expected here."; |
| 274 break; | 313 break; |
| 275 } | 314 } |
| 315 | |
| 316 pending_requests_.Remove(request_id); | |
| 276 } | 317 } |
| 277 | 318 |
| 278 void AwPermissionManager::ResetPermission(PermissionType permission, | 319 void AwPermissionManager::ResetPermission(PermissionType permission, |
| 279 const GURL& requesting_origin, | 320 const GURL& requesting_origin, |
| 280 const GURL& embedding_origin) { | 321 const GURL& embedding_origin) { |
| 281 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); | 322 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); |
| 282 } | 323 } |
| 283 | 324 |
| 284 PermissionStatus AwPermissionManager::GetPermissionStatus( | 325 PermissionStatus AwPermissionManager::GetPermissionStatus( |
| 285 PermissionType permission, | 326 PermissionType permission, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 300 PermissionType permission, | 341 PermissionType permission, |
| 301 const GURL& requesting_origin, | 342 const GURL& requesting_origin, |
| 302 const GURL& embedding_origin) { | 343 const GURL& embedding_origin) { |
| 303 } | 344 } |
| 304 | 345 |
| 305 int AwPermissionManager::SubscribePermissionStatusChange( | 346 int AwPermissionManager::SubscribePermissionStatusChange( |
| 306 PermissionType permission, | 347 PermissionType permission, |
| 307 const GURL& requesting_origin, | 348 const GURL& requesting_origin, |
| 308 const GURL& embedding_origin, | 349 const GURL& embedding_origin, |
| 309 const base::Callback<void(PermissionStatus)>& callback) { | 350 const base::Callback<void(PermissionStatus)>& callback) { |
| 310 return -1; | 351 return kNoPendingOperation; |
| 311 } | 352 } |
| 312 | 353 |
| 313 void AwPermissionManager::UnsubscribePermissionStatusChange( | 354 void AwPermissionManager::UnsubscribePermissionStatusChange( |
| 314 int subscription_id) { | 355 int subscription_id) { |
| 315 } | 356 } |
| 316 | 357 |
| 317 } // namespace android_webview | 358 } // namespace android_webview |
| OLD | NEW |