Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | |
| 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/gtest_prod_util.h" | |
| 15 #include "base/memory/singleton.h" | |
| 16 #include "base/string16.h" | |
| 17 #include "chrome/common/extensions/url_pattern.h" | |
| 18 #include "chrome/common/extensions/url_pattern_set.h" | |
| 19 | |
| 20 class DictionaryValue; | |
| 21 class Extension; | |
| 22 class ExtensionPermissionsInfo; | |
| 23 class ExtensionPrefs; | |
| 24 class ListValue; | |
| 25 | |
| 26 // When prompting the user to install or approve permissions, we display | |
| 27 // messages describing the effects of the permissions and not the permissions | |
| 28 // themselves. Each ExtensionPermissionMessage represents one of the messages | |
| 29 // shown to the user. | |
| 30 class ExtensionPermissionMessage { | |
| 31 public: | |
| 32 // Do not reorder or add new enumerations in this list. If you need to add a | |
| 33 // new enum, add it just prior to ID_ENUM_BOUNDARY and enter its l10n | |
| 34 // message in kMessageIds. | |
| 35 enum MessageId { | |
| 36 ID_UNKNOWN, | |
| 37 ID_NONE, | |
| 38 ID_BOOKMARKS, | |
| 39 ID_GEOLOCATION, | |
| 40 ID_BROWSING_HISTORY, | |
| 41 ID_TABS, | |
| 42 ID_MANAGEMENT, | |
| 43 ID_DEBUGGER, | |
| 44 ID_HOSTS_1, | |
| 45 ID_HOSTS_2, | |
| 46 ID_HOSTS_3, | |
| 47 ID_HOSTS_4_OR_MORE, | |
| 48 ID_HOSTS_ALL, | |
| 49 ID_FULL_ACCESS, | |
| 50 ID_CLIPBOARD, | |
| 51 ID_ENUM_BOUNDARY | |
| 52 }; | |
| 53 | |
| 54 // Creates the corresponding permission message for a list of hosts. | |
| 55 static ExtensionPermissionMessage CreateFromHostList( | |
| 56 const std::vector<std::string> hosts); | |
| 57 | |
| 58 ExtensionPermissionMessage(MessageId message_id, string16 message_); | |
| 59 | |
| 60 // Gets the id of the permission message, which can be used in UMA | |
| 61 // histograms. | |
| 62 MessageId message_id() const { return message_id_; } | |
| 63 | |
| 64 // Gets a localized message describing this permission. Please note that | |
| 65 // the message will be empty for message types TYPE_NONE and TYPE_UNKNOWN. | |
| 66 const string16& message() const { return message_; } | |
| 67 | |
| 68 // Comparator to work with std::set. | |
| 69 bool operator<(const ExtensionPermissionMessage& that) const { | |
| 70 return message_id_ < that.message_id_; | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 MessageId message_id_; | |
| 75 string16 message_; | |
| 76 }; | |
| 77 | |
| 78 typedef std::vector<ExtensionPermissionMessage> ExtensionPermissionMessages; | |
| 79 | |
| 80 | |
| 81 // The ExtensionAPIPermission is an immutable class that describes a single | |
| 82 // API permission. | |
| 83 class ExtensionAPIPermission { | |
| 84 public: | |
| 85 static const ExtensionAPIPermission& Background(); | |
|
Matt Perry
2011/06/09 22:28:42
Most consumers don't need to access the internal b
| |
| 86 static const ExtensionAPIPermission& Bookmark(); | |
| 87 static const ExtensionAPIPermission& ClipboardRead(); | |
| 88 static const ExtensionAPIPermission& ClipboardWrite(); | |
| 89 static const ExtensionAPIPermission& ContentSettings(); | |
| 90 static const ExtensionAPIPermission& ContextMenus(); | |
| 91 static const ExtensionAPIPermission& Cookie(); | |
| 92 static const ExtensionAPIPermission& ChromePrivate(); | |
| 93 static const ExtensionAPIPermission& ChromeosInfoPrivate(); | |
| 94 static const ExtensionAPIPermission& Debugger(); | |
| 95 static const ExtensionAPIPermission& Experimental(); | |
| 96 static const ExtensionAPIPermission& FileBrowserHandler(); | |
| 97 static const ExtensionAPIPermission& FileBrowserPrivate(); | |
| 98 static const ExtensionAPIPermission& Geolocation(); | |
| 99 static const ExtensionAPIPermission& History(); | |
| 100 static const ExtensionAPIPermission& Idle(); | |
| 101 static const ExtensionAPIPermission& Management(); | |
| 102 static const ExtensionAPIPermission& MediaPlayerPrivate(); | |
| 103 static const ExtensionAPIPermission& Notification(); | |
| 104 static const ExtensionAPIPermission& Proxy(); | |
| 105 static const ExtensionAPIPermission& Tab(); | |
| 106 static const ExtensionAPIPermission& UnlimitedStorage(); | |
| 107 static const ExtensionAPIPermission& WebSocketProxyPrivate(); | |
| 108 static const ExtensionAPIPermission& WebstorePrivate(); | |
| 109 | |
| 110 // Returns the set of all ExtensionAPIPermissions. | |
| 111 static std::set<ExtensionAPIPermission> GetAll(); | |
| 112 | |
| 113 // Gets the permission with the given |name| and returns NULL if none exists. | |
| 114 static ExtensionAPIPermission* GetByName(std::string name); | |
| 115 | |
| 116 // Converts the set of permission names into a set of ExtensionAPIPermissions, | |
| 117 // discarding any permissions with invalid names. | |
| 118 static std::set<ExtensionAPIPermission> GetAllByName( | |
| 119 const std::set<std::string>& permissions_str); | |
| 120 | |
| 121 ~ExtensionAPIPermission(); | |
| 122 | |
| 123 // Gets the total number of API permissions. | |
| 124 static size_t permission_count() { return permission_count_; } | |
| 125 | |
| 126 // Gets the total number of API permissions available to hosted apps. | |
| 127 static size_t hosted_app_permission_count() { | |
| 128 return hosted_app_permission_count_; | |
| 129 } | |
| 130 | |
| 131 // Returns the localized permission message associated with this api. | |
| 132 ExtensionPermissionMessage GetMessage() const; | |
| 133 | |
| 134 // Returns the message id associated with this permission. | |
| 135 ExtensionPermissionMessage::MessageId message_id() const { | |
| 136 return message_id_; | |
| 137 } | |
| 138 | |
| 139 // Returns the name of this permission. | |
| 140 const char* name() const { return name_; } | |
| 141 | |
| 142 // Returns true if this permission can be accessed by hosted apps. | |
| 143 bool is_hosted_app() const { return is_hosted_app_; } | |
| 144 | |
| 145 // Returns true if this permission can only be acquired by COMPONENT | |
| 146 // extensions. | |
| 147 bool is_component_only() const { return is_component_only_; } | |
| 148 | |
| 149 bool operator==(const ExtensionAPIPermission& permission) const; | |
| 150 bool operator<(const ExtensionAPIPermission& permission) const; | |
| 151 | |
| 152 private: | |
| 153 static size_t permission_count_; | |
| 154 static size_t hosted_app_permission_count_; | |
| 155 | |
| 156 explicit ExtensionAPIPermission( | |
| 157 ExtensionPermissionsInfo* info, | |
| 158 const char* name, | |
| 159 bool is_hosted_app, | |
| 160 bool is_component_only, | |
| 161 int l10n_message_id, | |
| 162 ExtensionPermissionMessage::MessageId message_id); | |
| 163 | |
| 164 const char* name_; | |
| 165 bool is_hosted_app_; | |
| 166 bool is_component_only_; | |
| 167 int l10n_message_id_; | |
| 168 ExtensionPermissionMessage::MessageId message_id_; | |
| 169 | |
| 170 friend class ExtensionPermissionsInfo; | |
| 171 }; | |
| 172 | |
| 173 // Singleton that holds the extension permission instances, so that the static | |
| 174 // method accessors (e.g. ExtensionAPIPermission::Background()) can return const | |
| 175 // refs. | |
| 176 class ExtensionPermissionsInfo { | |
| 177 public: | |
| 178 static ExtensionPermissionsInfo* GetInstance(); | |
| 179 ~ExtensionPermissionsInfo(); | |
| 180 | |
| 181 private: | |
| 182 ExtensionPermissionsInfo(); | |
| 183 | |
| 184 // A map from names to api permissions. | |
| 185 typedef std::map<std::string, ExtensionAPIPermission*> PermissionMap; | |
| 186 | |
| 187 PermissionMap permissions_; | |
| 188 | |
| 189 ExtensionAPIPermission background_; | |
| 190 ExtensionAPIPermission bookmark_; | |
| 191 ExtensionAPIPermission clipboard_read_; | |
| 192 ExtensionAPIPermission clipboard_write_; | |
| 193 ExtensionAPIPermission content_settings_; | |
| 194 ExtensionAPIPermission context_menus_; | |
| 195 ExtensionAPIPermission cookie_; | |
| 196 ExtensionAPIPermission chrome_private_; | |
| 197 ExtensionAPIPermission chromeos_info_private_; | |
| 198 ExtensionAPIPermission debugger_; | |
| 199 ExtensionAPIPermission experimental_; | |
| 200 ExtensionAPIPermission file_browser_handler_; | |
| 201 ExtensionAPIPermission file_browser_private_; | |
| 202 ExtensionAPIPermission geolocation_; | |
| 203 ExtensionAPIPermission history_; | |
| 204 ExtensionAPIPermission idle_; | |
| 205 ExtensionAPIPermission management_; | |
| 206 ExtensionAPIPermission media_player_private_; | |
| 207 ExtensionAPIPermission notification_; | |
| 208 ExtensionAPIPermission proxy_; | |
| 209 ExtensionAPIPermission tab_; | |
| 210 ExtensionAPIPermission unlimited_storage_; | |
| 211 ExtensionAPIPermission web_socket_proxy_private_; | |
| 212 ExtensionAPIPermission webstore_private_; | |
| 213 | |
| 214 friend class ExtensionAPIPermission; | |
| 215 friend struct DefaultSingletonTraits<ExtensionPermissionsInfo>; | |
| 216 DISALLOW_COPY_AND_ASSIGN(ExtensionPermissionsInfo); | |
| 217 }; | |
| 218 | |
| 219 // The ExtensionPermissionSet is an immutable class that encapsulates an | |
| 220 // extension's permissions. The class exposes set operations for combining and | |
| 221 // manipulating the permissions. | |
| 222 class ExtensionPermissionSet { | |
| 223 public: | |
| 224 // Creates an empty permission set (e.g. default permissions). | |
| 225 ExtensionPermissionSet(); | |
| 226 | |
| 227 // Creates a new permission set based on the |extension| manifest data, and | |
| 228 // the api and host permissions (|apis| and |hosts|). | |
| 229 ExtensionPermissionSet(Extension* extension, | |
| 230 std::set<ExtensionAPIPermission> apis, | |
| 231 URLPatternList hosts); | |
| 232 | |
| 233 ExtensionPermissionSet(bool full_access, | |
| 234 std::set<ExtensionAPIPermission> apis, | |
| 235 URLPatternSet effective_hosts); | |
| 236 | |
| 237 ~ExtensionPermissionSet(); | |
| 238 | |
| 239 // Creates a new permission set that's the union of |set1| and |set2|. | |
| 240 // Passes ownership of the new set to the caller. | |
| 241 static ExtensionPermissionSet* CreateUnion( | |
| 242 const ExtensionPermissionSet& set1, const ExtensionPermissionSet& set2); | |
| 243 | |
| 244 // Creates a new permission set that's equal to |set1| - |set2|. | |
| 245 // Passes ownership of the new set to the caller. | |
| 246 static ExtensionPermissionSet* CreateDifference( | |
| 247 const ExtensionPermissionSet& set1, const ExtensionPermissionSet& set2); | |
| 248 | |
| 249 // Creates a new permission set that's the intersection of |set1| and |set2|. | |
| 250 // Passes ownership of the new set to the caller. | |
| 251 static ExtensionPermissionSet* CreateIntersection( | |
| 252 const ExtensionPermissionSet& set1, const ExtensionPermissionSet& set2); | |
| 253 | |
| 254 // Returns true if |set| is a subset of this. | |
| 255 bool Contains(const ExtensionPermissionSet& set) const; | |
| 256 | |
| 257 // Gets the API permissions in this set as a set of strings. | |
| 258 std::set<std::string> GetAPIsAsStrings() const; | |
| 259 | |
| 260 // Gets a list of the distinct hosts for displaying to the user. | |
| 261 // NOTE: do not use this for comparing permissions, since this disgards some | |
| 262 // information. | |
| 263 std::vector<std::string> GetDistinctHostsForDisplay() const; | |
| 264 | |
| 265 // Gets the localized permission messages that represent this set. | |
| 266 ExtensionPermissionMessages GetPermissionMessages() const; | |
| 267 | |
| 268 // Gets the localized permission messages that represent this set (represented | |
| 269 // as strings). | |
| 270 std::vector<string16> GetWarningMessages() const; | |
| 271 | |
| 272 // Returns true if this is an empty set (e.g., the default permission set). | |
| 273 bool IsEmpty() const; | |
| 274 | |
| 275 // Returns true if the set has the specified API permission. | |
| 276 bool HasAPIPermission(const ExtensionAPIPermission& permission) const; | |
| 277 | |
| 278 // Returns true if the permissions in this set grant access to the specified | |
| 279 // |function_name|. | |
| 280 bool HasAccessToFunction(const std::string& function_name) const; | |
| 281 | |
| 282 // Returns true if this permission set includes access to |origin|. | |
| 283 bool HasAccessToHost(const GURL& origin) const; | |
| 284 | |
| 285 // Returns true if this permission set includes effective access to all | |
| 286 // origins. | |
| 287 bool HasEffectiveAccessToAllHosts() const; | |
| 288 | |
| 289 // Returns true if this permission set includes permissions that are | |
| 290 // restricted to internal extensions. | |
| 291 bool HasPrivatePermissions() const; | |
| 292 | |
| 293 // Returns true if |permissions| has a greater privilege level than this | |
| 294 // permission set (e.g., this permission set has less permissions). | |
| 295 bool HasLessPrivilegesThan(const ExtensionPermissionSet& permissions); | |
| 296 | |
| 297 const std::set<ExtensionAPIPermission>& apis() const { return apis_; } | |
| 298 | |
| 299 const URLPatternSet& effective_hosts() const { return effective_hosts_; } | |
| 300 | |
| 301 bool native_code() const { return native_code_; } | |
| 302 | |
| 303 private: | |
| 304 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest, | |
| 305 HasLessHostPrivilegesThan); | |
| 306 | |
| 307 static std::vector<std::string> GetDistinctHosts( | |
| 308 const URLPatternList& host_patterns, bool include_rcd); | |
| 309 | |
| 310 void InitFromExtension(Extension* extension, const URLPatternList& hosts); | |
| 311 | |
| 312 string16 GetHostPermissionMessage() const; | |
| 313 | |
| 314 std::set<ExtensionPermissionMessage> GetSimplePermissionMessages() const; | |
| 315 | |
| 316 bool HasLessAPIPrivilegesThan( | |
| 317 const ExtensionPermissionSet& permissions); | |
| 318 | |
| 319 bool HasLessHostPrivilegesThan( | |
| 320 const ExtensionPermissionSet& permissions); | |
| 321 | |
| 322 std::set<ExtensionAPIPermission> apis_; | |
| 323 | |
| 324 URLPatternSet effective_hosts_; | |
| 325 | |
| 326 bool native_code_; | |
| 327 | |
| 328 }; | |
| 329 | |
| 330 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | |
| OLD | NEW |