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.
5 #include "chrome/common/extensions/extension_file_util.h"
10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/path_service.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/extensions/api/extension_action/action_info.h"
23 #include "chrome/common/extensions/extension_icon_set.h"
24 #include "chrome/common/extensions/extension_l10n_util.h"
25 #include "chrome/common/extensions/extension_messages.h"
26 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
27 #include "chrome/common/extensions/manifest_handlers/theme_handler.h"
28 #include "chrome/common/extensions/message_bundle.h"
29 #include "extensions/common/constants.h"
30 #include "extensions/common/extension.h"
31 #include "extensions/common/extension_resource.h"
32 #include "extensions/common/install_warning.h"
33 #include "extensions/common/manifest.h"
34 #include "extensions/common/manifest_constants.h"
35 #include "extensions/common/manifest_handler.h"
36 #include "grit/generated_resources.h"
37 #include "net/base/file_stream.h"
38 #include "ui/base/l10n/l10n_util.h"
40 using extensions::Extension
;
41 using extensions::ExtensionResource
;
42 using extensions::Manifest
;
44 namespace errors
= extensions::manifest_errors
;
48 const base::FilePath::CharType kTempDirectoryName
[] = FILE_PATH_LITERAL("Temp");
50 // Add the image paths contained in the |icon_set| to |image_paths|.
51 void AddPathsFromIconSet(const ExtensionIconSet
& icon_set
,
52 std::set
<base::FilePath
>* image_paths
) {
53 // TODO(viettrungluu): These |FilePath::FromUTF8Unsafe()| indicate that we're
54 // doing something wrong.
55 for (ExtensionIconSet::IconMap::const_iterator iter
= icon_set
.map().begin();
56 iter
!= icon_set
.map().end(); ++iter
) {
57 image_paths
->insert(base::FilePath::FromUTF8Unsafe(iter
->second
));
63 namespace extension_file_util
{
65 base::FilePath
InstallExtension(const base::FilePath
& unpacked_source_dir
,
66 const std::string
& id
,
67 const std::string
& version
,
68 const base::FilePath
& extensions_dir
) {
69 base::FilePath extension_dir
= extensions_dir
.AppendASCII(id
);
70 base::FilePath version_dir
;
72 // Create the extension directory if it doesn't exist already.
73 if (!base::PathExists(extension_dir
)) {
74 if (!base::CreateDirectory(extension_dir
))
75 return base::FilePath();
78 // Get a temp directory on the same file system as the profile.
79 base::FilePath install_temp_dir
= GetInstallTempDir(extensions_dir
);
80 base::ScopedTempDir extension_temp_dir
;
81 if (install_temp_dir
.empty() ||
82 !extension_temp_dir
.CreateUniqueTempDirUnderPath(install_temp_dir
)) {
83 LOG(ERROR
) << "Creating of temp dir under in the profile failed.";
84 return base::FilePath();
86 base::FilePath crx_temp_source
=
87 extension_temp_dir
.path().Append(unpacked_source_dir
.BaseName());
88 if (!base::Move(unpacked_source_dir
, crx_temp_source
)) {
89 LOG(ERROR
) << "Moving extension from : " << unpacked_source_dir
.value()
90 << " to : " << crx_temp_source
.value() << " failed.";
91 return base::FilePath();
94 // Try to find a free directory. There can be legitimate conflicts in the case
95 // of overinstallation of the same version.
96 const int kMaxAttempts
= 100;
97 for (int i
= 0; i
< kMaxAttempts
; ++i
) {
98 base::FilePath candidate
= extension_dir
.AppendASCII(
99 base::StringPrintf("%s_%u", version
.c_str(), i
));
100 if (!base::PathExists(candidate
)) {
101 version_dir
= candidate
;
106 if (version_dir
.empty()) {
107 LOG(ERROR
) << "Could not find a home for extension " << id
<< " with "
108 << "version " << version
<< ".";
109 return base::FilePath();
112 if (!base::Move(crx_temp_source
, version_dir
)) {
113 LOG(ERROR
) << "Installing extension from : " << crx_temp_source
.value()
114 << " into : " << version_dir
.value() << " failed.";
115 return base::FilePath();
121 void UninstallExtension(const base::FilePath
& extensions_dir
,
122 const std::string
& id
) {
123 // We don't care about the return value. If this fails (and it can, due to
124 // plugins that aren't unloaded yet), it will get cleaned up by
125 // ExtensionService::GarbageCollectExtensions.
126 base::DeleteFile(extensions_dir
.AppendASCII(id
), true); // recursive.
129 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_path
,
130 Manifest::Location location
,
132 std::string
* error
) {
133 return LoadExtension(extension_path
, std::string(), location
, flags
, error
);
136 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_path
,
137 const std::string
& extension_id
,
138 Manifest::Location location
,
140 std::string
* error
) {
141 scoped_ptr
<base::DictionaryValue
> manifest(
142 LoadManifest(extension_path
, error
));
145 if (!extension_l10n_util::LocalizeExtension(extension_path
, manifest
.get(),
150 scoped_refptr
<Extension
> extension(Extension::Create(extension_path
,
156 if (!extension
.get())
159 std::vector
<extensions::InstallWarning
> warnings
;
160 if (!ValidateExtension(extension
.get(), error
, &warnings
))
162 extension
->AddInstallWarnings(warnings
);
167 base::DictionaryValue
* LoadManifest(const base::FilePath
& extension_path
,
168 std::string
* error
) {
169 base::FilePath manifest_path
=
170 extension_path
.Append(extensions::kManifestFilename
);
171 if (!base::PathExists(manifest_path
)) {
172 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE
);
176 JSONFileValueSerializer
serializer(manifest_path
);
177 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, error
));
179 if (error
->empty()) {
180 // If |error| is empty, than the file could not be read.
181 // It would be cleaner to have the JSON reader give a specific error
182 // in this case, but other code tests for a file error with
183 // error->empty(). For now, be consistent.
184 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE
);
186 *error
= base::StringPrintf("%s %s",
187 errors::kManifestParseError
,
193 if (!root
->IsType(base::Value::TYPE_DICTIONARY
)) {
194 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID
);
198 return static_cast<base::DictionaryValue
*>(root
.release());
201 std::vector
<base::FilePath
> FindPrivateKeyFiles(
202 const base::FilePath
& extension_dir
) {
203 std::vector
<base::FilePath
> result
;
204 // Pattern matching only works at the root level, so filter manually.
205 base::FileEnumerator
traversal(extension_dir
, /*recursive=*/true,
206 base::FileEnumerator::FILES
);
207 for (base::FilePath current
= traversal
.Next(); !current
.empty();
208 current
= traversal
.Next()) {
209 if (!current
.MatchesExtension(extensions::kExtensionKeyFileExtension
))
212 std::string key_contents
;
213 if (!base::ReadFileToString(current
, &key_contents
)) {
214 // If we can't read the file, assume it's not a private key.
217 std::string key_bytes
;
218 if (!Extension::ParsePEMKeyBytes(key_contents
, &key_bytes
)) {
219 // If we can't parse the key, assume it's ok too.
223 result
.push_back(current
);
228 bool ValidateFilePath(const base::FilePath
& path
) {
230 if (!base::PathExists(path
) ||
231 !base::GetFileSize(path
, &size
) ||
239 bool ValidateExtensionIconSet(const ExtensionIconSet
& icon_set
,
240 const Extension
* extension
,
241 int error_message_id
,
242 std::string
* error
) {
243 for (ExtensionIconSet::IconMap::const_iterator iter
= icon_set
.map().begin();
244 iter
!= icon_set
.map().end();
246 const base::FilePath path
=
247 extension
->GetResource(iter
->second
).GetFilePath();
248 if (!ValidateFilePath(path
)) {
249 *error
= l10n_util::GetStringFUTF8(error_message_id
,
250 base::UTF8ToUTF16(iter
->second
));
257 bool ValidateExtension(const Extension
* extension
,
259 std::vector
<extensions::InstallWarning
>* warnings
) {
260 // Ask registered manifest handlers to validate their paths.
261 if (!extensions::ManifestHandler::ValidateExtension(
262 extension
, error
, warnings
))
265 // Check children of extension root to see if any of them start with _ and is
266 // not on the reserved list. We only warn, and do not block the loading of the
269 if (!CheckForIllegalFilenames(extension
->path(), &warning
))
270 warnings
->push_back(extensions::InstallWarning(warning
));
272 // Check that extensions don't include private key files.
273 std::vector
<base::FilePath
> private_keys
=
274 FindPrivateKeyFiles(extension
->path());
275 if (extension
->creation_flags() & Extension::ERROR_ON_PRIVATE_KEY
) {
276 if (!private_keys
.empty()) {
277 // Only print one of the private keys because l10n_util doesn't have a way
278 // to translate a list of strings.
279 *error
= l10n_util::GetStringFUTF8(
280 IDS_EXTENSION_CONTAINS_PRIVATE_KEY
,
281 private_keys
.front().LossyDisplayName());
285 for (size_t i
= 0; i
< private_keys
.size(); ++i
) {
286 warnings
->push_back(extensions::InstallWarning(
287 l10n_util::GetStringFUTF8(
288 IDS_EXTENSION_CONTAINS_PRIVATE_KEY
,
289 private_keys
[i
].LossyDisplayName())));
291 // Only warn; don't block loading the extension.
296 std::set
<base::FilePath
> GetBrowserImagePaths(const Extension
* extension
) {
297 std::set
<base::FilePath
> image_paths
;
299 AddPathsFromIconSet(extensions::IconsInfo::GetIcons(extension
), &image_paths
);
302 const base::DictionaryValue
* theme_images
=
303 extensions::ThemeInfo::GetImages(extension
);
305 for (base::DictionaryValue::Iterator
it(*theme_images
); !it
.IsAtEnd();
307 base::FilePath::StringType path
;
308 if (it
.value().GetAsString(&path
))
309 image_paths
.insert(base::FilePath(path
));
313 const extensions::ActionInfo
* page_action
=
314 extensions::ActionInfo::GetPageActionInfo(extension
);
315 if (page_action
&& !page_action
->default_icon
.empty())
316 AddPathsFromIconSet(page_action
->default_icon
, &image_paths
);
318 const extensions::ActionInfo
* browser_action
=
319 extensions::ActionInfo::GetBrowserActionInfo(extension
);
320 if (browser_action
&& !browser_action
->default_icon
.empty())
321 AddPathsFromIconSet(browser_action
->default_icon
, &image_paths
);
326 void GarbageCollectExtensions(
327 const base::FilePath
& install_directory
,
328 const std::multimap
<std::string
, base::FilePath
>& extension_paths
,
329 bool clean_temp_dir
) {
330 // Nothing to clean up if it doesn't exist.
331 if (!base::DirectoryExists(install_directory
))
334 DVLOG(1) << "Garbage collecting extensions...";
335 base::FileEnumerator
enumerator(install_directory
,
336 false, // Not recursive.
337 base::FileEnumerator::DIRECTORIES
);
338 base::FilePath extension_path
;
339 for (extension_path
= enumerator
.Next(); !extension_path
.value().empty();
340 extension_path
= enumerator
.Next()) {
341 std::string extension_id
;
343 base::FilePath basename
= extension_path
.BaseName();
344 // Clean up temporary files left if Chrome crashed or quit in the middle
345 // of an extension install.
346 if (basename
.value() == kTempDirectoryName
) {
348 base::DeleteFile(extension_path
, true); // Recursive
352 // Parse directory name as a potential extension ID.
353 if (IsStringASCII(basename
.value())) {
354 extension_id
= UTF16ToASCII(basename
.LossyDisplayName());
355 if (!Extension::IdIsValid(extension_id
))
356 extension_id
.clear();
359 // Delete directories that aren't valid IDs.
360 if (extension_id
.empty()) {
361 DLOG(WARNING
) << "Invalid extension ID encountered in extensions "
362 "directory: " << basename
.value();
363 DVLOG(1) << "Deleting invalid extension directory "
364 << extension_path
.value() << ".";
365 base::DeleteFile(extension_path
, true); // Recursive.
369 typedef std::multimap
<std::string
, base::FilePath
>::const_iterator Iter
;
370 std::pair
<Iter
, Iter
> iter_pair
= extension_paths
.equal_range(extension_id
);
372 // If there is no entry in the prefs file, just delete the directory and
373 // move on. This can legitimately happen when an uninstall does not
374 // complete, for example, when a plugin is in use at uninstall time.
375 if (iter_pair
.first
== iter_pair
.second
) {
376 DVLOG(1) << "Deleting unreferenced install for directory "
377 << extension_path
.LossyDisplayName() << ".";
378 base::DeleteFile(extension_path
, true); // Recursive.
382 // Clean up old version directories.
383 base::FileEnumerator
versions_enumerator(
385 false, // Not recursive.
386 base::FileEnumerator::DIRECTORIES
);
387 for (base::FilePath version_dir
= versions_enumerator
.Next();
388 !version_dir
.value().empty();
389 version_dir
= versions_enumerator
.Next()) {
390 bool knownVersion
= false;
391 for (Iter it
= iter_pair
.first
; it
!= iter_pair
.second
; ++it
)
392 if (version_dir
.BaseName() == it
->second
.BaseName()) {
397 DVLOG(1) << "Deleting old version for directory "
398 << version_dir
.LossyDisplayName() << ".";
399 base::DeleteFile(version_dir
, true); // Recursive.
405 extensions::MessageBundle
* LoadMessageBundle(
406 const base::FilePath
& extension_path
,
407 const std::string
& default_locale
,
408 std::string
* error
) {
410 // Load locale information if available.
411 base::FilePath locale_path
= extension_path
.Append(extensions::kLocaleFolder
);
412 if (!base::PathExists(locale_path
))
415 std::set
<std::string
> locales
;
416 if (!extension_l10n_util::GetValidLocales(locale_path
, &locales
, error
))
419 if (default_locale
.empty() ||
420 locales
.find(default_locale
) == locales
.end()) {
421 *error
= l10n_util::GetStringUTF8(
422 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED
);
426 extensions::MessageBundle
* message_bundle
=
427 extension_l10n_util::LoadMessageCatalogs(
430 extension_l10n_util::CurrentLocaleOrDefault(),
434 return message_bundle
;
437 SubstitutionMap
* LoadMessageBundleSubstitutionMap(
438 const base::FilePath
& extension_path
,
439 const std::string
& extension_id
,
440 const std::string
& default_locale
) {
441 SubstitutionMap
* returnValue
= new SubstitutionMap();
442 if (!default_locale
.empty()) {
443 // Touch disk only if extension is localized.
445 scoped_ptr
<extensions::MessageBundle
> bundle(
446 LoadMessageBundle(extension_path
, default_locale
, &error
));
449 *returnValue
= *bundle
->dictionary();
452 // Add @@extension_id reserved message here, so it's available to
453 // non-localized extensions too.
455 std::make_pair(extensions::MessageBundle::kExtensionIdKey
, extension_id
));
460 bool CheckForIllegalFilenames(const base::FilePath
& extension_path
,
461 std::string
* error
) {
462 // Reserved underscore names.
463 static const base::FilePath::CharType
* reserved_names
[] = {
464 extensions::kLocaleFolder
,
465 extensions::kPlatformSpecificFolder
,
466 FILE_PATH_LITERAL("__MACOSX"),
468 CR_DEFINE_STATIC_LOCAL(
469 std::set
<base::FilePath::StringType
>, reserved_underscore_names
,
470 (reserved_names
, reserved_names
+ arraysize(reserved_names
)));
472 // Enumerate all files and directories in the extension root.
473 // There is a problem when using pattern "_*" with FileEnumerator, so we have
474 // to cheat with find_first_of and match all.
475 const int kFilesAndDirectories
=
476 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
;
477 base::FileEnumerator
all_files(extension_path
, false, kFilesAndDirectories
);
480 while (!(file
= all_files
.Next()).empty()) {
481 base::FilePath::StringType filename
= file
.BaseName().value();
482 // Skip all that don't start with "_".
483 if (filename
.find_first_of(FILE_PATH_LITERAL("_")) != 0) continue;
484 if (reserved_underscore_names
.find(filename
) ==
485 reserved_underscore_names
.end()) {
486 *error
= base::StringPrintf(
487 "Cannot load extension with file or directory name %s. "
488 "Filenames starting with \"_\" are reserved for use by the system.",
489 file
.BaseName().AsUTF8Unsafe().c_str());
497 base::FilePath
GetInstallTempDir(const base::FilePath
& extensions_dir
) {
498 // We do file IO in this function, but only when the current profile's
499 // Temp directory has never been used before, or in a rare error case.
500 // Developers are not likely to see these situations often, so do an
501 // explicit thread check.
502 base::ThreadRestrictions::AssertIOAllowed();
504 // Create the temp directory as a sub-directory of the Extensions directory.
505 // This guarantees it is on the same file system as the extension's eventual
507 base::FilePath temp_path
= extensions_dir
.Append(kTempDirectoryName
);
508 if (base::PathExists(temp_path
)) {
509 if (!base::DirectoryExists(temp_path
)) {
510 DLOG(WARNING
) << "Not a directory: " << temp_path
.value();
511 return base::FilePath();
513 if (!base::PathIsWritable(temp_path
)) {
514 DLOG(WARNING
) << "Can't write to path: " << temp_path
.value();
515 return base::FilePath();
517 // This is a directory we can write to.
521 // Directory doesn't exist, so create it.
522 if (!base::CreateDirectory(temp_path
)) {
523 DLOG(WARNING
) << "Couldn't create directory: " << temp_path
.value();
524 return base::FilePath();
529 void DeleteFile(const base::FilePath
& path
, bool recursive
) {
530 base::DeleteFile(path
, recursive
);
533 } // namespace extension_file_util