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 "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 | 29 |
30 using content::RenderProcessHost; | 30 using content::RenderProcessHost; |
31 using extensions::permissions_api_helpers::PackPermissionSet; | 31 using extensions::permissions_api_helpers::PackPermissionSet; |
32 | 32 |
33 namespace extensions { | 33 namespace extensions { |
34 | 34 |
35 namespace permissions = api::permissions; | 35 namespace permissions = api::permissions; |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // Returns a set of single origin permissions from |permissions| that match | |
40 // |bounds|. This is necessary for two reasons: | |
41 // a) single origin active permissions can get filtered out in | |
42 // GetBoundedActivePermissions because they are not recognized as a subset | |
43 // of all-host permissions | |
44 // b) active permissions that do not match any manifest permissions can | |
45 // exist if a manifest permission is dropped | |
46 URLPatternSet FilterSingleOriginPermissions(const URLPatternSet& permissions, | |
47 const URLPatternSet& bounds) { | |
48 URLPatternSet single_origin_permissions; | |
49 for (URLPatternSet::const_iterator iter = permissions.begin(); | |
50 iter != permissions.end(); | |
51 ++iter) { | |
52 if (iter->MatchesSingleOrigin() && | |
53 bounds.MatchesURL(GURL(iter->GetAsString()))) { | |
54 single_origin_permissions.AddPattern(*iter); | |
55 } | |
56 } | |
57 return single_origin_permissions; | |
58 } | |
59 | |
39 // Returns a PermissionSet that has the active permissions of the extension, | 60 // Returns a PermissionSet that has the active permissions of the extension, |
40 // bounded to its current manifest. | 61 // bounded to its current manifest. |
41 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( | 62 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( |
42 const Extension* extension, ExtensionPrefs* extension_prefs) { | 63 const Extension* extension, |
64 const PermissionSet* active_permissions) { | |
Devlin
2014/08/14 01:20:19
might as well make this take a const refptr&
gpdavis
2014/08/14 20:05:43
Done.
| |
43 // If the extension has used the optional permissions API, it will have a | 65 // If the extension has used the optional permissions API, it will have a |
44 // custom set of active permissions defined in the extension prefs. Here, | 66 // custom set of active permissions defined in the extension prefs. Here, |
45 // we update the extension's active permissions based on the prefs. | 67 // we update the extension's active permissions based on the prefs. |
46 scoped_refptr<const PermissionSet> active_permissions = | |
47 extension_prefs->GetActivePermissions(extension->id()); | |
48 if (!active_permissions) | 68 if (!active_permissions) |
49 return extension->permissions_data()->active_permissions(); | 69 return extension->permissions_data()->active_permissions(); |
50 | 70 |
51 scoped_refptr<const PermissionSet> required_permissions = | 71 scoped_refptr<const PermissionSet> required_permissions = |
52 PermissionsParser::GetRequiredPermissions(extension); | 72 PermissionsParser::GetRequiredPermissions(extension); |
53 | 73 |
54 // We restrict the active permissions to be within the bounds defined in the | 74 // We restrict the active permissions to be within the bounds defined in the |
55 // extension's manifest. | 75 // extension's manifest. |
56 // a) active permissions must be a subset of optional + default permissions | 76 // a) active permissions must be a subset of optional + default permissions |
57 // b) active permissions must contains all default permissions | 77 // b) active permissions must contains all default permissions |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 if (!Manifest::IsUnpackedLocation(extension->location()) && | 157 if (!Manifest::IsUnpackedLocation(extension->location()) && |
138 extension->location() != Manifest::INTERNAL) | 158 extension->location() != Manifest::INTERNAL) |
139 return; | 159 return; |
140 | 160 |
141 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( | 161 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( |
142 extension->id(), | 162 extension->id(), |
143 extension->permissions_data()->active_permissions().get()); | 163 extension->permissions_data()->active_permissions().get()); |
144 } | 164 } |
145 | 165 |
146 void PermissionsUpdater::InitializePermissions(const Extension* extension) { | 166 void PermissionsUpdater::InitializePermissions(const Extension* extension) { |
167 scoped_refptr<const PermissionSet> active_permissions = | |
168 ExtensionPrefs::Get(browser_context_) | |
169 ->GetActivePermissions(extension->id()); | |
147 scoped_refptr<const PermissionSet> bounded_active = | 170 scoped_refptr<const PermissionSet> bounded_active = |
148 GetBoundedActivePermissions(extension, | 171 GetBoundedActivePermissions(extension, active_permissions.get()); |
149 ExtensionPrefs::Get(browser_context_)); | |
150 | 172 |
151 // We withhold permissions iff the switch to do so is enabled, the extension | 173 // We withhold permissions iff the switch to do so is enabled, the extension |
152 // shows up in chrome:extensions (so the user can grant withheld permissions), | 174 // shows up in chrome:extensions (so the user can grant withheld permissions), |
153 // the extension is not part of chrome or corporate policy, and also not on | 175 // the extension is not part of chrome or corporate policy, and also not on |
154 // the scripting whitelist. Additionally, we don't withhold if the extension | 176 // the scripting whitelist. Additionally, we don't withhold if the extension |
155 // has the preference to allow scripting on all urls. | 177 // has the preference to allow scripting on all urls. |
156 bool should_withhold_permissions = | 178 bool should_withhold_permissions = |
157 FeatureSwitch::scripts_require_action()->IsEnabled() && | 179 FeatureSwitch::scripts_require_action()->IsEnabled() && |
158 extension->ShouldDisplayInExtensionSettings() && | 180 extension->ShouldDisplayInExtensionSettings() && |
159 !Manifest::IsPolicyLocation(extension->location()) && | 181 !Manifest::IsPolicyLocation(extension->location()) && |
160 !Manifest::IsComponentLocation(extension->location()) && | 182 !Manifest::IsComponentLocation(extension->location()) && |
161 !PermissionsData::CanExecuteScriptEverywhere(extension) && | 183 !PermissionsData::CanExecuteScriptEverywhere(extension) && |
162 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); | 184 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); |
163 | 185 |
164 URLPatternSet granted_explicit_hosts; | 186 URLPatternSet granted_explicit_hosts; |
165 URLPatternSet withheld_explicit_hosts; | 187 URLPatternSet withheld_explicit_hosts; |
166 SegregateUrlPermissions(bounded_active->explicit_hosts(), | 188 SegregateUrlPermissions(bounded_active->explicit_hosts(), |
167 should_withhold_permissions, | 189 should_withhold_permissions, |
168 &granted_explicit_hosts, | 190 &granted_explicit_hosts, |
169 &withheld_explicit_hosts); | 191 &withheld_explicit_hosts); |
170 | 192 |
171 URLPatternSet granted_scriptable_hosts; | 193 URLPatternSet granted_scriptable_hosts; |
172 URLPatternSet withheld_scriptable_hosts; | 194 URLPatternSet withheld_scriptable_hosts; |
173 SegregateUrlPermissions(bounded_active->scriptable_hosts(), | 195 SegregateUrlPermissions(bounded_active->scriptable_hosts(), |
174 should_withhold_permissions, | 196 should_withhold_permissions, |
175 &granted_scriptable_hosts, | 197 &granted_scriptable_hosts, |
176 &withheld_scriptable_hosts); | 198 &withheld_scriptable_hosts); |
177 | 199 |
200 // After withholding permissions, add back any origins to the active set that | |
201 // may have been lost during the set operations that would have dropped them. | |
202 // For example, the union of <all_urls> and <"example.com"> is <all_urls>, so | |
Devlin
2014/08/14 01:20:19
drop the brackets around "example.com"
gpdavis
2014/08/14 20:05:43
Done.
| |
203 // we may lose "example.com". However, "example.com" is important once | |
204 // <all_urls> is stripped during withholding. | |
205 if (active_permissions) { | |
206 granted_explicit_hosts.AddPatterns( | |
207 FilterSingleOriginPermissions(active_permissions->explicit_hosts(), | |
208 bounded_active->explicit_hosts())); | |
209 granted_scriptable_hosts.AddPatterns( | |
210 FilterSingleOriginPermissions(active_permissions->scriptable_hosts(), | |
211 bounded_active->scriptable_hosts())); | |
212 } | |
213 | |
178 bounded_active = new PermissionSet(bounded_active->apis(), | 214 bounded_active = new PermissionSet(bounded_active->apis(), |
179 bounded_active->manifest_permissions(), | 215 bounded_active->manifest_permissions(), |
180 granted_explicit_hosts, | 216 granted_explicit_hosts, |
181 granted_scriptable_hosts); | 217 granted_scriptable_hosts); |
182 | 218 |
183 scoped_refptr<const PermissionSet> withheld = | 219 scoped_refptr<const PermissionSet> withheld = |
184 new PermissionSet(APIPermissionSet(), | 220 new PermissionSet(APIPermissionSet(), |
185 ManifestPermissionSet(), | 221 ManifestPermissionSet(), |
186 withheld_explicit_hosts, | 222 withheld_explicit_hosts, |
187 withheld_scriptable_hosts); | 223 withheld_scriptable_hosts); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 357 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
322 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 358 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
323 } | 359 } |
324 } | 360 } |
325 | 361 |
326 // Trigger the onAdded and onRemoved events in the extension. | 362 // Trigger the onAdded and onRemoved events in the extension. |
327 DispatchEvent(extension->id(), event_name, changed); | 363 DispatchEvent(extension->id(), event_name, changed); |
328 } | 364 } |
329 | 365 |
330 } // namespace extensions | 366 } // namespace extensions |
OLD | NEW |