blob: cc44cd55c089b88d357163226d7a11edb3e04007 [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>
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/string16.h"
14#include "base/values.h"
[email protected]cc655912009-01-29 23:19:1915#include "base/version.h"
[email protected]eab9b452009-01-23 20:48:5916#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.
24extern const char kExtensionURLScheme[];
25extern const char kUserScriptURLScheme[];
[email protected]7713d632008-12-02 07:52:3326
27// Represents a Chromium extension.
28class Extension {
29 public:
[email protected]cc655912009-01-29 23:19:1930 Extension() {}
31 explicit Extension(const FilePath& path);
[email protected]7713d632008-12-02 07:52:3332
33 // The format for extension manifests that this code understands.
[email protected]cc655912009-01-29 23:19:1934 static const unsigned int kExpectedFormatVersion = 1;
[email protected]7713d632008-12-02 07:52:3335
[email protected]6014d672008-12-05 00:38:2536 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3437 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2538
[email protected]7713d632008-12-02 07:52:3339 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0540 static const wchar_t* kContentScriptsKey;
[email protected]eab9b452009-01-23 20:48:5941 static const wchar_t* kDescriptionKey;
[email protected]17122322009-01-22 20:03:3042 static const wchar_t* kFormatVersionKey;
43 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]0afe8272009-02-14 04:15:1647 static const wchar_t* kRunAtKey;
[email protected]17122322009-01-22 20:03:3048 static const wchar_t* kVersionKey;
[email protected]cc655912009-01-29 23:19:1949 static const wchar_t* kZipHashKey;
[email protected]367230c52009-02-21 01:44:3050 static const wchar_t* kPluginsDirKey;
[email protected]7713d632008-12-02 07:52:3351
[email protected]0afe8272009-02-14 04:15:1652 // Some values expected in manifests.
53 static const char* kRunAtDocumentStartValue;
54 static const char* kRunAtDocumentEndValue;
55
[email protected]7713d632008-12-02 07:52:3356 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0557 static const char* kInvalidContentScriptError;
58 static const char* kInvalidContentScriptsListError;
[email protected]17122322009-01-22 20:03:3059 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5960 static const char* kInvalidFormatVersionError;
61 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0562 static const char* kInvalidJsCountError;
63 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]0afe8272009-02-14 04:15:1670 static const char* kInvalidRunAtError;
[email protected]17122322009-01-22 20:03:3071 static const char* kInvalidVersionError;
[email protected]cc655912009-01-29 23:19:1972 static const char* kInvalidZipHashError;
[email protected]367230c52009-02-21 01:44:3073 static const char* kInvalidPluginsDirError;
[email protected]7713d632008-12-02 07:52:3374
[email protected]eab9b452009-01-23 20:48:5975 // 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]82891262008-12-24 00:21:2691 // The path to the folder the extension is stored in.
92 const FilePath& path() const { return path_; }
93
[email protected]eab9b452009-01-23 20:48:5994 // The base URL for the extension.
95 const GURL& url() const { return extension_url_; }
96
[email protected]7713d632008-12-02 07:52:3397 // 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]e1cec06c2008-12-18 01:22:23103 const std::string& id() const { return id_; }
[email protected]7713d632008-12-02 07:52:33104
[email protected]64a02b802009-01-12 19:36:42105 // The version number for the extension.
[email protected]cc655912009-01-29 23:19:19106 const Version* version() const { return version_.get(); }
107
108 // String representation of the version number.
109 const std::string VersionString() const;
[email protected]64a02b802009-01-12 19:36:42110
[email protected]7713d632008-12-02 07:52:33111 // A human-readable name of the extension.
[email protected]e1cec06c2008-12-18 01:22:23112 const std::string& name() const { return name_; }
[email protected]7713d632008-12-02 07:52:33113
114 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23115 const std::string& description() const { return description_; }
[email protected]7713d632008-12-02 07:52:33116
117 // Paths to the content scripts that the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05118 const UserScriptList& content_scripts() const {
119 return content_scripts_;
[email protected]7713d632008-12-02 07:52:33120 }
121
[email protected]367230c52009-02-21 01:44:30122 // 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]7713d632008-12-02 07:52:33127 // Initialize the extension from a parsed manifest.
[email protected]3acbd422008-12-08 18:25:00128 bool InitFromValue(const DictionaryValue& value, std::string* error);
[email protected]7713d632008-12-02 07:52:33129
[email protected]7713d632008-12-02 07:52:33130 private:
[email protected]82891262008-12-24 00:21:26131 // The path to the directory the extension is stored in.
132 FilePath path_;
133
[email protected]eab9b452009-01-23 20:48:59134 // The base extension url for the extension.
135 GURL extension_url_;
136
[email protected]82891262008-12-24 00:21:26137 // The extension's ID.
[email protected]e1cec06c2008-12-18 01:22:23138 std::string id_;
[email protected]82891262008-12-24 00:21:26139
[email protected]64a02b802009-01-12 19:36:42140 // The extension's version.
[email protected]cc655912009-01-29 23:19:19141 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42142
[email protected]82891262008-12-24 00:21:26143 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23144 std::string name_;
[email protected]82891262008-12-24 00:21:26145
146 // An optional description for the extension.
[email protected]e1cec06c2008-12-18 01:22:23147 std::string description_;
[email protected]82891262008-12-24 00:21:26148
149 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05150 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33151
[email protected]367230c52009-02-21 01:44:30152 // Path to the directory of NPAPI plugins that the extension contains.
153 FilePath plugins_dir_;
154
[email protected]cc655912009-01-29 23:19:19155 // 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]7713d632008-12-02 07:52:33160 DISALLOW_COPY_AND_ASSIGN(Extension);
161};
162
[email protected]6014d672008-12-05 00:38:25163#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_