blob: 9b79f30fa16be4bbf548bad3e2a7f2ae181c2567 [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
8#include <string>
9#include <vector>
10
[email protected]6014d672008-12-05 00:38:2511#include "base/file_path.h"
[email protected]cc655912009-01-29 23:19:1912#include "base/scoped_ptr.h"
[email protected]7713d632008-12-02 07:52:3313#include "base/values.h"
[email protected]cc655912009-01-29 23:19:1914#include "base/version.h"
[email protected]5b1a0e22009-05-26 19:00:5815#include "chrome/common/extensions/user_script.h"
[email protected]e2eb43112009-05-29 21:19:5416#include "chrome/browser/extensions/user_script_master.h"
[email protected]7197f4992009-03-23 05:05:4917#include "chrome/common/extensions/url_pattern.h"
[email protected]f7f3a5f2009-05-01 22:02:3418#include "chrome/common/page_action.h"
[email protected]eab9b452009-01-23 20:48:5919#include "googleurl/src/gurl.h"
20
[email protected]c533bb22009-06-03 19:06:1121// Represents a Chrome extension.
[email protected]7713d632008-12-02 07:52:3322class Extension {
23 public:
[email protected]631cf822009-05-15 07:01:2524 // What an extension was loaded from.
25 enum Location {
26 INVALID,
[email protected]25b34332009-06-05 21:53:1927 INTERNAL, // A crx file from the internal Extensions directory.
28 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
29 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
30 // registry on Windows).
31 LOAD // --load-extension.
32 };
33
34 enum State {
35 DISABLED,
36 ENABLED,
37 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]631cf822009-05-15 07:01:2538 };
[email protected]7713d632008-12-02 07:52:3339
[email protected]c533bb22009-06-03 19:06:1140 // An NPAPI plugin included in the extension.
41 struct PluginInfo {
42 FilePath path; // Path to the plugin.
43 bool is_public; // False if only this extension can load this plugin.
44 };
45
[email protected]6014d672008-12-05 00:38:2546 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3447 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2548
[email protected]7713d632008-12-02 07:52:3349 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0550 static const wchar_t* kContentScriptsKey;
[email protected]7197f4992009-03-23 05:05:4951 static const wchar_t* kCssKey;
[email protected]eab9b452009-01-23 20:48:5952 static const wchar_t* kDescriptionKey;
[email protected]f7f3a5f2009-05-01 22:02:3453 static const wchar_t* kIconPathKey;
[email protected]17122322009-01-22 20:03:3054 static const wchar_t* kIdKey;
[email protected]34aa8dc2009-02-19 07:03:0555 static const wchar_t* kJsKey;
[email protected]eab9b452009-01-23 20:48:5956 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3057 static const wchar_t* kNameKey;
[email protected]f7f3a5f2009-05-01 22:02:3458 static const wchar_t* kPageActionsKey;
[email protected]7197f4992009-03-23 05:05:4959 static const wchar_t* kPermissionsKey;
[email protected]c533bb22009-06-03 19:06:1160 static const wchar_t* kPluginsKey;
61 static const wchar_t* kPluginsPathKey;
62 static const wchar_t* kPluginsPublicKey;
63
[email protected]c64631652009-04-29 22:24:3164 static const wchar_t* kBackgroundKey;
[email protected]7197f4992009-03-23 05:05:4965 static const wchar_t* kRunAtKey;
[email protected]07c00d992009-03-04 20:27:0466 static const wchar_t* kThemeKey;
[email protected]4a190632009-05-09 01:07:4267 static const wchar_t* kThemeImagesKey;
68 static const wchar_t* kThemeColorsKey;
69 static const wchar_t* kThemeTintsKey;
[email protected]7895ea22009-06-02 20:53:5070 static const wchar_t* kThemeDisplayPropertiesKey;
[email protected]d5cef1b2009-03-21 05:36:4971 static const wchar_t* kToolstripsKey;
[email protected]f7f3a5f2009-05-01 22:02:3472 static const wchar_t* kTooltipKey;
73 static const wchar_t* kTypeKey;
[email protected]7197f4992009-03-23 05:05:4974 static const wchar_t* kVersionKey;
75 static const wchar_t* kZipHashKey;
[email protected]7713d632008-12-02 07:52:3376
[email protected]0afe8272009-02-14 04:15:1677 // Some values expected in manifests.
78 static const char* kRunAtDocumentStartValue;
79 static const char* kRunAtDocumentEndValue;
[email protected]f7f3a5f2009-05-01 22:02:3480 static const char* kPageActionTypeTab;
81 static const char* kPageActionTypePermanent;
[email protected]0afe8272009-02-14 04:15:1682
[email protected]7713d632008-12-02 07:52:3383 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0584 static const char* kInvalidContentScriptError;
85 static const char* kInvalidContentScriptsListError;
[email protected]3cfbd0e2009-03-18 21:26:2486 static const char* kInvalidCssError;
87 static const char* kInvalidCssListError;
[email protected]17122322009-01-22 20:03:3088 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5989 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0590 static const char* kInvalidJsError;
91 static const char* kInvalidJsListError;
[email protected]eab9b452009-01-23 20:48:5992 static const char* kInvalidManifestError;
93 static const char* kInvalidMatchCountError;
94 static const char* kInvalidMatchError;
95 static const char* kInvalidMatchesError;
96 static const char* kInvalidNameError;
[email protected]c533bb22009-06-03 19:06:1197 static const char* kInvalidPluginsError;
98 static const char* kInvalidPluginsPathError;
99 static const char* kInvalidPluginsPublicError;
100
[email protected]c64631652009-04-29 22:24:31101 static const char* kInvalidBackgroundError;
[email protected]0afe8272009-02-14 04:15:16102 static const char* kInvalidRunAtError;
[email protected]3cfbd0e2009-03-18 21:26:24103 static const char* kInvalidToolstripError;
[email protected]d5cef1b2009-03-21 05:36:49104 static const char* kInvalidToolstripsError;
[email protected]17122322009-01-22 20:03:30105 static const char* kInvalidVersionError;
[email protected]f7f3a5f2009-05-01 22:02:34106 static const char* kInvalidPageActionError;
107 static const char* kInvalidPageActionsListError;
108 static const char* kInvalidPageActionIconPathError;
109 static const char* kInvalidPageActionTooltipError;
110 static const char* kInvalidPageActionTypeValueError;
[email protected]7197f4992009-03-23 05:05:49111 static const char* kInvalidPermissionsError;
112 static const char* kInvalidPermissionCountWarning;
113 static const char* kInvalidPermissionError;
114 static const char* kInvalidPermissionSchemeError;
[email protected]cc655912009-01-29 23:19:19115 static const char* kInvalidZipHashError;
[email protected]4a190632009-05-09 01:07:42116 static const char* kInvalidThemeError;
117 static const char* kInvalidThemeImagesMissingError;
118 static const char* kInvalidThemeImagesError;
119 static const char* kInvalidThemeColorsError;
120 static const char* kInvalidThemeTintsError;
[email protected]e2eb43112009-05-29 21:19:54121 static const char* kThemesCannotContainExtensionsError;
[email protected]3cfbd0e2009-03-18 21:26:24122 static const char* kMissingFileError;
[email protected]7713d632008-12-02 07:52:33123
[email protected]25b34332009-06-05 21:53:19124#if defined(OS_WIN)
125 static const char* kExtensionRegistryPath;
126#endif
127
[email protected]37eeb5a2009-02-26 23:36:17128 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48129 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17130
[email protected]e2eb43112009-05-29 21:19:54131 Extension() : location_(INVALID), is_theme_(false) {}
[email protected]631cf822009-05-15 07:01:25132 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25133 virtual ~Extension();
134
[email protected]25b34332009-06-05 21:53:19135 // Checks to see if the extension has a valid ID.
136 static bool IdIsValid(const std::string& id);
137
138 // Whether the |location| is external or not.
139 static inline bool IsExternalLocation(Location location) {
140 return location == Extension::EXTERNAL_PREF ||
141 location == Extension::EXTERNAL_REGISTRY;
142 }
143
[email protected]07c00d992009-03-04 20:27:04144 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59145 // |extension_url| argument should be the url() from an Extension object. The
146 // |relative_path| can be untrusted user input. The returned URL will either
147 // be invalid() or a child of |extension_url|.
148 // NOTE: Static so that it can be used from multiple threads.
149 static GURL GetResourceURL(const GURL& extension_url,
150 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24151 GURL GetResourceURL(const std::string& relative_path) {
152 return GetResourceURL(url(), relative_path);
153 }
[email protected]eab9b452009-01-23 20:48:59154
[email protected]07c00d992009-03-04 20:27:04155 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59156 // |extension_path| argument should be the path() from an Extension object.
157 // The |relative_path| can be untrusted user input. The returned path will
158 // either be empty or a child of extension_path.
159 // NOTE: Static so that it can be used from multiple threads.
160 static FilePath GetResourcePath(const FilePath& extension_path,
161 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24162 FilePath GetResourcePath(const std::string& relative_path) {
163 return GetResourcePath(path(), relative_path);
164 }
[email protected]eab9b452009-01-23 20:48:59165
[email protected]4a8d3272009-03-10 19:15:08166 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30167 // If |require_id| is true, will return an error if the "id" key is missing
168 // from the value.
169 bool InitFromValue(const DictionaryValue& value, bool require_id,
170 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08171
[email protected]82891262008-12-24 00:21:26172 const FilePath& path() const { return path_; }
[email protected]5bfb1eb0a2009-04-08 18:33:30173 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25174 const Location location() const { return location_; }
175 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08176 const std::string& id() const { return id_; }
177 const Version* version() const { return version_.get(); }
178 // String representation of the version number.
179 const std::string VersionString() const;
180 const std::string& name() const { return name_; }
181 const std::string& description() const { return description_; }
182 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]f7f3a5f2009-05-01 22:02:34183 const PageActionMap& page_actions() const { return page_actions_; }
[email protected]c533bb22009-06-03 19:06:11184 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]c64631652009-04-29 22:24:31185 const GURL& background_url() const { return background_url_; }
[email protected]d5cef1b2009-03-21 05:36:49186 const std::vector<std::string>& toolstrips() const { return toolstrips_; }
[email protected]c533bb22009-06-03 19:06:11187 const std::vector<URLPattern>& permissions() const { return permissions_; }
[email protected]4a8d3272009-03-10 19:15:08188
[email protected]631cf822009-05-15 07:01:25189 // Retrieves a page action by |id|.
190 const PageAction* GetPageAction(std::string id) const;
191
[email protected]25b34332009-06-05 21:53:19192 // Returns the origin of this extension. This function takes a |registry_path|
193 // so that the registry location can be overwritten during testing.
194 Location ExternalExtensionInstallType(std::string registry_path);
195
196 // Theme-related.
[email protected]631cf822009-05-15 07:01:25197 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
198 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
199 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
[email protected]7895ea22009-06-02 20:53:50200 DictionaryValue* GetThemeDisplayProperties() const {
201 return theme_display_properties_.get();
202 }
[email protected]631cf822009-05-15 07:01:25203 bool IsTheme() { return is_theme_; }
204
[email protected]4a8d3272009-03-10 19:15:08205 private:
[email protected]3cfbd0e2009-03-18 21:26:24206 // Helper method that loads a UserScript object from a
207 // dictionary in the content_script list of the manifest.
208 bool LoadUserScriptHelper(const DictionaryValue* content_script,
209 int definition_index,
210 std::string* error,
211 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34212
213 // Helper method that loads a PageAction object from a
214 // dictionary in the page_action list of the manifest.
[email protected]ce5c4502009-05-06 16:46:11215 PageAction* LoadPageActionHelper(const DictionaryValue* page_action,
216 int definition_index,
217 std::string* error);
[email protected]f7f3a5f2009-05-01 22:02:34218
[email protected]e2eb43112009-05-29 21:19:54219 // Figures out if a source contains keys not associated with themes - we
220 // don't want to allow scripts and such to be bundled with themes.
221 bool ContainsNonThemeKeys(const DictionaryValue& source);
222
[email protected]4a8d3272009-03-10 19:15:08223 // The absolute path to the directory the extension is stored in.
224 FilePath path_;
225
226 // The base extension url for the extension.
227 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59228
[email protected]631cf822009-05-15 07:01:25229 // The location the extension was loaded from.
230 Location location_;
231
[email protected]7713d632008-12-02 07:52:33232 // A human-readable ID for the extension. The convention is to use something
233 // like 'com.example.myextension', but this is not currently enforced. An
234 // extension's ID is used in things like directory structures and URLs, and
235 // is expected to not change across versions. In the case of conflicts,
236 // updates will only be allowed if the extension can be validated using the
237 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23238 std::string id_;
[email protected]82891262008-12-24 00:21:26239
[email protected]64a02b802009-01-12 19:36:42240 // The extension's version.
[email protected]cc655912009-01-29 23:19:19241 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42242
[email protected]82891262008-12-24 00:21:26243 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23244 std::string name_;
[email protected]82891262008-12-24 00:21:26245
[email protected]4a8d3272009-03-10 19:15:08246 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23247 std::string description_;
[email protected]82891262008-12-24 00:21:26248
249 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05250 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33251
[email protected]f7f3a5f2009-05-01 22:02:34252 // A list of page actions.
253 PageActionMap page_actions_;
254
[email protected]c533bb22009-06-03 19:06:11255 // Optional list of NPAPI plugins and associated properties.
256 std::vector<PluginInfo> plugins_;
[email protected]367230c52009-02-21 01:44:30257
[email protected]c64631652009-04-29 22:24:31258 // Optional URL to a master page of which a single instance should be always
259 // loaded in the background.
260 GURL background_url_;
261
[email protected]d5cef1b2009-03-21 05:36:49262 // Paths to HTML files to be displayed in the toolbar.
263 std::vector<std::string> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08264
[email protected]cc655912009-01-29 23:19:19265 // A SHA1 hash of the contents of the zip file. Note that this key is only
266 // present in the manifest that's prepended to the zip. The inner manifest
267 // will not have this key.
268 std::string zip_hash_;
269
[email protected]07c00d992009-03-04 20:27:04270 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07271 scoped_ptr<DictionaryValue> theme_images_;
[email protected]4a190632009-05-09 01:07:42272
273 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07274 scoped_ptr<DictionaryValue> theme_colors_;
[email protected]4a190632009-05-09 01:07:42275
276 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07277 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42278
[email protected]7895ea22009-06-02 20:53:50279 // A map of display properties.
280 scoped_ptr<DictionaryValue> theme_display_properties_;
281
[email protected]4a190632009-05-09 01:07:42282 // Whether the extension is a theme - if it is, certain things are disabled.
283 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04284
[email protected]c64631652009-04-29 22:24:31285 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]7197f4992009-03-23 05:05:49286 std::vector<URLPattern> permissions_;
287
[email protected]894bb502009-05-21 22:39:57288 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33289};
290
[email protected]5b1a0e22009-05-26 19:00:58291#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_