blob: 0160abd251a523b4eb5b6023ef7b4d9f52b23a0f [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]6014d672008-12-05 00:38:255#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_H_
[email protected]7713d632008-12-02 07:52:337
8#include <string>
9#include <vector>
[email protected]7197f4992009-03-23 05:05:4910#include <map>
[email protected]7713d632008-12-02 07:52:3311
[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/string16.h"
15#include "base/values.h"
[email protected]cc655912009-01-29 23:19:1916#include "base/version.h"
[email protected]eab9b452009-01-23 20:48:5917#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"
[email protected]c43c6682009-05-19 14:51:4421#include "third_party/skia/include/core/SkColor.h"
[email protected]eab9b452009-01-23 20:48:5922
[email protected]7713d632008-12-02 07:52:3323// Represents a Chromium extension.
24class Extension {
25 public:
[email protected]631cf822009-05-15 07:01:2526 // What an extension was loaded from.
27 enum Location {
28 INVALID,
[email protected]d24070e22009-05-21 19:26:5929 INTERNAL, // A crx file from the internal Extensions directory.
[email protected]631cf822009-05-15 07:01:2530 EXTERNAL, // A crx file from an external directory (via eg the registry
[email protected]d24070e22009-05-21 19:26:5931 // on Windows).
32 LOAD // --load-extension.
[email protected]631cf822009-05-15 07:01:2533 };
[email protected]7713d632008-12-02 07:52:3334
[email protected]6014d672008-12-05 00:38:2535 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3436 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2537
[email protected]7713d632008-12-02 07:52:3338 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0539 static const wchar_t* kContentScriptsKey;
[email protected]7197f4992009-03-23 05:05:4940 static const wchar_t* kCssKey;
[email protected]eab9b452009-01-23 20:48:5941 static const wchar_t* kDescriptionKey;
[email protected]f7f3a5f2009-05-01 22:02:3442 static const wchar_t* kIconPathKey;
[email protected]17122322009-01-22 20:03:3043 static const wchar_t* kIdKey;
[email protected]34aa8dc2009-02-19 07:03:0544 static const wchar_t* kJsKey;
[email protected]eab9b452009-01-23 20:48:5945 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3046 static const wchar_t* kNameKey;
[email protected]f7f3a5f2009-05-01 22:02:3447 static const wchar_t* kPageActionsKey;
[email protected]7197f4992009-03-23 05:05:4948 static const wchar_t* kPermissionsKey;
[email protected]367230c52009-02-21 01:44:3049 static const wchar_t* kPluginsDirKey;
[email protected]c64631652009-04-29 22:24:3150 static const wchar_t* kBackgroundKey;
[email protected]7197f4992009-03-23 05:05:4951 static const wchar_t* kRunAtKey;
[email protected]07c00d992009-03-04 20:27:0452 static const wchar_t* kThemeKey;
[email protected]4a190632009-05-09 01:07:4253 static const wchar_t* kThemeImagesKey;
54 static const wchar_t* kThemeColorsKey;
55 static const wchar_t* kThemeTintsKey;
[email protected]d5cef1b2009-03-21 05:36:4956 static const wchar_t* kToolstripsKey;
[email protected]f7f3a5f2009-05-01 22:02:3457 static const wchar_t* kTooltipKey;
58 static const wchar_t* kTypeKey;
[email protected]7197f4992009-03-23 05:05:4959 static const wchar_t* kVersionKey;
60 static const wchar_t* kZipHashKey;
[email protected]7713d632008-12-02 07:52:3361
[email protected]0afe8272009-02-14 04:15:1662 // Some values expected in manifests.
63 static const char* kRunAtDocumentStartValue;
64 static const char* kRunAtDocumentEndValue;
[email protected]f7f3a5f2009-05-01 22:02:3465 static const char* kPageActionTypeTab;
66 static const char* kPageActionTypePermanent;
[email protected]0afe8272009-02-14 04:15:1667
[email protected]7713d632008-12-02 07:52:3368 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0569 static const char* kInvalidContentScriptError;
70 static const char* kInvalidContentScriptsListError;
[email protected]3cfbd0e2009-03-18 21:26:2471 static const char* kInvalidCssError;
72 static const char* kInvalidCssListError;
[email protected]17122322009-01-22 20:03:3073 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5974 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0575 static const char* kInvalidJsError;
76 static const char* kInvalidJsListError;
[email protected]eab9b452009-01-23 20:48:5977 static const char* kInvalidManifestError;
78 static const char* kInvalidMatchCountError;
79 static const char* kInvalidMatchError;
80 static const char* kInvalidMatchesError;
81 static const char* kInvalidNameError;
[email protected]3cfbd0e2009-03-18 21:26:2482 static const char* kInvalidPluginsDirError;
[email protected]c64631652009-04-29 22:24:3183 static const char* kInvalidBackgroundError;
[email protected]0afe8272009-02-14 04:15:1684 static const char* kInvalidRunAtError;
[email protected]3cfbd0e2009-03-18 21:26:2485 static const char* kInvalidToolstripError;
[email protected]d5cef1b2009-03-21 05:36:4986 static const char* kInvalidToolstripsError;
[email protected]17122322009-01-22 20:03:3087 static const char* kInvalidVersionError;
[email protected]f7f3a5f2009-05-01 22:02:3488 static const char* kInvalidPageActionError;
89 static const char* kInvalidPageActionsListError;
90 static const char* kInvalidPageActionIconPathError;
91 static const char* kInvalidPageActionTooltipError;
92 static const char* kInvalidPageActionTypeValueError;
[email protected]7197f4992009-03-23 05:05:4993 static const char* kInvalidPermissionsError;
94 static const char* kInvalidPermissionCountWarning;
95 static const char* kInvalidPermissionError;
96 static const char* kInvalidPermissionSchemeError;
[email protected]cc655912009-01-29 23:19:1997 static const char* kInvalidZipHashError;
[email protected]4a190632009-05-09 01:07:4298 static const char* kInvalidThemeError;
99 static const char* kInvalidThemeImagesMissingError;
100 static const char* kInvalidThemeImagesError;
101 static const char* kInvalidThemeColorsError;
102 static const char* kInvalidThemeTintsError;
[email protected]3cfbd0e2009-03-18 21:26:24103 static const char* kMissingFileError;
[email protected]7713d632008-12-02 07:52:33104
[email protected]37eeb5a2009-02-26 23:36:17105 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48106 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17107
[email protected]631cf822009-05-15 07:01:25108 Extension() : location_(INVALID) {}
109 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25110 virtual ~Extension();
111
[email protected]07c00d992009-03-04 20:27:04112 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59113 // |extension_url| argument should be the url() from an Extension object. The
114 // |relative_path| can be untrusted user input. The returned URL will either
115 // be invalid() or a child of |extension_url|.
116 // NOTE: Static so that it can be used from multiple threads.
117 static GURL GetResourceURL(const GURL& extension_url,
118 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24119 GURL GetResourceURL(const std::string& relative_path) {
120 return GetResourceURL(url(), relative_path);
121 }
[email protected]eab9b452009-01-23 20:48:59122
[email protected]07c00d992009-03-04 20:27:04123 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59124 // |extension_path| argument should be the path() from an Extension object.
125 // The |relative_path| can be untrusted user input. The returned path will
126 // either be empty or a child of extension_path.
127 // NOTE: Static so that it can be used from multiple threads.
128 static FilePath GetResourcePath(const FilePath& extension_path,
129 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24130 FilePath GetResourcePath(const std::string& relative_path) {
131 return GetResourcePath(path(), relative_path);
132 }
[email protected]eab9b452009-01-23 20:48:59133
[email protected]4a8d3272009-03-10 19:15:08134 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30135 // If |require_id| is true, will return an error if the "id" key is missing
136 // from the value.
137 bool InitFromValue(const DictionaryValue& value, bool require_id,
138 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08139
[email protected]82891262008-12-24 00:21:26140 const FilePath& path() const { return path_; }
[email protected]5bfb1eb0a2009-04-08 18:33:30141 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25142 const Location location() const { return location_; }
143 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08144 const std::string& id() const { return id_; }
145 const Version* version() const { return version_.get(); }
146 // String representation of the version number.
147 const std::string VersionString() const;
148 const std::string& name() const { return name_; }
149 const std::string& description() const { return description_; }
150 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]f7f3a5f2009-05-01 22:02:34151 const PageActionMap& page_actions() const { return page_actions_; }
[email protected]4a8d3272009-03-10 19:15:08152 const FilePath& plugins_dir() const { return plugins_dir_; }
[email protected]c64631652009-04-29 22:24:31153 const GURL& background_url() const { return background_url_; }
[email protected]d5cef1b2009-03-21 05:36:49154 const std::vector<std::string>& toolstrips() const { return toolstrips_; }
[email protected]7197f4992009-03-23 05:05:49155 const std::vector<URLPattern>& permissions() const {
156 return permissions_; }
[email protected]4a8d3272009-03-10 19:15:08157
[email protected]631cf822009-05-15 07:01:25158 // Retrieves a page action by |id|.
159 const PageAction* GetPageAction(std::string id) const;
160
161 // Theme-related
162 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
163 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
164 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
165 bool IsTheme() { return is_theme_; }
166
[email protected]4a8d3272009-03-10 19:15:08167 private:
[email protected]3cfbd0e2009-03-18 21:26:24168 // Helper method that loads a UserScript object from a
169 // dictionary in the content_script list of the manifest.
170 bool LoadUserScriptHelper(const DictionaryValue* content_script,
171 int definition_index,
172 std::string* error,
173 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34174
175 // Helper method that loads a PageAction object from a
176 // dictionary in the page_action list of the manifest.
[email protected]ce5c4502009-05-06 16:46:11177 PageAction* LoadPageActionHelper(const DictionaryValue* page_action,
178 int definition_index,
179 std::string* error);
[email protected]f7f3a5f2009-05-01 22:02:34180
[email protected]4a8d3272009-03-10 19:15:08181 // The absolute path to the directory the extension is stored in.
182 FilePath path_;
183
184 // The base extension url for the extension.
185 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59186
[email protected]631cf822009-05-15 07:01:25187 // The location the extension was loaded from.
188 Location location_;
189
[email protected]7713d632008-12-02 07:52:33190 // A human-readable ID for the extension. The convention is to use something
191 // like 'com.example.myextension', but this is not currently enforced. An
192 // extension's ID is used in things like directory structures and URLs, and
193 // is expected to not change across versions. In the case of conflicts,
194 // updates will only be allowed if the extension can be validated using the
195 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23196 std::string id_;
[email protected]82891262008-12-24 00:21:26197
[email protected]64a02b802009-01-12 19:36:42198 // The extension's version.
[email protected]cc655912009-01-29 23:19:19199 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42200
[email protected]82891262008-12-24 00:21:26201 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23202 std::string name_;
[email protected]82891262008-12-24 00:21:26203
[email protected]4a8d3272009-03-10 19:15:08204 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23205 std::string description_;
[email protected]82891262008-12-24 00:21:26206
207 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05208 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33209
[email protected]f7f3a5f2009-05-01 22:02:34210 // A list of page actions.
211 PageActionMap page_actions_;
212
[email protected]4a8d3272009-03-10 19:15:08213 // Optional absolute path to the directory of NPAPI plugins that the extension
214 // contains.
[email protected]367230c52009-02-21 01:44:30215 FilePath plugins_dir_;
216
[email protected]c64631652009-04-29 22:24:31217 // Optional URL to a master page of which a single instance should be always
218 // loaded in the background.
219 GURL background_url_;
220
[email protected]d5cef1b2009-03-21 05:36:49221 // Paths to HTML files to be displayed in the toolbar.
222 std::vector<std::string> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08223
[email protected]cc655912009-01-29 23:19:19224 // A SHA1 hash of the contents of the zip file. Note that this key is only
225 // present in the manifest that's prepended to the zip. The inner manifest
226 // will not have this key.
227 std::string zip_hash_;
228
[email protected]07c00d992009-03-04 20:27:04229 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07230 scoped_ptr<DictionaryValue> theme_images_;
[email protected]4a190632009-05-09 01:07:42231
232 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07233 scoped_ptr<DictionaryValue> theme_colors_;
[email protected]4a190632009-05-09 01:07:42234
235 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07236 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42237
238 // Whether the extension is a theme - if it is, certain things are disabled.
239 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04240
[email protected]c64631652009-04-29 22:24:31241 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]7197f4992009-03-23 05:05:49242 std::vector<URLPattern> permissions_;
243
[email protected]894bb502009-05-21 22:39:57244 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33245};
246
[email protected]6014d672008-12-05 00:38:25247#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_