blob: 095e7bd4c5e7dcac177645b6df565c301a7e899d [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
[email protected]7713d632008-12-02 07:52:3319// Represents a Chromium extension.
20class Extension {
21 public:
[email protected]cc655912009-01-29 23:19:1922 Extension() {}
23 explicit Extension(const FilePath& path);
[email protected]7713d632008-12-02 07:52:3324
25 // The format for extension manifests that this code understands.
[email protected]cc655912009-01-29 23:19:1926 static const unsigned int kExpectedFormatVersion = 1;
[email protected]7713d632008-12-02 07:52:3327
[email protected]6014d672008-12-05 00:38:2528 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3429 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2530
[email protected]7713d632008-12-02 07:52:3331 // Keys used in JSON representation of extensions.
[email protected]34aa8dc2009-02-19 07:03:0532 static const wchar_t* kContentScriptsKey;
[email protected]eab9b452009-01-23 20:48:5933 static const wchar_t* kDescriptionKey;
[email protected]17122322009-01-22 20:03:3034 static const wchar_t* kFormatVersionKey;
35 static const wchar_t* kIdKey;
[email protected]34aa8dc2009-02-19 07:03:0536 static const wchar_t* kJsKey;
[email protected]eab9b452009-01-23 20:48:5937 static const wchar_t* kMatchesKey;
[email protected]17122322009-01-22 20:03:3038 static const wchar_t* kNameKey;
[email protected]0afe8272009-02-14 04:15:1639 static const wchar_t* kRunAtKey;
[email protected]17122322009-01-22 20:03:3040 static const wchar_t* kVersionKey;
[email protected]cc655912009-01-29 23:19:1941 static const wchar_t* kZipHashKey;
[email protected]367230c52009-02-21 01:44:3042 static const wchar_t* kPluginsDirKey;
[email protected]7713d632008-12-02 07:52:3343
[email protected]0afe8272009-02-14 04:15:1644 // Some values expected in manifests.
45 static const char* kRunAtDocumentStartValue;
46 static const char* kRunAtDocumentEndValue;
47
[email protected]7713d632008-12-02 07:52:3348 // Error messages returned from InitFromValue().
[email protected]34aa8dc2009-02-19 07:03:0549 static const char* kInvalidContentScriptError;
50 static const char* kInvalidContentScriptsListError;
[email protected]17122322009-01-22 20:03:3051 static const char* kInvalidDescriptionError;
[email protected]eab9b452009-01-23 20:48:5952 static const char* kInvalidFormatVersionError;
53 static const char* kInvalidIdError;
[email protected]34aa8dc2009-02-19 07:03:0554 static const char* kInvalidJsCountError;
55 static const char* kInvalidJsError;
56 static const char* kInvalidJsListError;
[email protected]eab9b452009-01-23 20:48:5957 static const char* kInvalidManifestError;
58 static const char* kInvalidMatchCountError;
59 static const char* kInvalidMatchError;
60 static const char* kInvalidMatchesError;
61 static const char* kInvalidNameError;
[email protected]0afe8272009-02-14 04:15:1662 static const char* kInvalidRunAtError;
[email protected]17122322009-01-22 20:03:3063 static const char* kInvalidVersionError;
[email protected]cc655912009-01-29 23:19:1964 static const char* kInvalidZipHashError;
[email protected]367230c52009-02-21 01:44:3065 static const char* kInvalidPluginsDirError;
[email protected]7713d632008-12-02 07:52:3366
[email protected]37eeb5a2009-02-26 23:36:1767 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:4868 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:1769
[email protected]eab9b452009-01-23 20:48:5970 // Creates an absolute url to a resource inside an extension. The
71 // |extension_url| argument should be the url() from an Extension object. The
72 // |relative_path| can be untrusted user input. The returned URL will either
73 // be invalid() or a child of |extension_url|.
74 // NOTE: Static so that it can be used from multiple threads.
75 static GURL GetResourceURL(const GURL& extension_url,
76 const std::string& relative_path);
77
78 // Creates an absolute path to a resource inside an extension. The
79 // |extension_path| argument should be the path() from an Extension object.
80 // The |relative_path| can be untrusted user input. The returned path will
81 // either be empty or a child of extension_path.
82 // NOTE: Static so that it can be used from multiple threads.
83 static FilePath GetResourcePath(const FilePath& extension_path,
84 const std::string& relative_path);
85
[email protected]82891262008-12-24 00:21:2686 // The path to the folder the extension is stored in.
87 const FilePath& path() const { return path_; }
88
[email protected]eab9b452009-01-23 20:48:5989 // The base URL for the extension.
90 const GURL& url() const { return extension_url_; }
91
[email protected]7713d632008-12-02 07:52:3392 // A human-readable ID for the extension. The convention is to use something
93 // like 'com.example.myextension', but this is not currently enforced. An
94 // extension's ID is used in things like directory structures and URLs, and
95 // is expected to not change across versions. In the case of conflicts,
96 // updates will only be allowed if the extension can be validated using the
97 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:2398 const std::string& id() const { return id_; }
[email protected]7713d632008-12-02 07:52:3399
[email protected]64a02b802009-01-12 19:36:42100 // The version number for the extension.
[email protected]cc655912009-01-29 23:19:19101 const Version* version() const { return version_.get(); }
102
103 // String representation of the version number.
104 const std::string VersionString() const;
[email protected]64a02b802009-01-12 19:36:42105
[email protected]7713d632008-12-02 07:52:33106 // A human-readable name of the extension.
[email protected]e1cec06c2008-12-18 01:22:23107 const std::string& name() const { return name_; }
[email protected]7713d632008-12-02 07:52:33108
109 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23110 const std::string& description() const { return description_; }
[email protected]7713d632008-12-02 07:52:33111
112 // Paths to the content scripts that the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05113 const UserScriptList& content_scripts() const {
114 return content_scripts_;
[email protected]7713d632008-12-02 07:52:33115 }
116
[email protected]367230c52009-02-21 01:44:30117 // Path to the directory of NPAPI plugins that the extension contains.
118 const FilePath& plugins_dir() const {
119 return plugins_dir_;
120 }
121
[email protected]7713d632008-12-02 07:52:33122 // Initialize the extension from a parsed manifest.
[email protected]3acbd422008-12-08 18:25:00123 bool InitFromValue(const DictionaryValue& value, std::string* error);
[email protected]7713d632008-12-02 07:52:33124
[email protected]7713d632008-12-02 07:52:33125 private:
[email protected]82891262008-12-24 00:21:26126 // The path to the directory the extension is stored in.
127 FilePath path_;
128
[email protected]eab9b452009-01-23 20:48:59129 // The base extension url for the extension.
130 GURL extension_url_;
131
[email protected]82891262008-12-24 00:21:26132 // The extension's ID.
[email protected]e1cec06c2008-12-18 01:22:23133 std::string id_;
[email protected]82891262008-12-24 00:21:26134
[email protected]64a02b802009-01-12 19:36:42135 // The extension's version.
[email protected]cc655912009-01-29 23:19:19136 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42137
[email protected]82891262008-12-24 00:21:26138 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23139 std::string name_;
[email protected]82891262008-12-24 00:21:26140
141 // An optional description for the extension.
[email protected]e1cec06c2008-12-18 01:22:23142 std::string description_;
[email protected]82891262008-12-24 00:21:26143
144 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05145 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33146
[email protected]367230c52009-02-21 01:44:30147 // Path to the directory of NPAPI plugins that the extension contains.
148 FilePath plugins_dir_;
149
[email protected]cc655912009-01-29 23:19:19150 // A SHA1 hash of the contents of the zip file. Note that this key is only
151 // present in the manifest that's prepended to the zip. The inner manifest
152 // will not have this key.
153 std::string zip_hash_;
154
[email protected]7713d632008-12-02 07:52:33155 DISALLOW_COPY_AND_ASSIGN(Extension);
156};
157
[email protected]6014d672008-12-05 00:38:25158#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_