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> |
(...skipping 17 matching lines...) Expand all Loading... |
28 class URLPatternSet; | 28 class URLPatternSet; |
29 class UserScript; | 29 class UserScript; |
30 | 30 |
31 // A container for the active permissions of an extension. | 31 // A container for the active permissions of an extension. |
32 // TODO(rdevlin.cronin): For the love of everything good, rename this class to | 32 // TODO(rdevlin.cronin): For the love of everything good, rename this class to |
33 // ActivePermissions. We do *not* need PermissionsParser, PermissionSet, | 33 // ActivePermissions. We do *not* need PermissionsParser, PermissionSet, |
34 // PermissionInfo, and PermissionsData. No one will be able to keep them | 34 // PermissionInfo, and PermissionsData. No one will be able to keep them |
35 // straight. | 35 // straight. |
36 class PermissionsData { | 36 class PermissionsData { |
37 public: | 37 public: |
| 38 // The possible types of access for a given frame. |
| 39 enum AccessType { |
| 40 ACCESS_DENIED, // The extension is not allowed to access the given page. |
| 41 ACCESS_ALLOWED, // The extension is allowed to access the given page. |
| 42 ACCESS_WITHHELD // The browser must determine if the extension can access |
| 43 // the given page. |
| 44 }; |
| 45 |
38 // Delegate class to allow different contexts (e.g. browser vs renderer) to | 46 // Delegate class to allow different contexts (e.g. browser vs renderer) to |
39 // have control over policy decisions. | 47 // have control over policy decisions. |
40 class PolicyDelegate { | 48 class PolicyDelegate { |
41 public: | 49 public: |
42 virtual ~PolicyDelegate() {} | 50 virtual ~PolicyDelegate() {} |
43 | 51 |
44 // Returns false if script access should be blocked on this page. | 52 // Returns false if script access should be blocked on this page. |
45 // Otherwise, default policy should decide. | 53 // Otherwise, default policy should decide. |
46 virtual bool CanExecuteScriptOnPage(const Extension* extension, | 54 virtual bool CanExecuteScriptOnPage(const Extension* extension, |
47 const GURL& document_url, | 55 const GURL& document_url, |
(...skipping 21 matching lines...) Expand all Loading... |
69 static bool CanExecuteScriptEverywhere(const Extension* extension); | 77 static bool CanExecuteScriptEverywhere(const Extension* extension); |
70 | 78 |
71 // Returns true if the given |url| is restricted for the given |extension|, | 79 // Returns true if the given |url| is restricted for the given |extension|, |
72 // as is commonly the case for chrome:// urls. | 80 // as is commonly the case for chrome:// urls. |
73 // NOTE: You probably want to use CanAccessPage(). | 81 // NOTE: You probably want to use CanAccessPage(). |
74 static bool IsRestrictedUrl(const GURL& document_url, | 82 static bool IsRestrictedUrl(const GURL& document_url, |
75 const GURL& top_frame_url, | 83 const GURL& top_frame_url, |
76 const Extension* extension, | 84 const Extension* extension, |
77 std::string* error); | 85 std::string* error); |
78 | 86 |
79 // Sets the runtime permissions of the given |extension| to |permissions|. | 87 // Sets the runtime permissions of the given |extension| to |active| and |
80 void SetActivePermissions(const PermissionSet* active) const; | 88 // |withheld|. |
| 89 void SetPermissions(const scoped_refptr<const PermissionSet>& active, |
| 90 const scoped_refptr<const PermissionSet>& withheld) const; |
81 | 91 |
82 // Updates the tab-specific permissions of |tab_id| to include those from | 92 // Updates the tab-specific permissions of |tab_id| to include those from |
83 // |permissions|. | 93 // |permissions|. |
84 void UpdateTabSpecificPermissions( | 94 void UpdateTabSpecificPermissions( |
85 int tab_id, | 95 int tab_id, |
86 scoped_refptr<const PermissionSet> permissions) const; | 96 scoped_refptr<const PermissionSet> permissions) const; |
87 | 97 |
88 // Clears the tab-specific permissions of |tab_id|. | 98 // Clears the tab-specific permissions of |tab_id|. |
89 void ClearTabSpecificPermissions(int tab_id) const; | 99 void ClearTabSpecificPermissions(int tab_id) const; |
90 | 100 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 PermissionMessages GetPermissionMessages() const; | 135 PermissionMessages GetPermissionMessages() const; |
126 | 136 |
127 // Returns the full list of permission messages that should display at install | 137 // Returns the full list of permission messages that should display at install |
128 // time as strings. | 138 // time as strings. |
129 std::vector<base::string16> GetPermissionMessageStrings() const; | 139 std::vector<base::string16> GetPermissionMessageStrings() const; |
130 | 140 |
131 // Returns the full list of permission details for messages that should | 141 // Returns the full list of permission details for messages that should |
132 // display at install time as strings. | 142 // display at install time as strings. |
133 std::vector<base::string16> GetPermissionMessageDetailsStrings() const; | 143 std::vector<base::string16> GetPermissionMessageDetailsStrings() const; |
134 | 144 |
| 145 // Returns true if the extension has requested all-hosts permissions (or |
| 146 // something close to it), but has had it withheld. |
| 147 bool HasWithheldImpliedAllHosts() const; |
| 148 |
135 // Returns true if the |extension| has permission to access and interact with | 149 // Returns true if the |extension| has permission to access and interact with |
136 // the specified page, in order to do things like inject scripts or modify | 150 // the specified page, in order to do things like inject scripts or modify |
137 // the content. | 151 // the content. |
138 // If this returns false and |error| is non-NULL, |error| will be popualted | 152 // If this returns false and |error| is non-NULL, |error| will be popualted |
139 // with the reason the extension cannot access the page. | 153 // with the reason the extension cannot access the page. |
140 bool CanAccessPage(const Extension* extension, | 154 bool CanAccessPage(const Extension* extension, |
141 const GURL& document_url, | 155 const GURL& document_url, |
142 const GURL& top_document_url, | 156 const GURL& top_document_url, |
143 int tab_id, | 157 int tab_id, |
144 int process_id, | 158 int process_id, |
145 std::string* error) const; | 159 std::string* error) const; |
| 160 // Like CanAccessPage, but also takes withheld permissions into account. |
| 161 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers |
| 162 // know how to wait for permission. |
| 163 AccessType GetPageAccess(const Extension* extension, |
| 164 const GURL& document_url, |
| 165 const GURL& top_document_url, |
| 166 int tab_id, |
| 167 int process_id, |
| 168 std::string* error) const; |
146 | 169 |
147 // Returns true if the |extension| has permission to inject a content script | 170 // Returns true if the |extension| has permission to inject a content script |
148 // on the page. | 171 // on the page. |
149 // If this returns false and |error| is non-NULL, |error| will be popualted | 172 // If this returns false and |error| is non-NULL, |error| will be popualted |
150 // with the reason the extension cannot script the page. | 173 // with the reason the extension cannot script the page. |
151 // NOTE: You almost certainly want to use CanAccessPage() instead of this | 174 // NOTE: You almost certainly want to use CanAccessPage() instead of this |
152 // method. | 175 // method. |
153 bool CanRunContentScriptOnPage(const Extension* extension, | 176 bool CanRunContentScriptOnPage(const Extension* extension, |
154 const GURL& document_url, | 177 const GURL& document_url, |
155 const GURL& top_document_url, | 178 const GURL& top_document_url, |
156 int tab_id, | 179 int tab_id, |
157 int process_id, | 180 int process_id, |
158 std::string* error) const; | 181 std::string* error) const; |
| 182 // Like CanRunContentScriptOnPage, but also takes withheld permissions into |
| 183 // account. |
| 184 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers |
| 185 // know how to wait for permission. |
| 186 AccessType GetContentScriptAccess(const Extension* extension, |
| 187 const GURL& document_url, |
| 188 const GURL& top_document_url, |
| 189 int tab_id, |
| 190 int process_id, |
| 191 std::string* error) const; |
159 | 192 |
160 // Returns true if extension is allowed to obtain the contents of a page as | 193 // Returns true if extension is allowed to obtain the contents of a page as |
161 // an image. Since a page may contain sensitive information, this is | 194 // an image. Since a page may contain sensitive information, this is |
162 // restricted to the extension's host permissions as well as the extension | 195 // restricted to the extension's host permissions as well as the extension |
163 // page itself. | 196 // page itself. |
164 bool CanCaptureVisiblePage(int tab_id, std::string* error) const; | 197 bool CanCaptureVisiblePage(int tab_id, std::string* error) const; |
165 | 198 |
166 // Returns true if the user should be alerted that the |extension| is running | |
167 // a script. If |tab_id| and |url| are included, this also considers tab- | |
168 // specific permissions. | |
169 bool RequiresActionForScriptExecution(const Extension* extension) const; | |
170 bool RequiresActionForScriptExecution(const Extension* extension, | |
171 int tab_id, | |
172 const GURL& url) const; | |
173 | |
174 scoped_refptr<const PermissionSet> active_permissions() const { | 199 scoped_refptr<const PermissionSet> active_permissions() const { |
175 base::AutoLock auto_lock(runtime_lock_); | 200 base::AutoLock auto_lock(runtime_lock_); |
176 return active_permissions_unsafe_; | 201 return active_permissions_unsafe_; |
177 } | 202 } |
178 | 203 |
| 204 scoped_refptr<const PermissionSet> withheld_permissions() const { |
| 205 base::AutoLock auto_lock(runtime_lock_); |
| 206 return withheld_permissions_unsafe_; |
| 207 } |
| 208 |
179 #if defined(UNIT_TEST) | 209 #if defined(UNIT_TEST) |
180 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting( | 210 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting( |
181 int tab_id) const { | 211 int tab_id) const { |
182 return GetTabSpecificPermissions(tab_id); | 212 return GetTabSpecificPermissions(tab_id); |
183 } | 213 } |
184 #endif | 214 #endif |
185 | 215 |
186 private: | 216 private: |
187 typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap; | 217 typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap; |
188 | 218 |
189 // Gets the tab-specific host permissions of |tab_id|, or NULL if there | 219 // Gets the tab-specific host permissions of |tab_id|, or NULL if there |
190 // aren't any. | 220 // aren't any. |
191 scoped_refptr<const PermissionSet> GetTabSpecificPermissions( | 221 scoped_refptr<const PermissionSet> GetTabSpecificPermissions( |
192 int tab_id) const; | 222 int tab_id) const; |
193 | 223 |
194 // Returns true if the |extension| has tab-specific permission to operate on | 224 // Returns true if the |extension| has tab-specific permission to operate on |
195 // the tab specified by |tab_id| with the given |url|. | 225 // the tab specified by |tab_id| with the given |url|. |
196 // Note that if this returns false, it doesn't mean the extension can't run on | 226 // Note that if this returns false, it doesn't mean the extension can't run on |
197 // the given tab, only that it does not have tab-specific permission to do so. | 227 // the given tab, only that it does not have tab-specific permission to do so. |
198 bool HasTabSpecificPermissionToExecuteScript(int tab_id, | 228 bool HasTabSpecificPermissionToExecuteScript(int tab_id, |
199 const GURL& url) const; | 229 const GURL& url) const; |
200 | 230 |
201 // Returns true if the extension is permitted to run on the given page, | 231 // Returns whether or not the extension is permitted to run on the given page, |
202 // checking against |permitted_url_patterns| in addition to blocking special | 232 // checking against |permitted_url_patterns| in addition to blocking special |
203 // sites (like the webstore or chrome:// urls). | 233 // sites (like the webstore or chrome:// urls). |
204 bool CanRunOnPage(const Extension* extension, | 234 AccessType CanRunOnPage(const Extension* extension, |
205 const GURL& document_url, | 235 const GURL& document_url, |
206 const GURL& top_document_url, | 236 const GURL& top_document_url, |
207 int tab_id, | 237 int tab_id, |
208 int process_id, | 238 int process_id, |
209 const URLPatternSet& permitted_url_patterns, | 239 const URLPatternSet& permitted_url_patterns, |
210 std::string* error) const; | 240 const URLPatternSet& withheld_url_patterns, |
| 241 std::string* error) const; |
211 | 242 |
212 // The associated extension's id. | 243 // The associated extension's id. |
213 std::string extension_id_; | 244 std::string extension_id_; |
214 | 245 |
215 // The associated extension's manifest type. | 246 // The associated extension's manifest type. |
216 Manifest::Type manifest_type_; | 247 Manifest::Type manifest_type_; |
217 | 248 |
218 mutable base::Lock runtime_lock_; | 249 mutable base::Lock runtime_lock_; |
219 | 250 |
220 // The permission's which are currently active on the extension during | 251 // The permission's which are currently active on the extension during |
221 // runtime. | 252 // runtime. |
222 // Unsafe indicates that we must lock anytime this is directly accessed. | 253 // Unsafe indicates that we must lock anytime this is directly accessed. |
223 // Unless you need to change |active_permissions_unsafe_|, use the (safe) | 254 // Unless you need to change |active_permissions_unsafe_|, use the (safe) |
224 // active_permissions() accessor. | 255 // active_permissions() accessor. |
225 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_; | 256 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_; |
226 | 257 |
| 258 // The permissions the extension requested, but was not granted due because |
| 259 // they are too powerful. This includes things like all_hosts. |
| 260 // Unsafe indicates that we must lock anytime this is directly accessed. |
| 261 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe) |
| 262 // withheld_permissions() accessor. |
| 263 mutable scoped_refptr<const PermissionSet> withheld_permissions_unsafe_; |
| 264 |
227 mutable TabPermissionsMap tab_specific_permissions_; | 265 mutable TabPermissionsMap tab_specific_permissions_; |
228 | 266 |
229 DISALLOW_COPY_AND_ASSIGN(PermissionsData); | 267 DISALLOW_COPY_AND_ASSIGN(PermissionsData); |
230 }; | 268 }; |
231 | 269 |
232 } // namespace extensions | 270 } // namespace extensions |
233 | 271 |
234 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ | 272 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ |
OLD | NEW |