| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_PATCHER_OPERATION_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ |
| 6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 |
| 9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "chrome/browser/component_updater/component_patcher.h" |
| 12 #include "chrome/browser/component_updater/component_unpacker.h" | 16 #include "chrome/browser/component_updater/component_unpacker.h" |
| 17 #include "content/public/browser/utility_process_host_client.h" |
| 13 | 18 |
| 14 namespace base { | 19 namespace base { |
| 15 class DictionaryValue; | 20 class DictionaryValue; |
| 16 } // namespace base | 21 } // namespace base |
| 17 | 22 |
| 18 namespace component_updater { | 23 namespace component_updater { |
| 19 | 24 |
| 20 class ComponentInstaller; | 25 class ComponentInstaller; |
| 21 class ComponentPatcher; | |
| 22 | 26 |
| 23 class DeltaUpdateOp { | 27 class DeltaUpdateOp : public base::RefCountedThreadSafe<DeltaUpdateOp> { |
| 24 public: | 28 public: |
| 29 DeltaUpdateOp(); |
| 25 | 30 |
| 26 DeltaUpdateOp(); | 31 // Parses, runs, and verifies the operation. Calls |callback| with the |
| 32 // result of the operation. The callback is called using |task_runner|. |
| 33 void Run(const base::DictionaryValue* command_args, |
| 34 const base::FilePath& input_dir, |
| 35 const base::FilePath& unpack_dir, |
| 36 ComponentInstaller* installer, |
| 37 bool in_process, |
| 38 const ComponentUnpacker::Callback& callback, |
| 39 scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 40 |
| 41 protected: |
| 27 virtual ~DeltaUpdateOp(); | 42 virtual ~DeltaUpdateOp(); |
| 28 | 43 |
| 29 // Parses, runs, and verifies the operation, returning an error code if an | 44 bool InProcess(); |
| 30 // error is encountered, and DELTA_OK otherwise. In case of errors, | |
| 31 // extended error information can be returned in the |error| parameter. | |
| 32 ComponentUnpacker::Error Run(base::DictionaryValue* command_args, | |
| 33 const base::FilePath& input_dir, | |
| 34 const base::FilePath& unpack_dir, | |
| 35 ComponentPatcher* patcher, | |
| 36 ComponentInstaller* installer, | |
| 37 int* error); | |
| 38 | 45 |
| 39 protected: | |
| 40 std::string output_sha256_; | 46 std::string output_sha256_; |
| 41 base::FilePath output_abs_path_; | 47 base::FilePath output_abs_path_; |
| 42 | 48 |
| 43 private: | 49 private: |
| 50 friend class base::RefCountedThreadSafe<DeltaUpdateOp>; |
| 51 |
| 44 ComponentUnpacker::Error CheckHash(); | 52 ComponentUnpacker::Error CheckHash(); |
| 45 | 53 |
| 46 // Subclasses must override DoParseArguments to parse operation-specific | 54 // Subclasses must override DoParseArguments to parse operation-specific |
| 47 // arguments. DoParseArguments returns DELTA_OK on success; any other code | 55 // arguments. DoParseArguments returns DELTA_OK on success; any other code |
| 48 // represents failure. | 56 // represents failure. |
| 49 virtual ComponentUnpacker::Error DoParseArguments( | 57 virtual ComponentUnpacker::Error DoParseArguments( |
| 50 base::DictionaryValue* command_args, | 58 const base::DictionaryValue* command_args, |
| 51 const base::FilePath& input_dir, | 59 const base::FilePath& input_dir, |
| 52 ComponentInstaller* installer) = 0; | 60 ComponentInstaller* installer) = 0; |
| 53 | 61 |
| 54 // Subclasses must override DoRun to actually perform the patching operation. | 62 // Subclasses must override DoRun to actually perform the patching operation. |
| 55 // DoRun returns DELTA_OK on success; any other code represents failure. | 63 // They must call the provided callback when they have completed their |
| 56 // Additional error information can be returned in the |error| parameter. | 64 // operations. In practice, the provided callback is always for "DoneRunning". |
| 57 virtual ComponentUnpacker::Error DoRun(ComponentPatcher* patcher, | 65 virtual void DoRun(const ComponentUnpacker::Callback& callback) = 0; |
| 58 int* error) = 0; | 66 |
| 67 // Callback given to subclasses for when they complete their operation. |
| 68 // Validates the output, and posts a task to the patching operation's |
| 69 // callback. |
| 70 void DoneRunning(ComponentUnpacker::Error error, int extended_error); |
| 71 |
| 72 bool in_process_; |
| 73 ComponentUnpacker::Callback callback_; |
| 74 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 59 | 75 |
| 60 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp); | 76 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp); |
| 61 }; | 77 }; |
| 62 | 78 |
| 63 // A 'copy' operation takes a file currently residing on the disk and moves it | 79 // A 'copy' operation takes a file currently residing on the disk and moves it |
| 64 // into the unpacking directory: this represents "no change" in the file being | 80 // into the unpacking directory: this represents "no change" in the file being |
| 65 // installed. | 81 // installed. |
| 66 class DeltaUpdateOpCopy : public DeltaUpdateOp { | 82 class DeltaUpdateOpCopy : public DeltaUpdateOp { |
| 67 public: | 83 public: |
| 68 DeltaUpdateOpCopy(); | 84 DeltaUpdateOpCopy(); |
| 69 | 85 |
| 86 protected: |
| 87 virtual ~DeltaUpdateOpCopy(); |
| 88 |
| 70 private: | 89 private: |
| 71 // Overrides of DeltaUpdateOp. | 90 // Overrides of DeltaUpdateOp. |
| 72 virtual ComponentUnpacker::Error DoParseArguments( | 91 virtual ComponentUnpacker::Error DoParseArguments( |
| 73 base::DictionaryValue* command_args, | 92 const base::DictionaryValue* command_args, |
| 74 const base::FilePath& input_dir, | 93 const base::FilePath& input_dir, |
| 75 ComponentInstaller* installer) OVERRIDE; | 94 ComponentInstaller* installer) OVERRIDE; |
| 76 | 95 |
| 77 virtual ComponentUnpacker::Error DoRun(ComponentPatcher* patcher, | 96 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; |
| 78 int* error) OVERRIDE; | |
| 79 | 97 |
| 80 base::FilePath input_abs_path_; | 98 base::FilePath input_abs_path_; |
| 81 | 99 |
| 82 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCopy); | 100 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCopy); |
| 83 }; | 101 }; |
| 84 | 102 |
| 85 // A 'create' operation takes a full file that was sent in the delta update | 103 // A 'create' operation takes a full file that was sent in the delta update |
| 86 // archive and moves it into the unpacking directory: this represents the | 104 // archive and moves it into the unpacking directory: this represents the |
| 87 // addition of a new file, or a file so different that no bandwidth could be | 105 // addition of a new file, or a file so different that no bandwidth could be |
| 88 // saved by transmitting a differential update. | 106 // saved by transmitting a differential update. |
| 89 class DeltaUpdateOpCreate : public DeltaUpdateOp { | 107 class DeltaUpdateOpCreate : public DeltaUpdateOp { |
| 90 public: | 108 public: |
| 91 DeltaUpdateOpCreate(); | 109 DeltaUpdateOpCreate(); |
| 92 | 110 |
| 111 protected: |
| 112 virtual ~DeltaUpdateOpCreate(); |
| 113 |
| 93 private: | 114 private: |
| 94 // Overrides of DeltaUpdateOp. | 115 // Overrides of DeltaUpdateOp. |
| 95 virtual ComponentUnpacker::Error DoParseArguments( | 116 virtual ComponentUnpacker::Error DoParseArguments( |
| 96 base::DictionaryValue* command_args, | 117 const base::DictionaryValue* command_args, |
| 97 const base::FilePath& input_dir, | 118 const base::FilePath& input_dir, |
| 98 ComponentInstaller* installer) OVERRIDE; | 119 ComponentInstaller* installer) OVERRIDE; |
| 99 | 120 |
| 100 virtual ComponentUnpacker::Error DoRun(ComponentPatcher* patcher, | 121 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; |
| 101 int* error) OVERRIDE; | |
| 102 | 122 |
| 103 base::FilePath patch_abs_path_; | 123 base::FilePath patch_abs_path_; |
| 104 | 124 |
| 105 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate); | 125 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate); |
| 106 }; | 126 }; |
| 107 | 127 |
| 108 // A 'bsdiff' operation takes an existing file on disk, and a bsdiff- | 128 class DeltaUpdateOpPatchStrategy { |
| 109 // format patch file provided in the delta update package, and runs bsdiff | |
| 110 // to construct an output file in the unpacking directory. | |
| 111 class DeltaUpdateOpPatchBsdiff : public DeltaUpdateOp { | |
| 112 public: | 129 public: |
| 113 DeltaUpdateOpPatchBsdiff(); | 130 virtual ~DeltaUpdateOpPatchStrategy(); |
| 131 |
| 132 // Returns an integer to add to error codes to disambiguate their source. |
| 133 virtual int GetErrorOffset() const = 0; |
| 134 |
| 135 // Returns the "error code" that is expected in the successful install case. |
| 136 virtual int GetSuccessCode() const = 0; |
| 137 |
| 138 // Returns an IPC message that will start patching if it is sent to a |
| 139 // UtilityProcessClient. |
| 140 virtual IPC::Message* GetPatchMessage(base::FilePath input_abs_path, |
| 141 base::FilePath patch_abs_path, |
| 142 base::FilePath output_abs_path) = 0; |
| 143 |
| 144 // Does the actual patching operation, and returns an error code. |
| 145 virtual int Patch(base::FilePath input_abs_path, |
| 146 base::FilePath patch_abs_path, |
| 147 base::FilePath output_abs_path) = 0; |
| 148 }; |
| 149 |
| 150 // Both 'bsdiff' and 'courgette' operations take an existing file on disk, |
| 151 // and a bsdiff- or Courgette-format patch file provided in the delta update |
| 152 // package, and run bsdiff or Courgette to construct an output file in the |
| 153 // unpacking directory. |
| 154 class DeltaUpdateOpPatch : public DeltaUpdateOp, |
| 155 public content::UtilityProcessHostClient { |
| 156 public: |
| 157 explicit DeltaUpdateOpPatch(scoped_ptr<DeltaUpdateOpPatchStrategy> traits); |
| 158 |
| 159 protected: |
| 160 virtual ~DeltaUpdateOpPatch(); |
| 114 | 161 |
| 115 private: | 162 private: |
| 163 void DonePatching(ComponentUnpacker::Error error, int error_code); |
| 164 |
| 165 void OnPatchSucceeded(); |
| 166 |
| 167 void OnPatchFailed(int error_code); |
| 168 |
| 169 // Overrides of content::UtilityProcessHostClient. |
| 170 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 171 |
| 172 virtual void OnProcessCrashed(int exit_code) OVERRIDE; |
| 173 |
| 116 // Overrides of DeltaUpdateOp. | 174 // Overrides of DeltaUpdateOp. |
| 117 virtual ComponentUnpacker::Error DoParseArguments( | 175 virtual ComponentUnpacker::Error DoParseArguments( |
| 118 base::DictionaryValue* command_args, | 176 const base::DictionaryValue* command_args, |
| 119 const base::FilePath& input_dir, | 177 const base::FilePath& input_dir, |
| 120 ComponentInstaller* installer) OVERRIDE; | 178 ComponentInstaller* installer) OVERRIDE; |
| 121 | 179 |
| 122 virtual ComponentUnpacker::Error DoRun(ComponentPatcher* patcher, | 180 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; |
| 123 int* error) OVERRIDE; | |
| 124 | 181 |
| 182 void StartProcess(); |
| 183 |
| 184 ComponentUnpacker::Callback callback_; |
| 125 base::FilePath patch_abs_path_; | 185 base::FilePath patch_abs_path_; |
| 126 base::FilePath input_abs_path_; | 186 base::FilePath input_abs_path_; |
| 187 scoped_ptr<DeltaUpdateOpPatchStrategy> traits_; |
| 127 | 188 |
| 128 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatchBsdiff); | 189 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch); |
| 129 }; | 190 }; |
| 130 | 191 |
| 131 // A 'courgette' operation takes an existing file on disk, and a Courgette- | 192 // Factory functions to create DeltaUpdateOp instances. |
| 132 // format patch file provided in the delta update package, and runs Courgette | 193 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command); |
| 133 // to construct an output file in the unpacking directory. | |
| 134 class DeltaUpdateOpPatchCourgette : public DeltaUpdateOp { | |
| 135 public: | |
| 136 DeltaUpdateOpPatchCourgette(); | |
| 137 | 194 |
| 138 private: | 195 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation); |
| 139 // Overrides of DeltaUpdateOp. | |
| 140 virtual ComponentUnpacker::Error DoParseArguments( | |
| 141 base::DictionaryValue* command_args, | |
| 142 const base::FilePath& input_dir, | |
| 143 ComponentInstaller* installer) OVERRIDE; | |
| 144 | |
| 145 virtual ComponentUnpacker::Error DoRun(ComponentPatcher* patcher, | |
| 146 int* error) OVERRIDE; | |
| 147 | |
| 148 base::FilePath patch_abs_path_; | |
| 149 base::FilePath input_abs_path_; | |
| 150 | |
| 151 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatchCourgette); | |
| 152 }; | |
| 153 | |
| 154 // Factory function to create DeltaUpdateOp instances. | |
| 155 DeltaUpdateOp* CreateDeltaUpdateOp(base::DictionaryValue* command); | |
| 156 | 196 |
| 157 } // namespace component_updater | 197 } // namespace component_updater |
| 158 | 198 |
| 159 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ | 199 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ |
| OLD | NEW |