Fix dcheck in message port code on Mandoline shutdown.
[chromium-blink-merge.git] / android_webview / browser / aw_permission_manager.cc
blobf4d62b189691ef98a49077900e10f4dba916b7d8
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/browser/aw_permission_manager.h"
7 #include <string>
9 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/permission_type.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/web_contents.h"
19 using content::PermissionStatus;
20 using content::PermissionType;
22 namespace android_webview {
24 class LastRequestResultCache {
25 public:
26 LastRequestResultCache() : weak_factory_(this) {}
28 void SetResult(PermissionType permission,
29 const GURL& requesting_origin,
30 const GURL& embedding_origin,
31 PermissionStatus status) {
32 DCHECK(status == content::PERMISSION_STATUS_GRANTED ||
33 status == content::PERMISSION_STATUS_DENIED);
35 // TODO(ddorwin): We should be denying empty origins at a higher level.
36 if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
37 DLOG(WARNING) << "Not caching result because of empty origin.";
38 return;
41 if (!requesting_origin.is_valid()) {
42 NOTREACHED() << requesting_origin.possibly_invalid_spec();
43 return;
45 if (!embedding_origin.is_valid()) {
46 NOTREACHED() << embedding_origin.possibly_invalid_spec();
47 return;
50 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
51 // Other permissions are not cached.
52 return;
55 std::string key = GetCacheKey(requesting_origin, embedding_origin);
56 if (key.empty()) {
57 NOTREACHED();
58 // Never store an empty key because it could inadvertently be used for
59 // another combination.
60 return;
62 pmi_result_cache_[key] = status;
65 PermissionStatus GetResult(PermissionType permission,
66 const GURL& requesting_origin,
67 const GURL& embedding_origin) const {
68 // TODO(ddorwin): We should be denying empty origins at a higher level.
69 if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
70 return content::PERMISSION_STATUS_ASK;
73 DCHECK(requesting_origin.is_valid())
74 << requesting_origin.possibly_invalid_spec();
75 DCHECK(embedding_origin.is_valid())
76 << embedding_origin.possibly_invalid_spec();
78 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
79 NOTREACHED() << "Results are only cached for PROTECTED_MEDIA_IDENTIFIER";
80 return content::PERMISSION_STATUS_ASK;
83 std::string key = GetCacheKey(requesting_origin, embedding_origin);
84 StatusMap::const_iterator it = pmi_result_cache_.find(key);
85 if (it == pmi_result_cache_.end()) {
86 DLOG(WARNING) << "GetResult() called for uncached origins: " << key;
87 return content::PERMISSION_STATUS_ASK;
90 DCHECK(!key.empty());
91 return it->second;
94 void ClearResult(PermissionType permission,
95 const GURL& requesting_origin,
96 const GURL& embedding_origin) {
97 // TODO(ddorwin): We should be denying empty origins at a higher level.
98 if (requesting_origin.is_empty() || embedding_origin.is_empty()) {
99 return;
102 DCHECK(requesting_origin.is_valid())
103 << requesting_origin.possibly_invalid_spec();
104 DCHECK(embedding_origin.is_valid())
105 << embedding_origin.possibly_invalid_spec();
108 if (permission != PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
109 // Other permissions are not cached, so nothing to clear.
110 return;
113 std::string key = GetCacheKey(requesting_origin, embedding_origin);
114 pmi_result_cache_.erase(key);
117 base::WeakPtr<LastRequestResultCache> GetWeakPtr() {
118 return weak_factory_.GetWeakPtr();
121 private:
122 // Returns a concatenation of the origins to be used as the index.
123 // Returns the empty string if either origin is invalid or empty.
124 static std::string GetCacheKey(const GURL& requesting_origin,
125 const GURL& embedding_origin) {
126 const std::string& requesting = requesting_origin.spec();
127 const std::string& embedding = embedding_origin.spec();
128 if (requesting.empty() || embedding.empty())
129 return std::string();
130 return requesting + "," + embedding;
133 using StatusMap = base::hash_map<std::string, PermissionStatus>;
134 StatusMap pmi_result_cache_;
136 base::WeakPtrFactory<LastRequestResultCache> weak_factory_;
138 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache);
141 namespace {
143 void CallbackPermisisonStatusWrapper(
144 const base::WeakPtr<LastRequestResultCache>& result_cache,
145 const base::Callback<void(PermissionStatus)>& callback,
146 PermissionType permission,
147 const GURL& requesting_origin,
148 const GURL& embedding_origin,
149 bool allowed) {
150 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED
151 : content::PERMISSION_STATUS_DENIED;
152 if (result_cache.get()) {
153 result_cache->SetResult(permission, requesting_origin, embedding_origin,
154 status);
157 callback.Run(status);
160 } // anonymous namespace
162 AwPermissionManager::AwPermissionManager()
163 : content::PermissionManager(), result_cache_(new LastRequestResultCache) {
166 AwPermissionManager::~AwPermissionManager() {
169 void AwPermissionManager::RequestPermission(
170 PermissionType permission,
171 content::RenderFrameHost* render_frame_host,
172 int request_id,
173 const GURL& origin,
174 bool user_gesture,
175 const base::Callback<void(PermissionStatus)>& callback) {
176 int render_process_id = render_frame_host->GetProcess()->GetID();
177 int render_frame_id = render_frame_host->GetRoutingID();
178 AwBrowserPermissionRequestDelegate* delegate =
179 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
180 render_frame_id);
181 if (!delegate) {
182 DVLOG(0) << "Dropping permission request for "
183 << static_cast<int>(permission);
184 callback.Run(content::PERMISSION_STATUS_DENIED);
185 return;
188 const GURL& embedding_origin =
189 content::WebContents::FromRenderFrameHost(render_frame_host)
190 ->GetLastCommittedURL().GetOrigin();
192 switch (permission) {
193 case PermissionType::GEOLOCATION:
194 delegate->RequestGeolocationPermission(
195 origin, base::Bind(&CallbackPermisisonStatusWrapper,
196 result_cache_->GetWeakPtr(), callback, permission,
197 origin, embedding_origin));
198 break;
199 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
200 delegate->RequestProtectedMediaIdentifierPermission(
201 origin, base::Bind(&CallbackPermisisonStatusWrapper,
202 result_cache_->GetWeakPtr(), callback, permission,
203 origin, embedding_origin));
204 break;
205 case PermissionType::MIDI_SYSEX:
206 delegate->RequestMIDISysexPermission(
207 origin, base::Bind(&CallbackPermisisonStatusWrapper,
208 result_cache_->GetWeakPtr(), callback, permission,
209 origin, embedding_origin));
210 break;
211 case PermissionType::NOTIFICATIONS:
212 case PermissionType::PUSH_MESSAGING:
213 NOTIMPLEMENTED() << "RequestPermission is not implemented for "
214 << static_cast<int>(permission);
215 callback.Run(content::PERMISSION_STATUS_DENIED);
216 break;
217 case PermissionType::NUM:
218 NOTREACHED() << "PermissionType::NUM was not expected here.";
219 callback.Run(content::PERMISSION_STATUS_DENIED);
220 break;
224 void AwPermissionManager::CancelPermissionRequest(
225 PermissionType permission,
226 content::RenderFrameHost* render_frame_host,
227 int request_id,
228 const GURL& origin) {
229 // The caller is canceling (presumably) the most recent request. Assuming the
230 // request did not complete, the user did not respond to the requset.
231 // Thus, assume we do not know the result.
232 const GURL& embedding_origin =
233 content::WebContents::FromRenderFrameHost(render_frame_host)
234 ->GetLastCommittedURL().GetOrigin();
235 result_cache_->ClearResult(permission, origin, embedding_origin);
237 int render_process_id = render_frame_host->GetProcess()->GetID();
238 int render_frame_id = render_frame_host->GetRoutingID();
239 AwBrowserPermissionRequestDelegate* delegate =
240 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
241 render_frame_id);
242 if (!delegate)
243 return;
245 switch (permission) {
246 case PermissionType::GEOLOCATION:
247 delegate->CancelGeolocationPermissionRequests(origin);
248 break;
249 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
250 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin);
251 break;
252 case PermissionType::MIDI_SYSEX:
253 delegate->CancelMIDISysexPermissionRequests(origin);
254 break;
255 case PermissionType::NOTIFICATIONS:
256 case PermissionType::PUSH_MESSAGING:
257 NOTIMPLEMENTED() << "CancelPermission not implemented for "
258 << static_cast<int>(permission);
259 break;
260 case PermissionType::NUM:
261 NOTREACHED() << "PermissionType::NUM was not expected here.";
262 break;
266 void AwPermissionManager::ResetPermission(PermissionType permission,
267 const GURL& requesting_origin,
268 const GURL& embedding_origin) {
269 result_cache_->ClearResult(permission, requesting_origin, embedding_origin);
272 PermissionStatus AwPermissionManager::GetPermissionStatus(
273 PermissionType permission,
274 const GURL& requesting_origin,
275 const GURL& embedding_origin) {
276 // Method is called outside the Permissions API only for this permission.
277 if (permission == PermissionType::PROTECTED_MEDIA_IDENTIFIER) {
278 return result_cache_->GetResult(permission, requesting_origin,
279 embedding_origin);
282 return content::PERMISSION_STATUS_DENIED;
285 void AwPermissionManager::RegisterPermissionUsage(
286 PermissionType permission,
287 const GURL& requesting_origin,
288 const GURL& embedding_origin) {
291 int AwPermissionManager::SubscribePermissionStatusChange(
292 PermissionType permission,
293 const GURL& requesting_origin,
294 const GURL& embedding_origin,
295 const base::Callback<void(PermissionStatus)>& callback) {
296 return -1;
299 void AwPermissionManager::UnsubscribePermissionStatusChange(
300 int subscription_id) {
303 } // namespace android_webview