Extensions: Remove PermissionMessage (permission_message.h/cc)
[chromium-blink-merge.git] / extensions / common / permissions / permissions_data.h
blob011221073d2bf1b8315d9a81a837220393836b47
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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/synchronization/lock.h"
15 #include "extensions/common/manifest.h"
16 #include "extensions/common/permissions/api_permission.h"
17 #include "extensions/common/permissions/coalesced_permission_message.h"
18 #include "extensions/common/permissions/permission_message_provider.h"
19 #include "extensions/common/permissions/permission_set.h"
21 class GURL;
23 namespace extensions {
24 class Extension;
25 class URLPatternSet;
27 // A container for the permissions state of an extension, including active,
28 // withheld, and tab-specific permissions.
29 class PermissionsData {
30 public:
31 // The possible types of access for a given frame.
32 enum AccessType {
33 ACCESS_DENIED, // The extension is not allowed to access the given page.
34 ACCESS_ALLOWED, // The extension is allowed to access the given page.
35 ACCESS_WITHHELD // The browser must determine if the extension can access
36 // the given page.
39 using TabPermissionsMap = std::map<int, scoped_refptr<const PermissionSet>>;
41 // Delegate class to allow different contexts (e.g. browser vs renderer) to
42 // have control over policy decisions.
43 class PolicyDelegate {
44 public:
45 virtual ~PolicyDelegate() {}
47 // Returns false if script access should be blocked on this page.
48 // Otherwise, default policy should decide.
49 virtual bool CanExecuteScriptOnPage(const Extension* extension,
50 const GURL& document_url,
51 int tab_id,
52 int process_id,
53 std::string* error) = 0;
56 static void SetPolicyDelegate(PolicyDelegate* delegate);
58 PermissionsData(const Extension* extension);
59 virtual ~PermissionsData();
61 // Returns true if the extension is a COMPONENT extension or is on the
62 // whitelist of extensions that can script all pages.
63 static bool CanExecuteScriptEverywhere(const Extension* extension);
65 // Returns true if the --scripts-require-action flag would possibly affect
66 // the given |extension| and |permissions|. We pass in the |permissions|
67 // explicitly, as we may need to check with permissions other than the ones
68 // that are currently on the extension's PermissionsData.
69 static bool ScriptsMayRequireActionForExtension(
70 const Extension* extension,
71 const PermissionSet* permissions);
73 // Returns true if we should skip the permissions warning for the extension
74 // with the given |extension_id|.
75 static bool ShouldSkipPermissionWarnings(const std::string& extension_id);
77 // Returns true if the given |url| is restricted for the given |extension|,
78 // as is commonly the case for chrome:// urls.
79 // NOTE: You probably want to use CanAccessPage().
80 static bool IsRestrictedUrl(const GURL& document_url,
81 const Extension* extension,
82 std::string* error);
84 // Sets the runtime permissions of the given |extension| to |active| and
85 // |withheld|.
86 void SetPermissions(const scoped_refptr<const PermissionSet>& active,
87 const scoped_refptr<const PermissionSet>& withheld) const;
89 // Updates the tab-specific permissions of |tab_id| to include those from
90 // |permissions|.
91 void UpdateTabSpecificPermissions(
92 int tab_id,
93 scoped_refptr<const PermissionSet> permissions) const;
95 // Clears the tab-specific permissions of |tab_id|.
96 void ClearTabSpecificPermissions(int tab_id) const;
98 // Returns true if the |extension| has the given |permission|. Prefer
99 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
100 // api that requires a permission they didn't know about, e.g. open web apis.
101 // Note this does not include APIs with no corresponding permission, like
102 // "runtime" or "browserAction".
103 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
104 bool HasAPIPermission(APIPermission::ID permission) const;
105 bool HasAPIPermission(const std::string& permission_name) const;
106 bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
107 bool CheckAPIPermissionWithParam(
108 APIPermission::ID permission,
109 const APIPermission::CheckParam* param) const;
111 // Returns the hosts this extension effectively has access to, including
112 // explicit and scriptable hosts, and any hosts on tabs the extension has
113 // active tab permissions for.
114 URLPatternSet GetEffectiveHostPermissions() const;
116 // TODO(rdevlin.cronin): HasHostPermission() and
117 // HasEffectiveAccessToAllHosts() are just forwards for the active
118 // permissions. We should either get rid of these, and have callers use
119 // active_permissions(), or should get rid of active_permissions(), and make
120 // callers use PermissionsData for everything. We should not do both.
122 // Whether the extension has access to the given |url|.
123 bool HasHostPermission(const GURL& url) const;
125 // Whether the extension has effective access to all hosts. This is true if
126 // there is a content script that matches all hosts, if there is a host
127 // permission grants access to all hosts (like <all_urls>) or an api
128 // permission that effectively grants access to all hosts (e.g. proxy,
129 // network, etc.)
130 bool HasEffectiveAccessToAllHosts() const;
132 // Returns the full list of permission details for messages that should
133 // display at install time, in a nested format ready for display.
134 CoalescedPermissionMessages GetPermissionMessages() const;
136 // Returns true if the extension has requested all-hosts permissions (or
137 // something close to it), but has had it withheld.
138 bool HasWithheldImpliedAllHosts() const;
140 // Returns true if the |extension| has permission to access and interact with
141 // the specified page, in order to do things like inject scripts or modify
142 // the content.
143 // If this returns false and |error| is non-NULL, |error| will be popualted
144 // with the reason the extension cannot access the page.
145 bool CanAccessPage(const Extension* extension,
146 const GURL& document_url,
147 int tab_id,
148 int process_id,
149 std::string* error) const;
150 // Like CanAccessPage, but also takes withheld permissions into account.
151 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
152 // know how to wait for permission.
153 AccessType GetPageAccess(const Extension* extension,
154 const GURL& document_url,
155 int tab_id,
156 int process_id,
157 std::string* error) const;
159 // Returns true if the |extension| has permission to inject a content script
160 // on the page.
161 // If this returns false and |error| is non-NULL, |error| will be popualted
162 // with the reason the extension cannot script the page.
163 // NOTE: You almost certainly want to use CanAccessPage() instead of this
164 // method.
165 bool CanRunContentScriptOnPage(const Extension* extension,
166 const GURL& document_url,
167 int tab_id,
168 int process_id,
169 std::string* error) const;
170 // Like CanRunContentScriptOnPage, but also takes withheld permissions into
171 // account.
172 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
173 // know how to wait for permission.
174 AccessType GetContentScriptAccess(const Extension* extension,
175 const GURL& document_url,
176 int tab_id,
177 int process_id,
178 std::string* error) const;
180 // Returns true if extension is allowed to obtain the contents of a page as
181 // an image. Since a page may contain sensitive information, this is
182 // restricted to the extension's host permissions as well as the extension
183 // page itself.
184 bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
186 // Returns the tab permissions map.
187 TabPermissionsMap CopyTabSpecificPermissionsMap() const;
189 scoped_refptr<const PermissionSet> active_permissions() const {
190 // We lock so that we can't also be setting the permissions while returning.
191 base::AutoLock auto_lock(runtime_lock_);
192 return active_permissions_unsafe_;
195 scoped_refptr<const PermissionSet> withheld_permissions() const {
196 // We lock so that we can't also be setting the permissions while returning.
197 base::AutoLock auto_lock(runtime_lock_);
198 return withheld_permissions_unsafe_;
201 #if defined(UNIT_TEST)
202 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting(
203 int tab_id) const {
204 return GetTabSpecificPermissions(tab_id);
206 #endif
208 private:
209 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
210 // aren't any.
211 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
212 int tab_id) const;
214 // Returns true if the |extension| has tab-specific permission to operate on
215 // the tab specified by |tab_id| with the given |url|.
216 // Note that if this returns false, it doesn't mean the extension can't run on
217 // the given tab, only that it does not have tab-specific permission to do so.
218 bool HasTabSpecificPermissionToExecuteScript(int tab_id,
219 const GURL& url) const;
221 // Returns whether or not the extension is permitted to run on the given page,
222 // checking against |permitted_url_patterns| in addition to blocking special
223 // sites (like the webstore or chrome:// urls).
224 AccessType CanRunOnPage(const Extension* extension,
225 const GURL& document_url,
226 int tab_id,
227 int process_id,
228 const URLPatternSet& permitted_url_patterns,
229 const URLPatternSet& withheld_url_patterns,
230 std::string* error) const;
232 // The associated extension's id.
233 std::string extension_id_;
235 // The associated extension's manifest type.
236 Manifest::Type manifest_type_;
238 mutable base::Lock runtime_lock_;
240 // The permission's which are currently active on the extension during
241 // runtime.
242 // Unsafe indicates that we must lock anytime this is directly accessed.
243 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
244 // active_permissions() accessor.
245 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_;
247 // The permissions the extension requested, but was not granted due because
248 // they are too powerful. This includes things like all_hosts.
249 // Unsafe indicates that we must lock anytime this is directly accessed.
250 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe)
251 // withheld_permissions() accessor.
252 mutable scoped_refptr<const PermissionSet> withheld_permissions_unsafe_;
254 mutable TabPermissionsMap tab_specific_permissions_;
256 DISALLOW_COPY_AND_ASSIGN(PermissionsData);
259 } // namespace extensions
261 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_