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.
5 #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_UNPACKER_H_
6 #define COMPONENTS_UPDATE_CLIENT_COMPONENT_UNPACKER_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/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/sequenced_task_runner.h"
20 namespace update_client
{
22 class ComponentInstaller
;
23 class ComponentPatcher
;
24 class OutOfProcessPatcher
;
26 // Deserializes the CRX manifest. The top level must be a dictionary.
27 scoped_ptr
<base::DictionaryValue
> ReadManifest(
28 const base::FilePath
& unpack_path
);
30 // In charge of unpacking the component CRX package and verifying that it is
31 // well formed and the cryptographic signature is correct. If there is no
32 // error the component specific installer will be invoked to proceed with
33 // the component installation or update.
35 // This class should be used only by the component updater. It is inspired by
36 // and overlaps with code in the extension's SandboxedUnpacker.
37 // The main differences are:
38 // - The public key hash is full SHA256.
39 // - Does not use a sandboxed unpacker. A valid component is fully trusted.
40 // - The manifest can have different attributes and resources are not
43 // If the CRX is a delta CRX, the flow is:
44 // [ComponentUpdater] [ComponentPatcher]
48 // \_ BeginPatching ---> DifferentialUpdatePatch
50 // EndPatching <------------ ...
54 // For a full CRX, the flow is:
66 // In both cases, if there is an error at any point, the remaining steps will
67 // be skipped and Finish will be called.
68 class ComponentUnpacker
: public base::RefCountedThreadSafe
<ComponentUnpacker
> {
70 // Possible error conditions.
71 // Add only to the bottom of this enum; the order must be kept stable.
84 kDeltaVerificationFailure
,
86 kDeltaUnsupportedCommand
,
87 kDeltaOperationFailure
,
88 kDeltaPatchProcessFailure
,
89 kDeltaMissingExistingFile
,
90 kFingerprintWriteFailed
,
93 typedef base::Callback
<void(Error
, int)> Callback
;
95 // Constructs an unpacker for a specific component unpacking operation.
96 // |pk_hash| is the expected/ public key SHA256 hash. |path| is the current
97 // location of the CRX.
99 const std::vector
<uint8_t>& pk_hash
,
100 const base::FilePath
& path
,
101 const std::string
& fingerprint
,
102 const scoped_refptr
<ComponentInstaller
>& installer
,
103 const scoped_refptr
<OutOfProcessPatcher
>& oop_patcher
,
104 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
106 // Begins the actual unpacking of the files. May invoke a patcher if the
107 // package is a differential update. Calls |callback| with the result.
108 void Unpack(const Callback
& callback
);
111 friend class base::RefCountedThreadSafe
<ComponentUnpacker
>;
113 virtual ~ComponentUnpacker();
115 bool UnpackInternal();
117 // The first step of unpacking is to verify the file. Returns false if an
118 // error is encountered, the file is malformed, or the file is incorrectly
122 // The second step of unpacking is to unzip. Returns false if an error
123 // occurs as part of unzipping.
126 // The third step is to optionally patch files - this is a no-op for full
127 // (non-differential) updates. This step is asynchronous. Returns false if an
128 // error is encountered.
129 bool BeginPatching();
131 // When patching is complete, EndPatching is called before moving on to step
133 void EndPatching(Error error
, int extended_error
);
135 // The fourth step is to install the unpacked component.
138 // The final step is to do clean-up for things that can't be tidied as we go.
139 // If there is an error at any step, the remaining steps are skipped and
140 // and Finish is called.
141 // Finish is responsible for calling the callback provided in Start().
144 std::vector
<uint8_t> pk_hash_
;
145 base::FilePath path_
;
146 base::FilePath unpack_path_
;
147 base::FilePath unpack_diff_path_
;
149 std::string fingerprint_
;
150 scoped_refptr
<ComponentPatcher
> patcher_
;
151 scoped_refptr
<ComponentInstaller
> installer_
;
153 scoped_refptr
<OutOfProcessPatcher
> oop_patcher_
;
156 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
158 DISALLOW_COPY_AND_ASSIGN(ComponentUnpacker
);
161 } // namespace update_client
163 #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_UNPACKER_H_