Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / permissions / permission_manager.cc
blobe6d4c7d7d8b9136f749bb3e1748a49de64e5c0ec
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 "chrome/browser/permissions/permission_manager.h"
7 #include "base/callback.h"
8 #include "chrome/browser/permissions/permission_context.h"
9 #include "chrome/browser/permissions/permission_context_base.h"
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.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 {
24 // Helper method to convert ContentSetting to PermissionStatus.
25 PermissionStatus ContentSettingToPermissionStatus(ContentSetting setting) {
26 switch (setting) {
27 case CONTENT_SETTING_ALLOW:
28 case CONTENT_SETTING_SESSION_ONLY:
29 return content::PERMISSION_STATUS_GRANTED;
30 case CONTENT_SETTING_BLOCK:
31 return content::PERMISSION_STATUS_DENIED;
32 case CONTENT_SETTING_ASK:
33 return content::PERMISSION_STATUS_ASK;
34 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
35 case CONTENT_SETTING_DEFAULT:
36 case CONTENT_SETTING_NUM_SETTINGS:
37 break;
40 NOTREACHED();
41 return content::PERMISSION_STATUS_DENIED;
44 // Helper method to convert PermissionType to ContentSettingType.
45 ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) {
46 switch (permission) {
47 case PermissionType::MIDI_SYSEX:
48 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX;
49 case PermissionType::PUSH_MESSAGING:
50 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING;
51 case PermissionType::NOTIFICATIONS:
52 return CONTENT_SETTINGS_TYPE_NOTIFICATIONS;
53 case PermissionType::GEOLOCATION:
54 return CONTENT_SETTINGS_TYPE_GEOLOCATION;
55 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
56 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
57 return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER;
58 #else
59 NOTIMPLEMENTED();
60 break;
61 #endif
62 case PermissionType::DURABLE_STORAGE:
63 return CONTENT_SETTINGS_TYPE_DURABLE_STORAGE;
64 case PermissionType::MIDI:
65 // This will hit the NOTREACHED below.
66 break;
67 case PermissionType::AUDIO_CAPTURE:
68 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC;
69 case PermissionType::VIDEO_CAPTURE:
70 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA;
71 case PermissionType::NUM:
72 // This will hit the NOTREACHED below.
73 break;
76 NOTREACHED() << "Unknown content setting for permission "
77 << static_cast<int>(permission);
78 return CONTENT_SETTINGS_TYPE_DEFAULT;
81 // Helper method that wraps a callback a void(PermissionStatus)
82 // callback into a void(ContentSetting) callback.
83 void PermissionStatusCallbackWrapper(
84 const base::Callback<void(PermissionStatus)>& callback,
85 ContentSetting content_setting) {
86 callback.Run(ContentSettingToPermissionStatus(content_setting));
89 // Returns whether the permission has a constant PermissionStatus value (i.e.
90 // always approved or always denied)
91 // The PermissionTypes for which true is returned should be exactly those which
92 // return nullptr in PermissionContext::Get since they don't have a context.
93 bool IsConstantPermission(PermissionType type) {
94 switch (type) {
95 case PermissionType::MIDI:
96 return true;
97 default:
98 return false;
102 // Function used for handling permission types which do not change their
103 // value i.e. they are always approved or always denied etc.
104 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling.
105 // This function should only be called when IsConstantPermission has returned
106 // true for the PermissionType.
107 ContentSetting GetContentSettingForConstantPermission(PermissionType type) {
108 DCHECK(IsConstantPermission(type));
109 switch (type) {
110 case PermissionType::MIDI:
111 return CONTENT_SETTING_ALLOW;
112 default:
113 return CONTENT_SETTING_DEFAULT;
117 PermissionStatus GetPermissionStatusForConstantPermission(PermissionType type) {
118 return ContentSettingToPermissionStatus(
119 GetContentSettingForConstantPermission(type));
122 } // anonymous namespace
124 struct PermissionManager::Subscription {
125 PermissionType permission;
126 GURL requesting_origin;
127 GURL embedding_origin;
128 base::Callback<void(PermissionStatus)> callback;
129 ContentSetting current_value;
132 PermissionManager::PermissionManager(Profile* profile)
133 : profile_(profile) {
136 PermissionManager::~PermissionManager() {
137 if (!subscriptions_.IsEmpty())
138 profile_->GetHostContentSettingsMap()->RemoveObserver(this);
141 void PermissionManager::RequestPermission(
142 PermissionType permission,
143 content::RenderFrameHost* render_frame_host,
144 int request_id,
145 const GURL& requesting_origin,
146 bool user_gesture,
147 const base::Callback<void(PermissionStatus)>& callback) {
148 if (IsConstantPermission(permission)) {
149 callback.Run(GetPermissionStatusForConstantPermission(permission));
150 return;
153 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
154 if (!context) {
155 callback.Run(content::PERMISSION_STATUS_DENIED);
156 return;
159 content::WebContents* web_contents =
160 content::WebContents::FromRenderFrameHost(render_frame_host);
161 if (IsPermissionBubbleManagerMissing(web_contents)) {
162 callback.Run(
163 GetPermissionStatus(permission, requesting_origin,
164 web_contents->GetLastCommittedURL().GetOrigin()));
165 return;
168 int render_process_id = render_frame_host->GetProcess()->GetID();
169 int render_frame_id = render_frame_host->GetRoutingID();
170 const PermissionRequestID request(render_process_id,
171 render_frame_id,
172 request_id,
173 requesting_origin);
175 context->RequestPermission(
176 web_contents, request, requesting_origin, user_gesture,
177 base::Bind(&PermissionStatusCallbackWrapper, callback));
180 void PermissionManager::CancelPermissionRequest(
181 PermissionType permission,
182 content::RenderFrameHost* render_frame_host,
183 int request_id,
184 const GURL& requesting_origin) {
185 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
186 if (!context)
187 return;
189 content::WebContents* web_contents =
190 content::WebContents::FromRenderFrameHost(render_frame_host);
191 if (IsPermissionBubbleManagerMissing(web_contents))
192 return;
194 int render_process_id = render_frame_host->GetProcess()->GetID();
195 int render_frame_id = render_frame_host->GetRoutingID();
196 const PermissionRequestID request(render_process_id,
197 render_frame_id,
198 request_id,
199 requesting_origin);
201 context->CancelPermissionRequest(web_contents, request);
204 void PermissionManager::ResetPermission(PermissionType permission,
205 const GURL& requesting_origin,
206 const GURL& embedding_origin) {
207 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
208 if (!context)
209 return;
211 context->ResetPermission(requesting_origin.GetOrigin(),
212 embedding_origin.GetOrigin());
215 PermissionStatus PermissionManager::GetPermissionStatus(
216 PermissionType permission,
217 const GURL& requesting_origin,
218 const GURL& embedding_origin) {
219 if (IsConstantPermission(permission))
220 return GetPermissionStatusForConstantPermission(permission);
222 PermissionContextBase* context = PermissionContext::Get(profile_, permission);
223 if (!context)
224 return content::PERMISSION_STATUS_DENIED;
226 return ContentSettingToPermissionStatus(context->GetPermissionStatus(
227 requesting_origin.GetOrigin(), embedding_origin.GetOrigin()));
230 void PermissionManager::RegisterPermissionUsage(PermissionType permission,
231 const GURL& requesting_origin,
232 const GURL& embedding_origin) {
233 // This is required because constant permissions don't have a
234 // ContentSettingsType.
235 if (IsConstantPermission(permission))
236 return;
238 profile_->GetHostContentSettingsMap()->UpdateLastUsage(
239 requesting_origin,
240 embedding_origin,
241 PermissionTypeToContentSetting(permission));
244 int PermissionManager::SubscribePermissionStatusChange(
245 PermissionType permission,
246 const GURL& requesting_origin,
247 const GURL& embedding_origin,
248 const base::Callback<void(PermissionStatus)>& callback) {
249 if (subscriptions_.IsEmpty())
250 profile_->GetHostContentSettingsMap()->AddObserver(this);
252 Subscription* subscription = new Subscription();
253 subscription->permission = permission;
254 subscription->requesting_origin = requesting_origin;
255 subscription->embedding_origin = embedding_origin;
256 subscription->callback = callback;
258 if (IsConstantPermission(permission)) {
259 subscription->current_value = GetContentSettingForConstantPermission(
260 permission);
261 } else {
262 subscription->current_value = PermissionContext::Get(profile_, permission)
263 ->GetPermissionStatus(subscription->requesting_origin,
264 subscription->embedding_origin);
267 return subscriptions_.Add(subscription);
270 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) {
271 // Whether |subscription_id| is known will be checked by the Remove() call.
272 subscriptions_.Remove(subscription_id);
274 if (subscriptions_.IsEmpty())
275 profile_->GetHostContentSettingsMap()->RemoveObserver(this);
278 bool PermissionManager::IsPermissionBubbleManagerMissing(
279 content::WebContents* web_contents) {
280 // Can't be missing if it isn't needed to begin with.
281 if (!PermissionBubbleManager::Enabled())
282 return false;
284 return PermissionBubbleManager::FromWebContents(web_contents) == nullptr;
287 void PermissionManager::OnContentSettingChanged(
288 const ContentSettingsPattern& primary_pattern,
289 const ContentSettingsPattern& secondary_pattern,
290 ContentSettingsType content_type,
291 std::string resource_identifier) {
292 std::list<base::Closure> callbacks;
294 for (SubscriptionsMap::iterator iter(&subscriptions_);
295 !iter.IsAtEnd(); iter.Advance()) {
296 Subscription* subscription = iter.GetCurrentValue();
297 if (PermissionTypeToContentSetting(subscription->permission) !=
298 content_type) {
299 continue;
302 if (primary_pattern.IsValid() &&
303 !primary_pattern.Matches(subscription->requesting_origin))
304 continue;
305 if (secondary_pattern.IsValid() &&
306 !secondary_pattern.Matches(subscription->embedding_origin))
307 continue;
309 ContentSetting new_value =
310 PermissionContext::Get(profile_, subscription->permission)
311 ->GetPermissionStatus(subscription->requesting_origin,
312 subscription->embedding_origin);
313 if (subscription->current_value == new_value)
314 continue;
316 subscription->current_value = new_value;
318 // Add the callback to |callbacks| which will be run after the loop to
319 // prevent re-entrance issues.
320 callbacks.push_back(
321 base::Bind(subscription->callback,
322 ContentSettingToPermissionStatus(new_value)));
325 for (const auto& callback : callbacks)
326 callback.Run();