Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(813)

Side by Side Diff: extensions/common/permissions/permissions_data.h

Issue 1349613003: [Extensions] Un-refcount PermissionSet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
15 #include "extensions/common/manifest.h" 16 #include "extensions/common/manifest.h"
16 #include "extensions/common/permissions/api_permission.h" 17 #include "extensions/common/permissions/api_permission.h"
17 #include "extensions/common/permissions/permission_message.h" 18 #include "extensions/common/permissions/permission_message.h"
18 #include "extensions/common/permissions/permission_set.h" 19 #include "extensions/common/permissions/permission_set.h"
19 20
20 class GURL; 21 class GURL;
21 22
23 namespace base {
24 class ThreadChecker;
25 }
26
22 namespace extensions { 27 namespace extensions {
23 class Extension; 28 class Extension;
24 class URLPatternSet; 29 class URLPatternSet;
25 30
26 // A container for the permissions state of an extension, including active, 31 // A container for the permissions state of an extension, including active,
27 // withheld, and tab-specific permissions. 32 // withheld, and tab-specific permissions.
33 // Thread-Safety: Since this is an object on the Extension object, *some* thread
34 // safety is provided. All utility functions for checking if a permission is
35 // present or an operation is allowed are thread-safe. However, permissions can
36 // only be set (or updated) on the thread on which this object was created.
not at google - send to devlin 2015/09/22 21:51:26 Why is this? Just for sanity checking? Because you
Devlin 2015/09/22 22:08:45 So this is (one of the) fun parts. We lock, but w
not at google - send to devlin 2015/09/22 22:32:58 I see. So to say it in a more general way, to mak
37 // Permissions may be accessed synchronously on that same thread.
38 // Accessing on an improper thread will CHECK().
not at google - send to devlin 2015/09/22 21:51:26 Brave! Honestly I'd make this (all instances) a D
Devlin 2015/09/23 17:09:00 Done.
28 class PermissionsData { 39 class PermissionsData {
29 public: 40 public:
30 // The possible types of access for a given frame. 41 // The possible types of access for a given frame.
31 enum AccessType { 42 enum AccessType {
32 ACCESS_DENIED, // The extension is not allowed to access the given page. 43 ACCESS_DENIED, // The extension is not allowed to access the given page.
33 ACCESS_ALLOWED, // The extension is allowed to access the given page. 44 ACCESS_ALLOWED, // The extension is allowed to access the given page.
34 ACCESS_WITHHELD // The browser must determine if the extension can access 45 ACCESS_WITHHELD // The browser must determine if the extension can access
35 // the given page. 46 // the given page.
36 }; 47 };
37 48
38 using TabPermissionsMap = std::map<int, scoped_refptr<const PermissionSet>>; 49 using TabPermissionsMap =
50 base::ScopedPtrMap<int, scoped_ptr<const PermissionSet>>;
39 51
40 // Delegate class to allow different contexts (e.g. browser vs renderer) to 52 // Delegate class to allow different contexts (e.g. browser vs renderer) to
41 // have control over policy decisions. 53 // have control over policy decisions.
42 class PolicyDelegate { 54 class PolicyDelegate {
43 public: 55 public:
44 virtual ~PolicyDelegate() {} 56 virtual ~PolicyDelegate() {}
45 57
46 // Returns false if script access should be blocked on this page. 58 // Returns false if script access should be blocked on this page.
47 // Otherwise, default policy should decide. 59 // Otherwise, default policy should decide.
48 virtual bool CanExecuteScriptOnPage(const Extension* extension, 60 virtual bool CanExecuteScriptOnPage(const Extension* extension,
(...skipping 24 matching lines...) Expand all
73 // with the given |extension_id|. 85 // with the given |extension_id|.
74 static bool ShouldSkipPermissionWarnings(const std::string& extension_id); 86 static bool ShouldSkipPermissionWarnings(const std::string& extension_id);
75 87
76 // Returns true if the given |url| is restricted for the given |extension|, 88 // Returns true if the given |url| is restricted for the given |extension|,
77 // as is commonly the case for chrome:// urls. 89 // as is commonly the case for chrome:// urls.
78 // NOTE: You probably want to use CanAccessPage(). 90 // NOTE: You probably want to use CanAccessPage().
79 static bool IsRestrictedUrl(const GURL& document_url, 91 static bool IsRestrictedUrl(const GURL& document_url,
80 const Extension* extension, 92 const Extension* extension,
81 std::string* error); 93 std::string* error);
82 94
95 // Locks the permissions data to the current thread. We don't do this on
96 // construction, since extensions are initialized across multiple threads.
97 void LockToThread() const;
not at google - send to devlin 2015/09/22 21:51:27 "Lock" is an unfortunate term to be using, since i
Devlin 2015/09/23 17:09:00 Done.
98
83 // Sets the runtime permissions of the given |extension| to |active| and 99 // Sets the runtime permissions of the given |extension| to |active| and
84 // |withheld|. 100 // |withheld|.
85 void SetPermissions(const scoped_refptr<const PermissionSet>& active, 101 void SetPermissions(scoped_ptr<const PermissionSet> active,
86 const scoped_refptr<const PermissionSet>& withheld) const; 102 scoped_ptr<const PermissionSet> withheld) const;
103
104 // Sets the active permissions, leaving withheld the same.
105 void SetActivePermissions(scoped_ptr<const PermissionSet> active) const;
87 106
88 // Updates the tab-specific permissions of |tab_id| to include those from 107 // Updates the tab-specific permissions of |tab_id| to include those from
89 // |permissions|. 108 // |permissions|.
90 void UpdateTabSpecificPermissions( 109 void UpdateTabSpecificPermissions(int tab_id,
91 int tab_id, 110 const PermissionSet& permissions) const;
92 scoped_refptr<const PermissionSet> permissions) const;
93 111
94 // Clears the tab-specific permissions of |tab_id|. 112 // Clears the tab-specific permissions of |tab_id|.
95 void ClearTabSpecificPermissions(int tab_id) const; 113 void ClearTabSpecificPermissions(int tab_id) const;
96 114
97 // Returns true if the |extension| has the given |permission|. Prefer 115 // Returns true if the |extension| has the given |permission|. Prefer
98 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an 116 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
99 // api that requires a permission they didn't know about, e.g. open web apis. 117 // 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 118 // Note this does not include APIs with no corresponding permission, like
101 // "runtime" or "browserAction". 119 // "runtime" or "browserAction".
102 // TODO(mpcomplete): drop the "API" from these names, it's confusing. 120 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 int tab_id, 193 int tab_id,
176 int process_id, 194 int process_id,
177 std::string* error) const; 195 std::string* error) const;
178 196
179 // Returns true if extension is allowed to obtain the contents of a page as 197 // 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 198 // an image. Since a page may contain sensitive information, this is
181 // restricted to the extension's host permissions as well as the extension 199 // restricted to the extension's host permissions as well as the extension
182 // page itself. 200 // page itself.
183 bool CanCaptureVisiblePage(int tab_id, std::string* error) const; 201 bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
184 202
185 // Returns the tab permissions map. 203 const TabPermissionsMap& tab_specific_permissions() const {
186 TabPermissionsMap CopyTabSpecificPermissionsMap() const; 204 CHECK(CalledOnValidThread());
187 205 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 } 206 }
193 207
194 scoped_refptr<const PermissionSet> withheld_permissions() const { 208 const PermissionSet* active_permissions() const {
not at google - send to devlin 2015/09/22 21:51:27 (noting place which should be a reference)
195 // We lock so that we can't also be setting the permissions while returning. 209 CHECK(CalledOnValidThread());
196 base::AutoLock auto_lock(runtime_lock_); 210 return active_permissions_unsafe_.get();
197 return withheld_permissions_unsafe_; 211 }
212
213 const PermissionSet* withheld_permissions() const {
214 CHECK(CalledOnValidThread());
215 return withheld_permissions_unsafe_.get();
198 } 216 }
199 217
200 #if defined(UNIT_TEST) 218 #if defined(UNIT_TEST)
201 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting( 219 const PermissionSet* GetTabSpecificPermissionsForTesting(int tab_id) const {
202 int tab_id) const { 220 base::AutoLock auto_lock(runtime_lock_);
203 return GetTabSpecificPermissions(tab_id); 221 return GetTabSpecificPermissions(tab_id);
204 } 222 }
205 #endif 223 #endif
206 224
207 private: 225 private:
226 // Returns true if this is called on a valid thread.
227 bool CalledOnValidThread() const;
228
208 // Gets the tab-specific host permissions of |tab_id|, or NULL if there 229 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
209 // aren't any. 230 // aren't any.
not at google - send to devlin 2015/09/22 21:51:27 Mention "must be called with |runtime_lock_| acqui
Devlin 2015/09/23 17:09:00 Done.
210 scoped_refptr<const PermissionSet> GetTabSpecificPermissions( 231 const PermissionSet* GetTabSpecificPermissions(int tab_id) const;
211 int tab_id) const;
212 232
213 // Returns true if the |extension| has tab-specific permission to operate on 233 // Returns true if the |extension| has tab-specific permission to operate on
214 // the tab specified by |tab_id| with the given |url|. 234 // 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 235 // 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. 236 // the given tab, only that it does not have tab-specific permission to do so.
217 bool HasTabSpecificPermissionToExecuteScript(int tab_id, 237 bool HasTabSpecificPermissionToExecuteScript(int tab_id,
218 const GURL& url) const; 238 const GURL& url) const;
219 239
220 // Returns whether or not the extension is permitted to run on the given page, 240 // 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 241 // checking against |permitted_url_patterns| in addition to blocking special
(...skipping 12 matching lines...) Expand all
234 // The associated extension's manifest type. 254 // The associated extension's manifest type.
235 Manifest::Type manifest_type_; 255 Manifest::Type manifest_type_;
236 256
237 mutable base::Lock runtime_lock_; 257 mutable base::Lock runtime_lock_;
238 258
239 // The permission's which are currently active on the extension during 259 // The permission's which are currently active on the extension during
240 // runtime. 260 // runtime.
241 // Unsafe indicates that we must lock anytime this is directly accessed. 261 // Unsafe indicates that we must lock anytime this is directly accessed.
242 // Unless you need to change |active_permissions_unsafe_|, use the (safe) 262 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
243 // active_permissions() accessor. 263 // active_permissions() accessor.
244 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_; 264 mutable scoped_ptr<const PermissionSet> active_permissions_unsafe_;
245 265
246 // The permissions the extension requested, but was not granted due because 266 // The permissions the extension requested, but was not granted due because
247 // they are too powerful. This includes things like all_hosts. 267 // they are too powerful. This includes things like all_hosts.
248 // Unsafe indicates that we must lock anytime this is directly accessed. 268 // Unsafe indicates that we must lock anytime this is directly accessed.
249 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) 269 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe)
250 // withheld_permissions() accessor. 270 // withheld_permissions() accessor.
251 mutable scoped_refptr<const PermissionSet> withheld_permissions_unsafe_; 271 mutable scoped_ptr<const PermissionSet> withheld_permissions_unsafe_;
252 272
253 mutable TabPermissionsMap tab_specific_permissions_; 273 mutable TabPermissionsMap tab_specific_permissions_;
254 274
275 mutable scoped_ptr<base::ThreadChecker> thread_checker_;
276
255 DISALLOW_COPY_AND_ASSIGN(PermissionsData); 277 DISALLOW_COPY_AND_ASSIGN(PermissionsData);
256 }; 278 };
257 279
258 } // namespace extensions 280 } // namespace extensions
259 281
260 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ 282 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698