Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / extensions / common / permissions / permissions_data.h
blob3fee044642ea6a1ed7b1ba158bd57694cd4a8a44
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.h"
19 #include "extensions/common/permissions/permission_message_provider.h"
20 #include "extensions/common/permissions/permission_set.h"
22 class GURL;
24 namespace extensions {
25 class Extension;
26 class URLPatternSet;
28 // A container for the permissions state of an extension, including active,
29 // withheld, and tab-specific permissions.
30 class PermissionsData {
31 public:
32 // The possible types of access for a given frame.
33 enum AccessType {
34 ACCESS_DENIED, // The extension is not allowed to access the given page.
35 ACCESS_ALLOWED, // The extension is allowed to access the given page.
36 ACCESS_WITHHELD // The browser must determine if the extension can access
37 // the given page.
40 using TabPermissionsMap = std::map<int, scoped_refptr<const PermissionSet>>;
42 // Delegate class to allow different contexts (e.g. browser vs renderer) to
43 // have control over policy decisions.
44 class PolicyDelegate {
45 public:
46 virtual ~PolicyDelegate() {}
48 // Returns false if script access should be blocked on this page.
49 // Otherwise, default policy should decide.
50 virtual bool CanExecuteScriptOnPage(const Extension* extension,
51 const GURL& document_url,
52 const GURL& top_document_url,
53 int tab_id,
54 int process_id,
55 std::string* error) = 0;
58 static void SetPolicyDelegate(PolicyDelegate* delegate);
60 PermissionsData(const Extension* extension);
61 virtual ~PermissionsData();
63 // Returns true if the extension is a COMPONENT extension or is on the
64 // whitelist of extensions that can script all pages.
65 static bool CanExecuteScriptEverywhere(const Extension* extension);
67 // Returns true if the --scripts-require-action flag would possibly affect
68 // the given |extension| and |permissions|. We pass in the |permissions|
69 // explicitly, as we may need to check with permissions other than the ones
70 // that are currently on the extension's PermissionsData.
71 static bool ScriptsMayRequireActionForExtension(
72 const Extension* extension,
73 const PermissionSet* permissions);
75 // Returns true if we should skip the permissions warning for the extension
76 // with the given |extension_id|.
77 static bool ShouldSkipPermissionWarnings(const std::string& extension_id);
79 // Returns true if the given |url| is restricted for the given |extension|,
80 // as is commonly the case for chrome:// urls.
81 // NOTE: You probably want to use CanAccessPage().
82 static bool IsRestrictedUrl(const GURL& document_url,
83 const GURL& top_frame_url,
84 const Extension* extension,
85 std::string* error);
87 // Sets the runtime permissions of the given |extension| to |active| and
88 // |withheld|.
89 void SetPermissions(const scoped_refptr<const PermissionSet>& active,
90 const scoped_refptr<const PermissionSet>& withheld) const;
92 // Updates the tab-specific permissions of |tab_id| to include those from
93 // |permissions|.
94 void UpdateTabSpecificPermissions(
95 int tab_id,
96 scoped_refptr<const PermissionSet> permissions) const;
98 // Clears the tab-specific permissions of |tab_id|.
99 void ClearTabSpecificPermissions(int tab_id) const;
101 // Returns true if the |extension| has the given |permission|. Prefer
102 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
103 // api that requires a permission they didn't know about, e.g. open web apis.
104 // Note this does not include APIs with no corresponding permission, like
105 // "runtime" or "browserAction".
106 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
107 bool HasAPIPermission(APIPermission::ID permission) const;
108 bool HasAPIPermission(const std::string& permission_name) const;
109 bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
110 bool CheckAPIPermissionWithParam(
111 APIPermission::ID permission,
112 const APIPermission::CheckParam* param) const;
114 // Returns the hosts this extension effectively has access to, including
115 // explicit and scriptable hosts, and any hosts on tabs the extension has
116 // active tab permissions for.
117 URLPatternSet GetEffectiveHostPermissions() const;
119 // TODO(rdevlin.cronin): HasHostPermission() and
120 // HasEffectiveAccessToAllHosts() are just forwards for the active
121 // permissions. We should either get rid of these, and have callers use
122 // active_permissions(), or should get rid of active_permissions(), and make
123 // callers use PermissionsData for everything. We should not do both.
125 // Whether the extension has access to the given |url|.
126 bool HasHostPermission(const GURL& url) const;
128 // Whether the extension has effective access to all hosts. This is true if
129 // there is a content script that matches all hosts, if there is a host
130 // permission grants access to all hosts (like <all_urls>) or an api
131 // permission that effectively grants access to all hosts (e.g. proxy,
132 // network, etc.)
133 bool HasEffectiveAccessToAllHosts() const;
135 // Returns the full list of legacy permission message IDs.
136 // Deprecated. You DO NOT want to call this!
137 // TODO(treib): Remove once we've switched to the new system.
138 PermissionMessageIDs GetLegacyPermissionMessageIDs() const;
140 // Returns the full list of permission messages that should display at install
141 // time as strings.
142 PermissionMessageStrings GetPermissionMessageStrings() const;
144 // Returns the full list of permission messages that should display at install
145 // time as strings.
146 // TODO(sashab): Deprecate this in favor of GetPermissionMessageStrings.
147 std::vector<base::string16> GetLegacyPermissionMessageStrings() const;
149 // Returns the full list of permission details for messages that should
150 // display at install time as strings.
151 // TODO(sashab): Deprecate this in favor of GetPermissionMessageStrings.
152 std::vector<base::string16> GetLegacyPermissionMessageDetailsStrings() const;
154 // Returns the full list of permission details for messages that should
155 // display at install time, in a nested format ready for display.
156 CoalescedPermissionMessages GetCoalescedPermissionMessages() const;
158 // Returns true if the extension has requested all-hosts permissions (or
159 // something close to it), but has had it withheld.
160 bool HasWithheldImpliedAllHosts() const;
162 // Returns true if the |extension| has permission to access and interact with
163 // the specified page, in order to do things like inject scripts or modify
164 // the content.
165 // If this returns false and |error| is non-NULL, |error| will be popualted
166 // with the reason the extension cannot access the page.
167 bool CanAccessPage(const Extension* extension,
168 const GURL& document_url,
169 const GURL& top_document_url,
170 int tab_id,
171 int process_id,
172 std::string* error) const;
173 // Like CanAccessPage, but also takes withheld permissions into account.
174 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
175 // know how to wait for permission.
176 AccessType GetPageAccess(const Extension* extension,
177 const GURL& document_url,
178 const GURL& top_document_url,
179 int tab_id,
180 int process_id,
181 std::string* error) const;
183 // Returns true if the |extension| has permission to inject a content script
184 // on the page.
185 // If this returns false and |error| is non-NULL, |error| will be popualted
186 // with the reason the extension cannot script the page.
187 // NOTE: You almost certainly want to use CanAccessPage() instead of this
188 // method.
189 bool CanRunContentScriptOnPage(const Extension* extension,
190 const GURL& document_url,
191 const GURL& top_document_url,
192 int tab_id,
193 int process_id,
194 std::string* error) const;
195 // Like CanRunContentScriptOnPage, but also takes withheld permissions into
196 // account.
197 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
198 // know how to wait for permission.
199 AccessType GetContentScriptAccess(const Extension* extension,
200 const GURL& document_url,
201 const GURL& top_document_url,
202 int tab_id,
203 int process_id,
204 std::string* error) const;
206 // Returns true if extension is allowed to obtain the contents of a page as
207 // an image. Since a page may contain sensitive information, this is
208 // restricted to the extension's host permissions as well as the extension
209 // page itself.
210 bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
212 const scoped_refptr<const PermissionSet>& active_permissions() const {
213 // We lock so that we can't also be setting the permissions while returning.
214 base::AutoLock auto_lock(runtime_lock_);
215 return active_permissions_unsafe_;
218 const scoped_refptr<const PermissionSet>& withheld_permissions() const {
219 // We lock so that we can't also be setting the permissions while returning.
220 base::AutoLock auto_lock(runtime_lock_);
221 return withheld_permissions_unsafe_;
224 const TabPermissionsMap& tab_specific_permissions() const {
225 // We lock so that we can't also be setting the permissions while returning.
226 base::AutoLock auto_lock(runtime_lock_);
227 return tab_specific_permissions_;
230 #if defined(UNIT_TEST)
231 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting(
232 int tab_id) const {
233 return GetTabSpecificPermissions(tab_id);
235 #endif
237 private:
238 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
239 // aren't any.
240 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
241 int tab_id) const;
243 // Returns true if the |extension| has tab-specific permission to operate on
244 // the tab specified by |tab_id| with the given |url|.
245 // Note that if this returns false, it doesn't mean the extension can't run on
246 // the given tab, only that it does not have tab-specific permission to do so.
247 bool HasTabSpecificPermissionToExecuteScript(int tab_id,
248 const GURL& url) const;
250 // Returns whether or not the extension is permitted to run on the given page,
251 // checking against |permitted_url_patterns| in addition to blocking special
252 // sites (like the webstore or chrome:// urls).
253 AccessType CanRunOnPage(const Extension* extension,
254 const GURL& document_url,
255 const GURL& top_document_url,
256 int tab_id,
257 int process_id,
258 const URLPatternSet& permitted_url_patterns,
259 const URLPatternSet& withheld_url_patterns,
260 std::string* error) const;
262 // The associated extension's id.
263 std::string extension_id_;
265 // The associated extension's manifest type.
266 Manifest::Type manifest_type_;
268 mutable base::Lock runtime_lock_;
270 // The permission's which are currently active on the extension during
271 // runtime.
272 // Unsafe indicates that we must lock anytime this is directly accessed.
273 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
274 // active_permissions() accessor.
275 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_;
277 // The permissions the extension requested, but was not granted due because
278 // they are too powerful. This includes things like all_hosts.
279 // Unsafe indicates that we must lock anytime this is directly accessed.
280 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe)
281 // withheld_permissions() accessor.
282 mutable scoped_refptr<const PermissionSet> withheld_permissions_unsafe_;
284 mutable TabPermissionsMap tab_specific_permissions_;
286 DISALLOW_COPY_AND_ASSIGN(PermissionsData);
289 } // namespace extensions
291 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_