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_
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"
24 namespace extensions
{
28 // A container for the permissions state of an extension, including active,
29 // withheld, and tab-specific permissions.
30 class PermissionsData
{
32 // The possible types of access for a given frame.
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
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
{
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
,
54 std::string
* error
) = 0;
57 static void SetPolicyDelegate(PolicyDelegate
* delegate
);
59 PermissionsData(const Extension
* extension
);
60 virtual ~PermissionsData();
62 // Returns true if the extension is a COMPONENT extension or is on the
63 // whitelist of extensions that can script all pages.
64 static bool CanExecuteScriptEverywhere(const Extension
* extension
);
66 // Returns true if the --scripts-require-action flag would possibly affect
67 // the given |extension| and |permissions|. We pass in the |permissions|
68 // explicitly, as we may need to check with permissions other than the ones
69 // that are currently on the extension's PermissionsData.
70 static bool ScriptsMayRequireActionForExtension(
71 const Extension
* extension
,
72 const PermissionSet
* permissions
);
74 // Returns true if we should skip the permissions warning for the extension
75 // with the given |extension_id|.
76 static bool ShouldSkipPermissionWarnings(const std::string
& extension_id
);
78 // Returns true if the given |url| is restricted for the given |extension|,
79 // as is commonly the case for chrome:// urls.
80 // NOTE: You probably want to use CanAccessPage().
81 static bool IsRestrictedUrl(const GURL
& document_url
,
82 const Extension
* extension
,
85 // Sets the runtime permissions of the given |extension| to |active| and
87 void SetPermissions(const scoped_refptr
<const PermissionSet
>& active
,
88 const scoped_refptr
<const PermissionSet
>& withheld
) const;
90 // Updates the tab-specific permissions of |tab_id| to include those from
92 void UpdateTabSpecificPermissions(
94 scoped_refptr
<const PermissionSet
> permissions
) const;
96 // Clears the tab-specific permissions of |tab_id|.
97 void ClearTabSpecificPermissions(int tab_id
) const;
99 // Returns true if the |extension| has the given |permission|. Prefer
100 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
101 // api that requires a permission they didn't know about, e.g. open web apis.
102 // Note this does not include APIs with no corresponding permission, like
103 // "runtime" or "browserAction".
104 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
105 bool HasAPIPermission(APIPermission::ID permission
) const;
106 bool HasAPIPermission(const std::string
& permission_name
) const;
107 bool HasAPIPermissionForTab(int tab_id
, APIPermission::ID permission
) const;
108 bool CheckAPIPermissionWithParam(
109 APIPermission::ID permission
,
110 const APIPermission::CheckParam
* param
) const;
112 // Returns the hosts this extension effectively has access to, including
113 // explicit and scriptable hosts, and any hosts on tabs the extension has
114 // active tab permissions for.
115 URLPatternSet
GetEffectiveHostPermissions() const;
117 // TODO(rdevlin.cronin): HasHostPermission() and
118 // HasEffectiveAccessToAllHosts() are just forwards for the active
119 // permissions. We should either get rid of these, and have callers use
120 // active_permissions(), or should get rid of active_permissions(), and make
121 // callers use PermissionsData for everything. We should not do both.
123 // Whether the extension has access to the given |url|.
124 bool HasHostPermission(const GURL
& url
) const;
126 // Whether the extension has effective access to all hosts. This is true if
127 // there is a content script that matches all hosts, if there is a host
128 // permission grants access to all hosts (like <all_urls>) or an api
129 // permission that effectively grants access to all hosts (e.g. proxy,
131 bool HasEffectiveAccessToAllHosts() const;
133 // Returns the full list of permission details for messages that should
134 // display at install time, in a nested format ready for display.
135 CoalescedPermissionMessages
GetPermissionMessages() const;
137 // Returns true if the extension has requested all-hosts permissions (or
138 // something close to it), but has had it withheld.
139 bool HasWithheldImpliedAllHosts() const;
141 // Returns true if the |extension| has permission to access and interact with
142 // the specified page, in order to do things like inject scripts or modify
144 // If this returns false and |error| is non-NULL, |error| will be popualted
145 // with the reason the extension cannot access the page.
146 bool CanAccessPage(const Extension
* extension
,
147 const GURL
& document_url
,
150 std::string
* error
) const;
151 // Like CanAccessPage, but also takes withheld permissions into account.
152 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
153 // know how to wait for permission.
154 AccessType
GetPageAccess(const Extension
* extension
,
155 const GURL
& document_url
,
158 std::string
* error
) const;
160 // Returns true if the |extension| has permission to inject a content script
162 // If this returns false and |error| is non-NULL, |error| will be popualted
163 // with the reason the extension cannot script the page.
164 // NOTE: You almost certainly want to use CanAccessPage() instead of this
166 bool CanRunContentScriptOnPage(const Extension
* extension
,
167 const GURL
& document_url
,
170 std::string
* error
) const;
171 // Like CanRunContentScriptOnPage, but also takes withheld permissions into
173 // TODO(rdevlin.cronin) We shouldn't have two functions, but not all callers
174 // know how to wait for permission.
175 AccessType
GetContentScriptAccess(const Extension
* extension
,
176 const GURL
& document_url
,
179 std::string
* error
) const;
181 // Returns true if extension is allowed to obtain the contents of a page as
182 // an image. Since a page may contain sensitive information, this is
183 // restricted to the extension's host permissions as well as the extension
185 bool CanCaptureVisiblePage(int tab_id
, std::string
* error
) const;
187 // Returns the tab permissions map.
188 TabPermissionsMap
CopyTabSpecificPermissionsMap() const;
190 scoped_refptr
<const PermissionSet
> active_permissions() const {
191 // We lock so that we can't also be setting the permissions while returning.
192 base::AutoLock
auto_lock(runtime_lock_
);
193 return active_permissions_unsafe_
;
196 scoped_refptr
<const PermissionSet
> withheld_permissions() const {
197 // We lock so that we can't also be setting the permissions while returning.
198 base::AutoLock
auto_lock(runtime_lock_
);
199 return withheld_permissions_unsafe_
;
202 #if defined(UNIT_TEST)
203 scoped_refptr
<const PermissionSet
> GetTabSpecificPermissionsForTesting(
205 return GetTabSpecificPermissions(tab_id
);
210 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
212 scoped_refptr
<const PermissionSet
> GetTabSpecificPermissions(
215 // Returns true if the |extension| has tab-specific permission to operate on
216 // the tab specified by |tab_id| with the given |url|.
217 // Note that if this returns false, it doesn't mean the extension can't run on
218 // the given tab, only that it does not have tab-specific permission to do so.
219 bool HasTabSpecificPermissionToExecuteScript(int tab_id
,
220 const GURL
& url
) const;
222 // Returns whether or not the extension is permitted to run on the given page,
223 // checking against |permitted_url_patterns| in addition to blocking special
224 // sites (like the webstore or chrome:// urls).
225 AccessType
CanRunOnPage(const Extension
* extension
,
226 const GURL
& document_url
,
229 const URLPatternSet
& permitted_url_patterns
,
230 const URLPatternSet
& withheld_url_patterns
,
231 std::string
* error
) const;
233 // The associated extension's id.
234 std::string extension_id_
;
236 // The associated extension's manifest type.
237 Manifest::Type manifest_type_
;
239 mutable base::Lock runtime_lock_
;
241 // The permission's which are currently active on the extension during
243 // Unsafe indicates that we must lock anytime this is directly accessed.
244 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
245 // active_permissions() accessor.
246 mutable scoped_refptr
<const PermissionSet
> active_permissions_unsafe_
;
248 // The permissions the extension requested, but was not granted due because
249 // they are too powerful. This includes things like all_hosts.
250 // Unsafe indicates that we must lock anytime this is directly accessed.
251 // Unless you need to change |withheld_permissions_unsafe_|, use the (safe)
252 // withheld_permissions() accessor.
253 mutable scoped_refptr
<const PermissionSet
> withheld_permissions_unsafe_
;
255 mutable TabPermissionsMap tab_specific_permissions_
;
257 DISALLOW_COPY_AND_ASSIGN(PermissionsData
);
260 } // namespace extensions
262 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_