OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/permissions/permission_manager.h" | 5 #include "chrome/browser/permissions/permission_manager.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "chrome/browser/content_settings/permission_context_base.h" | 8 #include "chrome/browser/content_settings/permission_context_base.h" |
9 #include "chrome/browser/permissions/permission_context.h" | 9 #include "chrome/browser/permissions/permission_context.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // Helper method that wraps a callback a void(content::PermissionStatus) | 70 // Helper method that wraps a callback a void(content::PermissionStatus) |
71 // callback into a void(ContentSetting) callback. | 71 // callback into a void(ContentSetting) callback. |
72 void PermissionStatusCallbackWrapper( | 72 void PermissionStatusCallbackWrapper( |
73 const base::Callback<void(content::PermissionStatus)>& callback, | 73 const base::Callback<void(content::PermissionStatus)>& callback, |
74 ContentSetting content_setting) { | 74 ContentSetting content_setting) { |
75 callback.Run(ContentSettingToPermissionStatus(content_setting)); | 75 callback.Run(ContentSettingToPermissionStatus(content_setting)); |
76 } | 76 } |
77 | 77 |
78 } // anonymous namespace | 78 } // anonymous namespace |
79 | 79 |
| 80 struct PermissionManager::Subscription { |
| 81 content::PermissionType permission; |
| 82 GURL requesting_origin; |
| 83 GURL embedding_origin; |
| 84 base::Callback<void(content::PermissionStatus)> callback; |
| 85 ContentSetting current_value; |
| 86 }; |
| 87 |
80 PermissionManager::PermissionManager(Profile* profile) | 88 PermissionManager::PermissionManager(Profile* profile) |
81 : profile_(profile) { | 89 : profile_(profile) { |
82 } | 90 } |
83 | 91 |
84 PermissionManager::~PermissionManager() { | 92 PermissionManager::~PermissionManager() { |
| 93 if (!subscriptions_.IsEmpty()) |
| 94 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
85 } | 95 } |
86 | 96 |
87 void PermissionManager::RequestPermission( | 97 void PermissionManager::RequestPermission( |
88 content::PermissionType permission, | 98 content::PermissionType permission, |
89 content::WebContents* web_contents, | 99 content::WebContents* web_contents, |
90 int request_id, | 100 int request_id, |
91 const GURL& requesting_origin, | 101 const GURL& requesting_origin, |
92 bool user_gesture, | 102 bool user_gesture, |
93 const base::Callback<void(content::PermissionStatus)>& callback) { | 103 const base::Callback<void(content::PermissionStatus)>& callback) { |
94 PermissionContextBase* context = PermissionContext::Get(profile_, permission); | 104 PermissionContextBase* context = PermissionContext::Get(profile_, permission); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 166 |
157 void PermissionManager::RegisterPermissionUsage( | 167 void PermissionManager::RegisterPermissionUsage( |
158 content::PermissionType permission, | 168 content::PermissionType permission, |
159 const GURL& requesting_origin, | 169 const GURL& requesting_origin, |
160 const GURL& embedding_origin) { | 170 const GURL& embedding_origin) { |
161 profile_->GetHostContentSettingsMap()->UpdateLastUsage( | 171 profile_->GetHostContentSettingsMap()->UpdateLastUsage( |
162 requesting_origin, | 172 requesting_origin, |
163 embedding_origin, | 173 embedding_origin, |
164 PermissionTypeToContentSetting(permission)); | 174 PermissionTypeToContentSetting(permission)); |
165 } | 175 } |
| 176 |
| 177 int PermissionManager::SubscribePermissionStatusChange( |
| 178 content::PermissionType permission, |
| 179 const GURL& requesting_origin, |
| 180 const GURL& embedding_origin, |
| 181 const base::Callback<void(content::PermissionStatus)>& callback) { |
| 182 if (subscriptions_.IsEmpty()) |
| 183 profile_->GetHostContentSettingsMap()->AddObserver(this); |
| 184 |
| 185 Subscription* subscription = new Subscription(); |
| 186 subscription->permission = permission; |
| 187 subscription->requesting_origin = requesting_origin; |
| 188 subscription->embedding_origin = embedding_origin; |
| 189 subscription->callback = callback; |
| 190 subscription->current_value = PermissionContext::Get(profile_, permission) |
| 191 ->GetPermissionStatus(subscription->requesting_origin, |
| 192 subscription->embedding_origin); |
| 193 |
| 194 return subscriptions_.Add(subscription); |
| 195 } |
| 196 |
| 197 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) { |
| 198 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 199 subscriptions_.Remove(subscription_id); |
| 200 |
| 201 if (subscriptions_.IsEmpty()) |
| 202 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
| 203 } |
| 204 |
| 205 void PermissionManager::OnContentSettingChanged( |
| 206 const ContentSettingsPattern& primary_pattern, |
| 207 const ContentSettingsPattern& secondary_pattern, |
| 208 ContentSettingsType content_type, |
| 209 std::string resource_identifier) { |
| 210 for (SubscriptionsMap::iterator iter(&subscriptions_); |
| 211 !iter.IsAtEnd(); iter.Advance()) { |
| 212 Subscription* subscription = iter.GetCurrentValue(); |
| 213 if (PermissionTypeToContentSetting(subscription->permission) != |
| 214 content_type) { |
| 215 continue; |
| 216 } |
| 217 |
| 218 if (primary_pattern.IsValid() && |
| 219 !primary_pattern.Matches(subscription->requesting_origin)) |
| 220 continue; |
| 221 if (secondary_pattern.IsValid() && |
| 222 !secondary_pattern.Matches(subscription->embedding_origin)) |
| 223 continue; |
| 224 |
| 225 ContentSetting new_value = |
| 226 PermissionContext::Get(profile_, subscription->permission) |
| 227 ->GetPermissionStatus(subscription->requesting_origin, |
| 228 subscription->embedding_origin); |
| 229 if (subscription->current_value == new_value) |
| 230 continue; |
| 231 |
| 232 subscription->current_value = new_value; |
| 233 subscription->callback.Run(ContentSettingToPermissionStatus(new_value)); |
| 234 } |
| 235 } |
OLD | NEW |