| 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 <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/containers/scoped_ptr_map.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread_checker.h" |
| 15 #include "extensions/common/manifest.h" | 17 #include "extensions/common/manifest.h" |
| 16 #include "extensions/common/permissions/api_permission.h" | 18 #include "extensions/common/permissions/api_permission.h" |
| 17 #include "extensions/common/permissions/permission_message.h" | 19 #include "extensions/common/permissions/permission_message.h" |
| 18 #include "extensions/common/permissions/permission_set.h" | 20 #include "extensions/common/permissions/permission_set.h" |
| 19 | 21 |
| 20 class GURL; | 22 class GURL; |
| 21 | 23 |
| 22 namespace extensions { | 24 namespace extensions { |
| 23 class Extension; | 25 class Extension; |
| 24 class URLPatternSet; | 26 class URLPatternSet; |
| 25 | 27 |
| 26 // A container for the permissions state of an extension, including active, | 28 // A container for the permissions state of an extension, including active, |
| 27 // withheld, and tab-specific permissions. | 29 // withheld, and tab-specific permissions. |
| 30 // Thread-Safety: Since this is an object on the Extension object, *some* thread |
| 31 // safety is provided. All utility functions for checking if a permission is |
| 32 // present or an operation is allowed are thread-safe. However, permissions can |
| 33 // only be set (or updated) on the thread to which this object is bound. |
| 34 // Permissions may be accessed synchronously on that same thread. |
| 35 // Accessing on an improper thread will DCHECK(). |
| 36 // This is necessary to prevent a scenario in which one thread will access |
| 37 // permissions while another thread changes them. |
| 28 class PermissionsData { | 38 class PermissionsData { |
| 29 public: | 39 public: |
| 30 // The possible types of access for a given frame. | 40 // The possible types of access for a given frame. |
| 31 enum AccessType { | 41 enum AccessType { |
| 32 ACCESS_DENIED, // The extension is not allowed to access the given page. | 42 ACCESS_DENIED, // The extension is not allowed to access the given page. |
| 33 ACCESS_ALLOWED, // The extension is allowed to access the given page. | 43 ACCESS_ALLOWED, // The extension is allowed to access the given page. |
| 34 ACCESS_WITHHELD // The browser must determine if the extension can access | 44 ACCESS_WITHHELD // The browser must determine if the extension can access |
| 35 // the given page. | 45 // the given page. |
| 36 }; | 46 }; |
| 37 | 47 |
| 38 using TabPermissionsMap = std::map<int, scoped_refptr<const PermissionSet>>; | 48 using TabPermissionsMap = |
| 49 base::ScopedPtrMap<int, scoped_ptr<const PermissionSet>>; |
| 39 | 50 |
| 40 // Delegate class to allow different contexts (e.g. browser vs renderer) to | 51 // Delegate class to allow different contexts (e.g. browser vs renderer) to |
| 41 // have control over policy decisions. | 52 // have control over policy decisions. |
| 42 class PolicyDelegate { | 53 class PolicyDelegate { |
| 43 public: | 54 public: |
| 44 virtual ~PolicyDelegate() {} | 55 virtual ~PolicyDelegate() {} |
| 45 | 56 |
| 46 // Returns false if script access should be blocked on this page. | 57 // Returns false if script access should be blocked on this page. |
| 47 // Otherwise, default policy should decide. | 58 // Otherwise, default policy should decide. |
| 48 virtual bool CanExecuteScriptOnPage(const Extension* extension, | 59 virtual bool CanExecuteScriptOnPage(const Extension* extension, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 73 // with the given |extension_id|. | 84 // with the given |extension_id|. |
| 74 static bool ShouldSkipPermissionWarnings(const std::string& extension_id); | 85 static bool ShouldSkipPermissionWarnings(const std::string& extension_id); |
| 75 | 86 |
| 76 // Returns true if the given |url| is restricted for the given |extension|, | 87 // Returns true if the given |url| is restricted for the given |extension|, |
| 77 // as is commonly the case for chrome:// urls. | 88 // as is commonly the case for chrome:// urls. |
| 78 // NOTE: You probably want to use CanAccessPage(). | 89 // NOTE: You probably want to use CanAccessPage(). |
| 79 static bool IsRestrictedUrl(const GURL& document_url, | 90 static bool IsRestrictedUrl(const GURL& document_url, |
| 80 const Extension* extension, | 91 const Extension* extension, |
| 81 std::string* error); | 92 std::string* error); |
| 82 | 93 |
| 94 // Locks the permissions data to the current thread. We don't do this on |
| 95 // construction, since extensions are initialized across multiple threads. |
| 96 void BindToCurrentThread() const; |
| 97 |
| 83 // Sets the runtime permissions of the given |extension| to |active| and | 98 // Sets the runtime permissions of the given |extension| to |active| and |
| 84 // |withheld|. | 99 // |withheld|. |
| 85 void SetPermissions(const scoped_refptr<const PermissionSet>& active, | 100 void SetPermissions(scoped_ptr<const PermissionSet> active, |
| 86 const scoped_refptr<const PermissionSet>& withheld) const; | 101 scoped_ptr<const PermissionSet> withheld) const; |
| 102 |
| 103 // Sets the active permissions, leaving withheld the same. |
| 104 void SetActivePermissions(scoped_ptr<const PermissionSet> active) const; |
| 87 | 105 |
| 88 // Updates the tab-specific permissions of |tab_id| to include those from | 106 // Updates the tab-specific permissions of |tab_id| to include those from |
| 89 // |permissions|. | 107 // |permissions|. |
| 90 void UpdateTabSpecificPermissions( | 108 void UpdateTabSpecificPermissions(int tab_id, |
| 91 int tab_id, | 109 const PermissionSet& permissions) const; |
| 92 scoped_refptr<const PermissionSet> permissions) const; | |
| 93 | 110 |
| 94 // Clears the tab-specific permissions of |tab_id|. | 111 // Clears the tab-specific permissions of |tab_id|. |
| 95 void ClearTabSpecificPermissions(int tab_id) const; | 112 void ClearTabSpecificPermissions(int tab_id) const; |
| 96 | 113 |
| 97 // Returns true if the |extension| has the given |permission|. Prefer | 114 // Returns true if the |extension| has the given |permission|. Prefer |
| 98 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an | 115 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an |
| 99 // api that requires a permission they didn't know about, e.g. open web apis. | 116 // api that requires a permission they didn't know about, e.g. open web apis. |
| 100 // Note this does not include APIs with no corresponding permission, like | 117 // Note this does not include APIs with no corresponding permission, like |
| 101 // "runtime" or "browserAction". | 118 // "runtime" or "browserAction". |
| 102 // TODO(mpcomplete): drop the "API" from these names, it's confusing. | 119 // TODO(mpcomplete): drop the "API" from these names, it's confusing. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 int tab_id, | 192 int tab_id, |
| 176 int process_id, | 193 int process_id, |
| 177 std::string* error) const; | 194 std::string* error) const; |
| 178 | 195 |
| 179 // Returns true if extension is allowed to obtain the contents of a page as | 196 // Returns true if extension is allowed to obtain the contents of a page as |
| 180 // an image. Since a page may contain sensitive information, this is | 197 // an image. Since a page may contain sensitive information, this is |
| 181 // restricted to the extension's host permissions as well as the extension | 198 // restricted to the extension's host permissions as well as the extension |
| 182 // page itself. | 199 // page itself. |
| 183 bool CanCaptureVisiblePage(int tab_id, std::string* error) const; | 200 bool CanCaptureVisiblePage(int tab_id, std::string* error) const; |
| 184 | 201 |
| 185 // Returns the tab permissions map. | 202 const TabPermissionsMap& tab_specific_permissions() const { |
| 186 TabPermissionsMap CopyTabSpecificPermissionsMap() const; | 203 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 187 | 204 return tab_specific_permissions_; |
| 188 scoped_refptr<const PermissionSet> active_permissions() const { | |
| 189 // We lock so that we can't also be setting the permissions while returning. | |
| 190 base::AutoLock auto_lock(runtime_lock_); | |
| 191 return active_permissions_unsafe_; | |
| 192 } | 205 } |
| 193 | 206 |
| 194 scoped_refptr<const PermissionSet> withheld_permissions() const { | 207 const PermissionSet* active_permissions() const { |
| 195 // We lock so that we can't also be setting the permissions while returning. | 208 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 196 base::AutoLock auto_lock(runtime_lock_); | 209 return active_permissions_unsafe_.get(); |
| 197 return withheld_permissions_unsafe_; | 210 } |
| 211 |
| 212 const PermissionSet* withheld_permissions() const { |
| 213 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 214 return withheld_permissions_unsafe_.get(); |
| 198 } | 215 } |
| 199 | 216 |
| 200 #if defined(UNIT_TEST) | 217 #if defined(UNIT_TEST) |
| 201 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting( | 218 const PermissionSet* GetTabSpecificPermissionsForTesting(int tab_id) const { |
| 202 int tab_id) const { | 219 base::AutoLock auto_lock(runtime_lock_); |
| 203 return GetTabSpecificPermissions(tab_id); | 220 return GetTabSpecificPermissions(tab_id); |
| 204 } | 221 } |
| 205 #endif | 222 #endif |
| 206 | 223 |
| 207 private: | 224 private: |
| 208 // Gets the tab-specific host permissions of |tab_id|, or NULL if there | 225 // Gets the tab-specific host permissions of |tab_id|, or NULL if there |
| 209 // aren't any. | 226 // aren't any. |
| 210 scoped_refptr<const PermissionSet> GetTabSpecificPermissions( | 227 // Must be called with |runtime_lock_| acquired. |
| 211 int tab_id) const; | 228 const PermissionSet* GetTabSpecificPermissions(int tab_id) const; |
| 212 | 229 |
| 213 // Returns true if the |extension| has tab-specific permission to operate on | 230 // Returns true if the |extension| has tab-specific permission to operate on |
| 214 // the tab specified by |tab_id| with the given |url|. | 231 // the tab specified by |tab_id| with the given |url|. |
| 215 // Note that if this returns false, it doesn't mean the extension can't run on | 232 // Note that if this returns false, it doesn't mean the extension can't run on |
| 216 // the given tab, only that it does not have tab-specific permission to do so. | 233 // the given tab, only that it does not have tab-specific permission to do so. |
| 234 // Must be called with |runtime_lock_| acquired. |
| 217 bool HasTabSpecificPermissionToExecuteScript(int tab_id, | 235 bool HasTabSpecificPermissionToExecuteScript(int tab_id, |
| 218 const GURL& url) const; | 236 const GURL& url) const; |
| 219 | 237 |
| 220 // Returns whether or not the extension is permitted to run on the given page, | 238 // Returns whether or not the extension is permitted to run on the given page, |
| 221 // checking against |permitted_url_patterns| in addition to blocking special | 239 // checking against |permitted_url_patterns| in addition to blocking special |
| 222 // sites (like the webstore or chrome:// urls). | 240 // sites (like the webstore or chrome:// urls). |
| 241 // Must be called with |runtime_lock_| acquired. |
| 223 AccessType CanRunOnPage(const Extension* extension, | 242 AccessType CanRunOnPage(const Extension* extension, |
| 224 const GURL& document_url, | 243 const GURL& document_url, |
| 225 int tab_id, | 244 int tab_id, |
| 226 int process_id, | 245 int process_id, |
| 227 const URLPatternSet& permitted_url_patterns, | 246 const URLPatternSet& permitted_url_patterns, |
| 228 const URLPatternSet& withheld_url_patterns, | 247 const URLPatternSet& withheld_url_patterns, |
| 229 std::string* error) const; | 248 std::string* error) const; |
| 230 | 249 |
| 231 // The associated extension's id. | 250 // The associated extension's id. |
| 232 std::string extension_id_; | 251 std::string extension_id_; |
| 233 | 252 |
| 234 // The associated extension's manifest type. | 253 // The associated extension's manifest type. |
| 235 Manifest::Type manifest_type_; | 254 Manifest::Type manifest_type_; |
| 236 | 255 |
| 237 mutable base::Lock runtime_lock_; | 256 mutable base::Lock runtime_lock_; |
| 238 | 257 |
| 239 // The permission's which are currently active on the extension during | 258 // The permission's which are currently active on the extension during |
| 240 // runtime. | 259 // runtime. |
| 241 // Unsafe indicates that we must lock anytime this is directly accessed. | 260 // Unsafe indicates that we must lock anytime this is directly accessed. |
| 242 // Unless you need to change |active_permissions_unsafe_|, use the (safe) | 261 // Unless you need to change |active_permissions_unsafe_|, use the (safe) |
| 243 // active_permissions() accessor. | 262 // active_permissions() accessor. |
| 244 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_; | 263 mutable scoped_ptr<const PermissionSet> active_permissions_unsafe_; |
| 245 | 264 |
| 246 // The permissions the extension requested, but was not granted due because | 265 // The permissions the extension requested, but was not granted due because |
| 247 // they are too powerful. This includes things like all_hosts. | 266 // they are too powerful. This includes things like all_hosts. |
| 248 // Unsafe indicates that we must lock anytime this is directly accessed. | 267 // Unsafe indicates that we must lock anytime this is directly accessed. |
| 249 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) | 268 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) |
| 250 // withheld_permissions() accessor. | 269 // withheld_permissions() accessor. |
| 251 mutable scoped_refptr<const PermissionSet> withheld_permissions_unsafe_; | 270 mutable scoped_ptr<const PermissionSet> withheld_permissions_unsafe_; |
| 252 | 271 |
| 253 mutable TabPermissionsMap tab_specific_permissions_; | 272 mutable TabPermissionsMap tab_specific_permissions_; |
| 254 | 273 |
| 274 mutable scoped_ptr<base::ThreadChecker> thread_checker_; |
| 275 |
| 255 DISALLOW_COPY_AND_ASSIGN(PermissionsData); | 276 DISALLOW_COPY_AND_ASSIGN(PermissionsData); |
| 256 }; | 277 }; |
| 257 | 278 |
| 258 } // namespace extensions | 279 } // namespace extensions |
| 259 | 280 |
| 260 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ | 281 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ |
| OLD | NEW |