| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/permissions/permission_service_impl.h" | 5 #include "content/browser/permissions/permission_service_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/ptr_util.h" |
| 11 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/permission_manager.h" | 15 #include "content/public/browser/permission_manager.h" |
| 13 #include "content/public/browser/permission_type.h" | 16 #include "content/public/browser/permission_type.h" |
| 14 | 17 |
| 15 using blink::mojom::PermissionDescriptorPtr; | 18 using blink::mojom::PermissionDescriptorPtr; |
| 16 using blink::mojom::PermissionName; | 19 using blink::mojom::PermissionName; |
| 17 using blink::mojom::PermissionStatus; | 20 using blink::mojom::PermissionStatus; |
| 18 | 21 |
| 19 namespace content { | 22 namespace content { |
| 20 | 23 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // any UI, we want to still return something relevant so the current | 126 // any UI, we want to still return something relevant so the current |
| 124 // permission status is returned. | 127 // permission status is returned. |
| 125 BrowserContext* browser_context = context_->GetBrowserContext(); | 128 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 126 DCHECK(browser_context); | 129 DCHECK(browser_context); |
| 127 if (!context_->render_frame_host() || | 130 if (!context_->render_frame_host() || |
| 128 !browser_context->GetPermissionManager()) { | 131 !browser_context->GetPermissionManager()) { |
| 129 callback.Run(GetPermissionStatus(permission, origin)); | 132 callback.Run(GetPermissionStatus(permission, origin)); |
| 130 return; | 133 return; |
| 131 } | 134 } |
| 132 | 135 |
| 133 int pending_request_id = pending_requests_.Add(new PendingRequest( | 136 int pending_request_id = |
| 134 base::Bind(&PermissionRequestResponseCallbackWrapper, callback), 1)); | 137 pending_requests_.Add(base::MakeUnique<PendingRequest>( |
| 138 base::Bind(&PermissionRequestResponseCallbackWrapper, callback), 1)); |
| 135 int id = browser_context->GetPermissionManager()->RequestPermission( | 139 int id = browser_context->GetPermissionManager()->RequestPermission( |
| 136 PermissionDescriptorToPermissionType(permission), | 140 PermissionDescriptorToPermissionType(permission), |
| 137 context_->render_frame_host(), origin.GetURL(), user_gesture, | 141 context_->render_frame_host(), origin.GetURL(), user_gesture, |
| 138 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | 142 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, |
| 139 weak_factory_.GetWeakPtr(), pending_request_id)); | 143 weak_factory_.GetWeakPtr(), pending_request_id)); |
| 140 | 144 |
| 141 // Check if the request still exists. It might have been removed by the | 145 // Check if the request still exists. It might have been removed by the |
| 142 // callback if it was run synchronously. | 146 // callback if it was run synchronously. |
| 143 PendingRequest* pending_request = pending_requests_.Lookup( | 147 PendingRequest* pending_request = pending_requests_.Lookup( |
| 144 pending_request_id); | 148 pending_request_id); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 175 result[i] = GetPermissionStatus(permissions[i], origin); | 179 result[i] = GetPermissionStatus(permissions[i], origin); |
| 176 callback.Run(result); | 180 callback.Run(result); |
| 177 return; | 181 return; |
| 178 } | 182 } |
| 179 | 183 |
| 180 std::vector<PermissionType> types(permissions.size()); | 184 std::vector<PermissionType> types(permissions.size()); |
| 181 for (size_t i = 0; i < types.size(); ++i) | 185 for (size_t i = 0; i < types.size(); ++i) |
| 182 types[i] = PermissionDescriptorToPermissionType(permissions[i]); | 186 types[i] = PermissionDescriptorToPermissionType(permissions[i]); |
| 183 | 187 |
| 184 int pending_request_id = pending_requests_.Add( | 188 int pending_request_id = pending_requests_.Add( |
| 185 new PendingRequest(callback, permissions.size())); | 189 base::MakeUnique<PendingRequest>(callback, permissions.size())); |
| 186 int id = browser_context->GetPermissionManager()->RequestPermissions( | 190 int id = browser_context->GetPermissionManager()->RequestPermissions( |
| 187 types, context_->render_frame_host(), origin.GetURL(), user_gesture, | 191 types, context_->render_frame_host(), origin.GetURL(), user_gesture, |
| 188 base::Bind(&PermissionServiceImpl::OnRequestPermissionsResponse, | 192 base::Bind(&PermissionServiceImpl::OnRequestPermissionsResponse, |
| 189 weak_factory_.GetWeakPtr(), pending_request_id)); | 193 weak_factory_.GetWeakPtr(), pending_request_id)); |
| 190 | 194 |
| 191 // Check if the request still exists. It may have been removed by the | 195 // Check if the request still exists. It may have been removed by the |
| 192 // the response callback. | 196 // the response callback. |
| 193 PendingRequest* pending_request = pending_requests_.Lookup( | 197 PendingRequest* pending_request = pending_requests_.Lookup( |
| 194 pending_request_id); | 198 pending_request_id); |
| 195 if (!pending_request) | 199 if (!pending_request) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 callback.Run(current_status); | 284 callback.Run(current_status); |
| 281 return; | 285 return; |
| 282 } | 286 } |
| 283 | 287 |
| 284 PermissionType permission_type = | 288 PermissionType permission_type = |
| 285 PermissionDescriptorToPermissionType(permission); | 289 PermissionDescriptorToPermissionType(permission); |
| 286 | 290 |
| 287 // We need to pass the id of PendingSubscription in pending_subscriptions_ | 291 // We need to pass the id of PendingSubscription in pending_subscriptions_ |
| 288 // to the callback but SubscribePermissionStatusChange() will also return an | 292 // to the callback but SubscribePermissionStatusChange() will also return an |
| 289 // id which is different. | 293 // id which is different. |
| 290 PendingSubscription* subscription = | 294 auto subscription = |
| 291 new PendingSubscription(permission_type, origin, callback); | 295 base::MakeUnique<PendingSubscription>(permission_type, origin, callback); |
| 292 int pending_subscription_id = pending_subscriptions_.Add(subscription); | 296 PendingSubscription* subscription_raw = subscription.get(); |
| 297 int pending_subscription_id = |
| 298 pending_subscriptions_.Add(std::move(subscription)); |
| 293 | 299 |
| 294 GURL requesting_origin(origin.Serialize()); | 300 GURL requesting_origin(origin.Serialize()); |
| 295 GURL embedding_origin = context_->GetEmbeddingOrigin(); | 301 GURL embedding_origin = context_->GetEmbeddingOrigin(); |
| 296 subscription->id = | 302 subscription_raw->id = |
| 297 browser_context->GetPermissionManager()->SubscribePermissionStatusChange( | 303 browser_context->GetPermissionManager()->SubscribePermissionStatusChange( |
| 298 permission_type, requesting_origin, | 304 permission_type, requesting_origin, |
| 299 // If the embedding_origin is empty, we,ll use the |origin| instead. | 305 // If the embedding_origin is empty, we,ll use the |origin| instead. |
| 300 embedding_origin.is_empty() ? requesting_origin : embedding_origin, | 306 embedding_origin.is_empty() ? requesting_origin : embedding_origin, |
| 301 base::Bind(&PermissionServiceImpl::OnPermissionStatusChanged, | 307 base::Bind(&PermissionServiceImpl::OnPermissionStatusChanged, |
| 302 weak_factory_.GetWeakPtr(), pending_subscription_id)); | 308 weak_factory_.GetWeakPtr(), pending_subscription_id)); |
| 303 } | 309 } |
| 304 | 310 |
| 305 PermissionStatus PermissionServiceImpl::GetPermissionStatus( | 311 PermissionStatus PermissionServiceImpl::GetPermissionStatus( |
| 306 const PermissionDescriptorPtr& permission, | 312 const PermissionDescriptorPtr& permission, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 361 |
| 356 PermissionStatusCallback callback = subscription->callback; | 362 PermissionStatusCallback callback = subscription->callback; |
| 357 | 363 |
| 358 subscription->callback.Reset(); | 364 subscription->callback.Reset(); |
| 359 pending_subscriptions_.Remove(pending_subscription_id); | 365 pending_subscriptions_.Remove(pending_subscription_id); |
| 360 | 366 |
| 361 callback.Run(status); | 367 callback.Run(status); |
| 362 } | 368 } |
| 363 | 369 |
| 364 } // namespace content | 370 } // namespace content |
| OLD | NEW |