OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/permissions_updater.h" | 5 #include "chrome/browser/extensions/permissions_updater.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
8 #include "base/values.h" | 10 #include "base/values.h" |
9 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 11 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
10 #include "chrome/browser/extensions/scripting_permissions_modifier.h" | 12 #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/extensions/api/permissions.h" | 14 #include "chrome/common/extensions/api/permissions.h" |
13 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
15 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 const PermissionSet& active = | 87 const PermissionSet& active = |
86 extension->permissions_data()->active_permissions(); | 88 extension->permissions_data()->active_permissions(); |
87 scoped_ptr<const PermissionSet> total = | 89 scoped_ptr<const PermissionSet> total = |
88 PermissionSet::CreateUnion(active, permissions); | 90 PermissionSet::CreateUnion(active, permissions); |
89 scoped_ptr<const PermissionSet> added = | 91 scoped_ptr<const PermissionSet> added = |
90 PermissionSet::CreateDifference(*total, active); | 92 PermissionSet::CreateDifference(*total, active); |
91 | 93 |
92 scoped_ptr<const PermissionSet> new_withheld = | 94 scoped_ptr<const PermissionSet> new_withheld = |
93 PermissionSet::CreateDifference( | 95 PermissionSet::CreateDifference( |
94 extension->permissions_data()->withheld_permissions(), permissions); | 96 extension->permissions_data()->withheld_permissions(), permissions); |
95 SetPermissions(extension, total.Pass(), new_withheld.Pass()); | 97 SetPermissions(extension, std::move(total), std::move(new_withheld)); |
96 | 98 |
97 // Update the granted permissions so we don't auto-disable the extension. | 99 // Update the granted permissions so we don't auto-disable the extension. |
98 GrantActivePermissions(extension); | 100 GrantActivePermissions(extension); |
99 | 101 |
100 NotifyPermissionsUpdated(ADDED, extension, *added); | 102 NotifyPermissionsUpdated(ADDED, extension, *added); |
101 } | 103 } |
102 | 104 |
103 void PermissionsUpdater::RemovePermissions(const Extension* extension, | 105 void PermissionsUpdater::RemovePermissions(const Extension* extension, |
104 const PermissionSet& to_remove, | 106 const PermissionSet& to_remove, |
105 RemoveType remove_type) { | 107 RemoveType remove_type) { |
106 // We should only be revoking revokable permissions. | 108 // We should only be revoking revokable permissions. |
107 CHECK(GetRevokablePermissions(extension)->Contains(to_remove)); | 109 CHECK(GetRevokablePermissions(extension)->Contains(to_remove)); |
108 | 110 |
109 const PermissionSet& active = | 111 const PermissionSet& active = |
110 extension->permissions_data()->active_permissions(); | 112 extension->permissions_data()->active_permissions(); |
111 scoped_ptr<const PermissionSet> remaining = | 113 scoped_ptr<const PermissionSet> remaining = |
112 PermissionSet::CreateDifference(active, to_remove); | 114 PermissionSet::CreateDifference(active, to_remove); |
113 | 115 |
114 // Move any granted permissions that were in the withheld set back to the | 116 // Move any granted permissions that were in the withheld set back to the |
115 // withheld set so they can be added back later. | 117 // withheld set so they can be added back later. |
116 // Any revoked permission that isn't from the optional permissions can only | 118 // Any revoked permission that isn't from the optional permissions can only |
117 // be a withheld permission. | 119 // be a withheld permission. |
118 scoped_ptr<const PermissionSet> removed_withheld = | 120 scoped_ptr<const PermissionSet> removed_withheld = |
119 PermissionSet::CreateDifference( | 121 PermissionSet::CreateDifference( |
120 to_remove, PermissionsParser::GetOptionalPermissions(extension)); | 122 to_remove, PermissionsParser::GetOptionalPermissions(extension)); |
121 scoped_ptr<const PermissionSet> withheld = PermissionSet::CreateUnion( | 123 scoped_ptr<const PermissionSet> withheld = PermissionSet::CreateUnion( |
122 *removed_withheld, extension->permissions_data()->withheld_permissions()); | 124 *removed_withheld, extension->permissions_data()->withheld_permissions()); |
123 | 125 |
124 SetPermissions(extension, remaining.Pass(), withheld.Pass()); | 126 SetPermissions(extension, std::move(remaining), std::move(withheld)); |
125 | 127 |
126 // We might not want to revoke the granted permissions because the extension, | 128 // We might not want to revoke the granted permissions because the extension, |
127 // not the user, removed the permissions. This allows the extension to add | 129 // not the user, removed the permissions. This allows the extension to add |
128 // them again without prompting the user. | 130 // them again without prompting the user. |
129 if (remove_type == REMOVE_HARD) { | 131 if (remove_type == REMOVE_HARD) { |
130 ExtensionPrefs::Get(browser_context_) | 132 ExtensionPrefs::Get(browser_context_) |
131 ->RemoveGrantedPermissions(extension->id(), to_remove); | 133 ->RemoveGrantedPermissions(extension->id(), to_remove); |
132 } | 134 } |
133 | 135 |
134 NotifyPermissionsUpdated(REMOVED, extension, to_remove); | 136 NotifyPermissionsUpdated(REMOVED, extension, to_remove); |
135 } | 137 } |
136 | 138 |
137 void PermissionsUpdater::RemovePermissionsUnsafe( | 139 void PermissionsUpdater::RemovePermissionsUnsafe( |
138 const Extension* extension, | 140 const Extension* extension, |
139 const PermissionSet& to_remove) { | 141 const PermissionSet& to_remove) { |
140 const PermissionSet& active = | 142 const PermissionSet& active = |
141 extension->permissions_data()->active_permissions(); | 143 extension->permissions_data()->active_permissions(); |
142 scoped_ptr<const PermissionSet> total = | 144 scoped_ptr<const PermissionSet> total = |
143 PermissionSet::CreateDifference(active, to_remove); | 145 PermissionSet::CreateDifference(active, to_remove); |
144 // |successfully_removed| might not equal |to_remove| if |to_remove| contains | 146 // |successfully_removed| might not equal |to_remove| if |to_remove| contains |
145 // permissions the extension didn't have. | 147 // permissions the extension didn't have. |
146 scoped_ptr<const PermissionSet> successfully_removed = | 148 scoped_ptr<const PermissionSet> successfully_removed = |
147 PermissionSet::CreateDifference(active, *total); | 149 PermissionSet::CreateDifference(active, *total); |
148 | 150 |
149 SetPermissions(extension, total.Pass(), nullptr); | 151 SetPermissions(extension, std::move(total), nullptr); |
150 NotifyPermissionsUpdated(REMOVED, extension, *successfully_removed); | 152 NotifyPermissionsUpdated(REMOVED, extension, *successfully_removed); |
151 } | 153 } |
152 | 154 |
153 scoped_ptr<const PermissionSet> PermissionsUpdater::GetRevokablePermissions( | 155 scoped_ptr<const PermissionSet> PermissionsUpdater::GetRevokablePermissions( |
154 const Extension* extension) const { | 156 const Extension* extension) const { |
155 // The user can revoke any permissions they granted. In other words, any | 157 // The user can revoke any permissions they granted. In other words, any |
156 // permissions the extension didn't start with can be revoked. | 158 // permissions the extension didn't start with can be revoked. |
157 const PermissionSet& required = | 159 const PermissionSet& required = |
158 PermissionsParser::GetRequiredPermissions(extension); | 160 PermissionsParser::GetRequiredPermissions(extension); |
159 scoped_ptr<const PermissionSet> granted; | 161 scoped_ptr<const PermissionSet> granted; |
(...skipping 28 matching lines...) Expand all Loading... |
188 bounded_active = bounded_wrapper.get(); | 190 bounded_active = bounded_wrapper.get(); |
189 } | 191 } |
190 | 192 |
191 scoped_ptr<const PermissionSet> granted_permissions; | 193 scoped_ptr<const PermissionSet> granted_permissions; |
192 scoped_ptr<const PermissionSet> withheld_permissions; | 194 scoped_ptr<const PermissionSet> withheld_permissions; |
193 ScriptingPermissionsModifier(browser_context_, make_scoped_refptr(extension)) | 195 ScriptingPermissionsModifier(browser_context_, make_scoped_refptr(extension)) |
194 .WithholdPermissions(*bounded_active, &granted_permissions, | 196 .WithholdPermissions(*bounded_active, &granted_permissions, |
195 &withheld_permissions, | 197 &withheld_permissions, |
196 (init_flag_ & INIT_FLAG_TRANSIENT) != 0); | 198 (init_flag_ & INIT_FLAG_TRANSIENT) != 0); |
197 | 199 |
198 SetPermissions(extension, granted_permissions.Pass(), | 200 SetPermissions(extension, std::move(granted_permissions), |
199 withheld_permissions.Pass()); | 201 std::move(withheld_permissions)); |
200 } | 202 } |
201 | 203 |
202 void PermissionsUpdater::SetPermissions( | 204 void PermissionsUpdater::SetPermissions( |
203 const Extension* extension, | 205 const Extension* extension, |
204 scoped_ptr<const PermissionSet> active, | 206 scoped_ptr<const PermissionSet> active, |
205 scoped_ptr<const PermissionSet> withheld) { | 207 scoped_ptr<const PermissionSet> withheld) { |
206 DCHECK(active); | 208 DCHECK(active); |
207 const PermissionSet& active_weak = *active; | 209 const PermissionSet& active_weak = *active; |
208 if (withheld) { | 210 if (withheld) { |
209 extension->permissions_data()->SetPermissions(active.Pass(), | 211 extension->permissions_data()->SetPermissions(std::move(active), |
210 withheld.Pass()); | 212 std::move(withheld)); |
211 } else { | 213 } else { |
212 extension->permissions_data()->SetActivePermissions(active.Pass()); | 214 extension->permissions_data()->SetActivePermissions(std::move(active)); |
213 } | 215 } |
214 | 216 |
215 if ((init_flag_ & INIT_FLAG_TRANSIENT) == 0) { | 217 if ((init_flag_ & INIT_FLAG_TRANSIENT) == 0) { |
216 ExtensionPrefs::Get(browser_context_) | 218 ExtensionPrefs::Get(browser_context_) |
217 ->SetActivePermissions(extension->id(), active_weak); | 219 ->SetActivePermissions(extension->id(), active_weak); |
218 } | 220 } |
219 } | 221 } |
220 | 222 |
221 void PermissionsUpdater::DispatchEvent( | 223 void PermissionsUpdater::DispatchEvent( |
222 const std::string& extension_id, | 224 const std::string& extension_id, |
223 events::HistogramValue histogram_value, | 225 events::HistogramValue histogram_value, |
224 const char* event_name, | 226 const char* event_name, |
225 const PermissionSet& changed_permissions) { | 227 const PermissionSet& changed_permissions) { |
226 EventRouter* event_router = EventRouter::Get(browser_context_); | 228 EventRouter* event_router = EventRouter::Get(browser_context_); |
227 if (!event_router) | 229 if (!event_router) |
228 return; | 230 return; |
229 | 231 |
230 scoped_ptr<base::ListValue> value(new base::ListValue()); | 232 scoped_ptr<base::ListValue> value(new base::ListValue()); |
231 scoped_ptr<api::permissions::Permissions> permissions = | 233 scoped_ptr<api::permissions::Permissions> permissions = |
232 PackPermissionSet(changed_permissions); | 234 PackPermissionSet(changed_permissions); |
233 value->Append(permissions->ToValue().release()); | 235 value->Append(permissions->ToValue().release()); |
234 scoped_ptr<Event> event(new Event(histogram_value, event_name, value.Pass())); | 236 scoped_ptr<Event> event( |
| 237 new Event(histogram_value, event_name, std::move(value))); |
235 event->restrict_to_browser_context = browser_context_; | 238 event->restrict_to_browser_context = browser_context_; |
236 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 239 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
237 } | 240 } |
238 | 241 |
239 void PermissionsUpdater::NotifyPermissionsUpdated( | 242 void PermissionsUpdater::NotifyPermissionsUpdated( |
240 EventType event_type, | 243 EventType event_type, |
241 const Extension* extension, | 244 const Extension* extension, |
242 const PermissionSet& changed) { | 245 const PermissionSet& changed) { |
243 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); | 246 DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); |
244 if (changed.IsEmpty()) | 247 if (changed.IsEmpty()) |
245 return; | 248 return; |
246 | 249 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 286 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
284 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 287 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
285 } | 288 } |
286 } | 289 } |
287 | 290 |
288 // Trigger the onAdded and onRemoved events in the extension. | 291 // Trigger the onAdded and onRemoved events in the extension. |
289 DispatchEvent(extension->id(), histogram_value, event_name, changed); | 292 DispatchEvent(extension->id(), histogram_value, event_name, changed); |
290 } | 293 } |
291 | 294 |
292 } // namespace extensions | 295 } // namespace extensions |
OLD | NEW |