OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
13 #include "base/json/json_file_value_serializer.h" | 14 #include "base/json/json_file_value_serializer.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
20 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/extensions/api/extension_action/action_info.h" | 22 #include "chrome/common/extensions/api/extension_action/action_info.h" |
(...skipping 15 matching lines...) Expand all Loading... |
37 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
38 | 39 |
39 using extensions::Extension; | 40 using extensions::Extension; |
40 using extensions::ExtensionResource; | 41 using extensions::ExtensionResource; |
41 using extensions::Manifest; | 42 using extensions::Manifest; |
42 | 43 |
43 namespace errors = extensions::manifest_errors; | 44 namespace errors = extensions::manifest_errors; |
44 | 45 |
45 namespace { | 46 namespace { |
46 | 47 |
| 48 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); |
| 49 |
47 // Add the image paths contained in the |icon_set| to |image_paths|. | 50 // Add the image paths contained in the |icon_set| to |image_paths|. |
48 void AddPathsFromIconSet(const ExtensionIconSet& icon_set, | 51 void AddPathsFromIconSet(const ExtensionIconSet& icon_set, |
49 std::set<base::FilePath>* image_paths) { | 52 std::set<base::FilePath>* image_paths) { |
50 // TODO(viettrungluu): These |FilePath::FromUTF8Unsafe()| indicate that we're | 53 // TODO(viettrungluu): These |FilePath::FromUTF8Unsafe()| indicate that we're |
51 // doing something wrong. | 54 // doing something wrong. |
52 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set.map().begin(); | 55 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set.map().begin(); |
53 iter != icon_set.map().end(); ++iter) { | 56 iter != icon_set.map().end(); ++iter) { |
54 image_paths->insert(base::FilePath::FromUTF8Unsafe(iter->second)); | 57 image_paths->insert(base::FilePath::FromUTF8Unsafe(iter->second)); |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 } // namespace | 61 } // namespace |
59 | 62 |
60 namespace extension_file_util { | 63 namespace extension_file_util { |
61 | 64 |
62 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); | |
63 | |
64 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, | 65 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, |
65 const std::string& id, | 66 const std::string& id, |
66 const std::string& version, | 67 const std::string& version, |
67 const base::FilePath& extensions_dir) { | 68 const base::FilePath& extensions_dir) { |
68 base::FilePath extension_dir = extensions_dir.AppendASCII(id); | 69 base::FilePath extension_dir = extensions_dir.AppendASCII(id); |
69 base::FilePath version_dir; | 70 base::FilePath version_dir; |
70 | 71 |
71 // Create the extension directory if it doesn't exist already. | 72 // Create the extension directory if it doesn't exist already. |
72 if (!base::PathExists(extension_dir)) { | 73 if (!base::PathExists(extension_dir)) { |
73 if (!base::CreateDirectory(extension_dir)) | 74 if (!base::CreateDirectory(extension_dir)) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return base::FilePath(); | 115 return base::FilePath(); |
115 } | 116 } |
116 | 117 |
117 return version_dir; | 118 return version_dir; |
118 } | 119 } |
119 | 120 |
120 void UninstallExtension(const base::FilePath& extensions_dir, | 121 void UninstallExtension(const base::FilePath& extensions_dir, |
121 const std::string& id) { | 122 const std::string& id) { |
122 // We don't care about the return value. If this fails (and it can, due to | 123 // We don't care about the return value. If this fails (and it can, due to |
123 // plugins that aren't unloaded yet), it will get cleaned up by | 124 // plugins that aren't unloaded yet), it will get cleaned up by |
124 // ExtensionGarbageCollector::GarbageCollectExtensions. | 125 // ExtensionService::GarbageCollectExtensions. |
125 base::DeleteFile(extensions_dir.AppendASCII(id), true); // recursive. | 126 base::DeleteFile(extensions_dir.AppendASCII(id), true); // recursive. |
126 } | 127 } |
127 | 128 |
128 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, | 129 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, |
129 Manifest::Location location, | 130 Manifest::Location location, |
130 int flags, | 131 int flags, |
131 std::string* error) { | 132 std::string* error) { |
132 return LoadExtension(extension_path, std::string(), location, flags, error); | 133 return LoadExtension(extension_path, std::string(), location, flags, error); |
133 } | 134 } |
134 | 135 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 AddPathsFromIconSet(page_action->default_icon, &image_paths); | 316 AddPathsFromIconSet(page_action->default_icon, &image_paths); |
316 | 317 |
317 const extensions::ActionInfo* browser_action = | 318 const extensions::ActionInfo* browser_action = |
318 extensions::ActionInfo::GetBrowserActionInfo(extension); | 319 extensions::ActionInfo::GetBrowserActionInfo(extension); |
319 if (browser_action && !browser_action->default_icon.empty()) | 320 if (browser_action && !browser_action->default_icon.empty()) |
320 AddPathsFromIconSet(browser_action->default_icon, &image_paths); | 321 AddPathsFromIconSet(browser_action->default_icon, &image_paths); |
321 | 322 |
322 return image_paths; | 323 return image_paths; |
323 } | 324 } |
324 | 325 |
| 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)) |
| 332 return; |
| 333 |
| 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; |
| 342 |
| 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) { |
| 347 if (clean_temp_dir) |
| 348 base::DeleteFile(extension_path, true); // Recursive |
| 349 continue; |
| 350 } |
| 351 |
| 352 // Parse directory name as a potential extension ID. |
| 353 if (IsStringASCII(basename.value())) { |
| 354 extension_id = base::UTF16ToASCII(basename.LossyDisplayName()); |
| 355 if (!Extension::IdIsValid(extension_id)) |
| 356 extension_id.clear(); |
| 357 } |
| 358 |
| 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. |
| 366 continue; |
| 367 } |
| 368 |
| 369 typedef std::multimap<std::string, base::FilePath>::const_iterator Iter; |
| 370 std::pair<Iter, Iter> iter_pair = extension_paths.equal_range(extension_id); |
| 371 |
| 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. |
| 379 continue; |
| 380 } |
| 381 |
| 382 // Clean up old version directories. |
| 383 base::FileEnumerator versions_enumerator( |
| 384 extension_path, |
| 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()) { |
| 393 knownVersion = true; |
| 394 break; |
| 395 } |
| 396 if (!knownVersion) { |
| 397 DVLOG(1) << "Deleting old version for directory " |
| 398 << version_dir.LossyDisplayName() << "."; |
| 399 base::DeleteFile(version_dir, true); // Recursive. |
| 400 } |
| 401 } |
| 402 } |
| 403 } |
| 404 |
325 extensions::MessageBundle* LoadMessageBundle( | 405 extensions::MessageBundle* LoadMessageBundle( |
326 const base::FilePath& extension_path, | 406 const base::FilePath& extension_path, |
327 const std::string& default_locale, | 407 const std::string& default_locale, |
328 std::string* error) { | 408 std::string* error) { |
329 error->clear(); | 409 error->clear(); |
330 // Load locale information if available. | 410 // Load locale information if available. |
331 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); | 411 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); |
332 if (!base::PathExists(locale_path)) | 412 if (!base::PathExists(locale_path)) |
333 return NULL; | 413 return NULL; |
334 | 414 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 return base::FilePath(); | 524 return base::FilePath(); |
445 } | 525 } |
446 return temp_path; | 526 return temp_path; |
447 } | 527 } |
448 | 528 |
449 void DeleteFile(const base::FilePath& path, bool recursive) { | 529 void DeleteFile(const base::FilePath& path, bool recursive) { |
450 base::DeleteFile(path, recursive); | 530 base::DeleteFile(path, recursive); |
451 } | 531 } |
452 | 532 |
453 } // namespace extension_file_util | 533 } // namespace extension_file_util |
OLD | NEW |