blob: 565c8b5816f9c376518fb32f53fd32b5f5293452 [file] [log] [blame]
[email protected]7713d632008-12-02 07:52:331// Copyright (c) 2006-2008 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
[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]eab9b452009-01-23 20:48:5919#include "googleurl/src/gurl.h"
20
[email protected]7713d632008-12-02 07:52:3321// Represents a Chromium extension.
22class Extension {
23 public:
[email protected]cc655912009-01-29 23:19:1924 Extension() {}
25 explicit Extension(const FilePath& path);
[email protected]07c00d992009-03-04 20:27:0426 explicit Extension(const Extension& path);
[email protected]7713d632008-12-02 07:52:3327
28 // The format for extension manifests that this code understands.
[email protected]cc655912009-01-29 23:19:1929 static const unsigned int kExpectedFormatVersion = 1;
[email protected]7713d632008-12-02 07:52:3330
[email protected]6014d672008-12-05 00:38:2531 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3432 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2533
[email protected]7713d632008-12-02 07:52:3334 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0535 static const wchar_t* kContentScriptsKey;
[email protected]7197f4992009-03-23 05:05:4936 static const wchar_t* kCssKey;
[email protected]eab9b452009-01-23 20:48:5937 static const wchar_t* kDescriptionKey;
[email protected]17122322009-01-22 20:03:3038 static const wchar_t* kFormatVersionKey;
39 static const wchar_t* kIdKey;
[email protected]34aa8dc2009-02-19 07:03:0540 static const wchar_t* kJsKey;
[email protected]eab9b452009-01-23 20:48:5941 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3042 static const wchar_t* kNameKey;
[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]7197f4992009-03-23 05:05:4945 static const wchar_t* kRunAtKey;
[email protected]07c00d992009-03-04 20:27:0446 static const wchar_t* kThemeKey;
[email protected]d5cef1b2009-03-21 05:36:4947 static const wchar_t* kToolstripsKey;
[email protected]7197f4992009-03-23 05:05:4948 static const wchar_t* kVersionKey;
49 static const wchar_t* kZipHashKey;
[email protected]7713d632008-12-02 07:52:3350
[email protected]0afe8272009-02-14 04:15:1651 // Some values expected in manifests.
52 static const char* kRunAtDocumentStartValue;
53 static const char* kRunAtDocumentEndValue;
54
[email protected]7713d632008-12-02 07:52:3355 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0556 static const char* kInvalidContentScriptError;
57 static const char* kInvalidContentScriptsListError;
[email protected]3cfbd0e2009-03-18 21:26:2458 static const char* kInvalidCssError;
59 static const char* kInvalidCssListError;
[email protected]17122322009-01-22 20:03:3060 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5961 static const char* kInvalidFormatVersionError;
62 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0563 static const char* kInvalidJsError;
64 static const char* kInvalidJsListError;
[email protected]eab9b452009-01-23 20:48:5965 static const char* kInvalidManifestError;
66 static const char* kInvalidMatchCountError;
67 static const char* kInvalidMatchError;
68 static const char* kInvalidMatchesError;
69 static const char* kInvalidNameError;
[email protected]3cfbd0e2009-03-18 21:26:2470 static const char* kInvalidPluginsDirError;
[email protected]0afe8272009-02-14 04:15:1671 static const char* kInvalidRunAtError;
[email protected]3cfbd0e2009-03-18 21:26:2472 static const char* kInvalidToolstripError;
[email protected]d5cef1b2009-03-21 05:36:4973 static const char* kInvalidToolstripsError;
[email protected]17122322009-01-22 20:03:3074 static const char* kInvalidVersionError;
[email protected]7197f4992009-03-23 05:05:4975 static const char* kInvalidPermissionsError;
76 static const char* kInvalidPermissionCountWarning;
77 static const char* kInvalidPermissionError;
78 static const char* kInvalidPermissionSchemeError;
[email protected]cc655912009-01-29 23:19:1979 static const char* kInvalidZipHashError;
[email protected]3cfbd0e2009-03-18 21:26:2480 static const char* kMissingFileError;
[email protected]7713d632008-12-02 07:52:3381
[email protected]37eeb5a2009-02-26 23:36:1782 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:4883 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:1784
[email protected]07c00d992009-03-04 20:27:0485 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:5986 // |extension_url| argument should be the url() from an Extension object. The
87 // |relative_path| can be untrusted user input. The returned URL will either
88 // be invalid() or a child of |extension_url|.
89 // NOTE: Static so that it can be used from multiple threads.
90 static GURL GetResourceURL(const GURL& extension_url,
91 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:2492 GURL GetResourceURL(const std::string& relative_path) {
93 return GetResourceURL(url(), relative_path);
94 }
[email protected]eab9b452009-01-23 20:48:5995
[email protected]07c00d992009-03-04 20:27:0496 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:5997 // |extension_path| argument should be the path() from an Extension object.
98 // The |relative_path| can be untrusted user input. The returned path will
99 // either be empty or a child of extension_path.
100 // NOTE: Static so that it can be used from multiple threads.
101 static FilePath GetResourcePath(const FilePath& extension_path,
102 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24103 FilePath GetResourcePath(const std::string& relative_path) {
104 return GetResourcePath(path(), relative_path);
105 }
[email protected]eab9b452009-01-23 20:48:59106
[email protected]4a8d3272009-03-10 19:15:08107 // Initialize the extension from a parsed manifest.
108 bool InitFromValue(const DictionaryValue& value, std::string* error);
109
[email protected]07c00d992009-03-04 20:27:04110 // Returns an absolute path to a resource inside of an extension if the
111 // extension has a theme defined with the given |resource_id|. Otherwise
112 // the path will be empty. Note that this method is not static as it is
113 // only intended to be called on an extension which has registered itself
114 // as providing a theme.
115 FilePath GetThemeResourcePath(const int resource_id);
116
[email protected]82891262008-12-24 00:21:26117 const FilePath& path() const { return path_; }
[email protected]eab9b452009-01-23 20:48:59118 const GURL& url() const { return extension_url_; }
[email protected]4a8d3272009-03-10 19:15:08119 const std::string& id() const { return id_; }
120 const Version* version() const { return version_.get(); }
121 // String representation of the version number.
122 const std::string VersionString() const;
123 const std::string& name() const { return name_; }
124 const std::string& description() const { return description_; }
125 const UserScriptList& content_scripts() const { return content_scripts_; }
126 const FilePath& plugins_dir() const { return plugins_dir_; }
[email protected]d5cef1b2009-03-21 05:36:49127 const std::vector<std::string>& toolstrips() const { return toolstrips_; }
[email protected]7197f4992009-03-23 05:05:49128 const std::vector<URLPattern>& permissions() const {
129 return permissions_; }
[email protected]4a8d3272009-03-10 19:15:08130
131 private:
[email protected]3cfbd0e2009-03-18 21:26:24132 // Helper method that loads a UserScript object from a
133 // dictionary in the content_script list of the manifest.
134 bool LoadUserScriptHelper(const DictionaryValue* content_script,
135 int definition_index,
136 std::string* error,
137 UserScript* result);
[email protected]4a8d3272009-03-10 19:15:08138 // The absolute path to the directory the extension is stored in.
139 FilePath path_;
140
141 // The base extension url for the extension.
142 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59143
[email protected]7713d632008-12-02 07:52:33144 // A human-readable ID for the extension. The convention is to use something
145 // like 'com.example.myextension', but this is not currently enforced. An
146 // extension's ID is used in things like directory structures and URLs, and
147 // is expected to not change across versions. In the case of conflicts,
148 // updates will only be allowed if the extension can be validated using the
149 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23150 std::string id_;
[email protected]82891262008-12-24 00:21:26151
[email protected]64a02b802009-01-12 19:36:42152 // The extension's version.
[email protected]cc655912009-01-29 23:19:19153 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42154
[email protected]82891262008-12-24 00:21:26155 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23156 std::string name_;
[email protected]82891262008-12-24 00:21:26157
[email protected]4a8d3272009-03-10 19:15:08158 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23159 std::string description_;
[email protected]82891262008-12-24 00:21:26160
161 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05162 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33163
[email protected]4a8d3272009-03-10 19:15:08164 // Optional absolute path to the directory of NPAPI plugins that the extension
165 // contains.
[email protected]367230c52009-02-21 01:44:30166 FilePath plugins_dir_;
167
[email protected]d5cef1b2009-03-21 05:36:49168 // Paths to HTML files to be displayed in the toolbar.
169 std::vector<std::string> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08170
[email protected]cc655912009-01-29 23:19:19171 // A SHA1 hash of the contents of the zip file. Note that this key is only
172 // present in the manifest that's prepended to the zip. The inner manifest
173 // will not have this key.
174 std::string zip_hash_;
175
[email protected]07c00d992009-03-04 20:27:04176 // A map of resource id's to relative file paths.
177 std::map<const std::wstring, std::string> theme_paths_;
178
[email protected]7197f4992009-03-23 05:05:49179 std::vector<URLPattern> permissions_;
180
[email protected]07c00d992009-03-04 20:27:04181 // We implement copy, but not assign.
182 void operator=(const Extension&);
[email protected]7713d632008-12-02 07:52:33183};
184
[email protected]6014d672008-12-05 00:38:25185#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_