• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ref_counted.h"
44 #include "include/cef_base.h"
45 
46 class CefStreamReader;
47 
48 ///
49 // Thread-safe class for accessing zip archive file contents. This class should
50 // not be used with large archive files because all data will be resident in
51 // memory at the same time. This implementation supports a restricted set of zip
52 // archive features:
53 // (1) All file names are stored and compared in lower case.
54 // (2) File ordering from the original zip archive is not maintained. This
55 //     means that files from the same folder may not be located together in the
56 //     file content map.
57 ///
58 class CefZipArchive : public base::RefCountedThreadSafe<CefZipArchive> {
59  public:
60   ///
61   // Class representing a file in the archive. Accessing the file data from
62   // multiple threads is safe provided a reference to the File object is kept.
63   ///
64   class File : public CefBaseRefCounted {
65    public:
66     ///
67     // Returns the read-only data contained in the file.
68     ///
69     virtual const unsigned char* GetData() const = 0;
70 
71     ///
72     // Returns the size of the data in the file.
73     ///
74     virtual size_t GetDataSize() const = 0;
75 
76     ///
77     // Returns a CefStreamReader object for streaming the contents of the file.
78     ///
79     virtual CefRefPtr<CefStreamReader> GetStreamReader() const = 0;
80   };
81 
82   using FileMap = std::map<CefString, CefRefPtr<File>>;
83 
84   ///
85   // Create a new object.
86   ///
87   CefZipArchive();
88 
89   CefZipArchive(const CefZipArchive&) = delete;
90   CefZipArchive& operator=(const CefZipArchive&) = delete;
91 
92   ///
93   // Load the contents of the specified zip archive stream into this object.
94   // If the zip archive requires a password then provide it via |password|.
95   // If |overwriteExisting| is true then any files in this object that also
96   // exist in the specified archive will be replaced with the new files.
97   // Returns the number of files successfully loaded.
98   ///
99   size_t Load(CefRefPtr<CefStreamReader> stream,
100               const CefString& password,
101               bool overwriteExisting);
102 
103   ///
104   // Clears the contents of this object.
105   ///
106   void Clear();
107 
108   ///
109   // Returns the number of files in the archive.
110   ///
111   size_t GetFileCount() const;
112 
113   ///
114   // Returns true if the specified file exists and has contents.
115   ///
116   bool HasFile(const CefString& fileName) const;
117 
118   ///
119   // Returns the specified file.
120   ///
121   CefRefPtr<File> GetFile(const CefString& fileName) const;
122 
123   ///
124   // Removes the specified file.
125   ///
126   bool RemoveFile(const CefString& fileName);
127 
128   ///
129   // Returns the map of all files.
130   ///
131   size_t GetFiles(FileMap& map) const;
132 
133  private:
134   // Protect against accidental deletion of this object.
135   friend class base::RefCountedThreadSafe<CefZipArchive>;
136   ~CefZipArchive();
137 
138   FileMap contents_;
139 
140   mutable base::Lock lock_;
141 };
142 
143 #endif  // CEF_INCLUDE_WRAPPER_CEF_ZIP_ARCHIVE_H_
144