1 // Copyright (c) 2011 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 // 32 // The contents of this file are only available to applications that link 33 // against the libcef_dll_wrapper target. 34 // 35 36 #ifndef CEF_INCLUDE_WRAPPER_CEF_ZIP_ARCHIVE_H_ 37 #define CEF_INCLUDE_WRAPPER_CEF_ZIP_ARCHIVE_H_ 38 #pragma once 39 40 #include <map> 41 42 #include "include/base/cef_lock.h" 43 #include "include/base/cef_macros.h" 44 #include "include/base/cef_ref_counted.h" 45 #include "include/cef_base.h" 46 47 class CefStreamReader; 48 49 /// 50 // Thread-safe class for accessing zip archive file contents. This class should 51 // not be used with large archive files because all data will be resident in 52 // memory at the same time. This implementation supports a restricted set of zip 53 // archive features: 54 // (1) All file names are stored and compared in lower case. 55 // (2) File ordering from the original zip archive is not maintained. This 56 // means that files from the same folder may not be located together in the 57 // file content map. 58 /// 59 class CefZipArchive : public base::RefCountedThreadSafe<CefZipArchive> { 60 public: 61 /// 62 // Class representing a file in the archive. Accessing the file data from 63 // multiple threads is safe provided a reference to the File object is kept. 64 /// 65 class File : public CefBaseRefCounted { 66 public: 67 /// 68 // Returns the read-only data contained in the file. 69 /// 70 virtual const unsigned char* GetData() const = 0; 71 72 /// 73 // Returns the size of the data in the file. 74 /// 75 virtual size_t GetDataSize() const = 0; 76 77 /// 78 // Returns a CefStreamReader object for streaming the contents of the file. 79 /// 80 virtual CefRefPtr<CefStreamReader> GetStreamReader() const = 0; 81 }; 82 83 typedef std::map<CefString, CefRefPtr<File>> FileMap; 84 85 /// 86 // Create a new object. 87 /// 88 CefZipArchive(); 89 90 /// 91 // Load the contents of the specified zip archive stream into this object. 92 // If the zip archive requires a password then provide it via |password|. 93 // If |overwriteExisting| is true then any files in this object that also 94 // exist in the specified archive will be replaced with the new files. 95 // Returns the number of files successfully loaded. 96 /// 97 size_t Load(CefRefPtr<CefStreamReader> stream, 98 const CefString& password, 99 bool overwriteExisting); 100 101 /// 102 // Clears the contents of this object. 103 /// 104 void Clear(); 105 106 /// 107 // Returns the number of files in the archive. 108 /// 109 size_t GetFileCount() const; 110 111 /// 112 // Returns true if the specified file exists and has contents. 113 /// 114 bool HasFile(const CefString& fileName) const; 115 116 /// 117 // Returns the specified file. 118 /// 119 CefRefPtr<File> GetFile(const CefString& fileName) const; 120 121 /// 122 // Removes the specified file. 123 /// 124 bool RemoveFile(const CefString& fileName); 125 126 /// 127 // Returns the map of all files. 128 /// 129 size_t GetFiles(FileMap& map) const; 130 131 private: 132 // Protect against accidental deletion of this object. 133 friend class base::RefCountedThreadSafe<CefZipArchive>; 134 ~CefZipArchive(); 135 136 FileMap contents_; 137 138 mutable base::Lock lock_; 139 140 DISALLOW_COPY_AND_ASSIGN(CefZipArchive); 141 }; 142 143 #endif // CEF_INCLUDE_WRAPPER_CEF_ZIP_ARCHIVE_H_ 144