| 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 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "chrome/browser/component_updater/component_patcher.h" | |
| 16 #include "chrome/browser/component_updater/component_unpacker.h" | 15 #include "chrome/browser/component_updater/component_unpacker.h" |
| 17 #include "content/public/browser/utility_process_host_client.h" | |
| 18 | 16 |
| 19 namespace base { | 17 namespace base { |
| 20 class DictionaryValue; | 18 class DictionaryValue; |
| 21 } // namespace base | 19 } // namespace base |
| 22 | 20 |
| 23 namespace component_updater { | 21 namespace component_updater { |
| 24 | 22 |
| 23 const char kOp[] = "op"; |
| 24 const char kBsdiff[] = "bsdiff"; |
| 25 const char kCourgette[] = "courgette"; |
| 26 const char kInput[] = "input"; |
| 27 const char kPatch[] = "patch"; |
| 28 |
| 29 // The integer offset disambiguates between overlapping error ranges. |
| 30 const int kCourgetteErrorOffset = 300; |
| 31 const int kBsdiffErrorOffset = 600; |
| 32 |
| 25 class ComponentInstaller; | 33 class ComponentInstaller; |
| 26 | 34 |
| 27 class DeltaUpdateOp : public base::RefCountedThreadSafe<DeltaUpdateOp> { | 35 class DeltaUpdateOp : public base::RefCountedThreadSafe<DeltaUpdateOp> { |
| 28 public: | 36 public: |
| 29 DeltaUpdateOp(); | 37 DeltaUpdateOp(); |
| 30 | 38 |
| 31 // Parses, runs, and verifies the operation. Calls |callback| with the | 39 // Parses, runs, and verifies the operation. Calls |callback| with the |
| 32 // result of the operation. The callback is called using |task_runner|. | 40 // result of the operation. The callback is called using |task_runner|. |
| 33 void Run(const base::DictionaryValue* command_args, | 41 void Run(const base::DictionaryValue* command_args, |
| 34 const base::FilePath& input_dir, | 42 const base::FilePath& input_dir, |
| 35 const base::FilePath& unpack_dir, | 43 const base::FilePath& unpack_dir, |
| 36 ComponentInstaller* installer, | 44 ComponentInstaller* installer, |
| 37 bool in_process, | |
| 38 const ComponentUnpacker::Callback& callback, | 45 const ComponentUnpacker::Callback& callback, |
| 39 scoped_refptr<base::SequencedTaskRunner> task_runner); | 46 scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 40 | 47 |
| 41 protected: | 48 protected: |
| 42 virtual ~DeltaUpdateOp(); | 49 virtual ~DeltaUpdateOp(); |
| 43 | 50 |
| 44 bool InProcess(); | |
| 45 | |
| 46 scoped_refptr<base::SequencedTaskRunner> GetTaskRunner(); | 51 scoped_refptr<base::SequencedTaskRunner> GetTaskRunner(); |
| 47 | 52 |
| 48 std::string output_sha256_; | 53 std::string output_sha256_; |
| 49 base::FilePath output_abs_path_; | 54 base::FilePath output_abs_path_; |
| 50 | 55 |
| 51 private: | 56 private: |
| 52 friend class base::RefCountedThreadSafe<DeltaUpdateOp>; | 57 friend class base::RefCountedThreadSafe<DeltaUpdateOp>; |
| 53 | 58 |
| 54 ComponentUnpacker::Error CheckHash(); | 59 ComponentUnpacker::Error CheckHash(); |
| 55 | 60 |
| 56 // Subclasses must override DoParseArguments to parse operation-specific | 61 // Subclasses must override DoParseArguments to parse operation-specific |
| 57 // arguments. DoParseArguments returns DELTA_OK on success; any other code | 62 // arguments. DoParseArguments returns DELTA_OK on success; any other code |
| 58 // represents failure. | 63 // represents failure. |
| 59 virtual ComponentUnpacker::Error DoParseArguments( | 64 virtual ComponentUnpacker::Error DoParseArguments( |
| 60 const base::DictionaryValue* command_args, | 65 const base::DictionaryValue* command_args, |
| 61 const base::FilePath& input_dir, | 66 const base::FilePath& input_dir, |
| 62 ComponentInstaller* installer) = 0; | 67 ComponentInstaller* installer) = 0; |
| 63 | 68 |
| 64 // Subclasses must override DoRun to actually perform the patching operation. | 69 // Subclasses must override DoRun to actually perform the patching operation. |
| 65 // They must call the provided callback when they have completed their | 70 // They must call the provided callback when they have completed their |
| 66 // operations. In practice, the provided callback is always for "DoneRunning". | 71 // operations. In practice, the provided callback is always for "DoneRunning". |
| 67 virtual void DoRun(const ComponentUnpacker::Callback& callback) = 0; | 72 virtual void DoRun(const ComponentUnpacker::Callback& callback) = 0; |
| 68 | 73 |
| 69 // Callback given to subclasses for when they complete their operation. | 74 // Callback given to subclasses for when they complete their operation. |
| 70 // Validates the output, and posts a task to the patching operation's | 75 // Validates the output, and posts a task to the patching operation's |
| 71 // callback. | 76 // callback. |
| 72 void DoneRunning(ComponentUnpacker::Error error, int extended_error); | 77 void DoneRunning(ComponentUnpacker::Error error, int extended_error); |
| 73 | 78 |
| 74 bool in_process_; | |
| 75 ComponentUnpacker::Callback callback_; | 79 ComponentUnpacker::Callback callback_; |
| 76 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 80 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 77 | 81 |
| 78 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp); | 82 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp); |
| 79 }; | 83 }; |
| 80 | 84 |
| 81 // A 'copy' operation takes a file currently residing on the disk and moves it | 85 // A 'copy' operation takes a file currently residing on the disk and moves it |
| 82 // into the unpacking directory: this represents "no change" in the file being | 86 // into the unpacking directory: this represents "no change" in the file being |
| 83 // installed. | 87 // installed. |
| 84 class DeltaUpdateOpCopy : public DeltaUpdateOp { | 88 class DeltaUpdateOpCopy : public DeltaUpdateOp { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 const base::FilePath& input_dir, | 122 const base::FilePath& input_dir, |
| 119 ComponentInstaller* installer) OVERRIDE; | 123 ComponentInstaller* installer) OVERRIDE; |
| 120 | 124 |
| 121 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; | 125 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; |
| 122 | 126 |
| 123 base::FilePath patch_abs_path_; | 127 base::FilePath patch_abs_path_; |
| 124 | 128 |
| 125 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate); | 129 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate); |
| 126 }; | 130 }; |
| 127 | 131 |
| 128 class DeltaUpdateOpPatchStrategy { | |
| 129 public: | |
| 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 scoped_ptr<IPC::Message> GetPatchMessage( | |
| 141 base::FilePath input_abs_path, | |
| 142 base::FilePath patch_abs_path, | |
| 143 base::FilePath output_abs_path) = 0; | |
| 144 | |
| 145 // Does the actual patching operation, and returns an error code. | |
| 146 virtual int Patch(base::FilePath input_abs_path, | |
| 147 base::FilePath patch_abs_path, | |
| 148 base::FilePath output_abs_path) = 0; | |
| 149 }; | |
| 150 | |
| 151 class DeltaUpdateOpPatch; | |
| 152 | |
| 153 class DeltaUpdateOpPatchHost : public content::UtilityProcessHostClient { | |
| 154 public: | |
| 155 DeltaUpdateOpPatchHost(scoped_refptr<DeltaUpdateOpPatch> patcher, | |
| 156 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 157 | |
| 158 void StartProcess(scoped_ptr<IPC::Message> message); | |
| 159 | |
| 160 private: | |
| 161 virtual ~DeltaUpdateOpPatchHost(); | |
| 162 | |
| 163 void OnPatchSucceeded(); | |
| 164 | |
| 165 void OnPatchFailed(int error_code); | |
| 166 | |
| 167 // Overrides of content::UtilityProcessHostClient. | |
| 168 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 169 | |
| 170 virtual void OnProcessCrashed(int exit_code) OVERRIDE; | |
| 171 | |
| 172 scoped_refptr<DeltaUpdateOpPatch> patcher_; | |
| 173 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 174 }; | |
| 175 | |
| 176 // Both 'bsdiff' and 'courgette' operations take an existing file on disk, | 132 // Both 'bsdiff' and 'courgette' operations take an existing file on disk, |
| 177 // and a bsdiff- or Courgette-format patch file provided in the delta update | 133 // and a bsdiff- or Courgette-format patch file provided in the delta update |
| 178 // package, and run bsdiff or Courgette to construct an output file in the | 134 // package, and run bsdiff or Courgette to construct an output file in the |
| 179 // unpacking directory. | 135 // unpacking directory. |
| 180 class DeltaUpdateOpPatch : public DeltaUpdateOp { | 136 class DeltaUpdateOpPatchInProcess : public DeltaUpdateOp { |
| 181 public: | 137 public: |
| 182 explicit DeltaUpdateOpPatch(scoped_ptr<DeltaUpdateOpPatchStrategy> strategy); | 138 explicit DeltaUpdateOpPatchInProcess(const std::string& operation); |
| 183 | |
| 184 void DonePatching(ComponentUnpacker::Error error, int error_code); | |
| 185 | 139 |
| 186 private: | 140 private: |
| 187 virtual ~DeltaUpdateOpPatch(); | 141 virtual ~DeltaUpdateOpPatchInProcess(); |
| 188 | 142 |
| 189 // Overrides of DeltaUpdateOp. | 143 // Overrides of DeltaUpdateOp. |
| 190 virtual ComponentUnpacker::Error DoParseArguments( | 144 virtual ComponentUnpacker::Error DoParseArguments( |
| 191 const base::DictionaryValue* command_args, | 145 const base::DictionaryValue* command_args, |
| 192 const base::FilePath& input_dir, | 146 const base::FilePath& input_dir, |
| 193 ComponentInstaller* installer) OVERRIDE; | 147 ComponentInstaller* installer) OVERRIDE; |
| 194 | 148 |
| 195 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; | 149 virtual void DoRun(const ComponentUnpacker::Callback& callback) OVERRIDE; |
| 196 | 150 |
| 197 ComponentUnpacker::Callback callback_; | 151 std::string operation_; |
| 198 base::FilePath patch_abs_path_; | 152 base::FilePath patch_abs_path_; |
| 199 base::FilePath input_abs_path_; | 153 base::FilePath input_abs_path_; |
| 200 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy_; | |
| 201 scoped_refptr<DeltaUpdateOpPatchHost> host_; | |
| 202 | 154 |
| 203 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch); | 155 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatchInProcess); |
| 204 }; | 156 }; |
| 205 | 157 |
| 206 // Factory functions to create DeltaUpdateOp instances. | 158 // Factory class for DeltaUpdateOp. |
| 207 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command); | 159 class DeltaUpdateOpFactory { |
| 160 public: |
| 161 virtual ~DeltaUpdateOpFactory(); |
| 208 | 162 |
| 209 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation); | 163 // Create a DeltaUpdateOp for |operation|. |
| 164 virtual DeltaUpdateOp* CreateDeltaUpdateOp( |
| 165 const std::string& operation) const = 0; |
| 166 }; |
| 167 |
| 168 // Creates an in-process DeltaUpdateOpFactory. |
| 169 DeltaUpdateOpFactory* CreateInProcessDeltaUpdateOpFactory(); |
| 210 | 170 |
| 211 } // namespace component_updater | 171 } // namespace component_updater |
| 212 | 172 |
| 213 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ | 173 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_ |
| OLD | NEW |