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 "content/public/browser/content_browser_client.h" | 7 #include "base/bind.h" |
| 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/permission_manager.h" |
8 #include "content/public/browser/permission_type.h" | 10 #include "content/public/browser/permission_type.h" |
9 | 11 |
10 namespace content { | 12 namespace content { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 PermissionType PermissionNameToPermissionType(PermissionName name) { | 16 PermissionType PermissionNameToPermissionType(PermissionName name) { |
15 switch(name) { | 17 switch(name) { |
16 case PERMISSION_NAME_GEOLOCATION: | 18 case PERMISSION_NAME_GEOLOCATION: |
17 return PermissionType::GEOLOCATION; | 19 return PermissionType::GEOLOCATION; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // can. Even if the call comes from a context where it is not possible to show | 73 // can. Even if the call comes from a context where it is not possible to show |
72 // any UI, we want to still return something relevant so the current | 74 // any UI, we want to still return something relevant so the current |
73 // permission status is returned. | 75 // permission status is returned. |
74 if (!context_->web_contents()) { | 76 if (!context_->web_contents()) { |
75 // There is no way to show a UI so the call will simply return the current | 77 // There is no way to show a UI so the call will simply return the current |
76 // permission. | 78 // permission. |
77 HasPermission(permission, origin, callback); | 79 HasPermission(permission, origin, callback); |
78 return; | 80 return; |
79 } | 81 } |
80 | 82 |
| 83 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 84 DCHECK(browser_context); |
| 85 if (!browser_context->GetPermissionManager()) { |
| 86 callback.Run(content::PERMISSION_STATUS_DENIED); |
| 87 return; |
| 88 } |
| 89 |
81 PermissionType permission_type = PermissionNameToPermissionType(permission); | 90 PermissionType permission_type = PermissionNameToPermissionType(permission); |
82 int request_id = pending_requests_.Add( | 91 int request_id = pending_requests_.Add( |
83 new PendingRequest(permission_type, GURL(origin), callback)); | 92 new PendingRequest(permission_type, GURL(origin), callback)); |
84 | 93 |
85 GetContentClient()->browser()->RequestPermission( | 94 browser_context->GetPermissionManager()->RequestPermission( |
86 permission_type, | 95 permission_type, |
87 context_->web_contents(), | 96 context_->web_contents(), |
88 request_id, | 97 request_id, |
89 GURL(origin), | 98 GURL(origin), |
90 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) | 99 user_gesture, // TODO(mlamouri): should be removed (crbug.com/423770) |
91 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | 100 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, |
92 weak_factory_.GetWeakPtr(), | 101 weak_factory_.GetWeakPtr(), |
93 request_id)); | 102 request_id)); |
94 } | 103 } |
95 | 104 |
96 void PermissionServiceImpl::OnRequestPermissionResponse( | 105 void PermissionServiceImpl::OnRequestPermissionResponse( |
97 int request_id, | 106 int request_id, |
98 PermissionStatus status) { | 107 PermissionStatus status) { |
99 PendingRequest* request = pending_requests_.Lookup(request_id); | 108 PendingRequest* request = pending_requests_.Lookup(request_id); |
100 mojo::Callback<void(PermissionStatus)> callback(request->callback); | 109 mojo::Callback<void(PermissionStatus)> callback(request->callback); |
101 request->callback.reset(); | 110 request->callback.reset(); |
102 pending_requests_.Remove(request_id); | 111 pending_requests_.Remove(request_id); |
103 callback.Run(status); | 112 callback.Run(status); |
104 } | 113 } |
105 | 114 |
106 void PermissionServiceImpl::CancelPendingRequests() { | 115 void PermissionServiceImpl::CancelPendingRequests() { |
107 DCHECK(context_->web_contents()); | 116 DCHECK(context_->web_contents()); |
| 117 DCHECK(context_->GetBrowserContext()); |
| 118 |
| 119 PermissionManager* permission_manager = |
| 120 context_->GetBrowserContext()->GetPermissionManager(); |
| 121 if (!permission_manager) |
| 122 return; |
108 | 123 |
109 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); | 124 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); |
110 !it.IsAtEnd(); it.Advance()) { | 125 !it.IsAtEnd(); it.Advance()) { |
111 GetContentClient()->browser()->CancelPermissionRequest( | 126 permission_manager->CancelPermissionRequest( |
112 it.GetCurrentValue()->permission, | 127 it.GetCurrentValue()->permission, |
113 context_->web_contents(), | 128 context_->web_contents(), |
114 it.GetCurrentKey(), | 129 it.GetCurrentKey(), |
115 it.GetCurrentValue()->origin); | 130 it.GetCurrentValue()->origin); |
116 } | 131 } |
117 | 132 |
118 pending_requests_.Clear(); | 133 pending_requests_.Clear(); |
119 } | 134 } |
120 | 135 |
121 void PermissionServiceImpl::HasPermission( | 136 void PermissionServiceImpl::HasPermission( |
(...skipping 21 matching lines...) Expand all Loading... |
143 return; | 158 return; |
144 } | 159 } |
145 | 160 |
146 ResetPermissionStatus(permission_type, origin_url); | 161 ResetPermissionStatus(permission_type, origin_url); |
147 | 162 |
148 callback.Run(GetPermissionStatus(permission_type, origin_url)); | 163 callback.Run(GetPermissionStatus(permission_type, origin_url)); |
149 } | 164 } |
150 | 165 |
151 PermissionStatus PermissionServiceImpl::GetPermissionStatus(PermissionType type, | 166 PermissionStatus PermissionServiceImpl::GetPermissionStatus(PermissionType type, |
152 GURL origin) { | 167 GURL origin) { |
| 168 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 169 DCHECK(browser_context); |
| 170 if (!browser_context->GetPermissionManager()) |
| 171 return content::PERMISSION_STATUS_DENIED; |
| 172 |
153 // If the embedding_origin is empty we'll use |origin| instead. | 173 // If the embedding_origin is empty we'll use |origin| instead. |
154 GURL embedding_origin = context_->GetEmbeddingOrigin(); | 174 GURL embedding_origin = context_->GetEmbeddingOrigin(); |
155 return GetContentClient()->browser()->GetPermissionStatus( | 175 return browser_context->GetPermissionManager()->GetPermissionStatus( |
156 type, context_->GetBrowserContext(), origin, | 176 type, origin, embedding_origin.is_empty() ? origin : embedding_origin); |
157 embedding_origin.is_empty() ? origin : embedding_origin); | |
158 } | 177 } |
159 | 178 |
160 void PermissionServiceImpl::ResetPermissionStatus(PermissionType type, | 179 void PermissionServiceImpl::ResetPermissionStatus(PermissionType type, |
161 GURL origin) { | 180 GURL origin) { |
| 181 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 182 DCHECK(browser_context); |
| 183 if (!browser_context->GetPermissionManager()) |
| 184 return; |
| 185 |
162 // If the embedding_origin is empty we'll use |origin| instead. | 186 // If the embedding_origin is empty we'll use |origin| instead. |
163 GURL embedding_origin = context_->GetEmbeddingOrigin(); | 187 GURL embedding_origin = context_->GetEmbeddingOrigin(); |
164 GetContentClient()->browser()->ResetPermission( | 188 browser_context->GetPermissionManager()->ResetPermission( |
165 type, context_->GetBrowserContext(), origin, | 189 type, origin, embedding_origin.is_empty() ? origin : embedding_origin); |
166 embedding_origin.is_empty() ? origin : embedding_origin); | |
167 } | 190 } |
168 | 191 |
169 } // namespace content | 192 } // namespace content |
OLD | NEW |