| 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 URLPatternSet FilterSingleOriginPermissions(const URLPatternSet& permissions) { |
| 40 URLPatternSet single_origin_permissions; |
| 41 for (URLPatternSet::const_iterator iter = permissions.begin(); |
| 42 iter != permissions.end(); |
| 43 ++iter) { |
| 44 if (iter->MatchesSingleOrigin()) |
| 45 single_origin_permissions.AddPattern(*iter); |
| 46 } |
| 47 return single_origin_permissions; |
| 48 } |
| 49 |
| 39 // Returns a PermissionSet that has the active permissions of the extension, | 50 // Returns a PermissionSet that has the active permissions of the extension, |
| 40 // bounded to its current manifest. | 51 // bounded to its current manifest. |
| 41 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( | 52 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( |
| 42 const Extension* extension, ExtensionPrefs* extension_prefs) { | 53 const Extension* extension, const PermissionSet* active_permissions) { |
| 43 // If the extension has used the optional permissions API, it will have a | 54 // 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, | 55 // custom set of active permissions defined in the extension prefs. Here, |
| 45 // we update the extension's active permissions based on the prefs. | 56 // 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) | 57 if (!active_permissions) |
| 49 return extension->permissions_data()->active_permissions(); | 58 return extension->permissions_data()->active_permissions(); |
| 50 | 59 |
| 51 scoped_refptr<const PermissionSet> required_permissions = | 60 scoped_refptr<const PermissionSet> required_permissions = |
| 52 PermissionsParser::GetRequiredPermissions(extension); | 61 PermissionsParser::GetRequiredPermissions(extension); |
| 53 | 62 |
| 54 // We restrict the active permissions to be within the bounds defined in the | 63 // We restrict the active permissions to be within the bounds defined in the |
| 55 // extension's manifest. | 64 // extension's manifest. |
| 56 // a) active permissions must be a subset of optional + default permissions | 65 // a) active permissions must be a subset of optional + default permissions |
| 57 // b) active permissions must contains all default permissions | 66 // 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()) && | 146 if (!Manifest::IsUnpackedLocation(extension->location()) && |
| 138 extension->location() != Manifest::INTERNAL) | 147 extension->location() != Manifest::INTERNAL) |
| 139 return; | 148 return; |
| 140 | 149 |
| 141 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( | 150 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( |
| 142 extension->id(), | 151 extension->id(), |
| 143 extension->permissions_data()->active_permissions().get()); | 152 extension->permissions_data()->active_permissions().get()); |
| 144 } | 153 } |
| 145 | 154 |
| 146 void PermissionsUpdater::InitializePermissions(const Extension* extension) { | 155 void PermissionsUpdater::InitializePermissions(const Extension* extension) { |
| 156 scoped_refptr<const PermissionSet> active_permissions = ExtensionPrefs::Get( |
| 157 browser_context_)->GetActivePermissions(extension->id()); |
| 147 scoped_refptr<const PermissionSet> bounded_active = | 158 scoped_refptr<const PermissionSet> bounded_active = |
| 148 GetBoundedActivePermissions(extension, | 159 GetBoundedActivePermissions(extension, active_permissions.get()); |
| 149 ExtensionPrefs::Get(browser_context_)); | |
| 150 | 160 |
| 151 // We withhold permissions iff the switch to do so is enabled, the extension | 161 // 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), | 162 // 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 | 163 // 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 | 164 // the scripting whitelist. Additionally, we don't withhold if the extension |
| 155 // has the preference to allow scripting on all urls. | 165 // has the preference to allow scripting on all urls. |
| 156 bool should_withhold_permissions = | 166 bool should_withhold_permissions = |
| 157 FeatureSwitch::scripts_require_action()->IsEnabled() && | 167 FeatureSwitch::scripts_require_action()->IsEnabled() && |
| 158 extension->ShouldDisplayInExtensionSettings() && | 168 extension->ShouldDisplayInExtensionSettings() && |
| 159 !Manifest::IsPolicyLocation(extension->location()) && | 169 !Manifest::IsPolicyLocation(extension->location()) && |
| 160 !Manifest::IsComponentLocation(extension->location()) && | 170 !Manifest::IsComponentLocation(extension->location()) && |
| 161 !PermissionsData::CanExecuteScriptEverywhere(extension) && | 171 !PermissionsData::CanExecuteScriptEverywhere(extension) && |
| 162 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); | 172 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); |
| 163 | 173 |
| 164 URLPatternSet granted_explicit_hosts; | 174 URLPatternSet granted_explicit_hosts; |
| 165 URLPatternSet withheld_explicit_hosts; | 175 URLPatternSet withheld_explicit_hosts; |
| 166 SegregateUrlPermissions(bounded_active->explicit_hosts(), | 176 SegregateUrlPermissions(bounded_active->explicit_hosts(), |
| 167 should_withhold_permissions, | 177 should_withhold_permissions, |
| 168 &granted_explicit_hosts, | 178 &granted_explicit_hosts, |
| 169 &withheld_explicit_hosts); | 179 &withheld_explicit_hosts); |
| 170 | 180 |
| 171 URLPatternSet granted_scriptable_hosts; | 181 URLPatternSet granted_scriptable_hosts; |
| 172 URLPatternSet withheld_scriptable_hosts; | 182 URLPatternSet withheld_scriptable_hosts; |
| 173 SegregateUrlPermissions(bounded_active->scriptable_hosts(), | 183 SegregateUrlPermissions(bounded_active->scriptable_hosts(), |
| 174 should_withhold_permissions, | 184 should_withhold_permissions, |
| 175 &granted_scriptable_hosts, | 185 &granted_scriptable_hosts, |
| 176 &withheld_scriptable_hosts); | 186 &withheld_scriptable_hosts); |
| 177 | 187 |
| 188 // After withholding permissions, add back any origins to the active set that |
| 189 // may have been lost during the set operations that would have dropped them. |
| 190 // For example, the union of <all_urls> and <anything> is <all_urls>, so we |
| 191 // may lose the <anything>. However, that <anything> is important once |
| 192 // <all_urls> is stripped during withholding. |
| 193 if (active_permissions) { |
| 194 granted_explicit_hosts.AddPatterns( |
| 195 FilterSingleOriginPermissions(active_permissions->explicit_hosts())); |
| 196 granted_scriptable_hosts.AddPatterns( |
| 197 FilterSingleOriginPermissions(active_permissions->scriptable_hosts())); |
| 198 } |
| 199 |
| 178 bounded_active = new PermissionSet(bounded_active->apis(), | 200 bounded_active = new PermissionSet(bounded_active->apis(), |
| 179 bounded_active->manifest_permissions(), | 201 bounded_active->manifest_permissions(), |
| 180 granted_explicit_hosts, | 202 granted_explicit_hosts, |
| 181 granted_scriptable_hosts); | 203 granted_scriptable_hosts); |
| 182 | 204 |
| 183 scoped_refptr<const PermissionSet> withheld = | 205 scoped_refptr<const PermissionSet> withheld = |
| 184 new PermissionSet(APIPermissionSet(), | 206 new PermissionSet(APIPermissionSet(), |
| 185 ManifestPermissionSet(), | 207 ManifestPermissionSet(), |
| 186 withheld_explicit_hosts, | 208 withheld_explicit_hosts, |
| 187 withheld_scriptable_hosts); | 209 withheld_scriptable_hosts); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 343 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 322 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 344 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
| 323 } | 345 } |
| 324 } | 346 } |
| 325 | 347 |
| 326 // Trigger the onAdded and onRemoved events in the extension. | 348 // Trigger the onAdded and onRemoved events in the extension. |
| 327 DispatchEvent(extension->id(), event_name, changed); | 349 DispatchEvent(extension->id(), event_name, changed); |
| 328 } | 350 } |
| 329 | 351 |
| 330 } // namespace extensions | 352 } // namespace extensions |
| OLD | NEW |