Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: chrome/common/extensions/manifest_handlers/shared_module_info.cc

Issue 13971005: Basic multi-module support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Factor out shared module into its own handler Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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
5 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/version.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/extension_manifest_constants.h"
15 #include "chrome/common/extensions/permissions/permission_set.h"
16 #include "extensions/common/error_utils.h"
17
18 using base::DictionaryValue;
19 namespace keys = extension_manifest_keys;
20 namespace values = extension_manifest_values;
21 namespace errors = extension_manifest_errors;
22
23 namespace extensions {
24
25 namespace {
26
27 const char kSharedModule[] = "shared_module";
28
29 static base::LazyInstance<SharedModuleInfo> g_empty_shared_module_info =
30 LAZY_INSTANCE_INITIALIZER;
31
32 const SharedModuleInfo& GetSharedModuleInfo(const Extension* extension) {
33 SharedModuleInfo* info = static_cast<SharedModuleInfo*>(
34 extension->GetManifestData(kSharedModule));
35 if (!info)
36 return g_empty_shared_module_info.Get();
37 return *info;
38 }
39
40 } // namespace
41
42 SharedModuleInfo::SharedModuleInfo() {
43 }
44
45 SharedModuleInfo::~SharedModuleInfo() {
46 }
47
48 // static
49 void SharedModuleInfo::ParseImportedPath(const std::string& path,
50 std::string* import_id,
51 std::string* import_relative_path) {
52 std::vector<std::string> tokens;
53 Tokenize(path, std::string("/"), &tokens);
54 if (tokens.size() > 2 && tokens[0] == extension_filenames::kModulesDir &&
55 Extension::IdIsValid(tokens[1])) {
56 *import_id = tokens[1];
57 *import_relative_path = tokens[2];
58 for (size_t i = 3; i < tokens.size(); ++i)
59 *import_relative_path += "/" + tokens[i];
60 }
61 }
62
63 // static
64 bool SharedModuleInfo::IsImportedPath(const std::string& path) {
65 std::vector<std::string> tokens;
66 Tokenize(path, std::string("/"), &tokens);
67 if (tokens.size() > 2 && tokens[0] == extension_filenames::kModulesDir &&
68 Extension::IdIsValid(tokens[1])) {
69 return true;
70 }
71 return false;
72 }
73
74 // static
75 bool SharedModuleInfo::IsSharedModule(const Extension* extension) {
76 return extension->manifest()->is_shared_module();
77 }
78
79 // static
80 bool SharedModuleInfo::IsExportAllowed(const Extension* extension,
81 const std::string& relative_path) {
82 return GetSharedModuleInfo(extension).
83 exported_set_.MatchesURL(extension->url().Resolve(relative_path));
84 }
85
86 // static
87 bool SharedModuleInfo::ImportsExtensionById(const Extension* extension,
88 const std::string& other_id) {
89 const SharedModuleInfo& info = GetSharedModuleInfo(extension);
90 for (size_t i = 0; i < info.imports_.size(); i++) {
91 if (info.imports_[i].extension_id == other_id)
92 return true;
93 }
94 return false;
95 }
96
97 // static
98 bool SharedModuleInfo::ImportsModules(const Extension* extension) {
99 return GetSharedModuleInfo(extension).imports_.size() > 0;
100 }
101
102 // static
103 const std::vector<SharedModuleInfo::ImportInfo>& SharedModuleInfo::GetImports(
104 const Extension* extension) {
105 return GetSharedModuleInfo(extension).imports_;
106 }
107
108 bool SharedModuleInfo::Parse(const Extension* extension, string16* error) {
109 bool has_import = extension->manifest()->HasKey(keys::kImport);
110 bool has_export = extension->manifest()->HasKey(keys::kExport);
111 if (!has_import && !has_export)
112 return true;
113
114 if (has_import && has_export) {
115 *error = ASCIIToUTF16(errors::kInvalidImportAndExport);
116 return false;
117 }
118
119 if (has_export) {
120 const DictionaryValue* export_value = NULL;
121 if (!extension->manifest()->GetDictionary(keys::kExport, &export_value)) {
122 *error = ASCIIToUTF16(errors::kInvalidExport);
123 return false;
124 }
125 const ListValue* resources_list = NULL;
126 if (!export_value->GetList(keys::kResources, &resources_list)) {
127 *error = ASCIIToUTF16(errors::kInvalidExportResources);
128 return false;
129 }
130 for (size_t i = 0; i < resources_list->GetSize(); ++i) {
131 std::string resource_path;
132 if (!resources_list->GetString(i, &resource_path)) {
133 *error = ErrorUtils::FormatErrorMessageUTF16(
134 errors::kInvalidExportResourcesString, base::IntToString(i));
135 return false;
136 }
137 const GURL& resolved_path = extension->url().Resolve(resource_path);
138 if (!resolved_path.is_valid()) {
139 *error = ErrorUtils::FormatErrorMessageUTF16(
140 errors::kInvalidExportResourcesString, base::IntToString(i));
141 return false;
142 }
143 exported_set_.AddPattern(
144 URLPattern(URLPattern::SCHEME_EXTENSION, resolved_path.spec()));
145 }
146 }
147
148 if (has_import) {
149 const ListValue* import_list = NULL;
150 if (!extension->manifest()->GetList(keys::kImport, &import_list)) {
151 *error = ASCIIToUTF16(errors::kInvalidImport);
152 return false;
153 }
154 for (size_t i = 0; i < import_list->GetSize(); ++i) {
155 const DictionaryValue* import_entry = NULL;
156 if (!import_list->GetDictionary(i, &import_entry)) {
157 *error = ASCIIToUTF16(errors::kInvalidImport);
158 return false;
159 }
160 std::string extension_id;
161 imports_.push_back(ImportInfo());
162 if (!import_entry->GetString(keys::kId, &extension_id) ||
163 !Extension::IdIsValid(extension_id)) {
164 *error = ErrorUtils::FormatErrorMessageUTF16(
165 errors::kInvalidImportId, base::IntToString(i));
166 return false;
167 }
168 imports_.back().extension_id = extension_id;
169 if (import_entry->HasKey(keys::kMinimumVersion)) {
170 std::string min_version;
171 if (!import_entry->GetString(keys::kMinimumVersion, &min_version)) {
172 *error = ErrorUtils::FormatErrorMessageUTF16(
173 errors::kInvalidImportVersion, base::IntToString(i));
174 return false;
175 }
176 imports_.back().minimum_version = min_version;
177 Version v(min_version);
178 if (!v.IsValid()) {
179 *error = ErrorUtils::FormatErrorMessageUTF16(
180 errors::kInvalidImportVersion, base::IntToString(i));
181 return false;
182 }
183 }
184 }
185 }
186 return true;
187 }
188
189
190 SharedModuleHandler::SharedModuleHandler() {
191 }
192
193 SharedModuleHandler::~SharedModuleHandler() {
194 }
195
196 bool SharedModuleHandler::Parse(Extension* extension, string16* error) {
197 scoped_ptr<SharedModuleInfo> info(new SharedModuleInfo);
198 if (!info->Parse(extension, error))
199 return false;
200 extension->SetManifestData(kSharedModule, info.release());
201 return true;
202 }
203
204 bool SharedModuleHandler::Validate(
205 const Extension* extension,
206 std::string* error,
207 std::vector<InstallWarning>* warnings) const {
208 // Extensions that export resources should not have any permissions of their
209 // own, instead they rely on the permissions of the extensions which import
210 // them.
211 if (SharedModuleInfo::IsSharedModule(extension) &&
212 !extension->GetActivePermissions()->IsEmpty()) {
213 *error = errors::kInvalidExportPermissions;
214 return false;
215 }
216 return true;
217 }
218
219 const std::vector<std::string> SharedModuleHandler::Keys() const {
220 static const char* keys[] = {
221 keys::kExport,
222 keys::kImport
223 };
224 return std::vector<std::string>(keys, keys + arraysize(keys));
225 }
226
227 } // extensions
228
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698