blob: 5964ad4f10e4257a05edd9319feeed733812c9ec [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]eab9b452009-01-23 20:48:5940 static const wchar_t* kDescriptionKey;
41 static const wchar_t* kFilesKey;
[email protected]17122322009-01-22 20:03:3042 static const wchar_t* kFormatVersionKey;
43 static const wchar_t* kIdKey;
[email protected]eab9b452009-01-23 20:48:5944 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3045 static const wchar_t* kNameKey;
[email protected]eab9b452009-01-23 20:48:5946 static const wchar_t* kUserScriptsKey;
[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]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]17122322009-01-22 20:03:3056 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5957 static const char* kInvalidFileCountError;
58 static const char* kInvalidFileError;
59 static const char* kInvalidFilesError;
60 static const char* kInvalidFormatVersionError;
61 static const char* kInvalidIdError;
62 static const char* kInvalidManifestError;
63 static const char* kInvalidMatchCountError;
64 static const char* kInvalidMatchError;
65 static const char* kInvalidMatchesError;
66 static const char* kInvalidNameError;
[email protected]0afe8272009-02-14 04:15:1667 static const char* kInvalidRunAtError;
[email protected]eab9b452009-01-23 20:48:5968 static const char* kInvalidUserScriptError;
69 static const char* kInvalidUserScriptsListError;
[email protected]17122322009-01-22 20:03:3070 static const char* kInvalidVersionError;
[email protected]cc655912009-01-29 23:19:1971 static const char* kInvalidZipHashError;
[email protected]7713d632008-12-02 07:52:3372
[email protected]eab9b452009-01-23 20:48:5973 // Creates an absolute url to a resource inside an extension. The
74 // |extension_url| argument should be the url() from an Extension object. The
75 // |relative_path| can be untrusted user input. The returned URL will either
76 // be invalid() or a child of |extension_url|.
77 // NOTE: Static so that it can be used from multiple threads.
78 static GURL GetResourceURL(const GURL& extension_url,
79 const std::string& relative_path);
80
81 // Creates an absolute path to a resource inside an extension. The
82 // |extension_path| argument should be the path() from an Extension object.
83 // The |relative_path| can be untrusted user input. The returned path will
84 // either be empty or a child of extension_path.
85 // NOTE: Static so that it can be used from multiple threads.
86 static FilePath GetResourcePath(const FilePath& extension_path,
87 const std::string& relative_path);
88
[email protected]82891262008-12-24 00:21:2689 // The path to the folder the extension is stored in.
90 const FilePath& path() const { return path_; }
91
[email protected]eab9b452009-01-23 20:48:5992 // The base URL for the extension.
93 const GURL& url() const { return extension_url_; }
94
[email protected]7713d632008-12-02 07:52:3395 // A human-readable ID for the extension. The convention is to use something
96 // like 'com.example.myextension', but this is not currently enforced. An
97 // extension's ID is used in things like directory structures and URLs, and
98 // is expected to not change across versions. In the case of conflicts,
99 // updates will only be allowed if the extension can be validated using the
100 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23101 const std::string& id() const { return id_; }
[email protected]7713d632008-12-02 07:52:33102
[email protected]64a02b802009-01-12 19:36:42103 // The version number for the extension.
[email protected]cc655912009-01-29 23:19:19104 const Version* version() const { return version_.get(); }
105
106 // String representation of the version number.
107 const std::string VersionString() const;
[email protected]64a02b802009-01-12 19:36:42108
[email protected]7713d632008-12-02 07:52:33109 // A human-readable name of the extension.
[email protected]e1cec06c2008-12-18 01:22:23110 const std::string& name() const { return name_; }
[email protected]7713d632008-12-02 07:52:33111
112 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23113 const std::string& description() const { return description_; }
[email protected]7713d632008-12-02 07:52:33114
115 // Paths to the content scripts that the extension contains.
[email protected]eab9b452009-01-23 20:48:59116 const UserScriptList& user_scripts() const {
117 return user_scripts_;
[email protected]7713d632008-12-02 07:52:33118 }
119
120 // Initialize the extension from a parsed manifest.
[email protected]3acbd422008-12-08 18:25:00121 bool InitFromValue(const DictionaryValue& value, std::string* error);
[email protected]7713d632008-12-02 07:52:33122
[email protected]7713d632008-12-02 07:52:33123 private:
[email protected]82891262008-12-24 00:21:26124 // The path to the directory the extension is stored in.
125 FilePath path_;
126
[email protected]eab9b452009-01-23 20:48:59127 // The base extension url for the extension.
128 GURL extension_url_;
129
[email protected]82891262008-12-24 00:21:26130 // The extension's ID.
[email protected]e1cec06c2008-12-18 01:22:23131 std::string id_;
[email protected]82891262008-12-24 00:21:26132
[email protected]64a02b802009-01-12 19:36:42133 // The extension's version.
[email protected]cc655912009-01-29 23:19:19134 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42135
[email protected]82891262008-12-24 00:21:26136 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23137 std::string name_;
[email protected]82891262008-12-24 00:21:26138
139 // An optional description for the extension.
[email protected]e1cec06c2008-12-18 01:22:23140 std::string description_;
[email protected]82891262008-12-24 00:21:26141
142 // Paths to the content scripts the extension contains.
[email protected]eab9b452009-01-23 20:48:59143 UserScriptList user_scripts_;
[email protected]7713d632008-12-02 07:52:33144
[email protected]cc655912009-01-29 23:19:19145 // A SHA1 hash of the contents of the zip file. Note that this key is only
146 // present in the manifest that's prepended to the zip. The inner manifest
147 // will not have this key.
148 std::string zip_hash_;
149
[email protected]7713d632008-12-02 07:52:33150 DISALLOW_COPY_AND_ASSIGN(Extension);
151};
152
[email protected]6014d672008-12-05 00:38:25153#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_