blob: afd62c1c3b89e33d460ebfd694db837565cb8969 [file] [log] [blame]
[email protected]f7f3a5f2009-05-01 22:02:341// Copyright (c) 2009 The Chromium Authors. All rights reserved.
[email protected]7713d632008-12-02 07:52:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5b1a0e22009-05-26 19:00:585#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_H_
6#define CHROME_COMMON_EXTENSIONS_EXTENSION_H_
[email protected]7713d632008-12-02 07:52:337
[email protected]facd7a7652009-06-05 23:15:028#include <set>
[email protected]7713d632008-12-02 07:52:339#include <string>
10#include <vector>
11
[email protected]6014d672008-12-05 00:38:2512#include "base/file_path.h"
[email protected]cc655912009-01-29 23:19:1913#include "base/scoped_ptr.h"
[email protected]7713d632008-12-02 07:52:3314#include "base/values.h"
[email protected]cc655912009-01-29 23:19:1915#include "base/version.h"
[email protected]5b1a0e22009-05-26 19:00:5816#include "chrome/common/extensions/user_script.h"
[email protected]e2eb43112009-05-29 21:19:5417#include "chrome/browser/extensions/user_script_master.h"
[email protected]7197f4992009-03-23 05:05:4918#include "chrome/common/extensions/url_pattern.h"
[email protected]f7f3a5f2009-05-01 22:02:3419#include "chrome/common/page_action.h"
[email protected]eab9b452009-01-23 20:48:5920#include "googleurl/src/gurl.h"
21
[email protected]c533bb22009-06-03 19:06:1122// Represents a Chrome extension.
[email protected]7713d632008-12-02 07:52:3323class Extension {
24 public:
[email protected]631cf822009-05-15 07:01:2525 // What an extension was loaded from.
26 enum Location {
27 INVALID,
[email protected]25b34332009-06-05 21:53:1928 INTERNAL, // A crx file from the internal Extensions directory.
29 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
30 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
31 // registry on Windows).
32 LOAD // --load-extension.
33 };
34
35 enum State {
[email protected]0c6da502009-08-14 22:32:3936 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1937 ENABLED,
38 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3939
40 NUM_STATES
[email protected]631cf822009-05-15 07:01:2541 };
[email protected]7713d632008-12-02 07:52:3342
[email protected]fbcc40302009-06-12 20:45:4543 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3744 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4545 DOWNGRADE,
46 REINSTALL,
47 UPGRADE,
48 NEW_INSTALL
49 };
50
[email protected]d2817012009-08-04 06:46:2151 // NOTE: If you change this list, you should also change kIconSizes in the cc
52 // file.
53 enum Icons {
54 EXTENSION_ICON_LARGE = 128,
55 EXTENSION_ICON_MEDIUM = 48,
56 EXTENSION_ICON_SMALL = 32,
57 EXTENSION_ICON_BITTY = 16,
58 };
59
[email protected]c3e3def742009-07-17 07:51:0660 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:2161 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:0662
[email protected]35506352009-08-07 18:58:1963 // Each permission is a module that the extension is permitted to use.
64 static const char* kPermissionNames[];
65 static const size_t kNumPermissions;
66
[email protected]abaccb22009-08-12 21:50:4267 // A classification of how dangerous an extension can be, based on what it has
68 // access to.
69 enum PermissionClass {
70 PERMISSION_CLASS_LOW = 0, // green
71 PERMISSION_CLASS_MEDIUM, // yellow
72 PERMISSION_CLASS_HIGH, // orange
73 PERMISSION_CLASS_FULL, // red
74 };
75
[email protected]d6336a92009-08-13 17:25:1276 struct PrivacyBlacklistInfo {
77 FilePath path; // Path to the plain-text blacklist.
78 };
79
[email protected]c533bb22009-06-03 19:06:1180 // An NPAPI plugin included in the extension.
81 struct PluginInfo {
82 FilePath path; // Path to the plugin.
83 bool is_public; // False if only this extension can load this plugin.
84 };
85
[email protected]bbc945542009-07-26 00:11:4286 // A toolstrip and its associated mole.
87 struct ToolstripInfo {
88 ToolstripInfo() : mole_height(0) {}
89
90 GURL toolstrip;
91 GURL mole;
92 int mole_height;
93 };
94
[email protected]6014d672008-12-05 00:38:2595 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3496 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2597
[email protected]25b34332009-06-05 21:53:1998#if defined(OS_WIN)
99 static const char* kExtensionRegistryPath;
100#endif
101
[email protected]37eeb5a2009-02-26 23:36:17102 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48103 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17104
[email protected]e435d6b72009-07-25 03:15:58105 // The mimetype used for extensions.
106 static const char kMimeType[];
107
[email protected]e95ad332009-08-03 19:44:25108 Extension()
109 : location_(INVALID), is_theme_(false), background_page_ready_(false) {}
[email protected]631cf822009-05-15 07:01:25110 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25111 virtual ~Extension();
112
[email protected]fbcc40302009-06-12 20:45:45113 // Resets the id counter. This is only useful for unit tests.
114 static void ResetGeneratedIdCounter() {
115 id_counter_ = 0;
116 }
117
[email protected]25b34332009-06-05 21:53:19118 // Checks to see if the extension has a valid ID.
119 static bool IdIsValid(const std::string& id);
120
[email protected]e435d6b72009-07-25 03:15:58121 // Returns true if the specified file is an extension.
122 static bool IsExtension(const FilePath& file_name);
123
[email protected]25b34332009-06-05 21:53:19124 // Whether the |location| is external or not.
125 static inline bool IsExternalLocation(Location location) {
126 return location == Extension::EXTERNAL_PREF ||
127 location == Extension::EXTERNAL_REGISTRY;
128 }
129
[email protected]07c00d992009-03-04 20:27:04130 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59131 // |extension_url| argument should be the url() from an Extension object. The
132 // |relative_path| can be untrusted user input. The returned URL will either
133 // be invalid() or a child of |extension_url|.
134 // NOTE: Static so that it can be used from multiple threads.
135 static GURL GetResourceURL(const GURL& extension_url,
136 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24137 GURL GetResourceURL(const std::string& relative_path) {
138 return GetResourceURL(url(), relative_path);
139 }
[email protected]eab9b452009-01-23 20:48:59140
[email protected]07c00d992009-03-04 20:27:04141 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59142 // |extension_path| argument should be the path() from an Extension object.
143 // The |relative_path| can be untrusted user input. The returned path will
144 // either be empty or a child of extension_path.
145 // NOTE: Static so that it can be used from multiple threads.
146 static FilePath GetResourcePath(const FilePath& extension_path,
147 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24148 FilePath GetResourcePath(const std::string& relative_path) {
149 return GetResourcePath(path(), relative_path);
150 }
[email protected]eab9b452009-01-23 20:48:59151
[email protected]a17f9462009-06-09 02:56:41152 // |input| is expected to be the text of an rsa public or private key. It
153 // tolerates the presence or absence of bracking header/footer like this:
154 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
155 // and may contain newlines.
156 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
157
158 // Does a simple base64 encoding of |input| into |output|.
159 static bool ProducePEM(const std::string& input, std::string* output);
160
[email protected]fbcc40302009-06-12 20:45:45161 // Note: The result is coverted to lower-case because the browser enforces
162 // hosts to be lower-case in omni-bar.
163 static bool GenerateIdFromPublicKey(const std::string& input,
164 std::string* output);
165
[email protected]a17f9462009-06-09 02:56:41166 // Expects base64 encoded |input| and formats into |output| including
167 // the appropriate header & footer.
168 static bool FormatPEMForFileOutput(const std::string input,
169 std::string* output, bool is_public);
170
[email protected]4a8d3272009-03-10 19:15:08171 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30172 // If |require_id| is true, will return an error if the "id" key is missing
173 // from the value.
174 bool InitFromValue(const DictionaryValue& value, bool require_id,
175 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08176
[email protected]82891262008-12-24 00:21:26177 const FilePath& path() const { return path_; }
[email protected]af1277b2009-07-28 00:47:53178 void set_path(const FilePath& path) { path_ = path; }
[email protected]5bfb1eb0a2009-04-08 18:33:30179 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25180 const Location location() const { return location_; }
181 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08182 const std::string& id() const { return id_; }
183 const Version* version() const { return version_.get(); }
184 // String representation of the version number.
185 const std::string VersionString() const;
186 const std::string& name() const { return name_; }
[email protected]fbcc40302009-06-12 20:45:45187 const std::string& public_key() const { return public_key_; }
[email protected]4a8d3272009-03-10 19:15:08188 const std::string& description() const { return description_; }
189 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]f7f3a5f2009-05-01 22:02:34190 const PageActionMap& page_actions() const { return page_actions_; }
[email protected]d6336a92009-08-13 17:25:12191 const std::vector<PrivacyBlacklistInfo>& privacy_blacklists() const {
192 return privacy_blacklists_;
193 }
[email protected]c533bb22009-06-03 19:06:11194 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]c64631652009-04-29 22:24:31195 const GURL& background_url() const { return background_url_; }
[email protected]bbc945542009-07-26 00:11:42196 const std::vector<ToolstripInfo>& toolstrips() const { return toolstrips_; }
[email protected]35506352009-08-07 18:58:19197 const std::vector<URLPattern>& host_permissions() const {
198 return host_permissions_;
199 }
200 const std::vector<std::string>& api_permissions() const {
201 return api_permissions_;
202 }
[email protected]b29682ba22009-06-18 19:53:56203 const GURL& update_url() const { return update_url_; }
[email protected]c3e3def742009-07-17 07:51:06204 const std::map<int, std::string>& icons() { return icons_; }
[email protected]4a8d3272009-03-10 19:15:08205
[email protected]631cf822009-05-15 07:01:25206 // Retrieves a page action by |id|.
207 const PageAction* GetPageAction(std::string id) const;
208
[email protected]25b34332009-06-05 21:53:19209 // Returns the origin of this extension. This function takes a |registry_path|
210 // so that the registry location can be overwritten during testing.
211 Location ExternalExtensionInstallType(std::string registry_path);
212
213 // Theme-related.
[email protected]631cf822009-05-15 07:01:25214 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
215 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
216 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
[email protected]7895ea22009-06-02 20:53:50217 DictionaryValue* GetThemeDisplayProperties() const {
218 return theme_display_properties_.get();
219 }
[email protected]631cf822009-05-15 07:01:25220 bool IsTheme() { return is_theme_; }
221
[email protected]facd7a7652009-06-05 23:15:02222 // Returns a list of paths (relative to the extension dir) for images that
223 // the browser might load (like themes and page action icons).
224 std::set<FilePath> GetBrowserImages();
225
[email protected]866930682009-08-18 22:53:47226 // Calculates and returns the permission class this extension is in.
[email protected]abaccb22009-08-12 21:50:42227 PermissionClass GetPermissionClass();
228
[email protected]866930682009-08-18 22:53:47229 // Returns an absolute path to the given icon inside of the extension. Returns
230 // an empty FilePath if the extension does not have that icon.
231 FilePath GetIconPath(Icons icon);
232
[email protected]e95ad332009-08-03 19:44:25233
234 // Runtime data:
235 // Put dynamic data about the state of a running extension below.
236
237 // Whether the background page, if any, is ready. We don't load other
238 // components until then. If there is no background page, we consider it to
239 // be ready.
240 bool GetBackgroundPageReady();
241 void SetBackgroundPageReady();
242
[email protected]4a8d3272009-03-10 19:15:08243 private:
[email protected]b55530c2009-06-17 19:07:03244 // Counter used to assign ids to extensions that are loaded using
[email protected]fbcc40302009-06-12 20:45:45245 // --load-extension.
246 static int id_counter_;
247
248 // Returns the next counter id. Intentionally post-incrementing so that first
249 // value is 0.
250 static int NextGeneratedId() { return id_counter_++; }
251
[email protected]3cfbd0e2009-03-18 21:26:24252 // Helper method that loads a UserScript object from a
253 // dictionary in the content_script list of the manifest.
254 bool LoadUserScriptHelper(const DictionaryValue* content_script,
255 int definition_index,
256 std::string* error,
257 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34258
259 // Helper method that loads a PageAction object from a
260 // dictionary in the page_action list of the manifest.
[email protected]ce5c4502009-05-06 16:46:11261 PageAction* LoadPageActionHelper(const DictionaryValue* page_action,
262 int definition_index,
263 std::string* error);
[email protected]f7f3a5f2009-05-01 22:02:34264
[email protected]e2eb43112009-05-29 21:19:54265 // Figures out if a source contains keys not associated with themes - we
266 // don't want to allow scripts and such to be bundled with themes.
267 bool ContainsNonThemeKeys(const DictionaryValue& source);
268
[email protected]4a8d3272009-03-10 19:15:08269 // The absolute path to the directory the extension is stored in.
270 FilePath path_;
271
272 // The base extension url for the extension.
273 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59274
[email protected]631cf822009-05-15 07:01:25275 // The location the extension was loaded from.
276 Location location_;
277
[email protected]7713d632008-12-02 07:52:33278 // A human-readable ID for the extension. The convention is to use something
279 // like 'com.example.myextension', but this is not currently enforced. An
280 // extension's ID is used in things like directory structures and URLs, and
281 // is expected to not change across versions. In the case of conflicts,
282 // updates will only be allowed if the extension can be validated using the
283 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23284 std::string id_;
[email protected]82891262008-12-24 00:21:26285
[email protected]64a02b802009-01-12 19:36:42286 // The extension's version.
[email protected]cc655912009-01-29 23:19:19287 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42288
[email protected]82891262008-12-24 00:21:26289 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23290 std::string name_;
[email protected]82891262008-12-24 00:21:26291
[email protected]4a8d3272009-03-10 19:15:08292 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23293 std::string description_;
[email protected]82891262008-12-24 00:21:26294
295 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05296 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33297
[email protected]f7f3a5f2009-05-01 22:02:34298 // A list of page actions.
299 PageActionMap page_actions_;
300
[email protected]d6336a92009-08-13 17:25:12301 // Optional list of privacy blacklistrom.
302 std::vector<PrivacyBlacklistInfo> privacy_blacklists_;
303
[email protected]c533bb22009-06-03 19:06:11304 // Optional list of NPAPI plugins and associated properties.
305 std::vector<PluginInfo> plugins_;
[email protected]367230c52009-02-21 01:44:30306
[email protected]c64631652009-04-29 22:24:31307 // Optional URL to a master page of which a single instance should be always
308 // loaded in the background.
309 GURL background_url_;
310
[email protected]bbc945542009-07-26 00:11:42311 // Optional list of toolstrips_ and associated properties.
312 std::vector<ToolstripInfo> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08313
[email protected]fbcc40302009-06-12 20:45:45314 // The public key ('key' in the manifest) used to sign the contents of the
315 // crx package ('signature' in the manifest)
316 std::string public_key_;
[email protected]cc655912009-01-29 23:19:19317
[email protected]07c00d992009-03-04 20:27:04318 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07319 scoped_ptr<DictionaryValue> theme_images_;
[email protected]4a190632009-05-09 01:07:42320
321 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07322 scoped_ptr<DictionaryValue> theme_colors_;
[email protected]4a190632009-05-09 01:07:42323
324 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07325 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42326
[email protected]7895ea22009-06-02 20:53:50327 // A map of display properties.
328 scoped_ptr<DictionaryValue> theme_display_properties_;
329
[email protected]4a190632009-05-09 01:07:42330 // Whether the extension is a theme - if it is, certain things are disabled.
331 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04332
[email protected]35506352009-08-07 18:58:19333 // The set of module-level APIs this extension can use.
334 std::vector<std::string> api_permissions_;
335
[email protected]c64631652009-04-29 22:24:31336 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]35506352009-08-07 18:58:19337 std::vector<URLPattern> host_permissions_;
[email protected]7197f4992009-03-23 05:05:49338
[email protected]c3e3def742009-07-17 07:51:06339 // The paths to the icons the extension contains mapped by their width.
340 std::map<int, std::string> icons_;
341
[email protected]b29682ba22009-06-18 19:53:56342 // URL for fetching an update manifest
343 GURL update_url_;
[email protected]d6336a92009-08-13 17:25:12344
[email protected]e95ad332009-08-03 19:44:25345
346 // Runtime data:
347
348 // True if the background page is ready.
349 bool background_page_ready_;
[email protected]b29682ba22009-06-18 19:53:56350
[email protected]ae7fe712009-07-02 20:33:58351 FRIEND_TEST(ExtensionTest, LoadPageActionHelper);
352
[email protected]894bb502009-05-21 22:39:57353 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33354};
355
[email protected]5b1a0e22009-05-26 19:00:58356#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_