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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 const mojo::String& origin, | 84 const mojo::String& origin, |
85 bool user_gesture, | 85 bool user_gesture, |
86 const PermissionStatusCallback& callback) { | 86 const PermissionStatusCallback& callback) { |
87 // This condition is valid if the call is coming from a ChildThread instead of | 87 // This condition is valid if the call is coming from a ChildThread instead of |
88 // a RenderFrame. Some consumers of the service run in Workers and some in | 88 // a RenderFrame. Some consumers of the service run in Workers and some in |
89 // Frames. In the context of a Worker, it is not possible to show a | 89 // Frames. In the context of a Worker, it is not possible to show a |
90 // permission prompt because there is no tab. In the context of a Frame, we | 90 // permission prompt because there is no tab. In the context of a Frame, we |
91 // can. Even if the call comes from a context where it is not possible to show | 91 // can. Even if the call comes from a context where it is not possible to show |
92 // any UI, we want to still return something relevant so the current | 92 // any UI, we want to still return something relevant so the current |
93 // permission status is returned. | 93 // permission status is returned. |
94 if (!context_->web_contents()) { | 94 if (!context_->render_frame_host()) { |
95 // There is no way to show a UI so the call will simply return the current | 95 // There is no way to show a UI so the call will simply return the current |
96 // permission. | 96 // permission. |
97 HasPermission(permission, origin, callback); | 97 HasPermission(permission, origin, callback); |
98 return; | 98 return; |
99 } | 99 } |
100 | 100 |
101 BrowserContext* browser_context = context_->GetBrowserContext(); | 101 BrowserContext* browser_context = context_->GetBrowserContext(); |
102 DCHECK(browser_context); | 102 DCHECK(browser_context); |
103 if (!browser_context->GetPermissionManager()) { | 103 if (!browser_context->GetPermissionManager()) { |
104 callback.Run(content::PERMISSION_STATUS_DENIED); | 104 callback.Run(content::PERMISSION_STATUS_DENIED); |
105 return; | 105 return; |
106 } | 106 } |
107 | 107 |
108 PermissionType permission_type = PermissionNameToPermissionType(permission); | 108 PermissionType permission_type = PermissionNameToPermissionType(permission); |
109 int request_id = pending_requests_.Add( | 109 int request_id = pending_requests_.Add( |
110 new PendingRequest(permission_type, GURL(origin), callback)); | 110 new PendingRequest(permission_type, GURL(origin), callback)); |
111 | 111 |
112 browser_context->GetPermissionManager()->RequestPermission( | 112 browser_context->GetPermissionManager()->RequestPermission( |
113 permission_type, | 113 permission_type, |
114 context_->web_contents(), | 114 context_->render_frame_host(), |
115 request_id, | 115 request_id, |
116 GURL(origin), | 116 GURL(origin), |
117 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) | 117 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) |
118 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | 118 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, |
119 weak_factory_.GetWeakPtr(), | 119 weak_factory_.GetWeakPtr(), |
120 request_id)); | 120 request_id)); |
121 } | 121 } |
122 | 122 |
123 void PermissionServiceImpl::OnRequestPermissionResponse( | 123 void PermissionServiceImpl::OnRequestPermissionResponse( |
124 int request_id, | 124 int request_id, |
125 PermissionStatus status) { | 125 PermissionStatus status) { |
126 PendingRequest* request = pending_requests_.Lookup(request_id); | 126 PendingRequest* request = pending_requests_.Lookup(request_id); |
127 PermissionStatusCallback callback(request->callback); | 127 PermissionStatusCallback callback(request->callback); |
128 request->callback.reset(); | 128 request->callback.reset(); |
129 pending_requests_.Remove(request_id); | 129 pending_requests_.Remove(request_id); |
130 callback.Run(status); | 130 callback.Run(status); |
131 } | 131 } |
132 | 132 |
133 void PermissionServiceImpl::CancelPendingOperations() { | 133 void PermissionServiceImpl::CancelPendingOperations() { |
134 DCHECK(context_->web_contents()); | 134 DCHECK(context_->render_frame_host()); |
135 DCHECK(context_->GetBrowserContext()); | 135 DCHECK(context_->GetBrowserContext()); |
136 | 136 |
137 PermissionManager* permission_manager = | 137 PermissionManager* permission_manager = |
138 context_->GetBrowserContext()->GetPermissionManager(); | 138 context_->GetBrowserContext()->GetPermissionManager(); |
139 if (!permission_manager) | 139 if (!permission_manager) |
140 return; | 140 return; |
141 | 141 |
142 // Cancel pending requests. | 142 // Cancel pending requests. |
143 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); | 143 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); |
144 !it.IsAtEnd(); it.Advance()) { | 144 !it.IsAtEnd(); it.Advance()) { |
145 permission_manager->CancelPermissionRequest( | 145 permission_manager->CancelPermissionRequest( |
146 it.GetCurrentValue()->permission, | 146 it.GetCurrentValue()->permission, |
147 context_->web_contents(), | 147 context_->render_frame_host(), |
148 it.GetCurrentKey(), | 148 it.GetCurrentKey(), |
149 it.GetCurrentValue()->origin); | 149 it.GetCurrentValue()->origin); |
150 } | 150 } |
151 pending_requests_.Clear(); | 151 pending_requests_.Clear(); |
152 | 152 |
153 // Cancel pending subscriptions. | 153 // Cancel pending subscriptions. |
154 for (SubscriptionsMap::Iterator<PendingSubscription> | 154 for (SubscriptionsMap::Iterator<PendingSubscription> |
155 it(&pending_subscriptions_); !it.IsAtEnd(); it.Advance()) { | 155 it(&pending_subscriptions_); !it.IsAtEnd(); it.Advance()) { |
156 it.GetCurrentValue()->callback.Run(GetPermissionStatusFromType( | 156 it.GetCurrentValue()->callback.Run(GetPermissionStatusFromType( |
157 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); | 157 it.GetCurrentValue()->permission, it.GetCurrentValue()->origin)); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 278 |
279 PermissionStatusCallback callback = subscription->callback; | 279 PermissionStatusCallback callback = subscription->callback; |
280 | 280 |
281 subscription->callback.reset(); | 281 subscription->callback.reset(); |
282 pending_subscriptions_.Remove(pending_subscription_id); | 282 pending_subscriptions_.Remove(pending_subscription_id); |
283 | 283 |
284 callback.Run(status); | 284 callback.Run(status); |
285 } | 285 } |
286 | 286 |
287 } // namespace content | 287 } // namespace content |
OLD | NEW |