Remove unused parameter.
[chromium-blink-merge.git] / extensions / browser / sandboxed_unpacker.h
blob99a1ebbba84a5a0154bd92957ef1f980b93511c9
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_SANDBOXED_UNPACKER_H_
6 #define CHROME_BROWSER_EXTENSIONS_SANDBOXED_UNPACKER_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "content/public/browser/utility_process_host_client.h"
15 #include "extensions/browser/crx_file_info.h"
16 #include "extensions/browser/install/crx_install_error.h"
17 #include "extensions/common/manifest.h"
19 class SkBitmap;
21 namespace base {
22 class DictionaryValue;
23 class SequencedTaskRunner;
26 namespace crypto {
27 class SecureHash;
30 namespace extensions {
31 class Extension;
33 class SandboxedUnpackerClient
34 : public base::RefCountedThreadSafe<SandboxedUnpackerClient> {
35 public:
36 // temp_dir - A temporary directory containing the results of the extension
37 // unpacking. The client is responsible for deleting this directory.
39 // extension_root - The path to the extension root inside of temp_dir.
41 // original_manifest - The parsed but unmodified version of the manifest,
42 // with no modifications such as localization, etc.
44 // extension - The extension that was unpacked. The client is responsible
45 // for deleting this memory.
47 // install_icon - The icon we will display in the installation UI, if any.
48 virtual void OnUnpackSuccess(const base::FilePath& temp_dir,
49 const base::FilePath& extension_root,
50 const base::DictionaryValue* original_manifest,
51 const Extension* extension,
52 const SkBitmap& install_icon) = 0;
53 virtual void OnUnpackFailure(const CrxInstallError& error) = 0;
55 protected:
56 friend class base::RefCountedThreadSafe<SandboxedUnpackerClient>;
58 virtual ~SandboxedUnpackerClient() {}
61 // SandboxedUnpacker unpacks extensions from the CRX format into a
62 // directory. This is done in a sandboxed subprocess to protect the browser
63 // process from parsing complex formats like JPEG or JSON from untrusted
64 // sources.
66 // Unpacking an extension using this class makes minor changes to its source,
67 // such as transcoding all images to PNG, parsing all message catalogs
68 // and rewriting the manifest JSON. As such, it should not be used when the
69 // output is not intended to be given back to the author.
72 // Lifetime management:
74 // This class is ref-counted by each call it makes to itself on another thread,
75 // and by UtilityProcessHost.
77 // Additionally, we hold a reference to our own client so that it lives at least
78 // long enough to receive the result of unpacking.
81 // NOTE: This class should only be used on the file thread.
82 class SandboxedUnpacker : public content::UtilityProcessHostClient {
83 public:
84 // Unpacks the extension in |crx_path| into a temporary directory and calls
85 // |client| with the result. If |run_out_of_process| is provided, unpacking
86 // is done in a sandboxed subprocess. Otherwise, it is done in-process.
87 SandboxedUnpacker(
88 const CRXFileInfo& file,
89 Manifest::Location location,
90 int creation_flags,
91 const base::FilePath& extensions_dir,
92 const scoped_refptr<base::SequencedTaskRunner>& unpacker_io_task_runner,
93 SandboxedUnpackerClient* client);
95 // Start unpacking the extension. The client is called with the results.
96 void Start();
98 private:
99 class ProcessHostClient;
101 // Enumerate all the ways unpacking can fail. Calls to ReportFailure()
102 // take a failure reason as an argument, and put it in histogram
103 // Extensions.SandboxUnpackFailureReason.
104 enum FailureReason {
105 // SandboxedUnpacker::CreateTempDirectory()
106 COULD_NOT_GET_TEMP_DIRECTORY,
107 COULD_NOT_CREATE_TEMP_DIRECTORY,
109 // SandboxedUnpacker::Start()
110 FAILED_TO_COPY_EXTENSION_FILE_TO_TEMP_DIRECTORY,
111 COULD_NOT_GET_SANDBOX_FRIENDLY_PATH,
113 // SandboxedUnpacker::OnUnpackExtensionSucceeded()
114 COULD_NOT_LOCALIZE_EXTENSION,
115 INVALID_MANIFEST,
117 // SandboxedUnpacker::OnUnpackExtensionFailed()
118 UNPACKER_CLIENT_FAILED,
120 // SandboxedUnpacker::OnProcessCrashed()
121 UTILITY_PROCESS_CRASHED_WHILE_TRYING_TO_INSTALL,
123 // SandboxedUnpacker::ValidateSignature()
124 CRX_FILE_NOT_READABLE,
125 CRX_HEADER_INVALID,
126 CRX_MAGIC_NUMBER_INVALID,
127 CRX_VERSION_NUMBER_INVALID,
128 CRX_EXCESSIVELY_LARGE_KEY_OR_SIGNATURE,
129 CRX_ZERO_KEY_LENGTH,
130 CRX_ZERO_SIGNATURE_LENGTH,
131 CRX_PUBLIC_KEY_INVALID,
132 CRX_SIGNATURE_INVALID,
133 CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED,
134 CRX_SIGNATURE_VERIFICATION_FAILED,
136 // SandboxedUnpacker::RewriteManifestFile()
137 ERROR_SERIALIZING_MANIFEST_JSON,
138 ERROR_SAVING_MANIFEST_JSON,
140 // SandboxedUnpacker::RewriteImageFiles()
141 COULD_NOT_READ_IMAGE_DATA_FROM_DISK,
142 DECODED_IMAGES_DO_NOT_MATCH_THE_MANIFEST,
143 INVALID_PATH_FOR_BROWSER_IMAGE,
144 ERROR_REMOVING_OLD_IMAGE_FILE,
145 INVALID_PATH_FOR_BITMAP_IMAGE,
146 ERROR_RE_ENCODING_THEME_IMAGE,
147 ERROR_SAVING_THEME_IMAGE,
148 ABORTED_DUE_TO_SHUTDOWN,
150 // SandboxedUnpacker::RewriteCatalogFiles()
151 COULD_NOT_READ_CATALOG_DATA_FROM_DISK,
152 INVALID_CATALOG_DATA,
153 INVALID_PATH_FOR_CATALOG,
154 ERROR_SERIALIZING_CATALOG,
155 ERROR_SAVING_CATALOG,
157 // SandboxedUnpacker::ValidateSignature()
158 CRX_HASH_VERIFICATION_FAILED,
160 NUM_FAILURE_REASONS
163 friend class ProcessHostClient;
164 friend class SandboxedUnpackerTest;
166 ~SandboxedUnpacker() override;
168 // Set |temp_dir_| as a temporary directory to unpack the extension in.
169 // Return true on success.
170 virtual bool CreateTempDirectory();
172 // Finalizes hash calculation and checks the result against the expected
173 // package hash. In case of mismatch, depending on the command-line option,
174 // we will either fail installation, or just update histograms.
175 bool FinalizeHash(scoped_ptr<crypto::SecureHash>& hash);
177 // Validates the signature of the extension and extract the key to
178 // |public_key_|. Returns true if the signature validates, false otherwise.
180 // NOTE: Having this method here is a bit ugly. This code should really live
181 // in extensions::Unpacker as it is not specific to sandboxed unpacking. It
182 // was put here because we cannot run windows crypto code in the sandbox. But
183 // we could still have this method statically on extensions::Unpacker so that
184 // code just for unpacking is there and code just for sandboxing of unpacking
185 // is here.
186 bool ValidateSignature();
188 // Starts the utility process that unpacks our extension.
189 void StartProcessOnIOThread(const base::FilePath& temp_crx_path);
191 // UtilityProcessHostClient
192 bool OnMessageReceived(const IPC::Message& message) override;
193 void OnProcessCrashed(int exit_code) override;
195 // IPC message handlers.
196 void OnUnpackExtensionSucceeded(const base::DictionaryValue& manifest);
197 void OnUnpackExtensionFailed(const base::string16& error_message);
199 void ReportFailure(FailureReason reason, const base::string16& message);
200 void ReportSuccess(const base::DictionaryValue& original_manifest,
201 const SkBitmap& install_icon);
203 // Overwrites original manifest with safe result from utility process.
204 // Returns NULL on error. Caller owns the returned object.
205 base::DictionaryValue* RewriteManifestFile(
206 const base::DictionaryValue& manifest);
208 // Overwrites original files with safe results from utility process.
209 // Reports error and returns false if it fails.
210 bool RewriteImageFiles(SkBitmap* install_icon);
211 bool RewriteCatalogFiles();
213 // Cleans up temp directory artifacts.
214 void Cleanup();
216 // The path to the CRX to unpack.
217 base::FilePath crx_path_;
219 // The package SHA256 hash sum that was reported from the Web Store.
220 std::string package_hash_;
222 // Whether we need to check the .crx hash sum.
223 bool check_crx_hash_;
225 // Our client.
226 scoped_refptr<SandboxedUnpackerClient> client_;
228 // The Extensions directory inside the profile.
229 base::FilePath extensions_dir_;
231 // A temporary directory to use for unpacking.
232 base::ScopedTempDir temp_dir_;
234 // The root directory of the unpacked extension. This is a child of temp_dir_.
235 base::FilePath extension_root_;
237 // Represents the extension we're unpacking.
238 scoped_refptr<Extension> extension_;
240 // Whether we've received a response from the utility process yet.
241 bool got_response_;
243 // The public key that was extracted from the CRX header.
244 std::string public_key_;
246 // The extension's ID. This will be calculated from the public key in the crx
247 // header.
248 std::string extension_id_;
250 // Time at which unpacking started. Used to compute the time unpacking takes.
251 base::TimeTicks unpack_start_time_;
253 // Location to use for the unpacked extension.
254 Manifest::Location location_;
256 // Creation flags to use for the extension. These flags will be used
257 // when calling Extenion::Create() by the crx installer.
258 int creation_flags_;
260 // Sequenced task runner where file I/O operations will be performed at.
261 scoped_refptr<base::SequencedTaskRunner> unpacker_io_task_runner_;
264 } // namespace extensions
266 #endif // CHROME_BROWSER_EXTENSIONS_SANDBOXED_UNPACKER_H_