Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / update_client / component_unpacker.h
blobc1c6c9e39dcef8c2defa9f6b34a26c153173c872
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_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
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 CrxInstaller;
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
41 // transcoded.
43 // If the CRX is a delta CRX, the flow is:
44 // [ComponentUpdater] [ComponentPatcher]
45 // Unpack
46 // \_ Verify
47 // \_ Unzip
48 // \_ BeginPatching ---> DifferentialUpdatePatch
49 // ...
50 // EndPatching <------------ ...
51 // \_ Install
52 // \_ Finish
54 // For a full CRX, the flow is:
55 // [ComponentUpdater]
56 // Unpack
57 // \_ Verify
58 // \_ Unzip
59 // \_ BeginPatching
60 // |
61 // V
62 // EndPatching
63 // \_ Install
64 // \_ Finish
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> {
69 public:
70 // Possible error conditions.
71 // Add only to the bottom of this enum; the order must be kept stable.
72 enum Error {
73 kNone,
74 kInvalidParams,
75 kInvalidFile,
76 kUnzipPathError,
77 kUnzipFailed,
78 kNoManifest,
79 kBadManifest,
80 kBadExtension,
81 kInvalidId,
82 kInstallerError,
83 kIoError,
84 kDeltaVerificationFailure,
85 kDeltaBadCommands,
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.
98 ComponentUnpacker(
99 const std::vector<uint8_t>& pk_hash,
100 const base::FilePath& path,
101 const std::string& fingerprint,
102 const scoped_refptr<CrxInstaller>& 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);
110 private:
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
119 // signed.
120 bool Verify();
122 // The second step of unpacking is to unzip. Returns false if an error
123 // occurs as part of unzipping.
124 bool Unzip();
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
132 // four.
133 void EndPatching(Error error, int extended_error);
135 // The fourth step is to install the unpacked component.
136 void Install();
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().
142 void Finish();
144 std::vector<uint8_t> pk_hash_;
145 base::FilePath path_;
146 base::FilePath unpack_path_;
147 base::FilePath unpack_diff_path_;
148 bool is_delta_;
149 std::string fingerprint_;
150 scoped_refptr<ComponentPatcher> patcher_;
151 scoped_refptr<CrxInstaller> installer_;
152 Callback callback_;
153 scoped_refptr<OutOfProcessPatcher> oop_patcher_;
154 Error error_;
155 int extended_error_;
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_