Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: third_party/zlib/google/zip_reader.h

Issue 92873003: Adds asynchronous unzip functions to ZipReader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ 5 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
6 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ 6 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/platform_file.h" 17 #include "base/platform_file.h"
15 #include "base/time/time.h" 18 #include "base/time/time.h"
16 19
17 #if defined(USE_SYSTEM_MINIZIP) 20 #if defined(USE_SYSTEM_MINIZIP)
18 #include <minizip/unzip.h> 21 #include <minizip/unzip.h>
19 #else 22 #else
20 #include "third_party/zlib/contrib/minizip/unzip.h" 23 #include "third_party/zlib/contrib/minizip/unzip.h"
21 #endif 24 #endif
22 25
23 namespace zip { 26 namespace zip {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 72
70 private: 73 private:
71 const base::FilePath file_path_; 74 const base::FilePath file_path_;
72 int64 original_size_; 75 int64 original_size_;
73 base::Time last_modified_; 76 base::Time last_modified_;
74 bool is_directory_; 77 bool is_directory_;
75 bool is_unsafe_; 78 bool is_unsafe_;
76 DISALLOW_COPY_AND_ASSIGN(EntryInfo); 79 DISALLOW_COPY_AND_ASSIGN(EntryInfo);
77 }; 80 };
78 81
82 class Listener : public base::RefCountedThreadSafe<Listener> {
satorux1 2013/12/05 04:39:37 ref counted should usually be avoided, as it makes
Drew Haven 2013/12/09 23:33:12 I really wasn't sure how to handle this one. Expl
83 public:
84 // Reports progress information back to the caller. It may or may not be
85 // called, but should be called approximately once per write-chunk or once
86 // per percent, whichever is less. The progress is posted as an integer
87 // percentage.
88 virtual void OnUnzipProgress(int progress) = 0;
89
90 // Called when the operation completes.
91 virtual void OnUnzipSuccess() = 0;
92
93 // Called if there is an error.
94 virtual void OnUnzipFailed() = 0;
95 protected:
96 virtual ~Listener() {};
97 friend class base::RefCountedThreadSafe<Listener>;
98 };
99
79 ZipReader(); 100 ZipReader();
80 ~ZipReader(); 101 ~ZipReader();
81 102
82 // Opens the zip file specified by |zip_file_path|. Returns true on 103 // Opens the zip file specified by |zip_file_path|. Returns true on
83 // success. 104 // success.
84 bool Open(const base::FilePath& zip_file_path); 105 bool Open(const base::FilePath& zip_file_path);
85 106
86 // Opens the zip file referred to by the platform file |zip_fd|. 107 // Opens the zip file referred to by the platform file |zip_fd|.
87 // Returns true on success. 108 // Returns true on success.
88 bool OpenFromPlatformFile(base::PlatformFile zip_fd); 109 bool OpenFromPlatformFile(base::PlatformFile zip_fd);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); 144 bool LocateAndOpenEntry(const base::FilePath& path_in_zip);
124 145
125 // Extracts the current entry to the given output file path. If the 146 // Extracts the current entry to the given output file path. If the
126 // current file is a directory, just creates a directory 147 // current file is a directory, just creates a directory
127 // instead. Returns true on success. OpenCurrentEntryInZip() must be 148 // instead. Returns true on success. OpenCurrentEntryInZip() must be
128 // called beforehand. 149 // called beforehand.
129 // 150 //
130 // This function does not preserve the timestamp of the original entry. 151 // This function does not preserve the timestamp of the original entry.
131 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); 152 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path);
132 153
154 // Asynchronously extracts the current entry to the given output file path.
155 // See ExtractCurrentEntryToFilePath for more information.
156 void ExtractCurrentEntryToFilePathAsync(
157 const base::FilePath& output_file_path,
158 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
159 scoped_refptr<Listener> listener);
160
133 // Extracts the current entry to the given output directory path using 161 // Extracts the current entry to the given output directory path using
134 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed 162 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed
135 // based on the file path of the current entry. For example, if the file 163 // based on the file path of the current entry. For example, if the file
136 // path in zip is "foo/bar.txt", and the output directory is "output", 164 // path in zip is "foo/bar.txt", and the output directory is "output",
137 // "output/foo/bar.txt" will be created. 165 // "output/foo/bar.txt" will be created.
138 // 166 //
139 // Returns true on success. OpenCurrentEntryInZip() must be called 167 // Returns true on success. OpenCurrentEntryInZip() must be called
140 // beforehand. 168 // beforehand.
141 bool ExtractCurrentEntryIntoDirectory( 169 bool ExtractCurrentEntryIntoDirectory(
142 const base::FilePath& output_directory_path); 170 const base::FilePath& output_directory_path);
143 171
172 // Asynchronously extracts the current entry to the given output directory.
173 // See ExtractCurrentEntryIntoDirectory for more information.
174 void ExtractCurrentEntryIntoDirectoryAsync(
175 const base::FilePath& output_directory_path,
176 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
177 scoped_refptr<Listener> listener);
178
179 // Asynchronously extracts the current entry to the given PlatformFile. If
180 // the current entry is a directory this function will fail. Does not close
181 // the PlatformFile.
182 void ExtractCurrentEntryToPlatformFileAsync(
183 base::PlatformFile output_file,
184 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
185 scoped_refptr<Listener> listener);
186
144 #if defined(OS_POSIX) 187 #if defined(OS_POSIX)
145 // Extracts the current entry by writing directly to a file descriptor. 188 // Extracts the current entry by writing directly to a file descriptor.
146 // Does not close the file descriptor. Returns true on success. 189 // Does not close the file descriptor. Returns true on success.
147 bool ExtractCurrentEntryToFd(int fd); 190 bool ExtractCurrentEntryToFd(int fd);
191
192 // Asynchronously extracts the current entry by writing directly to a file
193 // descriptor.
194 // See ExtractCurrentEntryToFd for more information.
195 void ExtractCurrentEntryToFdAsync(
196 int fd,
197 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
satorux1 2013/12/05 04:39:37 Three functions were added: ExtractCurrentEntryTo
Drew Haven 2013/12/09 23:33:12 A lot of them were basically "free" so I put them
198 scoped_refptr<Listener> listener);
148 #endif 199 #endif
149 200
150 // Returns the current entry info. Returns NULL if the current entry is 201 // Returns the current entry info. Returns NULL if the current entry is
151 // not yet opened. OpenCurrentEntryInZip() must be called beforehand. 202 // not yet opened. OpenCurrentEntryInZip() must be called beforehand.
152 EntryInfo* current_entry_info() const { 203 EntryInfo* current_entry_info() const {
153 return current_entry_info_.get(); 204 return current_entry_info_.get();
154 } 205 }
155 206
156 // Returns the number of entries in the zip file. 207 // Returns the number of entries in the zip file.
157 // Open() must be called beforehand. 208 // Open() must be called beforehand.
158 int num_entries() const { return num_entries_; } 209 int num_entries() const { return num_entries_; }
159 210
160 private: 211 private:
161 // Common code used both in Open and OpenFromFd. 212 // Common code used both in Open and OpenFromFd.
162 bool OpenInternal(); 213 bool OpenInternal();
163 214
164 // Resets the internal state. 215 // Resets the internal state.
165 void Reset(); 216 void Reset();
166 217
218 // Unzip loop for asynchronous code.
219 void ExtractChunk(base::PlatformFile target_file,
220 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
221 scoped_refptr<Listener> listener,
222 int offset,
223 int size);
224
167 unzFile zip_file_; 225 unzFile zip_file_;
168 int num_entries_; 226 int num_entries_;
169 bool reached_end_; 227 bool reached_end_;
170 scoped_ptr<EntryInfo> current_entry_info_; 228 scoped_ptr<EntryInfo> current_entry_info_;
171 229
230 base::WeakPtrFactory<ZipReader> weak_factory_;
231
172 DISALLOW_COPY_AND_ASSIGN(ZipReader); 232 DISALLOW_COPY_AND_ASSIGN(ZipReader);
173 }; 233 };
174 234
175 } // namespace zip 235 } // namespace zip
176 236
177 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ 237 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698