zipfile_installer.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 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. #ifndef EXTENSIONS_BROWSER_ZIPFILE_INSTALLER_H_
  5. #define EXTENSIONS_BROWSER_ZIPFILE_INSTALLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/values.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace extensions {
  17. // ZipFileInstaller unzips an extension safely using the Unzipper and
  18. // SafeJSONParser services.
  19. // This class is not thread-safe: it is bound to the sequence it is created on.
  20. class ZipFileInstaller : public base::RefCountedThreadSafe<ZipFileInstaller> {
  21. public:
  22. // The callback invoked when the ZIP file installation is finished.
  23. // On success, |unzip_dir| points to the directory the ZIP file was installed
  24. // and |error| is empty. On failure, |unzip_dir| is empty and |error| contains
  25. // an error message describing the failure.
  26. using DoneCallback = base::OnceCallback<void(const base::FilePath& zip_file,
  27. const base::FilePath& unzip_dir,
  28. const std::string& error)>;
  29. ZipFileInstaller(const ZipFileInstaller&) = delete;
  30. ZipFileInstaller& operator=(const ZipFileInstaller&) = delete;
  31. // Creates a ZipFileInstaller that invokes |done_callback| when done.
  32. static scoped_refptr<ZipFileInstaller> Create(
  33. const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
  34. DoneCallback done_callback);
  35. // Creates a temporary directory and unzips the extension in it.
  36. void LoadFromZipFile(const base::FilePath& zip_file);
  37. // Unzips the extension in |unzip_dir|.
  38. void LoadFromZipFileInDir(const base::FilePath& zip_file,
  39. const base::FilePath& unzip_dir);
  40. private:
  41. friend class base::RefCountedThreadSafe<ZipFileInstaller>;
  42. FRIEND_TEST_ALL_PREFIXES(ZipFileInstallerTest, NonTheme_FileExtractionFilter);
  43. FRIEND_TEST_ALL_PREFIXES(ZipFileInstallerTest, Theme_FileExtractionFilter);
  44. FRIEND_TEST_ALL_PREFIXES(ZipFileInstallerTest, ManifestExtractionFilter);
  45. explicit ZipFileInstaller(
  46. const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
  47. DoneCallback done_callback);
  48. ~ZipFileInstaller();
  49. void LoadFromZipFileImpl(const base::FilePath& zip_file,
  50. const base::FilePath& unzip_dir);
  51. // Unzip an extension into |unzip_dir| and load it with an UnpackedInstaller.
  52. void Unzip(absl::optional<base::FilePath> unzip_dir);
  53. void ManifestUnzipped(const base::FilePath& unzip_dir, bool success);
  54. void ManifestRead(const base::FilePath& unzip_dir,
  55. absl::optional<std::string> manifest_content);
  56. void ManifestParsed(const base::FilePath& unzip_dir,
  57. absl::optional<base::Value> result,
  58. const absl::optional<std::string>& error);
  59. void UnzipDone(const base::FilePath& unzip_dir, bool success);
  60. // On failure, report the |error| reason.
  61. void ReportFailure(const std::string& error);
  62. // Callback invoked when unzipping has finished.
  63. DoneCallback done_callback_;
  64. // Whether a file should be extracted as part of installing an
  65. // extension/theme. Protects against unused or potentially hamrful files.
  66. static bool ShouldExtractFile(bool is_theme, const base::FilePath& file_path);
  67. // Returns true if |file_path| points to an extension manifest.
  68. static bool IsManifestFile(const base::FilePath& file_path);
  69. // File containing the extension to unzip.
  70. base::FilePath zip_file_;
  71. // Task runner for file I/O.
  72. scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
  73. SEQUENCE_CHECKER(sequence_checker_);
  74. };
  75. } // namespace extensions
  76. #endif // EXTENSIONS_BROWSER_ZIPFILE_INSTALLER_H_