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/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/synchronization/lock.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/common/permissions/api_permission.h"
19 #include "extensions/common/permissions/permission_message.h"
20 #include "extensions/common/permissions/permission_set.h"
24 namespace extensions {
31 // A container for the active permissions of an extension.
32 // TODO(rdevlin.cronin): For the love of everything good, rename this class to
33 // ActivePermissions. We do *not* need PermissionsParser, PermissionSet,
34 // PermissionInfo, and PermissionsData. No one will be able to keep them
36 class PermissionsData {
38 // Delegate class to allow different contexts (e.g. browser vs renderer) to
39 // have control over policy decisions.
40 class PolicyDelegate {
42 virtual ~PolicyDelegate() {}
44 // Returns false if script access should be blocked on this page.
45 // Otherwise, default policy should decide.
46 virtual bool CanExecuteScriptOnPage(const Extension* extension,
47 const GURL& document_url,
48 const GURL& top_document_url,
51 std::string* error) = 0;
54 static void SetPolicyDelegate(PolicyDelegate* delegate);
56 PermissionsData(const Extension* extension);
57 virtual ~PermissionsData();
59 // Returns true if the |extension| can silently increase its permission level.
60 // Users must approve permissions for unpacked and packed extensions in the
61 // following situations:
62 // - when installing or upgrading packed extensions
63 // - when installing unpacked extensions that have NPAPI plugins
64 // - when either type of extension requests optional permissions
65 static bool CanSilentlyIncreasePermissions(const Extension* extension);
67 // Returns true if the extension is a COMPONENT extension or is on the
68 // whitelist of extensions that can script all pages.
69 static bool CanExecuteScriptEverywhere(const Extension* extension);
71 // Sets the runtime permissions of the given |extension| to |permissions|.
72 void SetActivePermissions(const PermissionSet* active) const;
74 // Updates the tab-specific permissions of |tab_id| to include those from
76 void UpdateTabSpecificPermissions(
78 scoped_refptr<const PermissionSet> permissions) const;
80 // Clears the tab-specific permissions of |tab_id|.
81 void ClearTabSpecificPermissions(int tab_id) const;
83 // Returns true if the |extension| has the given |permission|. Prefer
84 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
85 // api that requires a permission they didn't know about, e.g. open web apis.
86 // Note this does not include APIs with no corresponding permission, like
87 // "runtime" or "browserAction".
88 // TODO(mpcomplete): drop the "API" from these names, it's confusing.
89 bool HasAPIPermission(APIPermission::ID permission) const;
90 bool HasAPIPermission(const std::string& permission_name) const;
91 bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
92 bool CheckAPIPermissionWithParam(
93 APIPermission::ID permission,
94 const APIPermission::CheckParam* param) const;
96 // TODO(rdevlin.cronin): GetEffectiveHostPermissions(), HasHostPermission(),
97 // and HasEffectiveAccessToAllHosts() are just forwards for the active
98 // permissions. We should either get rid of these, and have callers use
99 // active_permissions(), or should get rid of active_permissions(), and make
100 // callers use PermissionsData for everything. We should not do both.
102 // Returns the effective hosts associated with the active permissions.
103 const URLPatternSet& GetEffectiveHostPermissions() const;
105 // Whether the extension has access to the given |url|.
106 bool HasHostPermission(const GURL& url) const;
108 // Whether the extension has effective access to all hosts. This is true if
109 // there is a content script that matches all hosts, if there is a host
110 // permission grants access to all hosts (like <all_urls>) or an api
111 // permission that effectively grants access to all hosts (e.g. proxy,
113 bool HasEffectiveAccessToAllHosts() const;
115 // Returns the full list of permission messages that should display at
117 PermissionMessages GetPermissionMessages() const;
119 // Returns the full list of permission messages that should display at install
121 std::vector<base::string16> GetPermissionMessageStrings() const;
123 // Returns the full list of permission details for messages that should
124 // display at install time as strings.
125 std::vector<base::string16> GetPermissionMessageDetailsStrings() const;
127 // Returns true if the |extension| has permission to access and interact with
128 // the specified page, in order to do things like inject scripts or modify
130 // If this returns false and |error| is non-NULL, |error| will be popualted
131 // with the reason the extension cannot access the page.
132 bool CanAccessPage(const Extension* extension,
133 const GURL& document_url,
134 const GURL& top_document_url,
137 std::string* error) const;
139 // Returns true if the |extension| has permission to inject a content script
141 // If this returns false and |error| is non-NULL, |error| will be popualted
142 // with the reason the extension cannot script the page.
143 // NOTE: You almost certainly want to use CanAccessPage() instead of this
145 bool CanRunContentScriptOnPage(const Extension* extension,
146 const GURL& document_url,
147 const GURL& top_document_url,
150 std::string* error) const;
152 // Returns true if extension is allowed to obtain the contents of a page as
153 // an image. Since a page may contain sensitive information, this is
154 // restricted to the extension's host permissions as well as the extension
156 bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
158 // Returns true if the user should be alerted that the |extension| is running
159 // a script. If |tab_id| and |url| are included, this also considers tab-
160 // specific permissions.
161 bool RequiresActionForScriptExecution(const Extension* extension) const;
162 bool RequiresActionForScriptExecution(const Extension* extension,
164 const GURL& url) const;
166 scoped_refptr<const PermissionSet> active_permissions() const {
167 base::AutoLock auto_lock(runtime_lock_);
168 return active_permissions_unsafe_;
171 #if defined(UNIT_TEST)
172 scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting(
174 return GetTabSpecificPermissions(tab_id);
179 typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap;
181 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
183 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
186 // Returns true if the |extension| has tab-specific permission to operate on
187 // the tab specified by |tab_id| with the given |url|.
188 // Note that if this returns false, it doesn't mean the extension can't run on
189 // the given tab, only that it does not have tab-specific permission to do so.
190 bool HasTabSpecificPermissionToExecuteScript(int tab_id,
191 const GURL& url) const;
193 // Returns true if the extension is permitted to run on the given page,
194 // checking against |permitted_url_patterns| in addition to blocking special
195 // sites (like the webstore or chrome:// urls).
196 bool CanRunOnPage(const Extension* extension,
197 const GURL& document_url,
198 const GURL& top_document_url,
201 const URLPatternSet& permitted_url_patterns,
202 std::string* error) const;
204 // The associated extension's id.
205 std::string extension_id_;
207 // The associated extension's manifest type.
208 Manifest::Type manifest_type_;
210 mutable base::Lock runtime_lock_;
212 // The permission's which are currently active on the extension during
214 // Unsafe indicates that we must lock anytime this is directly accessed.
215 // Unless you need to change |active_permissions_unsafe_|, use the (safe)
216 // active_permissions() accessor.
217 mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_;
219 mutable TabPermissionsMap tab_specific_permissions_;
221 DISALLOW_COPY_AND_ASSIGN(PermissionsData);
224 } // namespace extensions
226 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_