| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #ifndef EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ | 5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ |
| 6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ | 6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // with the given |extension_id|. | 75 // with the given |extension_id|. |
| 76 static bool ShouldSkipPermissionWarnings(const std::string& extension_id); | 76 static bool ShouldSkipPermissionWarnings(const std::string& extension_id); |
| 77 | 77 |
| 78 // Returns true if the given |url| is restricted for the given |extension|, | 78 // Returns true if the given |url| is restricted for the given |extension|, |
| 79 // as is commonly the case for chrome:// urls. | 79 // as is commonly the case for chrome:// urls. |
| 80 // NOTE: You probably want to use CanAccessPage(). | 80 // NOTE: You probably want to use CanAccessPage(). |
| 81 static bool IsRestrictedUrl(const GURL& document_url, | 81 static bool IsRestrictedUrl(const GURL& document_url, |
| 82 const Extension* extension, | 82 const Extension* extension, |
| 83 std::string* error); | 83 std::string* error); |
| 84 | 84 |
| 85 // Is this extension using the default scope for policy_blocked_hosts and | |
| 86 // policy_allowed_hosts of the ExtensionSettings policy. | |
| 87 bool UsesDefaultPolicyHostRestrictions() const; | |
| 88 | |
| 89 // Locks the permissions data to the current thread. We don't do this on | 85 // Locks the permissions data to the current thread. We don't do this on |
| 90 // construction, since extensions are initialized across multiple threads. | 86 // construction, since extensions are initialized across multiple threads. |
| 91 void BindToCurrentThread() const; | 87 void BindToCurrentThread() const; |
| 92 | 88 |
| 93 // Sets the runtime permissions of the given |extension| to |active| and | 89 // Sets the runtime permissions of the given |extension| to |active| and |
| 94 // |withheld|. | 90 // |withheld|. |
| 95 void SetPermissions(std::unique_ptr<const PermissionSet> active, | 91 void SetPermissions(std::unique_ptr<const PermissionSet> active, |
| 96 std::unique_ptr<const PermissionSet> withheld) const; | 92 std::unique_ptr<const PermissionSet> withheld) const; |
| 97 | 93 |
| 98 // Applies restrictions from enterprise policy limiting which URLs this | |
| 99 // extension can interact with. The same policy can also define a default set | |
| 100 // of URL restrictions using SetDefaultPolicyHostRestrictions. This function | |
| 101 // overrides any default host restriction policy. | |
| 102 void SetPolicyHostRestrictions( | |
| 103 const URLPatternSet& runtime_blocked_hosts, | |
| 104 const URLPatternSet& runtime_allowed_hosts) const; | |
| 105 | |
| 106 // Marks this extension as using default enterprise policy limiting | |
| 107 // which URLs extensions can interact with. A default policy can be set with | |
| 108 // SetDefaultPolicyHostRestrictions. A policy specific to this extension | |
| 109 // can be set with SetPolicyHostRestrictions. | |
| 110 void SetUsesDefaultHostRestrictions() const; | |
| 111 | |
| 112 // Applies restrictions from enterprise policy limiting which URLs all | |
| 113 // extensions can interact with. This restriction can be overridden on a | |
| 114 // per-extension basis with SetPolicyHostRestrictions. | |
| 115 static void SetDefaultPolicyHostRestrictions( | |
| 116 const URLPatternSet& default_runtime_blocked_hosts, | |
| 117 const URLPatternSet& default_runtime_allowed_hosts); | |
| 118 | |
| 119 // Sets the active permissions, leaving withheld the same. | 94 // Sets the active permissions, leaving withheld the same. |
| 120 void SetActivePermissions(std::unique_ptr<const PermissionSet> active) const; | 95 void SetActivePermissions(std::unique_ptr<const PermissionSet> active) const; |
| 121 | 96 |
| 122 // Updates the tab-specific permissions of |tab_id| to include those from | 97 // Updates the tab-specific permissions of |tab_id| to include those from |
| 123 // |permissions|. | 98 // |permissions|. |
| 124 void UpdateTabSpecificPermissions(int tab_id, | 99 void UpdateTabSpecificPermissions(int tab_id, |
| 125 const PermissionSet& permissions) const; | 100 const PermissionSet& permissions) const; |
| 126 | 101 |
| 127 // Clears the tab-specific permissions of |tab_id|. | 102 // Clears the tab-specific permissions of |tab_id|. |
| 128 void ClearTabSpecificPermissions(int tab_id) const; | 103 void ClearTabSpecificPermissions(int tab_id) const; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 const PermissionSet& active_permissions() const { | 194 const PermissionSet& active_permissions() const { |
| 220 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | 195 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 221 return *active_permissions_unsafe_; | 196 return *active_permissions_unsafe_; |
| 222 } | 197 } |
| 223 | 198 |
| 224 const PermissionSet& withheld_permissions() const { | 199 const PermissionSet& withheld_permissions() const { |
| 225 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | 200 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 226 return *withheld_permissions_unsafe_; | 201 return *withheld_permissions_unsafe_; |
| 227 } | 202 } |
| 228 | 203 |
| 229 // Returns list of hosts this extension may not interact with by policy. | |
| 230 // This should only be used for 1. Serialization when initializing renderers | |
| 231 // or 2. Called from utility methods above. For all other uses, call utility | |
| 232 // methods instead (e.g. CanAccessPage()). | |
| 233 static const URLPatternSet& default_policy_blocked_hosts(); | |
| 234 | |
| 235 // Returns list of hosts this extension may interact with regardless of | |
| 236 // what is defined by policy_blocked_hosts(). | |
| 237 // This should only be used for 1. Serialization when initializing renderers | |
| 238 // or 2. Called from utility methods above. For all other uses, call utility | |
| 239 // methods instead (e.g. CanAccessPage()). | |
| 240 static const URLPatternSet& default_policy_allowed_hosts(); | |
| 241 | |
| 242 // Returns list of hosts this extension may not interact with by policy. | |
| 243 // This should only be used for 1. Serialization when initializing renderers | |
| 244 // or 2. Called from utility methods above. For all other uses, call utility | |
| 245 // methods instead (e.g. CanAccessPage()). | |
| 246 const URLPatternSet policy_blocked_hosts() const; | |
| 247 | |
| 248 // Returns list of hosts this extension may interact with regardless of | |
| 249 // what is defined by policy_blocked_hosts(). | |
| 250 // This should only be used for 1. Serialization when initializing renderers | |
| 251 // or 2. Called from utility methods above. For all other uses, call utility | |
| 252 // methods instead (e.g. CanAccessPage()). | |
| 253 const URLPatternSet policy_allowed_hosts() const; | |
| 254 | |
| 255 #if defined(UNIT_TEST) | 204 #if defined(UNIT_TEST) |
| 256 const PermissionSet* GetTabSpecificPermissionsForTesting(int tab_id) const { | 205 const PermissionSet* GetTabSpecificPermissionsForTesting(int tab_id) const { |
| 257 base::AutoLock auto_lock(runtime_lock_); | 206 base::AutoLock auto_lock(runtime_lock_); |
| 258 return GetTabSpecificPermissions(tab_id); | 207 return GetTabSpecificPermissions(tab_id); |
| 259 } | 208 } |
| 260 | |
| 261 bool IsRuntimeBlockedHostForTesting(const GURL& url) const { | |
| 262 base::AutoLock auto_lock(runtime_lock_); | |
| 263 return IsRuntimeBlockedHost(url); | |
| 264 } | |
| 265 #endif | 209 #endif |
| 266 | 210 |
| 267 private: | 211 private: |
| 268 // Gets the tab-specific host permissions of |tab_id|, or NULL if there | 212 // Gets the tab-specific host permissions of |tab_id|, or NULL if there |
| 269 // aren't any. | 213 // aren't any. |
| 270 // Must be called with |runtime_lock_| acquired. | 214 // Must be called with |runtime_lock_| acquired. |
| 271 const PermissionSet* GetTabSpecificPermissions(int tab_id) const; | 215 const PermissionSet* GetTabSpecificPermissions(int tab_id) const; |
| 272 | 216 |
| 273 // Returns true if the |extension| has tab-specific permission to operate on | 217 // Returns true if the |extension| has tab-specific permission to operate on |
| 274 // the tab specified by |tab_id| with the given |url|. | 218 // the tab specified by |tab_id| with the given |url|. |
| 275 // Note that if this returns false, it doesn't mean the extension can't run on | 219 // Note that if this returns false, it doesn't mean the extension can't run on |
| 276 // the given tab, only that it does not have tab-specific permission to do so. | 220 // the given tab, only that it does not have tab-specific permission to do so. |
| 277 // Must be called with |runtime_lock_| acquired. | 221 // Must be called with |runtime_lock_| acquired. |
| 278 bool HasTabSpecificPermissionToExecuteScript(int tab_id, | 222 bool HasTabSpecificPermissionToExecuteScript(int tab_id, |
| 279 const GURL& url) const; | 223 const GURL& url) const; |
| 280 | 224 |
| 281 // Returns whether or not the extension is permitted to run on the given page, | 225 // Returns whether or not the extension is permitted to run on the given page, |
| 282 // checking against |permitted_url_patterns| in addition to blocking special | 226 // checking against |permitted_url_patterns| in addition to blocking special |
| 283 // sites (like the webstore or chrome:// urls). | 227 // sites (like the webstore or chrome:// urls). |
| 284 // Must be called with |runtime_lock_| acquired. | 228 // Must be called with |runtime_lock_| acquired. |
| 285 AccessType CanRunOnPage(const Extension* extension, | 229 AccessType CanRunOnPage(const Extension* extension, |
| 286 const GURL& document_url, | 230 const GURL& document_url, |
| 287 int tab_id, | 231 int tab_id, |
| 288 const URLPatternSet& permitted_url_patterns, | 232 const URLPatternSet& permitted_url_patterns, |
| 289 const URLPatternSet& withheld_url_patterns, | 233 const URLPatternSet& withheld_url_patterns, |
| 290 std::string* error) const; | 234 std::string* error) const; |
| 291 | 235 |
| 292 // Check if a specific URL is blocked by policy from extension use at runtime. | |
| 293 bool IsRuntimeBlockedHost(const GURL& url) const; | |
| 294 | |
| 295 // Same as policy_blocked_hosts but instead returns a reference. | |
| 296 // You must acquire runtime_lock_ before calling this. | |
| 297 const URLPatternSet& PolicyBlockedHostsUnsafe() const; | |
| 298 | |
| 299 // Same as policy_allowed_hosts but instead returns a reference. | |
| 300 // You must acquire runtime_lock_ before calling this. | |
| 301 const URLPatternSet& PolicyAllowedHostsUnsafe() const; | |
| 302 | |
| 303 // The associated extension's id. | 236 // The associated extension's id. |
| 304 std::string extension_id_; | 237 std::string extension_id_; |
| 305 | 238 |
| 306 // The associated extension's manifest type. | 239 // The associated extension's manifest type. |
| 307 Manifest::Type manifest_type_; | 240 Manifest::Type manifest_type_; |
| 308 | 241 |
| 309 mutable base::Lock runtime_lock_; | 242 mutable base::Lock runtime_lock_; |
| 310 | 243 |
| 311 // The permission's which are currently active on the extension during | 244 // The permission's which are currently active on the extension during |
| 312 // runtime. | 245 // runtime. |
| 313 // Unsafe indicates that we must lock anytime this is directly accessed. | 246 // Unsafe indicates that we must lock anytime this is directly accessed. |
| 314 // Unless you need to change |active_permissions_unsafe_|, use the (safe) | 247 // Unless you need to change |active_permissions_unsafe_|, use the (safe) |
| 315 // active_permissions() accessor. | 248 // active_permissions() accessor. |
| 316 mutable std::unique_ptr<const PermissionSet> active_permissions_unsafe_; | 249 mutable std::unique_ptr<const PermissionSet> active_permissions_unsafe_; |
| 317 | 250 |
| 318 // The permissions the extension requested, but was not granted due because | 251 // The permissions the extension requested, but was not granted due because |
| 319 // they are too powerful. This includes things like all_hosts. | 252 // they are too powerful. This includes things like all_hosts. |
| 320 // Unsafe indicates that we must lock anytime this is directly accessed. | 253 // Unsafe indicates that we must lock anytime this is directly accessed. |
| 321 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) | 254 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) |
| 322 // withheld_permissions() accessor. | 255 // withheld_permissions() accessor. |
| 323 mutable std::unique_ptr<const PermissionSet> withheld_permissions_unsafe_; | 256 mutable std::unique_ptr<const PermissionSet> withheld_permissions_unsafe_; |
| 324 | 257 |
| 325 // The list of hosts an extension may not interact with by policy. | |
| 326 // Unless you need to change |policy_blocked_hosts_unsafe_|, use the (safe) | |
| 327 // policy_blocked_hosts() accessor. | |
| 328 mutable URLPatternSet policy_blocked_hosts_unsafe_; | |
| 329 | |
| 330 // The exclusive list of hosts an extension may interact with by policy. | |
| 331 // Unless you need to change |policy_allowed_hosts_unsafe_|, use the (safe) | |
| 332 // policy_allowed_hosts() accessor. | |
| 333 mutable URLPatternSet policy_allowed_hosts_unsafe_; | |
| 334 | |
| 335 // If the ExtensionSettings policy is not being used, or no per-extension | |
| 336 // exception to the default policy was declared for this extension. | |
| 337 mutable bool uses_default_policy_host_restrictions = true; | |
| 338 | |
| 339 mutable TabPermissionsMap tab_specific_permissions_; | 258 mutable TabPermissionsMap tab_specific_permissions_; |
| 340 | 259 |
| 341 mutable std::unique_ptr<base::ThreadChecker> thread_checker_; | 260 mutable std::unique_ptr<base::ThreadChecker> thread_checker_; |
| 342 | 261 |
| 343 DISALLOW_COPY_AND_ASSIGN(PermissionsData); | 262 DISALLOW_COPY_AND_ASSIGN(PermissionsData); |
| 344 }; | 263 }; |
| 345 | 264 |
| 346 } // namespace extensions | 265 } // namespace extensions |
| 347 | 266 |
| 348 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ | 267 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ |
| OLD | NEW |