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

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

Issue 309533007: Refactor PermissionsData pt1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Latest master Created 6 years, 6 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 | Annotate | Revision Log
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 <vector> 10 #include <vector>
10 11
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.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 "extensions/common/extension.h"
17 #include "extensions/common/manifest.h"
15 #include "extensions/common/permissions/api_permission.h" 18 #include "extensions/common/permissions/api_permission.h"
16 #include "extensions/common/permissions/permission_message.h" 19 #include "extensions/common/permissions/permission_message.h"
20 #include "extensions/common/permissions/permission_set.h"
17 21
18 class GURL; 22 class GURL;
19 23
20 namespace extensions { 24 namespace extensions {
21 25
22 class PermissionSet; 26 class PermissionSet;
23 class APIPermissionSet;
24 class Extension; 27 class Extension;
25 class ManifestPermissionSet;
26 class URLPatternSet; 28 class URLPatternSet;
27 class UserScript; 29 class UserScript;
28 30
29 // A container for the permissions data of the extension; also responsible for 31 // A container for the active permissions of an extension.
30 // parsing the "permissions" and "optional_permissions" manifest keys. This 32 // TODO(rdevlin.cronin): For the love of everything good, rename this class to
31 // class also contains the active (runtime) permissions for the extension. 33 // ActivePermissions. We do *not* need PermissionsParser, PermissionSet,
34 // PermissionInfo, and PermissionsData. No one will be able to keep them
35 // straight.
32 class PermissionsData { 36 class PermissionsData {
33 public: 37 public:
34 PermissionsData();
35 ~PermissionsData();
36
37 // Delegate class to allow different contexts (e.g. browser vs renderer) to 38 // Delegate class to allow different contexts (e.g. browser vs renderer) to
38 // have control over policy decisions. 39 // have control over policy decisions.
39 class PolicyDelegate { 40 class PolicyDelegate {
40 public: 41 public:
41 virtual ~PolicyDelegate() {} 42 virtual ~PolicyDelegate() {}
42 43
43 // Returns false if script access should be blocked on this page. 44 // Returns false if script access should be blocked on this page.
44 // Otherwise, default policy should decide. 45 // Otherwise, default policy should decide.
45 virtual bool CanExecuteScriptOnPage(const Extension* extension, 46 virtual bool CanExecuteScriptOnPage(const Extension* extension,
46 const GURL& document_url, 47 const GURL& document_url,
47 const GURL& top_document_url, 48 const GURL& top_document_url,
48 int tab_id, 49 int tab_id,
49 const UserScript* script, 50 const UserScript* script,
50 int process_id, 51 int process_id,
51 std::string* error) = 0; 52 std::string* error) = 0;
52 }; 53 };
53 54
54 static void SetPolicyDelegate(PolicyDelegate* delegate); 55 static void SetPolicyDelegate(PolicyDelegate* delegate);
55 56
56 // Return the optional or required permission set for the given |extension|. 57 PermissionsData(const Extension* extension);
57 static const PermissionSet* GetOptionalPermissions( 58 virtual ~PermissionsData();
58 const Extension* extension);
59 static const PermissionSet* GetRequiredPermissions(
60 const Extension* extension);
61 59
62 // Return the temporary API permission set which is used during extension 60 // Return the PermissionsData associated with the given |extension|.
63 // initialization. Once initialization completes, this is NULL. 61 static const PermissionsData* ForExtension(const Extension* extension);
64 static const APIPermissionSet* GetInitialAPIPermissions(
65 const Extension* extension);
66 static APIPermissionSet* GetInitialAPIPermissions(Extension* extension);
67 62
68 // Set the scriptable hosts for the given |extension| during initialization. 63 // Returns true if the |extension| can silently increase its permission level.
69 static void SetInitialScriptableHosts(Extension* extension, 64 // Users must approve permissions for unpacked and packed extensions in the
70 const URLPatternSet& scriptable_hosts); 65 // following situations:
66 // - when installing or upgrading packed extensions
67 // - when installing unpacked extensions that have NPAPI plugins
68 // - when either type of extension requests optional permissions
69 static bool CanSilentlyIncreasePermissions(const Extension* extension);
71 70
72 // Return the active (runtime) permissions for the given |extension|. 71 // Returns true if the extension is a COMPONENT extension or is on the
73 static scoped_refptr<const PermissionSet> GetActivePermissions( 72 // whitelist of extensions that can script all pages.
74 const Extension* extension); 73 static bool CanExecuteScriptEverywhere(const Extension* extension);
74
75 // Sets the runtime permissions of the given |extension| to |permissions|. 75 // Sets the runtime permissions of the given |extension| to |permissions|.
76 static void SetActivePermissions(const Extension* extension, 76 void SetActivePermissions(const PermissionSet* active) const;
77 const PermissionSet* active);
78 77
79 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
80 // aren't any.
81 static scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
82 const Extension* extension,
83 int tab_id);
84 // Updates the tab-specific permissions of |tab_id| to include those from 78 // Updates the tab-specific permissions of |tab_id| to include those from
85 // |permissions|. 79 // |permissions|.
86 static void UpdateTabSpecificPermissions( 80 void UpdateTabSpecificPermissions(
87 const Extension* extension,
88 int tab_id, 81 int tab_id,
89 scoped_refptr<const PermissionSet> permissions); 82 scoped_refptr<const PermissionSet> permissions) const;
83
90 // Clears the tab-specific permissions of |tab_id|. 84 // Clears the tab-specific permissions of |tab_id|.
91 static void ClearTabSpecificPermissions(const Extension* extension, 85 void ClearTabSpecificPermissions(int tab_id) const;
92 int tab_id);
93 86
94 // Returns true if the |extension| has the given |permission|. Prefer 87 // Returns true if the |extension| has the given |permission|. Prefer
95 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an 88 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
96 // api that requires a permission they didn't know about, e.g. open web apis. 89 // api that requires a permission they didn't know about, e.g. open web apis.
97 // Note this does not include APIs with no corresponding permission, like 90 // Note this does not include APIs with no corresponding permission, like
98 // "runtime" or "browserAction". 91 // "runtime" or "browserAction".
99 // TODO(mpcomplete): drop the "API" from these names, it's confusing. 92 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
100 static bool HasAPIPermission(const Extension* extension, 93 bool HasAPIPermission(APIPermission::ID permission) const;
101 APIPermission::ID permission); 94 bool HasAPIPermission(const std::string& permission_name) const;
102 static bool HasAPIPermission(const Extension* extension, 95 bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
103 const std::string& permission_name); 96 bool CheckAPIPermissionWithParam(
104 static bool HasAPIPermissionForTab(const Extension* extension, 97 APIPermission::ID permission,
105 int tab_id, 98 const APIPermission::CheckParam* param) const;
106 APIPermission::ID permission);
107 99
108 static bool CheckAPIPermissionWithParam( 100 // TODO(rdevlin.cronin): GetEffectiveHostPermissions(), HasHostPermission(),
109 const Extension* extension, 101 // and HasEffectiveAccessToAllHosts() are just forwards for the active
110 APIPermission::ID permission, 102 // permissions. We should either get rid of these, and have callers use
111 const APIPermission::CheckParam* param); 103 // active_permissions(), or should get rid of active_permissions(), and make
104 // callers use PermissionsData for everything. We should not do both.
112 105
113 static const URLPatternSet& GetEffectiveHostPermissions( 106 // Returns the effective hosts associated with the active permissions.
114 const Extension* extension); 107 const URLPatternSet& GetEffectiveHostPermissions() const;
115 108
116 // Returns true if the |extension| can silently increase its permission level. 109 // Whether the extension has access to the given |url|.
117 // Users must approve permissions for unpacked and packed extensions in the 110 bool HasHostPermission(const GURL& url) const;
118 // following situations:
119 // - when installing or upgrading packed extensions
120 // - when installing unpacked extensions that have NPAPI plugins
121 // - when either type of extension requests optional permissions
122 static bool CanSilentlyIncreasePermissions(const Extension* extension);
123 111
124 // Returns true if the extension does not require permission warnings 112 // Whether the extension has effective access to all hosts. This is true if
125 // to be displayed at install time.
126 static bool ShouldSkipPermissionWarnings(const Extension* extension);
127
128 // Whether the |extension| has access to the given |url|.
129 static bool HasHostPermission(const Extension* extension, const GURL& url);
130
131 // Whether the |extension| has effective access to all hosts. This is true if
132 // there is a content script that matches all hosts, if there is a host 113 // there is a content script that matches all hosts, if there is a host
133 // permission grants access to all hosts (like <all_urls>) or an api 114 // permission grants access to all hosts (like <all_urls>) or an api
134 // permission that effectively grants access to all hosts (e.g. proxy, 115 // permission that effectively grants access to all hosts (e.g. proxy,
135 // network, etc.) 116 // network, etc.)
136 static bool HasEffectiveAccessToAllHosts(const Extension* extension); 117 bool HasEffectiveAccessToAllHosts() const;
137 118
138 // Returns the full list of permission messages that the given |extension| 119 // Returns the full list of permission messages that should display at
139 // should display at install time. 120 // install time.
140 static PermissionMessages GetPermissionMessages(const Extension* extension); 121 PermissionMessages GetPermissionMessages() const;
141 // Returns the full list of permission messages that the given |extension|
142 // should display at install time. The messages are returned as strings
143 // for convenience.
144 static std::vector<base::string16> GetPermissionMessageStrings(
145 const Extension* extension);
146 122
147 // Returns the full list of permission details for messages that the given 123 // Returns the full list of permission messages that should display at install
148 // |extension| should display at install time. The messages are returned as 124 // time as strings.
149 // strings for convenience. 125 std::vector<base::string16> GetPermissionMessageStrings() const;
150 static std::vector<base::string16> GetPermissionMessageDetailsStrings( 126
151 const Extension* extension); 127 // Returns the full list of permission details for messages that should
128 // display at install time as strings.
129 std::vector<base::string16> GetPermissionMessageDetailsStrings() const;
152 130
153 // Returns true if the given |extension| can execute script on a page. If a 131 // Returns true if the given |extension| can execute script on a page. If a
154 // UserScript object is passed, permission to run that specific script is 132 // UserScript object is passed, permission to run that specific script is
155 // checked (using its matches list). Otherwise, permission to execute script 133 // checked (using its matches list). Otherwise, permission to execute script
156 // programmatically is checked (using the extension's host permission). 134 // programmatically is checked (using the extension's host permission).
157 // 135 //
158 // This method is also aware of certain special pages that extensions are 136 // This method is also aware of certain special pages that extensions are
159 // usually not allowed to run script on. 137 // usually not allowed to run script on.
160 static bool CanExecuteScriptOnPage(const Extension* extension, 138 bool CanExecuteScriptOnPage(const Extension* extension,
161 const GURL& document_url, 139 const GURL& document_url,
162 const GURL& top_document_url, 140 const GURL& top_document_url,
163 int tab_id, 141 int tab_id,
164 const UserScript* script, 142 const UserScript* script,
165 int process_id, 143 int process_id,
166 std::string* error); 144 std::string* error) const;
167 145
168 // Returns true if the given |extension| is a COMPONENT extension, or if it is 146 // Returns true if extension is allowed to obtain the contents of a page as
169 // on the whitelist of extensions that can script all pages. 147 // an image. Since a page may contain sensitive information, this is
170 static bool CanExecuteScriptEverywhere(const Extension* extension); 148 // restricted to the extension's host permissions as well as the extension
171 149 // page itself.
172 // Returns true if the |extension| is allowed to obtain the contents of a 150 bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
173 // page as an image. Since a page may contain sensitive information, this
174 // is restricted to the extension's host permissions as well as the
175 // extension page itself.
176 static bool CanCaptureVisiblePage(const Extension* extension,
177 int tab_id,
178 std::string* error);
179 151
180 // Returns true if the user should be alerted that the |extension| is running 152 // Returns true if the user should be alerted that the |extension| is running
181 // a script. If |tab_id| and |url| are included, this also considers tab- 153 // a script. If |tab_id| and |url| are included, this also considers tab-
182 // specific permissions. 154 // specific permissions.
183 static bool RequiresActionForScriptExecution(const Extension* extension); 155 bool RequiresActionForScriptExecution(const Extension* extension) const;
184 static bool RequiresActionForScriptExecution(const Extension* extension, 156 bool RequiresActionForScriptExecution(const Extension* extension,
185 int tab_id, 157 int tab_id,
186 const GURL& url); 158 const GURL& url) const;
187 159
188 // Parse the permissions of a given extension in the initialization process. 160 scoped_refptr<const PermissionSet> active_permissions() const {
189 bool ParsePermissions(Extension* extension, base::string16* error); 161 base::AutoLock auto_lock(runtime_lock_);
162 return active_permissions_unsafe_;
163 }
190 164
191 // Ensure manifest handlers provide their custom manifest permissions. 165 #if defined(UNIT_TEST)
192 void InitializeManifestPermissions(Extension* extension); 166 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting(
193 167 int tab_id) const {
194 // Finalize permissions after the initialization process completes. 168 return GetTabSpecificPermissions(tab_id);
195 void FinalizePermissions(Extension* extension); 169 }
170 #endif
196 171
197 private: 172 private:
198 // Whether the extension has access to so many hosts that we should treat it
199 // as "all_hosts" for warning purposes.
200 // For example, '*://*.com/*'.
201 static bool ShouldWarnAllHosts(const Extension* extension);
202
203 struct InitialPermissions;
204 typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap; 173 typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap;
205 174
206 // Temporary permissions during the initialization process; NULL after 175 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
207 // initialization completes. 176 // aren't any.
208 scoped_ptr<InitialPermissions> initial_required_permissions_; 177 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
209 scoped_ptr<InitialPermissions> initial_optional_permissions_; 178 int tab_id) const;
210 179
211 // The set of permissions the extension can request at runtime. 180 // Returns true if the |extension| has tab-specific permission to operate on
212 scoped_refptr<const PermissionSet> optional_permission_set_; 181 // the tab specified by |tab_id| with the given |url|.
182 // Note that if this returns false, it doesn't mean the extension can't run on
183 // the given tab, only that it does not have tab-specific permission to do so.
184 bool HasTabSpecificPermissionToExecuteScript(int tab_id,
185 const GURL& url) const;
213 186
214 // The extension's required / default set of permissions. 187 // The associated extension's id.
215 scoped_refptr<const PermissionSet> required_permission_set_; 188 std::string extension_id_;
189
190 // The associated extension's manifest type.
191 Manifest::Type manifest_type_;
216 192
217 mutable base::Lock runtime_lock_; 193 mutable base::Lock runtime_lock_;
218 194
219 // The permission's which are currently active on the extension during 195 // The permission's which are currently active on the extension during
220 // runtime. 196 // runtime.
221 mutable scoped_refptr<const PermissionSet> active_permissions_; 197 // Unsafe indicates that we must lock anytime this is directly accessed.
198 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
199 // active_permissions() accessor.
200 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_;
222 201
223 mutable TabPermissionsMap tab_specific_permissions_; 202 mutable TabPermissionsMap tab_specific_permissions_;
224 203
225 DISALLOW_COPY_AND_ASSIGN(PermissionsData); 204 DISALLOW_COPY_AND_ASSIGN(PermissionsData);
226 }; 205 };
227 206
228 } // namespace extensions 207 } // namespace extensions
229 208
230 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_ 209 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_
OLDNEW
« no previous file with comments | « extensions/common/manifest_handlers/permissions_parser.cc ('k') | extensions/common/permissions/permissions_data.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698