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; | |
michaelbai
2015/09/23 16:57:26
It seemed that all those data members can not be u
Lalit Maganti
2015/09/23 17:02:07
This has now changed. Since PermissionManager impl
michaelbai
2015/09/23 17:56:26
My question is
Can the data members of PendingRequ
Lalit Maganti
2015/09/23 18:02:37
No these members alone cannot identify a request i
michaelbai
2015/09/23 18:08:05
In the cases, WebView ignore the second one, in wh
Lalit Maganti
2015/09/23 18:20:05
With the permissions api you could have a request
michaelbai
2015/09/23 18:29:53
It seemed that the requests are actually duplicate
| |
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; | |
228 } | 235 } |
229 | 236 |
230 void AwPermissionManager::CancelPermissionRequest( | 237 // static |
231 PermissionType permission, | 238 void AwPermissionManager::OnRequestResponse( |
232 content::RenderFrameHost* render_frame_host, | 239 const base::WeakPtr<AwPermissionManager>& manager, |
233 int request_id, | 240 int request_id, |
234 const GURL& origin) { | 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); | |
256 } | |
257 | |
258 void AwPermissionManager::CancelPermissionRequest(int request_id) { | |
259 PendingRequest* pending_request = pending_requests_.Lookup(request_id); | |
260 if (!pending_request) | |
261 return; | |
262 | |
263 content::RenderFrameHost* render_frame_host = | |
264 content::RenderFrameHost::FromID(pending_request->render_process_id, | |
265 pending_request->render_frame_id); | |
266 DCHECK(render_frame_host); | |
267 | |
268 content::WebContents* web_contents = | |
269 content::WebContents::FromRenderFrameHost(render_frame_host); | |
270 DCHECK(web_contents); | |
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 = web_contents |
239 content::WebContents::FromRenderFrameHost(render_frame_host) | |
240 ->GetLastCommittedURL().GetOrigin(); | 276 ->GetLastCommittedURL().GetOrigin(); |
241 result_cache_->ClearResult(permission, origin, embedding_origin); | 277 result_cache_->ClearResult( |
278 pending_request->permission, | |
279 pending_request->requesting_origin, | |
280 embedding_origin); | |
242 | 281 |
243 int render_process_id = render_frame_host->GetProcess()->GetID(); | |
244 int render_frame_id = render_frame_host->GetRoutingID(); | |
245 AwBrowserPermissionRequestDelegate* delegate = | 282 AwBrowserPermissionRequestDelegate* delegate = |
246 AwBrowserPermissionRequestDelegate::FromID(render_process_id, | 283 AwBrowserPermissionRequestDelegate::FromID( |
247 render_frame_id); | 284 pending_request->render_process_id, |
248 if (!delegate) | 285 pending_request->render_frame_id); |
286 if (!delegate) { | |
287 pending_requests_.Remove(request_id); | |
249 return; | 288 return; |
289 } | |
250 | 290 |
251 switch (permission) { | 291 switch (pending_request->permission) { |
252 case PermissionType::GEOLOCATION: | 292 case PermissionType::GEOLOCATION: |
253 delegate->CancelGeolocationPermissionRequests(origin); | 293 delegate->CancelGeolocationPermissionRequests( |
294 pending_request->requesting_origin); | |
254 break; | 295 break; |
255 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: | 296 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
256 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); | 297 delegate->CancelProtectedMediaIdentifierPermissionRequests( |
298 pending_request->requesting_origin); | |
257 break; | 299 break; |
258 case PermissionType::MIDI_SYSEX: | 300 case PermissionType::MIDI_SYSEX: |
259 delegate->CancelMIDISysexPermissionRequests(origin); | 301 delegate->CancelMIDISysexPermissionRequests( |
302 pending_request->requesting_origin); | |
260 break; | 303 break; |
261 case PermissionType::NOTIFICATIONS: | 304 case PermissionType::NOTIFICATIONS: |
262 case PermissionType::PUSH_MESSAGING: | 305 case PermissionType::PUSH_MESSAGING: |
263 case PermissionType::DURABLE_STORAGE: | 306 case PermissionType::DURABLE_STORAGE: |
264 case PermissionType::AUDIO_CAPTURE: | 307 case PermissionType::AUDIO_CAPTURE: |
265 case PermissionType::VIDEO_CAPTURE: | 308 case PermissionType::VIDEO_CAPTURE: |
266 NOTIMPLEMENTED() << "CancelPermission not implemented for " | 309 NOTIMPLEMENTED() << "CancelPermission not implemented for " |
267 << static_cast<int>(permission); | 310 << static_cast<int>(pending_request->permission); |
268 break; | 311 break; |
269 case PermissionType::MIDI: | 312 case PermissionType::MIDI: |
270 // There is nothing to cancel so this is simply ignored. | 313 // There is nothing to cancel so this is simply ignored. |
271 break; | 314 break; |
272 case PermissionType::NUM: | 315 case PermissionType::NUM: |
273 NOTREACHED() << "PermissionType::NUM was not expected here."; | 316 NOTREACHED() << "PermissionType::NUM was not expected here."; |
274 break; | 317 break; |
275 } | 318 } |
319 | |
320 pending_requests_.Remove(request_id); | |
276 } | 321 } |
277 | 322 |
278 void AwPermissionManager::ResetPermission(PermissionType permission, | 323 void AwPermissionManager::ResetPermission(PermissionType permission, |
279 const GURL& requesting_origin, | 324 const GURL& requesting_origin, |
280 const GURL& embedding_origin) { | 325 const GURL& embedding_origin) { |
281 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); | 326 result_cache_->ClearResult(permission, requesting_origin, embedding_origin); |
282 } | 327 } |
283 | 328 |
284 PermissionStatus AwPermissionManager::GetPermissionStatus( | 329 PermissionStatus AwPermissionManager::GetPermissionStatus( |
285 PermissionType permission, | 330 PermissionType permission, |
(...skipping 14 matching lines...) Expand all Loading... | |
300 PermissionType permission, | 345 PermissionType permission, |
301 const GURL& requesting_origin, | 346 const GURL& requesting_origin, |
302 const GURL& embedding_origin) { | 347 const GURL& embedding_origin) { |
303 } | 348 } |
304 | 349 |
305 int AwPermissionManager::SubscribePermissionStatusChange( | 350 int AwPermissionManager::SubscribePermissionStatusChange( |
306 PermissionType permission, | 351 PermissionType permission, |
307 const GURL& requesting_origin, | 352 const GURL& requesting_origin, |
308 const GURL& embedding_origin, | 353 const GURL& embedding_origin, |
309 const base::Callback<void(PermissionStatus)>& callback) { | 354 const base::Callback<void(PermissionStatus)>& callback) { |
310 return -1; | 355 return kNoPendingOperation; |
311 } | 356 } |
312 | 357 |
313 void AwPermissionManager::UnsubscribePermissionStatusChange( | 358 void AwPermissionManager::UnsubscribePermissionStatusChange( |
314 int subscription_id) { | 359 int subscription_id) { |
315 } | 360 } |
316 | 361 |
317 } // namespace android_webview | 362 } // namespace android_webview |
OLD | NEW |