1 // Copyright 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_PERMISSION_SET_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSION_SET_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "extensions/common/permissions/api_permission.h"
14 #include "extensions/common/permissions/api_permission_set.h"
15 #include "extensions/common/permissions/manifest_permission.h"
16 #include "extensions/common/permissions/manifest_permission_set.h"
17 #include "extensions/common/url_pattern_set.h"
19 namespace extensions
{
21 // The PermissionSet is an immutable class that encapsulates an
22 // extension's permissions. The class exposes set operations for combining and
23 // manipulating the permissions.
24 // TODO(sashab): PermissionIDSet should be called PermissionSet. Once
25 // PermissionMessageProvider::GetCoalescedPermissionMessages() is the only
26 // method used for generating permission messages, find the other users of this
27 // class and deprecate or rename it as appropriate.
29 : public base::RefCountedThreadSafe
<PermissionSet
> {
31 // Creates an empty permission set (e.g. default permissions).
34 // Creates a new permission set based on the specified data: the API
35 // permissions, manifest key permissions, host permissions, and scriptable
36 // hosts. The effective hosts of the newly created permission set will be
37 // inferred from the given host permissions.
38 PermissionSet(const APIPermissionSet
& apis
,
39 const ManifestPermissionSet
& manifest_permissions
,
40 const URLPatternSet
& explicit_hosts
,
41 const URLPatternSet
& scriptable_hosts
);
43 // Creates a new permission set equal to |set1| - |set2|, passing ownership of
44 // the new set to the caller.
45 static PermissionSet
* CreateDifference(
46 const PermissionSet
* set1
, const PermissionSet
* set2
);
48 // Creates a new permission set equal to the intersection of |set1| and
49 // |set2|, passing ownership of the new set to the caller.
50 static PermissionSet
* CreateIntersection(
51 const PermissionSet
* set1
, const PermissionSet
* set2
);
53 // Creates a new permission set equal to the union of |set1| and |set2|.
54 // Passes ownership of the new set to the caller.
55 static PermissionSet
* CreateUnion(
56 const PermissionSet
* set1
, const PermissionSet
* set2
);
58 bool operator==(const PermissionSet
& rhs
) const;
60 // Returns true if every API or host permission available to |set| is also
61 // available to this. In other words, if the API permissions of |set| are a
62 // subset of this, and the host permissions in this encompass those in |set|.
63 bool Contains(const PermissionSet
& set
) const;
65 // Gets the API permissions in this set as a set of strings.
66 std::set
<std::string
> GetAPIsAsStrings() const;
68 // Returns true if this is an empty set (e.g., the default permission set).
71 // Returns true if the set has the specified API permission.
72 bool HasAPIPermission(APIPermission::ID permission
) const;
74 // Returns true if the |extension| explicitly requests access to the given
75 // |permission_name|. Note this does not include APIs without no corresponding
76 // permission, like "runtime" or "browserAction".
77 bool HasAPIPermission(const std::string
& permission_name
) const;
79 // Returns true if the set allows the given permission with the default
81 bool CheckAPIPermission(APIPermission::ID permission
) const;
83 // Returns true if the set allows the given permission and permission param.
84 bool CheckAPIPermissionWithParam(APIPermission::ID permission
,
85 const APIPermission::CheckParam
* param
) const;
87 // Returns true if this includes permission to access |origin|.
88 bool HasExplicitAccessToOrigin(const GURL
& origin
) const;
90 // Returns true if this permission set includes access to script |url|.
91 bool HasScriptableAccessToURL(const GURL
& url
) const;
93 // Returns true if this permission set includes effective access to all
95 bool HasEffectiveAccessToAllHosts() const;
97 // Returns true if this permission set has access to so many hosts, that we
98 // should treat it as all hosts for warning purposes.
99 // For example, '*://*.com/*'.
100 bool ShouldWarnAllHosts() const;
102 // Returns true if this permission set includes effective access to |url|.
103 bool HasEffectiveAccessToURL(const GURL
& url
) const;
105 // Returns true if this permission set effectively represents full access
106 // (e.g. native code).
107 bool HasEffectiveFullAccess() const;
109 const APIPermissionSet
& apis() const { return apis_
; }
111 const ManifestPermissionSet
& manifest_permissions() const {
112 return manifest_permissions_
;
115 const URLPatternSet
& effective_hosts() const { return effective_hosts_
; }
117 const URLPatternSet
& explicit_hosts() const { return explicit_hosts_
; }
119 const URLPatternSet
& scriptable_hosts() const { return scriptable_hosts_
; }
122 FRIEND_TEST_ALL_PREFIXES(PermissionsTest
, GetWarningMessages_AudioVideo
);
123 FRIEND_TEST_ALL_PREFIXES(PermissionsTest
, AccessToDevicesMessages
);
124 friend class base::RefCountedThreadSafe
<PermissionSet
>;
128 // Adds permissions implied independently of other context.
129 void InitImplicitPermissions();
131 // Initializes the effective host permission based on the data in this set.
132 void InitEffectiveHosts();
134 // Initializes |has_access_to_most_hosts_|.
135 void InitShouldWarnAllHosts() const;
137 // The api list is used when deciding if an extension can access certain
138 // extension APIs and features.
139 APIPermissionSet apis_
;
141 // The manifest key permission list is used when deciding if an extension
142 // can access certain extension APIs and features.
143 ManifestPermissionSet manifest_permissions_
;
145 // The list of hosts that can be accessed directly from the extension.
146 // TODO(jstritar): Rename to "hosts_"?
147 URLPatternSet explicit_hosts_
;
149 // The list of hosts that can be scripted by content scripts.
150 // TODO(jstritar): Rename to "user_script_hosts_"?
151 URLPatternSet scriptable_hosts_
;
153 // The list of hosts this effectively grants access to.
154 URLPatternSet effective_hosts_
;
156 enum ShouldWarnAllHostsType
{
161 // Whether or not this permission set includes access to so many origins, we
162 // should treat it as all_hosts for warning purposes.
163 // Lazily initialized (and therefore mutable).
164 mutable ShouldWarnAllHostsType should_warn_all_hosts_
;
167 } // namespace extensions
169 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSION_SET_H_