| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 NOTREACHED(); | 46 NOTREACHED(); |
| 47 return PermissionType::NUM; | 47 return PermissionType::NUM; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // This function allows the usage of the the multiple request map | 50 // This function allows the usage of the the multiple request map |
| 51 // with single requests. | 51 // with single requests. |
| 52 void PermissionRequestResponseCallbackWrapper( | 52 void PermissionRequestResponseCallbackWrapper( |
| 53 const base::Callback<void(PermissionStatus)>& callback, | 53 const base::Callback<void(PermissionStatus)>& callback, |
| 54 mojo::Array<PermissionStatus> vector) { | 54 const std::vector<PermissionStatus>& vector) { |
| 55 DCHECK_EQ(vector.size(), 1ul); | 55 DCHECK_EQ(vector.size(), 1ul); |
| 56 callback.Run(vector[0]); | 56 callback.Run(vector[0]); |
| 57 } | 57 } |
| 58 | 58 |
| 59 } // anonymous namespace | 59 } // anonymous namespace |
| 60 | 60 |
| 61 PermissionServiceImpl::PendingRequest::PendingRequest( | 61 PermissionServiceImpl::PendingRequest::PendingRequest( |
| 62 const RequestPermissionsCallback& callback, | 62 const RequestPermissionsCallback& callback, |
| 63 int request_count) | 63 int request_count) |
| 64 : callback(callback), | 64 : callback(callback), |
| 65 request_count(request_count) { | 65 request_count(request_count) { |
| 66 } | 66 } |
| 67 | 67 |
| 68 PermissionServiceImpl::PendingRequest::~PendingRequest() { | 68 PermissionServiceImpl::PendingRequest::~PendingRequest() { |
| 69 if (callback.is_null()) | 69 if (callback.is_null()) |
| 70 return; | 70 return; |
| 71 | 71 |
| 72 mojo::Array<PermissionStatus> result = | 72 std::vector<PermissionStatus> result(request_count, PermissionStatus::DENIED); |
| 73 mojo::Array<PermissionStatus>::New(request_count); | 73 callback.Run(result); |
| 74 for (int i = 0; i < request_count; ++i) | |
| 75 result[i] = PermissionStatus::DENIED; | |
| 76 callback.Run(std::move(result)); | |
| 77 } | 74 } |
| 78 | 75 |
| 79 PermissionServiceImpl::PendingSubscription::PendingSubscription( | 76 PermissionServiceImpl::PendingSubscription::PendingSubscription( |
| 80 PermissionType permission, | 77 PermissionType permission, |
| 81 const GURL& origin, | 78 const GURL& origin, |
| 82 const PermissionStatusCallback& callback) | 79 const PermissionStatusCallback& callback) |
| 83 : id(-1), | 80 : id(-1), |
| 84 permission(permission), | 81 permission(permission), |
| 85 origin(origin), | 82 origin(origin), |
| 86 callback(callback) { | 83 callback(callback) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 107 } | 104 } |
| 108 | 105 |
| 109 void PermissionServiceImpl::OnConnectionError() { | 106 void PermissionServiceImpl::OnConnectionError() { |
| 110 CancelPendingOperations(); | 107 CancelPendingOperations(); |
| 111 context_->ServiceHadConnectionError(this); | 108 context_->ServiceHadConnectionError(this); |
| 112 // After that call, |this| will be deleted. | 109 // After that call, |this| will be deleted. |
| 113 } | 110 } |
| 114 | 111 |
| 115 void PermissionServiceImpl::RequestPermission( | 112 void PermissionServiceImpl::RequestPermission( |
| 116 PermissionName permission, | 113 PermissionName permission, |
| 117 const mojo::String& origin, | 114 const std::string& origin, |
| 118 bool user_gesture, | 115 bool user_gesture, |
| 119 const PermissionStatusCallback& callback) { | 116 const PermissionStatusCallback& callback) { |
| 120 // This condition is valid if the call is coming from a ChildThread instead of | 117 // This condition is valid if the call is coming from a ChildThread instead of |
| 121 // a RenderFrame. Some consumers of the service run in Workers and some in | 118 // a RenderFrame. Some consumers of the service run in Workers and some in |
| 122 // Frames. In the context of a Worker, it is not possible to show a | 119 // Frames. In the context of a Worker, it is not possible to show a |
| 123 // permission prompt because there is no tab. In the context of a Frame, we | 120 // permission prompt because there is no tab. In the context of a Frame, we |
| 124 // can. Even if the call comes from a context where it is not possible to show | 121 // can. Even if the call comes from a context where it is not possible to show |
| 125 // any UI, we want to still return something relevant so the current | 122 // any UI, we want to still return something relevant so the current |
| 126 // permission status is returned. | 123 // permission status is returned. |
| 127 BrowserContext* browser_context = context_->GetBrowserContext(); | 124 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 128 DCHECK(browser_context); | 125 DCHECK(browser_context); |
| 129 if (!context_->render_frame_host() || | 126 if (!context_->render_frame_host() || |
| 130 !browser_context->GetPermissionManager()) { | 127 !browser_context->GetPermissionManager()) { |
| 131 callback.Run(GetPermissionStatusFromName(permission, GURL(origin.get()))); | 128 callback.Run(GetPermissionStatusFromName(permission, GURL(origin))); |
| 132 return; | 129 return; |
| 133 } | 130 } |
| 134 | 131 |
| 135 int pending_request_id = pending_requests_.Add(new PendingRequest( | 132 int pending_request_id = pending_requests_.Add(new PendingRequest( |
| 136 base::Bind(&PermissionRequestResponseCallbackWrapper, callback), 1)); | 133 base::Bind(&PermissionRequestResponseCallbackWrapper, callback), 1)); |
| 137 int id = browser_context->GetPermissionManager()->RequestPermission( | 134 int id = browser_context->GetPermissionManager()->RequestPermission( |
| 138 PermissionNameToPermissionType(permission), | 135 PermissionNameToPermissionType(permission), context_->render_frame_host(), |
| 139 context_->render_frame_host(), | 136 GURL(origin), user_gesture, |
| 140 GURL(origin.get()), | |
| 141 user_gesture, | |
| 142 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | 137 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, |
| 143 weak_factory_.GetWeakPtr(), | 138 weak_factory_.GetWeakPtr(), pending_request_id)); |
| 144 pending_request_id)); | |
| 145 | 139 |
| 146 // Check if the request still exists. It might have been removed by the | 140 // Check if the request still exists. It might have been removed by the |
| 147 // callback if it was run synchronously. | 141 // callback if it was run synchronously. |
| 148 PendingRequest* pending_request = pending_requests_.Lookup( | 142 PendingRequest* pending_request = pending_requests_.Lookup( |
| 149 pending_request_id); | 143 pending_request_id); |
| 150 if (!pending_request) | 144 if (!pending_request) |
| 151 return; | 145 return; |
| 152 pending_request->id = id; | 146 pending_request->id = id; |
| 153 } | 147 } |
| 154 | 148 |
| 155 void PermissionServiceImpl::OnRequestPermissionResponse( | 149 void PermissionServiceImpl::OnRequestPermissionResponse( |
| 156 int pending_request_id, | 150 int pending_request_id, |
| 157 PermissionStatus status) { | 151 PermissionStatus status) { |
| 158 OnRequestPermissionsResponse(pending_request_id, | 152 OnRequestPermissionsResponse(pending_request_id, |
| 159 std::vector<PermissionStatus>(1, status)); | 153 std::vector<PermissionStatus>(1, status)); |
| 160 } | 154 } |
| 161 | 155 |
| 162 void PermissionServiceImpl::RequestPermissions( | 156 void PermissionServiceImpl::RequestPermissions( |
| 163 mojo::Array<PermissionName> permissions, | 157 const std::vector<PermissionName>& permissions, |
| 164 const mojo::String& origin, | 158 const std::string& origin, |
| 165 bool user_gesture, | 159 bool user_gesture, |
| 166 const RequestPermissionsCallback& callback) { | 160 const RequestPermissionsCallback& callback) { |
| 167 if (permissions.is_null()) { | |
| 168 callback.Run(mojo::Array<PermissionStatus>()); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 // This condition is valid if the call is coming from a ChildThread instead of | 161 // This condition is valid if the call is coming from a ChildThread instead of |
| 173 // a RenderFrame. Some consumers of the service run in Workers and some in | 162 // a RenderFrame. Some consumers of the service run in Workers and some in |
| 174 // Frames. In the context of a Worker, it is not possible to show a | 163 // Frames. In the context of a Worker, it is not possible to show a |
| 175 // permission prompt because there is no tab. In the context of a Frame, we | 164 // permission prompt because there is no tab. In the context of a Frame, we |
| 176 // can. Even if the call comes from a context where it is not possible to show | 165 // can. Even if the call comes from a context where it is not possible to show |
| 177 // any UI, we want to still return something relevant so the current | 166 // any UI, we want to still return something relevant so the current |
| 178 // permission status is returned for each permission. | 167 // permission status is returned for each permission. |
| 179 BrowserContext* browser_context = context_->GetBrowserContext(); | 168 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 180 DCHECK(browser_context); | 169 DCHECK(browser_context); |
| 181 if (!context_->render_frame_host() || | 170 if (!context_->render_frame_host() || |
| 182 !browser_context->GetPermissionManager()) { | 171 !browser_context->GetPermissionManager()) { |
| 183 mojo::Array<PermissionStatus> result(permissions.size()); | 172 std::vector<PermissionStatus> result(permissions.size()); |
| 184 for (size_t i = 0; i < permissions.size(); ++i) { | 173 for (size_t i = 0; i < permissions.size(); ++i) { |
| 185 result[i] = | 174 result[i] = GetPermissionStatusFromName(permissions[i], GURL(origin)); |
| 186 GetPermissionStatusFromName(permissions[i], GURL(origin.get())); | |
| 187 } | 175 } |
| 188 callback.Run(std::move(result)); | 176 callback.Run(result); |
| 189 return; | 177 return; |
| 190 } | 178 } |
| 191 | 179 |
| 192 std::vector<PermissionType> types(permissions.size()); | 180 std::vector<PermissionType> types(permissions.size()); |
| 193 for (size_t i = 0; i < types.size(); ++i) | 181 for (size_t i = 0; i < types.size(); ++i) |
| 194 types[i] = PermissionNameToPermissionType(permissions[i]); | 182 types[i] = PermissionNameToPermissionType(permissions[i]); |
| 195 | 183 |
| 196 int pending_request_id = pending_requests_.Add( | 184 int pending_request_id = pending_requests_.Add( |
| 197 new PendingRequest(callback, permissions.size())); | 185 new PendingRequest(callback, permissions.size())); |
| 198 int id = browser_context->GetPermissionManager()->RequestPermissions( | 186 int id = browser_context->GetPermissionManager()->RequestPermissions( |
| 199 types, | 187 types, context_->render_frame_host(), GURL(origin), user_gesture, |
| 200 context_->render_frame_host(), | |
| 201 GURL(origin.get()), | |
| 202 user_gesture, | |
| 203 base::Bind(&PermissionServiceImpl::OnRequestPermissionsResponse, | 188 base::Bind(&PermissionServiceImpl::OnRequestPermissionsResponse, |
| 204 weak_factory_.GetWeakPtr(), | 189 weak_factory_.GetWeakPtr(), pending_request_id)); |
| 205 pending_request_id)); | |
| 206 | 190 |
| 207 // Check if the request still exists. It may have been removed by the | 191 // Check if the request still exists. It may have been removed by the |
| 208 // the response callback. | 192 // the response callback. |
| 209 PendingRequest* pending_request = pending_requests_.Lookup( | 193 PendingRequest* pending_request = pending_requests_.Lookup( |
| 210 pending_request_id); | 194 pending_request_id); |
| 211 if (!pending_request) | 195 if (!pending_request) |
| 212 return; | 196 return; |
| 213 pending_request->id = id; | 197 pending_request->id = id; |
| 214 } | 198 } |
| 215 | 199 |
| 216 void PermissionServiceImpl::OnRequestPermissionsResponse( | 200 void PermissionServiceImpl::OnRequestPermissionsResponse( |
| 217 int pending_request_id, | 201 int pending_request_id, |
| 218 const std::vector<PermissionStatus>& result) { | 202 const std::vector<PermissionStatus>& result) { |
| 219 PendingRequest* request = pending_requests_.Lookup(pending_request_id); | 203 PendingRequest* request = pending_requests_.Lookup(pending_request_id); |
| 220 RequestPermissionsCallback callback(request->callback); | 204 RequestPermissionsCallback callback(request->callback); |
| 221 request->callback.Reset(); | 205 request->callback.Reset(); |
| 222 pending_requests_.Remove(pending_request_id); | 206 pending_requests_.Remove(pending_request_id); |
| 223 callback.Run(mojo::Array<PermissionStatus>::From(result)); | 207 callback.Run(result); |
| 224 } | 208 } |
| 225 | 209 |
| 226 void PermissionServiceImpl::CancelPendingOperations() { | 210 void PermissionServiceImpl::CancelPendingOperations() { |
| 227 DCHECK(context_->GetBrowserContext()); | 211 DCHECK(context_->GetBrowserContext()); |
| 228 | 212 |
| 229 PermissionManager* permission_manager = | 213 PermissionManager* permission_manager = |
| 230 context_->GetBrowserContext()->GetPermissionManager(); | 214 context_->GetBrowserContext()->GetPermissionManager(); |
| 231 if (!permission_manager) | 215 if (!permission_manager) |
| 232 return; | 216 return; |
| 233 | 217 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 246 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); | 230 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); |
| 247 it.GetCurrentValue()->callback.Reset(); | 231 it.GetCurrentValue()->callback.Reset(); |
| 248 permission_manager->UnsubscribePermissionStatusChange( | 232 permission_manager->UnsubscribePermissionStatusChange( |
| 249 it.GetCurrentValue()->id); | 233 it.GetCurrentValue()->id); |
| 250 } | 234 } |
| 251 pending_subscriptions_.Clear(); | 235 pending_subscriptions_.Clear(); |
| 252 } | 236 } |
| 253 | 237 |
| 254 void PermissionServiceImpl::HasPermission( | 238 void PermissionServiceImpl::HasPermission( |
| 255 PermissionName permission, | 239 PermissionName permission, |
| 256 const mojo::String& origin, | 240 const std::string& origin, |
| 257 const PermissionStatusCallback& callback) { | 241 const PermissionStatusCallback& callback) { |
| 258 callback.Run(GetPermissionStatusFromName(permission, GURL(origin.get()))); | 242 callback.Run(GetPermissionStatusFromName(permission, GURL(origin))); |
| 259 } | 243 } |
| 260 | 244 |
| 261 void PermissionServiceImpl::RevokePermission( | 245 void PermissionServiceImpl::RevokePermission( |
| 262 PermissionName permission, | 246 PermissionName permission, |
| 263 const mojo::String& origin, | 247 const std::string& origin, |
| 264 const PermissionStatusCallback& callback) { | 248 const PermissionStatusCallback& callback) { |
| 265 GURL origin_url(origin.get()); | 249 GURL origin_url(origin); |
| 266 PermissionType permission_type = PermissionNameToPermissionType(permission); | 250 PermissionType permission_type = PermissionNameToPermissionType(permission); |
| 267 PermissionStatus status = | 251 PermissionStatus status = |
| 268 GetPermissionStatusFromType(permission_type, origin_url); | 252 GetPermissionStatusFromType(permission_type, origin_url); |
| 269 | 253 |
| 270 // Resetting the permission should only be possible if the permission is | 254 // Resetting the permission should only be possible if the permission is |
| 271 // already granted. | 255 // already granted. |
| 272 if (status != PermissionStatus::GRANTED) { | 256 if (status != PermissionStatus::GRANTED) { |
| 273 callback.Run(status); | 257 callback.Run(status); |
| 274 return; | 258 return; |
| 275 } | 259 } |
| 276 | 260 |
| 277 ResetPermissionStatus(permission_type, origin_url); | 261 ResetPermissionStatus(permission_type, origin_url); |
| 278 | 262 |
| 279 callback.Run(GetPermissionStatusFromType(permission_type, origin_url)); | 263 callback.Run(GetPermissionStatusFromType(permission_type, origin_url)); |
| 280 } | 264 } |
| 281 | 265 |
| 282 void PermissionServiceImpl::GetNextPermissionChange( | 266 void PermissionServiceImpl::GetNextPermissionChange( |
| 283 PermissionName permission, | 267 PermissionName permission, |
| 284 const mojo::String& mojo_origin, | 268 const std::string& mojo_origin, |
| 285 PermissionStatus last_known_status, | 269 PermissionStatus last_known_status, |
| 286 const PermissionStatusCallback& callback) { | 270 const PermissionStatusCallback& callback) { |
| 287 GURL origin(mojo_origin.get()); | 271 GURL origin(mojo_origin); |
| 288 PermissionStatus current_status = | 272 PermissionStatus current_status = |
| 289 GetPermissionStatusFromName(permission, origin); | 273 GetPermissionStatusFromName(permission, origin); |
| 290 if (current_status != last_known_status) { | 274 if (current_status != last_known_status) { |
| 291 callback.Run(current_status); | 275 callback.Run(current_status); |
| 292 return; | 276 return; |
| 293 } | 277 } |
| 294 | 278 |
| 295 BrowserContext* browser_context = context_->GetBrowserContext(); | 279 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 296 DCHECK(browser_context); | 280 DCHECK(browser_context); |
| 297 if (!browser_context->GetPermissionManager()) { | 281 if (!browser_context->GetPermissionManager()) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 353 |
| 370 PermissionStatusCallback callback = subscription->callback; | 354 PermissionStatusCallback callback = subscription->callback; |
| 371 | 355 |
| 372 subscription->callback.Reset(); | 356 subscription->callback.Reset(); |
| 373 pending_subscriptions_.Remove(pending_subscription_id); | 357 pending_subscriptions_.Remove(pending_subscription_id); |
| 374 | 358 |
| 375 callback.Run(status); | 359 callback.Run(status); |
| 376 } | 360 } |
| 377 | 361 |
| 378 } // namespace content | 362 } // namespace content |
| OLD | NEW |