OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ |
6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/basictypes.h" | 10 #include "base/callback.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/sequenced_task_runner.h" |
14 | 16 |
15 class ComponentInstaller; | 17 class ComponentInstaller; |
| 18 |
| 19 namespace component_updater { |
| 20 |
16 class ComponentPatcher; | 21 class ComponentPatcher; |
17 | 22 |
18 // Deserializes the CRX manifest. The top level must be a dictionary. | 23 // Deserializes the CRX manifest. The top level must be a dictionary. |
19 scoped_ptr<base::DictionaryValue> ReadManifest( | 24 scoped_ptr<base::DictionaryValue> ReadManifest( |
20 const base::FilePath& unpack_path); | 25 const base::FilePath& unpack_path); |
21 | 26 |
22 // In charge of unpacking the component CRX package and verifying that it is | 27 // In charge of unpacking the component CRX package and verifying that it is |
23 // well formed and the cryptographic signature is correct. If there is no | 28 // well formed and the cryptographic signature is correct. If there is no |
24 // error the component specific installer will be invoked to proceed with | 29 // error the component specific installer will be invoked to proceed with |
25 // the component installation or update. | 30 // the component installation or update. |
26 // | 31 // |
27 // This class should be used only by the component updater. It is inspired | 32 // This class should be used only by the component updater. It is inspired by |
28 // and overlaps with code in the extension's SandboxedUnpacker. | 33 // and overlaps with code in the extension's SandboxedUnpacker. |
29 // The main differences are: | 34 // The main differences are: |
30 // - The public key hash is full SHA256. | 35 // - The public key hash is full SHA256. |
31 // - Does not use a sandboxed unpacker. A valid component is fully trusted. | 36 // - Does not use a sandboxed unpacker. A valid component is fully trusted. |
32 // - The manifest can have different attributes and resources are not | 37 // - The manifest can have different attributes and resources are not |
33 // transcoded. | 38 // transcoded. |
| 39 // |
| 40 // If the CRX is a delta CRX, the flow is: |
| 41 // [ComponentUpdater] [ComponentPatcher] |
| 42 // Unpack |
| 43 // \_ Unzip |
| 44 // \_ BeginPatching ---> DifferentialUpdatePatch |
| 45 // ... |
| 46 // DonePatching <----------- ... |
| 47 // \_ Install |
| 48 // \_ Finish |
| 49 // |
| 50 // For a full CRX, the flow is: |
| 51 // [ComponentUpdater] |
| 52 // Unpack |
| 53 // \_ Unzip |
| 54 // \_ BeginPatching |
| 55 // | |
| 56 // V |
| 57 // DonePatching |
| 58 // \_ Install |
| 59 // \_ Finish |
| 60 // |
| 61 // In both cases, if there is an error at any point, the remaining steps will |
| 62 // be skipped and Finish will be called. |
34 class ComponentUnpacker { | 63 class ComponentUnpacker { |
35 public: | 64 public: |
36 // Possible error conditions. | 65 // Possible error conditions. |
37 // Add only to the bottom of this enum; the order must be kept stable. | 66 // Add only to the bottom of this enum; the order must be kept stable. |
38 enum Error { | 67 enum Error { |
39 kNone, | 68 kNone, |
40 kInvalidParams, | 69 kInvalidParams, |
41 kInvalidFile, | 70 kInvalidFile, |
42 kUnzipPathError, | 71 kUnzipPathError, |
43 kUnzipFailed, | 72 kUnzipFailed, |
44 kNoManifest, | 73 kNoManifest, |
45 kBadManifest, | 74 kBadManifest, |
46 kBadExtension, | 75 kBadExtension, |
47 kInvalidId, | 76 kInvalidId, |
48 kInstallerError, | 77 kInstallerError, |
49 kIoError, | 78 kIoError, |
50 kDeltaVerificationFailure, | 79 kDeltaVerificationFailure, |
51 kDeltaBadCommands, | 80 kDeltaBadCommands, |
52 kDeltaUnsupportedCommand, | 81 kDeltaUnsupportedCommand, |
53 kDeltaOperationFailure, | 82 kDeltaOperationFailure, |
54 kDeltaPatchProcessFailure, | 83 kDeltaPatchProcessFailure, |
55 kDeltaMissingExistingFile, | 84 kDeltaMissingExistingFile, |
56 kFingerprintWriteFailed, | 85 kFingerprintWriteFailed, |
57 }; | 86 }; |
58 // Unpacks, verifies and calls the installer. |pk_hash| is the expected | 87 |
59 // public key SHA256 hash. |path| is the current location of the CRX. | 88 // Constructs an unpacker for a specific component unpacking operation. |
| 89 // |pk_hash| is the expected/ public key SHA256 hash. |path| is the current |
| 90 // location of the CRX. |
60 ComponentUnpacker(const std::vector<uint8>& pk_hash, | 91 ComponentUnpacker(const std::vector<uint8>& pk_hash, |
61 const base::FilePath& path, | 92 const base::FilePath& path, |
62 const std::string& fingerprint, | 93 const std::string& fingerprint, |
63 ComponentPatcher* patcher, | 94 ComponentPatcher* patcher, |
64 ComponentInstaller* installer); | 95 ComponentInstaller* installer, |
| 96 scoped_refptr<base::SequencedTaskRunner> task_runner); |
65 | 97 |
66 // If something went wrong during unpacking or installer invocation, the | 98 virtual ~ComponentUnpacker(); |
67 // destructor will delete the unpacked CRX files. | |
68 ~ComponentUnpacker(); | |
69 | 99 |
70 Error error() const { return error_; } | 100 // Begins the actual unpacking of the files. May invoke a patcher if the |
71 | 101 // package is a differential update. Call |callback| with the result. |
72 int extended_error() const { return extended_error_; } | 102 void Unpack( |
| 103 const base::Callback<void(Error, int)>& callback); |
73 | 104 |
74 private: | 105 private: |
| 106 // The first step of unpacking is to unzip. Returns false if an error |
| 107 // occurred as part of unzipping. |
| 108 bool Unzip(); |
| 109 |
| 110 // The second step is to optionally patch files - this is a no-op for |
| 111 // full (non-differential) updates. This step is asynchronous. Returns false |
| 112 // if an error occurs. |
| 113 bool BeginPatching(); |
| 114 |
| 115 // When patching is complete, DonePatching is called before moving on to step |
| 116 // three. |
| 117 void DonePatching(Error error, int extended_error); |
| 118 |
| 119 // The third step is to install the component. |
| 120 void Install(); |
| 121 |
| 122 // The final step is to do clean-up for things that can't be tidied as we go. |
| 123 // If there is an error at any step, the remaining steps are skipped and |
| 124 // and Finish is called. |
| 125 // Finish is responsible for calling the callback provided in Start(). |
| 126 void Finish(); |
| 127 |
| 128 // Returns a weak pointer to this object. |
| 129 base::WeakPtr<ComponentUnpacker> GetWeakPtr(); |
| 130 |
| 131 std::vector<uint8> pk_hash_; |
| 132 base::FilePath path_; |
75 base::FilePath unpack_path_; | 133 base::FilePath unpack_path_; |
| 134 base::FilePath unpack_diff_path_; |
| 135 bool is_delta_; |
| 136 std::string fingerprint_; |
| 137 ComponentPatcher* patcher_; |
| 138 ComponentInstaller* installer_; |
| 139 base::Callback<void(Error, int)> callback_; |
76 Error error_; | 140 Error error_; |
77 int extended_error_; // Provides additional error information. | 141 int extended_error_; |
| 142 base::WeakPtrFactory<ComponentUnpacker> ptr_factory_; |
| 143 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
78 }; | 144 }; |
79 | 145 |
| 146 } // namespace component_updater |
| 147 |
80 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ | 148 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ |
OLD | NEW |