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