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.
4 
5 // Component updates can be either differential updates or full updates.
6 // Full updates come in CRX format; differential updates come in CRX-style
7 // archives, but have a different magic number. They contain "commands.json", a
8 // list of commands for the patcher to follow. The patcher uses these commands,
9 // the other files in the archive, and the files from the existing installation
10 // of the component to create the contents of a full update, which is then
11 // installed normally.
12 // Component updates are specified by the 'codebasediff' attribute of an
13 // updatecheck response:
14 //   <updatecheck codebase="http://example.com/extension_1.2.3.4.crx"
15 //                hash="12345" size="9854" status="ok" version="1.2.3.4"
16 //                prodversionmin="2.0.143.0"
17 //                codebasediff="http://example.com/diff_1.2.3.4.crx"
18 //                hashdiff="123" sizediff="101"
19 //                fp="1.123"/>
20 // The component updater will attempt a differential update if it is available
21 // and allowed to, and fall back to a full update if it fails.
22 //
23 // After installation (diff or full), the component updater records "fp", the
24 // fingerprint of the installed files, to later identify the existing files to
25 // the server so that a proper differential update can be provided next cycle.
26 
27 #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_
28 #define COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_
29 
30 #include <memory>
31 
32 #include "base/callback_forward.h"
33 #include "base/macros.h"
34 #include "base/memory/ref_counted.h"
35 #include "base/values.h"
36 #include "components/update_client/component_unpacker.h"
37 
38 namespace base {
39 class FilePath;
40 }
41 
42 namespace update_client {
43 
44 class CrxInstaller;
45 class DeltaUpdateOp;
46 class Patcher;
47 enum class UnpackerError;
48 
49 // The type of a patch file.
50 enum PatchType {
51   kPatchTypeUnknown,
52   kPatchTypeCourgette,
53   kPatchTypeBsdiff,
54 };
55 
56 // Encapsulates a task for applying a differential update to a component.
57 class ComponentPatcher : public base::RefCountedThreadSafe<ComponentPatcher> {
58  public:
59   using Callback = base::OnceCallback<void(UnpackerError, int)>;
60 
61   // Takes an unpacked differential CRX (|input_dir|) and a component installer,
62   // and sets up the class to create a new (non-differential) unpacked CRX.
63   // If |in_process| is true, patching will be done completely within the
64   // existing process. Otherwise, some steps of patching may be done
65   // out-of-process.
66   ComponentPatcher(const base::FilePath& input_dir,
67                    const base::FilePath& unpack_dir,
68                    scoped_refptr<CrxInstaller> installer,
69                    scoped_refptr<Patcher> patcher);
70 
71   // Starts patching files. This member function returns immediately, after
72   // posting a task to do the patching. When patching has been completed,
73   // |callback| will be called with the error codes if any error codes were
74   // encountered.
75   void Start(Callback callback);
76 
77  private:
78   friend class base::RefCountedThreadSafe<ComponentPatcher>;
79 
80   virtual ~ComponentPatcher();
81 
82   void StartPatching();
83 
84   void PatchNextFile();
85 
86   void DonePatchingFile(UnpackerError error, int extended_error);
87 
88   void DonePatching(UnpackerError error, int extended_error);
89 
90   const base::FilePath input_dir_;
91   const base::FilePath unpack_dir_;
92   scoped_refptr<CrxInstaller> installer_;
93   scoped_refptr<Patcher> patcher_;
94   Callback callback_;
95   std::unique_ptr<base::ListValue> commands_;
96   base::ListValue::const_iterator next_command_;
97   scoped_refptr<DeltaUpdateOp> current_operation_;
98 
99   DISALLOW_COPY_AND_ASSIGN(ComponentPatcher);
100 };
101 
102 }  // namespace update_client
103 
104 #endif  // COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_
105