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_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_
6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/sequenced_task_runner.h"
19 namespace component_updater {
21 class ComponentInstaller;
22 class ComponentPatcher;
24 // Deserializes the CRX manifest. The top level must be a dictionary.
25 scoped_ptr<base::DictionaryValue> ReadManifest(
26 const base::FilePath& unpack_path);
28 // In charge of unpacking the component CRX package and verifying that it is
29 // well formed and the cryptographic signature is correct. If there is no
30 // error the component specific installer will be invoked to proceed with
31 // the component installation or update.
33 // This class should be used only by the component updater. It is inspired by
34 // and overlaps with code in the extension's SandboxedUnpacker.
35 // The main differences are:
36 // - The public key hash is full SHA256.
37 // - Does not use a sandboxed unpacker. A valid component is fully trusted.
38 // - The manifest can have different attributes and resources are not
41 // If the CRX is a delta CRX, the flow is:
42 // [ComponentUpdater] [ComponentPatcher]
46 // \_ BeginPatching ---> DifferentialUpdatePatch
48 // EndPatching <------------ ...
52 // For a full CRX, the flow is:
64 // In both cases, if there is an error at any point, the remaining steps will
65 // be skipped and Finish will be called.
66 class ComponentUnpacker {
68 // Possible error conditions.
69 // Add only to the bottom of this enum; the order must be kept stable.
82 kDeltaVerificationFailure,
84 kDeltaUnsupportedCommand,
85 kDeltaOperationFailure,
86 kDeltaPatchProcessFailure,
87 kDeltaMissingExistingFile,
88 kFingerprintWriteFailed,
91 // Constructs an unpacker for a specific component unpacking operation.
92 // |pk_hash| is the expected/ public key SHA256 hash. |path| is the current
93 // location of the CRX.
94 ComponentUnpacker(const std::vector<uint8>& pk_hash,
95 const base::FilePath& path,
96 const std::string& fingerprint,
97 ComponentPatcher* patcher,
98 ComponentInstaller* installer,
99 scoped_refptr<base::SequencedTaskRunner> task_runner);
101 virtual ~ComponentUnpacker();
103 // Begins the actual unpacking of the files. May invoke a patcher if the
104 // package is a differential update. Calls |callback| with the result.
106 const base::Callback<void(Error, int)>& callback);
109 bool UnpackInternal();
111 // The first step of unpacking is to verify the file. Returns false if an
112 // error is encountered, the file is malformed, or the file is incorrectly
116 // The second step of unpacking is to unzip. Returns false if an error
117 // occurs as part of unzipping.
120 // The third step is to optionally patch files - this is a no-op for full
121 // (non-differential) updates. This step is asynchronous. Returns false if an
122 // error is encountered.
123 bool BeginPatching();
125 // When patching is complete, EndPatching is called before moving on to step
127 void EndPatching(Error error, int extended_error);
129 // The fourth step is to install the unpacked component.
132 // The final step is to do clean-up for things that can't be tidied as we go.
133 // If there is an error at any step, the remaining steps are skipped and
134 // and Finish is called.
135 // Finish is responsible for calling the callback provided in Start().
138 // Returns a weak pointer to this object.
139 base::WeakPtr<ComponentUnpacker> GetWeakPtr();
141 std::vector<uint8> pk_hash_;
142 base::FilePath path_;
143 base::FilePath unpack_path_;
144 base::FilePath unpack_diff_path_;
146 std::string fingerprint_;
147 ComponentPatcher* patcher_;
148 ComponentInstaller* installer_;
149 base::Callback<void(Error, int)> callback_;
152 base::WeakPtrFactory<ComponentUnpacker> ptr_factory_;
153 scoped_refptr<base::SequencedTaskRunner> task_runner_;
155 DISALLOW_COPY_AND_ASSIGN(ComponentUnpacker);
158 } // namespace component_updater
160 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_