blob: c612203b77d99d1adea9f2f78cc4c1139ef49db3 [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"
21
[email protected]7713d632008-12-02 07:52:3322// Represents a Chromium extension.
23class Extension {
24 public:
[email protected]cc655912009-01-29 23:19:1925 Extension() {}
26 explicit Extension(const FilePath& path);
[email protected]07c00d992009-03-04 20:27:0427 explicit Extension(const Extension& path);
[email protected]f7f3a5f2009-05-01 22:02:3428 virtual ~Extension();
[email protected]7713d632008-12-02 07:52:3329
[email protected]6014d672008-12-05 00:38:2530 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3431 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2532
[email protected]7713d632008-12-02 07:52:3333 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0534 static const wchar_t* kContentScriptsKey;
[email protected]7197f4992009-03-23 05:05:4935 static const wchar_t* kCssKey;
[email protected]eab9b452009-01-23 20:48:5936 static const wchar_t* kDescriptionKey;
[email protected]f7f3a5f2009-05-01 22:02:3437 static const wchar_t* kIconPathKey;
[email protected]17122322009-01-22 20:03:3038 static const wchar_t* kIdKey;
[email protected]34aa8dc2009-02-19 07:03:0539 static const wchar_t* kJsKey;
[email protected]eab9b452009-01-23 20:48:5940 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3041 static const wchar_t* kNameKey;
[email protected]f7f3a5f2009-05-01 22:02:3442 static const wchar_t* kPageActionsKey;
[email protected]7197f4992009-03-23 05:05:4943 static const wchar_t* kPermissionsKey;
[email protected]367230c52009-02-21 01:44:3044 static const wchar_t* kPluginsDirKey;
[email protected]c64631652009-04-29 22:24:3145 static const wchar_t* kBackgroundKey;
[email protected]7197f4992009-03-23 05:05:4946 static const wchar_t* kRunAtKey;
[email protected]07c00d992009-03-04 20:27:0447 static const wchar_t* kThemeKey;
[email protected]d5cef1b2009-03-21 05:36:4948 static const wchar_t* kToolstripsKey;
[email protected]f7f3a5f2009-05-01 22:02:3449 static const wchar_t* kTooltipKey;
50 static const wchar_t* kTypeKey;
[email protected]7197f4992009-03-23 05:05:4951 static const wchar_t* kVersionKey;
52 static const wchar_t* kZipHashKey;
[email protected]7713d632008-12-02 07:52:3353
[email protected]0afe8272009-02-14 04:15:1654 // Some values expected in manifests.
55 static const char* kRunAtDocumentStartValue;
56 static const char* kRunAtDocumentEndValue;
[email protected]f7f3a5f2009-05-01 22:02:3457 static const char* kPageActionTypeTab;
58 static const char* kPageActionTypePermanent;
[email protected]0afe8272009-02-14 04:15:1659
[email protected]7713d632008-12-02 07:52:3360 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0561 static const char* kInvalidContentScriptError;
62 static const char* kInvalidContentScriptsListError;
[email protected]3cfbd0e2009-03-18 21:26:2463 static const char* kInvalidCssError;
64 static const char* kInvalidCssListError;
[email protected]17122322009-01-22 20:03:3065 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5966 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0567 static const char* kInvalidJsError;
68 static const char* kInvalidJsListError;
[email protected]eab9b452009-01-23 20:48:5969 static const char* kInvalidManifestError;
70 static const char* kInvalidMatchCountError;
71 static const char* kInvalidMatchError;
72 static const char* kInvalidMatchesError;
73 static const char* kInvalidNameError;
[email protected]3cfbd0e2009-03-18 21:26:2474 static const char* kInvalidPluginsDirError;
[email protected]c64631652009-04-29 22:24:3175 static const char* kInvalidBackgroundError;
[email protected]0afe8272009-02-14 04:15:1676 static const char* kInvalidRunAtError;
[email protected]3cfbd0e2009-03-18 21:26:2477 static const char* kInvalidToolstripError;
[email protected]d5cef1b2009-03-21 05:36:4978 static const char* kInvalidToolstripsError;
[email protected]17122322009-01-22 20:03:3079 static const char* kInvalidVersionError;
[email protected]f7f3a5f2009-05-01 22:02:3480 static const char* kInvalidPageActionError;
81 static const char* kInvalidPageActionsListError;
82 static const char* kInvalidPageActionIconPathError;
83 static const char* kInvalidPageActionTooltipError;
84 static const char* kInvalidPageActionTypeValueError;
[email protected]7197f4992009-03-23 05:05:4985 static const char* kInvalidPermissionsError;
86 static const char* kInvalidPermissionCountWarning;
87 static const char* kInvalidPermissionError;
88 static const char* kInvalidPermissionSchemeError;
[email protected]cc655912009-01-29 23:19:1989 static const char* kInvalidZipHashError;
[email protected]3cfbd0e2009-03-18 21:26:2490 static const char* kMissingFileError;
[email protected]f7f3a5f2009-05-01 22:02:3491 static const char* kMissingPageActionIcon;
[email protected]7713d632008-12-02 07:52:3392
[email protected]37eeb5a2009-02-26 23:36:1793 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:4894 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:1795
[email protected]07c00d992009-03-04 20:27:0496 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:5997 // |extension_url| argument should be the url() from an Extension object. The
98 // |relative_path| can be untrusted user input. The returned URL will either
99 // be invalid() or a child of |extension_url|.
100 // NOTE: Static so that it can be used from multiple threads.
101 static GURL GetResourceURL(const GURL& extension_url,
102 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24103 GURL GetResourceURL(const std::string& relative_path) {
104 return GetResourceURL(url(), relative_path);
105 }
[email protected]eab9b452009-01-23 20:48:59106
[email protected]07c00d992009-03-04 20:27:04107 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59108 // |extension_path| argument should be the path() from an Extension object.
109 // The |relative_path| can be untrusted user input. The returned path will
110 // either be empty or a child of extension_path.
111 // NOTE: Static so that it can be used from multiple threads.
112 static FilePath GetResourcePath(const FilePath& extension_path,
113 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24114 FilePath GetResourcePath(const std::string& relative_path) {
115 return GetResourcePath(path(), relative_path);
116 }
[email protected]eab9b452009-01-23 20:48:59117
[email protected]4a8d3272009-03-10 19:15:08118 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30119 // If |require_id| is true, will return an error if the "id" key is missing
120 // from the value.
121 bool InitFromValue(const DictionaryValue& value, bool require_id,
122 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08123
[email protected]07c00d992009-03-04 20:27:04124 // Returns an absolute path to a resource inside of an extension if the
125 // extension has a theme defined with the given |resource_id|. Otherwise
126 // the path will be empty. Note that this method is not static as it is
127 // only intended to be called on an extension which has registered itself
128 // as providing a theme.
129 FilePath GetThemeResourcePath(const int resource_id);
130
[email protected]ce5c4502009-05-06 16:46:11131 // Retrieves a page action by |id|.
132 const PageAction* GetPageAction(std::string id) const;
[email protected]f7f3a5f2009-05-01 22:02:34133
[email protected]82891262008-12-24 00:21:26134 const FilePath& path() const { return path_; }
[email protected]5bfb1eb0a2009-04-08 18:33:30135 const GURL& url() const { return extension_url_; }
[email protected]4a8d3272009-03-10 19:15:08136 const std::string& id() const { return id_; }
137 const Version* version() const { return version_.get(); }
138 // String representation of the version number.
139 const std::string VersionString() const;
140 const std::string& name() const { return name_; }
141 const std::string& description() const { return description_; }
142 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]f7f3a5f2009-05-01 22:02:34143 const PageActionMap& page_actions() const { return page_actions_; }
[email protected]4a8d3272009-03-10 19:15:08144 const FilePath& plugins_dir() const { return plugins_dir_; }
[email protected]c64631652009-04-29 22:24:31145 const GURL& background_url() const { return background_url_; }
[email protected]d5cef1b2009-03-21 05:36:49146 const std::vector<std::string>& toolstrips() const { return toolstrips_; }
[email protected]7197f4992009-03-23 05:05:49147 const std::vector<URLPattern>& permissions() const {
148 return permissions_; }
[email protected]4a8d3272009-03-10 19:15:08149
150 private:
[email protected]3cfbd0e2009-03-18 21:26:24151 // Helper method that loads a UserScript object from a
152 // dictionary in the content_script list of the manifest.
153 bool LoadUserScriptHelper(const DictionaryValue* content_script,
154 int definition_index,
155 std::string* error,
156 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34157
158 // Helper method that loads a PageAction object from a
159 // dictionary in the page_action list of the manifest.
[email protected]ce5c4502009-05-06 16:46:11160 PageAction* LoadPageActionHelper(const DictionaryValue* page_action,
161 int definition_index,
162 std::string* error);
[email protected]f7f3a5f2009-05-01 22:02:34163
[email protected]4a8d3272009-03-10 19:15:08164 // The absolute path to the directory the extension is stored in.
165 FilePath path_;
166
167 // The base extension url for the extension.
168 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59169
[email protected]7713d632008-12-02 07:52:33170 // A human-readable ID for the extension. The convention is to use something
171 // like 'com.example.myextension', but this is not currently enforced. An
172 // extension's ID is used in things like directory structures and URLs, and
173 // is expected to not change across versions. In the case of conflicts,
174 // updates will only be allowed if the extension can be validated using the
175 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23176 std::string id_;
[email protected]82891262008-12-24 00:21:26177
[email protected]64a02b802009-01-12 19:36:42178 // The extension's version.
[email protected]cc655912009-01-29 23:19:19179 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42180
[email protected]82891262008-12-24 00:21:26181 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23182 std::string name_;
[email protected]82891262008-12-24 00:21:26183
[email protected]4a8d3272009-03-10 19:15:08184 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23185 std::string description_;
[email protected]82891262008-12-24 00:21:26186
187 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05188 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33189
[email protected]f7f3a5f2009-05-01 22:02:34190 // A list of page actions.
191 PageActionMap page_actions_;
192
[email protected]4a8d3272009-03-10 19:15:08193 // Optional absolute path to the directory of NPAPI plugins that the extension
194 // contains.
[email protected]367230c52009-02-21 01:44:30195 FilePath plugins_dir_;
196
[email protected]c64631652009-04-29 22:24:31197 // Optional URL to a master page of which a single instance should be always
198 // loaded in the background.
199 GURL background_url_;
200
[email protected]d5cef1b2009-03-21 05:36:49201 // Paths to HTML files to be displayed in the toolbar.
202 std::vector<std::string> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08203
[email protected]cc655912009-01-29 23:19:19204 // A SHA1 hash of the contents of the zip file. Note that this key is only
205 // present in the manifest that's prepended to the zip. The inner manifest
206 // will not have this key.
207 std::string zip_hash_;
208
[email protected]07c00d992009-03-04 20:27:04209 // A map of resource id's to relative file paths.
210 std::map<const std::wstring, std::string> theme_paths_;
211
[email protected]c64631652009-04-29 22:24:31212 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]7197f4992009-03-23 05:05:49213 std::vector<URLPattern> permissions_;
214
[email protected]07c00d992009-03-04 20:27:04215 // We implement copy, but not assign.
216 void operator=(const Extension&);
[email protected]7713d632008-12-02 07:52:33217};
218
[email protected]6014d672008-12-05 00:38:25219#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_