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