| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/public/browser/browser_context.h" | 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/permission_manager.h" | 9 #include "content/public/browser/permission_manager.h" |
| 10 #include "content/public/browser/permission_type.h" | 10 #include "content/public/browser/permission_type.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 NOTREACHED(); | 38 NOTREACHED(); |
| 39 return PermissionType::NUM; | 39 return PermissionType::NUM; |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // anonymous namespace | 42 } // anonymous namespace |
| 43 | 43 |
| 44 PermissionServiceImpl::PendingRequest::PendingRequest( | 44 PermissionServiceImpl::PendingRequest::PendingRequest( |
| 45 PermissionType permission, | 45 PermissionType permission, |
| 46 const GURL& origin, | 46 const GURL& origin, |
| 47 const PermissionStatusCallback& callback) | 47 const PermissionStatusCallback& callback) |
| 48 : permission(permission), | 48 : id(PermissionManager::kNoPendingOperation), |
| 49 permission(permission), |
| 49 origin(origin), | 50 origin(origin), |
| 50 callback(callback) { | 51 callback(callback) { |
| 51 } | 52 } |
| 52 | 53 |
| 53 PermissionServiceImpl::PendingRequest::~PendingRequest() { | 54 PermissionServiceImpl::PendingRequest::~PendingRequest() { |
| 54 if (!callback.is_null()) | 55 if (!callback.is_null()) |
| 55 callback.Run(PERMISSION_STATUS_ASK); | 56 callback.Run(PERMISSION_STATUS_ASK); |
| 56 } | 57 } |
| 57 | 58 |
| 58 PermissionServiceImpl::PendingSubscription::PendingSubscription( | 59 PermissionServiceImpl::PendingSubscription::PendingSubscription( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 111 } |
| 111 | 112 |
| 112 BrowserContext* browser_context = context_->GetBrowserContext(); | 113 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 113 DCHECK(browser_context); | 114 DCHECK(browser_context); |
| 114 if (!browser_context->GetPermissionManager()) { | 115 if (!browser_context->GetPermissionManager()) { |
| 115 callback.Run(content::PERMISSION_STATUS_DENIED); | 116 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 116 return; | 117 return; |
| 117 } | 118 } |
| 118 | 119 |
| 119 PermissionType permission_type = PermissionNameToPermissionType(permission); | 120 PermissionType permission_type = PermissionNameToPermissionType(permission); |
| 120 int request_id = pending_requests_.Add( | 121 int pending_request_id = pending_requests_.Add( |
| 121 new PendingRequest(permission_type, GURL(origin), callback)); | 122 new PendingRequest(permission_type, GURL(origin), callback)); |
| 122 | 123 |
| 123 browser_context->GetPermissionManager()->RequestPermission( | 124 int id = browser_context->GetPermissionManager()->RequestPermission( |
| 124 permission_type, | 125 permission_type, |
| 125 context_->render_frame_host(), | 126 context_->render_frame_host(), |
| 126 request_id, | |
| 127 GURL(origin), | 127 GURL(origin), |
| 128 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) | 128 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) |
| 129 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | 129 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, |
| 130 weak_factory_.GetWeakPtr(), | 130 weak_factory_.GetWeakPtr(), |
| 131 request_id)); | 131 pending_request_id)); |
| 132 |
| 133 // Check if the request still exists. It may have been removed by the |
| 134 // callback if it was run synchronously. |
| 135 PendingRequest* pending_request = pending_requests_.Lookup( |
| 136 pending_request_id); |
| 137 if (!pending_request) |
| 138 return; |
| 139 pending_request->id = id; |
| 132 } | 140 } |
| 133 | 141 |
| 134 void PermissionServiceImpl::RequestPermissions( | 142 void PermissionServiceImpl::RequestPermissions( |
| 135 mojo::Array<PermissionName> permissions, | 143 mojo::Array<PermissionName> permissions, |
| 136 const mojo::String& origin, | 144 const mojo::String& origin, |
| 137 bool user_gesture, | 145 bool user_gesture, |
| 138 const PermissionsStatusCallback& callback) { | 146 const PermissionsStatusCallback& callback) { |
| 139 NOTIMPLEMENTED(); | 147 NOTIMPLEMENTED(); |
| 140 | 148 |
| 141 // TODO(lalitm,mlamouri): this is returning the current permission statuses | 149 // TODO(lalitm,mlamouri): this is returning the current permission statuses |
| 142 // in order for the call to successfully return. It will be changed later. | 150 // in order for the call to successfully return. It will be changed later. |
| 143 // See https://crbug.com/516626 | 151 // See https://crbug.com/516626 |
| 144 mojo::Array<PermissionStatus> result(permissions.size()); | 152 mojo::Array<PermissionStatus> result(permissions.size()); |
| 145 for (size_t i = 0; i < permissions.size(); ++i) | 153 for (size_t i = 0; i < permissions.size(); ++i) |
| 146 result[i] = GetPermissionStatusFromName(permissions[i], GURL(origin)); | 154 result[i] = GetPermissionStatusFromName(permissions[i], GURL(origin)); |
| 147 callback.Run(result.Pass()); | 155 callback.Run(result.Pass()); |
| 148 } | 156 } |
| 149 | 157 |
| 150 void PermissionServiceImpl::OnRequestPermissionResponse( | 158 void PermissionServiceImpl::OnRequestPermissionResponse( |
| 151 int request_id, | 159 int request_id, |
| 152 PermissionStatus status) { | 160 PermissionStatus status) { |
| 153 PendingRequest* request = pending_requests_.Lookup(request_id); | 161 PendingRequest* request = pending_requests_.Lookup(request_id); |
| 154 PermissionStatusCallback callback(request->callback); | 162 PermissionStatusCallback callback(request->callback); |
| 155 request->callback.reset(); | 163 request->callback.reset(); |
| 156 pending_requests_.Remove(request_id); | 164 pending_requests_.Remove(request_id); |
| 157 callback.Run(status); | 165 callback.Run(status); |
| 158 } | 166 } |
| 159 | 167 |
| 160 void PermissionServiceImpl::CancelPendingOperations() { | 168 void PermissionServiceImpl::CancelPendingOperations() { |
| 161 DCHECK(context_->render_frame_host()); | |
| 162 DCHECK(context_->GetBrowserContext()); | 169 DCHECK(context_->GetBrowserContext()); |
| 163 | 170 |
| 164 PermissionManager* permission_manager = | 171 PermissionManager* permission_manager = |
| 165 context_->GetBrowserContext()->GetPermissionManager(); | 172 context_->GetBrowserContext()->GetPermissionManager(); |
| 166 if (!permission_manager) | 173 if (!permission_manager) |
| 167 return; | 174 return; |
| 168 | 175 |
| 169 // Cancel pending requests. | 176 // Cancel pending requests. |
| 170 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); | 177 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); |
| 171 !it.IsAtEnd(); it.Advance()) { | 178 !it.IsAtEnd(); it.Advance()) { |
| 172 permission_manager->CancelPermissionRequest( | 179 permission_manager->CancelPermissionRequest( |
| 173 it.GetCurrentValue()->permission, | 180 it.GetCurrentValue()->id); |
| 174 context_->render_frame_host(), | |
| 175 it.GetCurrentKey(), | |
| 176 it.GetCurrentValue()->origin); | |
| 177 } | 181 } |
| 178 pending_requests_.Clear(); | 182 pending_requests_.Clear(); |
| 179 | 183 |
| 180 // Cancel pending subscriptions. | 184 // Cancel pending subscriptions. |
| 181 for (SubscriptionsMap::Iterator<PendingSubscription> | 185 for (SubscriptionsMap::Iterator<PendingSubscription> |
| 182 it(&pending_subscriptions_); !it.IsAtEnd(); it.Advance()) { | 186 it(&pending_subscriptions_); !it.IsAtEnd(); it.Advance()) { |
| 183 it.GetCurrentValue()->callback.Run(GetPermissionStatusFromType( | 187 it.GetCurrentValue()->callback.Run(GetPermissionStatusFromType( |
| 184 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); | 188 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); |
| 185 it.GetCurrentValue()->callback.reset(); | 189 it.GetCurrentValue()->callback.reset(); |
| 186 permission_manager->UnsubscribePermissionStatusChange( | 190 permission_manager->UnsubscribePermissionStatusChange( |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 | 309 |
| 306 PermissionStatusCallback callback = subscription->callback; | 310 PermissionStatusCallback callback = subscription->callback; |
| 307 | 311 |
| 308 subscription->callback.reset(); | 312 subscription->callback.reset(); |
| 309 pending_subscriptions_.Remove(pending_subscription_id); | 313 pending_subscriptions_.Remove(pending_subscription_id); |
| 310 | 314 |
| 311 callback.Run(status); | 315 callback.Run(status); |
| 312 } | 316 } |
| 313 | 317 |
| 314 } // namespace content | 318 } // namespace content |
| OLD | NEW |