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