[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 1 | // 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] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |
6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ | ||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 7 | |
8 | #include <string> | ||||
9 | #include <vector> | ||||
10 | |||||
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 12 | #include "base/scoped_ptr.h" |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 13 | #include "base/string16.h" |
14 | #include "base/values.h" | ||||
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 15 | #include "base/version.h" |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 16 | #include "chrome/browser/extensions/user_script_master.h" |
17 | #include "googleurl/src/gurl.h" | ||||
18 | |||||
19 | // The URL schemes Chromium extensions and user scripts are served from. These | ||||
20 | // really should be in extension_protocols.h, but that causes link errors on | ||||
21 | // linux because extension_protocols.cc refers to undefined symbols. | ||||
22 | // TODO(aa): Move these back to extension_protocols.h when more of Linux and | ||||
23 | // Mac are up and running. | ||||
24 | extern const char kExtensionURLScheme[]; | ||||
25 | extern const char kUserScriptURLScheme[]; | ||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 26 | |
27 | // Represents a Chromium extension. | ||||
28 | class Extension { | ||||
29 | public: | ||||
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 30 | Extension() {} |
31 | explicit Extension(const FilePath& path); | ||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 32 | |
33 | // The format for extension manifests that this code understands. | ||||
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 34 | static const unsigned int kExpectedFormatVersion = 1; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 35 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 36 | // The name of the manifest inside an extension. |
[email protected] | 0e29223 | 2009-01-22 15:23:34 | [diff] [blame] | 37 | static const char kManifestFilename[]; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 38 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 39 | // Keys used in JSON representation of extensions. |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 40 | static const wchar_t* kContentScriptsKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 41 | static const wchar_t* kDescriptionKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 42 | static const wchar_t* kFormatVersionKey; |
43 | static const wchar_t* kIdKey; | ||||
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 44 | static const wchar_t* kJsKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 45 | static const wchar_t* kMatchesKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 46 | static const wchar_t* kNameKey; |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 47 | static const wchar_t* kRunAtKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 48 | static const wchar_t* kVersionKey; |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 49 | static const wchar_t* kZipHashKey; |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame^] | 50 | static const wchar_t* kPluginsDirKey; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 51 | |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 52 | // Some values expected in manifests. |
53 | static const char* kRunAtDocumentStartValue; | ||||
54 | static const char* kRunAtDocumentEndValue; | ||||
55 | |||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 56 | // Error messages returned from InitFromValue(). |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 57 | static const char* kInvalidContentScriptError; |
58 | static const char* kInvalidContentScriptsListError; | ||||
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 59 | static const char* kInvalidDescriptionError; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 60 | static const char* kInvalidFormatVersionError; |
61 | static const char* kInvalidIdError; | ||||
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 62 | static const char* kInvalidJsCountError; |
63 | static const char* kInvalidJsError; | ||||
64 | static const char* kInvalidJsListError; | ||||
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 65 | 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] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 70 | static const char* kInvalidRunAtError; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 71 | static const char* kInvalidVersionError; |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 72 | static const char* kInvalidZipHashError; |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame^] | 73 | static const char* kInvalidPluginsDirError; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 74 | |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 75 | // Creates an absolute url to a resource inside an extension. The |
76 | // |extension_url| argument should be the url() from an Extension object. The | ||||
77 | // |relative_path| can be untrusted user input. The returned URL will either | ||||
78 | // be invalid() or a child of |extension_url|. | ||||
79 | // NOTE: Static so that it can be used from multiple threads. | ||||
80 | static GURL GetResourceURL(const GURL& extension_url, | ||||
81 | const std::string& relative_path); | ||||
82 | |||||
83 | // Creates an absolute path to a resource inside an extension. The | ||||
84 | // |extension_path| argument should be the path() from an Extension object. | ||||
85 | // The |relative_path| can be untrusted user input. The returned path will | ||||
86 | // either be empty or a child of extension_path. | ||||
87 | // NOTE: Static so that it can be used from multiple threads. | ||||
88 | static FilePath GetResourcePath(const FilePath& extension_path, | ||||
89 | const std::string& relative_path); | ||||
90 | |||||
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 91 | // The path to the folder the extension is stored in. |
92 | const FilePath& path() const { return path_; } | ||||
93 | |||||
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 94 | // The base URL for the extension. |
95 | const GURL& url() const { return extension_url_; } | ||||
96 | |||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 97 | // A human-readable ID for the extension. The convention is to use something |
98 | // like 'com.example.myextension', but this is not currently enforced. An | ||||
99 | // extension's ID is used in things like directory structures and URLs, and | ||||
100 | // is expected to not change across versions. In the case of conflicts, | ||||
101 | // updates will only be allowed if the extension can be validated using the | ||||
102 | // previous version's update key. | ||||
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 103 | const std::string& id() const { return id_; } |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 104 | |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 105 | // The version number for the extension. |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 106 | const Version* version() const { return version_.get(); } |
107 | |||||
108 | // String representation of the version number. | ||||
109 | const std::string VersionString() const; | ||||
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 110 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 111 | // A human-readable name of the extension. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 112 | const std::string& name() const { return name_; } |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 113 | |
114 | // An optional longer description of the extension. | ||||
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 115 | const std::string& description() const { return description_; } |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 116 | |
117 | // Paths to the content scripts that the extension contains. | ||||
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 118 | const UserScriptList& content_scripts() const { |
119 | return content_scripts_; | ||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 120 | } |
121 | |||||
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame^] | 122 | // Path to the directory of NPAPI plugins that the extension contains. |
123 | const FilePath& plugins_dir() const { | ||||
124 | return plugins_dir_; | ||||
125 | } | ||||
126 | |||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 127 | // Initialize the extension from a parsed manifest. |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 128 | bool InitFromValue(const DictionaryValue& value, std::string* error); |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 129 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 130 | private: |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 131 | // The path to the directory the extension is stored in. |
132 | FilePath path_; | ||||
133 | |||||
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 134 | // The base extension url for the extension. |
135 | GURL extension_url_; | ||||
136 | |||||
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 137 | // The extension's ID. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 138 | std::string id_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 139 | |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 140 | // The extension's version. |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 141 | scoped_ptr<Version> version_; |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 142 | |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 143 | // The extension's human-readable name. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 144 | std::string name_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 145 | |
146 | // An optional description for the extension. | ||||
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 147 | std::string description_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 148 | |
149 | // Paths to the content scripts the extension contains. | ||||
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 150 | UserScriptList content_scripts_; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 151 | |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame^] | 152 | // Path to the directory of NPAPI plugins that the extension contains. |
153 | FilePath plugins_dir_; | ||||
154 | |||||
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 155 | // A SHA1 hash of the contents of the zip file. Note that this key is only |
156 | // present in the manifest that's prepended to the zip. The inner manifest | ||||
157 | // will not have this key. | ||||
158 | std::string zip_hash_; | ||||
159 | |||||
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(Extension); |
161 | }; | ||||
162 | |||||
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 163 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |