Connect PPAPI IPC channels for non-SFI mode.
[chromium-blink-merge.git] / extensions / common / manifest_handlers / shared_module_info.cc
blob5f4416099271c38d4565c835eb2f68b9cbe65af0
1 // Copyright 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.
5 #include "extensions/common/manifest_handlers/shared_module_info.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/version.h"
13 #include "extensions/common/constants.h"
14 #include "extensions/common/error_utils.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "extensions/common/permissions/permission_set.h"
18 namespace extensions {
20 namespace keys = manifest_keys;
21 namespace values = manifest_values;
22 namespace errors = manifest_errors;
24 namespace {
26 const char kSharedModule[] = "shared_module";
28 static base::LazyInstance<SharedModuleInfo> g_empty_shared_module_info =
29 LAZY_INSTANCE_INITIALIZER;
31 const SharedModuleInfo& GetSharedModuleInfo(const Extension* extension) {
32 SharedModuleInfo* info = static_cast<SharedModuleInfo*>(
33 extension->GetManifestData(kSharedModule));
34 if (!info)
35 return g_empty_shared_module_info.Get();
36 return *info;
39 } // namespace
41 SharedModuleInfo::SharedModuleInfo() {
44 SharedModuleInfo::~SharedModuleInfo() {
47 // static
48 void SharedModuleInfo::ParseImportedPath(const std::string& path,
49 std::string* import_id,
50 std::string* import_relative_path) {
51 std::vector<std::string> tokens;
52 Tokenize(path, std::string("/"), &tokens);
53 if (tokens.size() > 2 && tokens[0] == kModulesDir &&
54 Extension::IdIsValid(tokens[1])) {
55 *import_id = tokens[1];
56 *import_relative_path = tokens[2];
57 for (size_t i = 3; i < tokens.size(); ++i)
58 *import_relative_path += "/" + tokens[i];
62 // static
63 bool SharedModuleInfo::IsImportedPath(const std::string& path) {
64 std::vector<std::string> tokens;
65 Tokenize(path, std::string("/"), &tokens);
66 if (tokens.size() > 2 && tokens[0] == kModulesDir &&
67 Extension::IdIsValid(tokens[1])) {
68 return true;
70 return false;
73 // static
74 bool SharedModuleInfo::IsSharedModule(const Extension* extension) {
75 CHECK(extension);
76 return extension->manifest()->is_shared_module();
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));
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;
94 return false;
97 // static
98 bool SharedModuleInfo::ImportsModules(const Extension* extension) {
99 return GetSharedModuleInfo(extension).imports_.size() > 0;
102 // static
103 const std::vector<SharedModuleInfo::ImportInfo>& SharedModuleInfo::GetImports(
104 const Extension* extension) {
105 return GetSharedModuleInfo(extension).imports_;
108 bool SharedModuleInfo::Parse(const Extension* extension,
109 base::string16* error) {
110 bool has_import = extension->manifest()->HasKey(keys::kImport);
111 bool has_export = extension->manifest()->HasKey(keys::kExport);
112 if (!has_import && !has_export)
113 return true;
115 if (has_import && has_export) {
116 *error = base::ASCIIToUTF16(errors::kInvalidImportAndExport);
117 return false;
120 if (has_export) {
121 const base::DictionaryValue* export_value = NULL;
122 if (!extension->manifest()->GetDictionary(keys::kExport, &export_value)) {
123 *error = base::ASCIIToUTF16(errors::kInvalidExport);
124 return false;
126 const base::ListValue* resources_list = NULL;
127 if (!export_value->GetList(keys::kResources, &resources_list)) {
128 *error = base::ASCIIToUTF16(errors::kInvalidExportResources);
129 return false;
131 for (size_t i = 0; i < resources_list->GetSize(); ++i) {
132 std::string resource_path;
133 if (!resources_list->GetString(i, &resource_path)) {
134 *error = ErrorUtils::FormatErrorMessageUTF16(
135 errors::kInvalidExportResourcesString, base::IntToString(i));
136 return false;
138 const GURL& resolved_path = extension->url().Resolve(resource_path);
139 if (!resolved_path.is_valid()) {
140 *error = ErrorUtils::FormatErrorMessageUTF16(
141 errors::kInvalidExportResourcesString, base::IntToString(i));
142 return false;
144 exported_set_.AddPattern(
145 URLPattern(URLPattern::SCHEME_EXTENSION, resolved_path.spec()));
149 if (has_import) {
150 const base::ListValue* import_list = NULL;
151 if (!extension->manifest()->GetList(keys::kImport, &import_list)) {
152 *error = base::ASCIIToUTF16(errors::kInvalidImport);
153 return false;
155 for (size_t i = 0; i < import_list->GetSize(); ++i) {
156 const base::DictionaryValue* import_entry = NULL;
157 if (!import_list->GetDictionary(i, &import_entry)) {
158 *error = base::ASCIIToUTF16(errors::kInvalidImport);
159 return false;
161 std::string extension_id;
162 imports_.push_back(ImportInfo());
163 if (!import_entry->GetString(keys::kId, &extension_id) ||
164 !Extension::IdIsValid(extension_id)) {
165 *error = ErrorUtils::FormatErrorMessageUTF16(
166 errors::kInvalidImportId, base::IntToString(i));
167 return false;
169 imports_.back().extension_id = extension_id;
170 if (import_entry->HasKey(keys::kMinimumVersion)) {
171 std::string min_version;
172 if (!import_entry->GetString(keys::kMinimumVersion, &min_version)) {
173 *error = ErrorUtils::FormatErrorMessageUTF16(
174 errors::kInvalidImportVersion, base::IntToString(i));
175 return false;
177 imports_.back().minimum_version = min_version;
178 Version v(min_version);
179 if (!v.IsValid()) {
180 *error = ErrorUtils::FormatErrorMessageUTF16(
181 errors::kInvalidImportVersion, base::IntToString(i));
182 return false;
187 return true;
191 SharedModuleHandler::SharedModuleHandler() {
194 SharedModuleHandler::~SharedModuleHandler() {
197 bool SharedModuleHandler::Parse(Extension* extension, base::string16* error) {
198 scoped_ptr<SharedModuleInfo> info(new SharedModuleInfo);
199 if (!info->Parse(extension, error))
200 return false;
201 extension->SetManifestData(kSharedModule, info.release());
202 return true;
205 bool SharedModuleHandler::Validate(
206 const Extension* extension,
207 std::string* error,
208 std::vector<InstallWarning>* warnings) const {
209 // Extensions that export resources should not have any permissions of their
210 // own, instead they rely on the permissions of the extensions which import
211 // them.
212 if (SharedModuleInfo::IsSharedModule(extension) &&
213 !extension->GetActivePermissions()->IsEmpty()) {
214 *error = errors::kInvalidExportPermissions;
215 return false;
217 return true;
220 const std::vector<std::string> SharedModuleHandler::Keys() const {
221 static const char* keys[] = {
222 keys::kExport,
223 keys::kImport
225 return std::vector<std::string>(keys, keys + arraysize(keys));
228 } // namespace extensions