cef/libcef_dll/wrapper/cef_zip_archive.cc
Marshall Greenblatt 122397acfc Introduce the use of Chromium types (issue #1336).
Changes to the CEF public API:
- Add base::Bind, base::Callback, base::Lock, base::WeakPtr, scoped_refptr, scoped_ptr and supporting types.
- Add include/wrapper/cef_closure_task.h helpers for converting a base::Closure to a CefTask.
- Change CefRefPtr to extend scoped_refptr.
-- Change CefBase method signatures to match RefCountedThreadSafeBase.
- Change IMPLEMENT_REFCOUNTING to use base::AtomicRefCount*.
-- Remove the CefAtomic* functions.
-- IMPLEMENT_REFCOUNTING now enforces via a compile-time error that the correct class name was passed to the macro.
- Change IMPLEMENT_LOCKING to use base::Lock.
-- Remove the CefCriticalSection class.
-- Deprecate the IMPLEMENT_LOCKING macro.
-- base::Lock will DCHECK() in Debug builds if lock usage is reentrant.
- Move include/internal/cef_tuple.h to include/base/cef_tuple.h.
- Allow an empty |callback| parameter passed to CefBeginTracing.

Changes to the CEF implementation:
- Fix incorrect names passed to the IMPLEMENT_REFCOUNTING macro.
- Fix instances of reentrant locking in the CefXmlObject and CefRequest implementations.
- Remove use of the IMPLEMENT_LOCKING macro.

Changes to cef_unittests:
- Add tests/unittests/chromium_includes.h and always include it first from unit test .cc files to avoid name conflicts with Chromium types.
- Fix wrong header include ordering.
- Remove use of the IMPLEMENT_LOCKING macro.

Changes to cefclient and cefsimple:
- Use base::Bind and cef_closure_task.h instead of NewCefRunnable*.
- Remove use of the IMPEMENT_LOCKING macro.
- Fix incorrect/unnecessary locking.
- Add additional runtime thread checks.
- Windows: Perform actions on the UI thread instead of the main thread when running in multi-threaded-message-loop mode to avoid excessive locking.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1769 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
2014-07-14 22:18:51 +00:00

167 lines
4.1 KiB
C++

// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "include/wrapper/cef_zip_archive.h"
#include <algorithm>
#include <vector>
#include "include/base/cef_logging.h"
#include "include/base/cef_macros.h"
#include "include/cef_stream.h"
#include "include/cef_zip_reader.h"
#include "include/wrapper/cef_byte_read_handler.h"
#if defined(OS_LINUX)
#include <wctype.h>
#endif
namespace {
class CefZipFile : public CefZipArchive::File {
public:
explicit CefZipFile(size_t size) : data_(size) {}
~CefZipFile() {}
// Returns the read-only data contained in the file.
virtual const unsigned char* GetData() { return &data_[0]; }
// Returns the size of the data in the file.
virtual size_t GetDataSize() { return data_.size(); }
// Returns a CefStreamReader object for streaming the contents of the file.
virtual CefRefPtr<CefStreamReader> GetStreamReader() {
CefRefPtr<CefReadHandler> handler(
new CefByteReadHandler(GetData(), GetDataSize(), this));
return CefStreamReader::CreateForHandler(handler);
}
std::vector<unsigned char>* GetDataVector() { return &data_; }
private:
std::vector<unsigned char> data_;
IMPLEMENT_REFCOUNTING(CefZipFile);
};
} // namespace
// CefZipArchive implementation
CefZipArchive::CefZipArchive() {
}
CefZipArchive::~CefZipArchive() {
}
size_t CefZipArchive::Load(CefRefPtr<CefStreamReader> stream,
const CefString& password,
bool overwriteExisting) {
base::AutoLock lock_scope(lock_);
CefRefPtr<CefZipReader> reader(CefZipReader::Create(stream));
if (!reader.get())
return 0;
if (!reader->MoveToFirstFile())
return 0;
std::wstring name;
CefRefPtr<CefZipFile> contents;
FileMap::iterator it;
std::vector<unsigned char>* data;
size_t count = 0, size, offset;
do {
size = static_cast<size_t>(reader->GetFileSize());
if (size == 0) {
// Skip directories and empty files.
continue;
}
if (!reader->OpenFile(password))
break;
name = reader->GetFileName();
std::transform(name.begin(), name.end(), name.begin(), towlower);
it = contents_.find(name);
if (it != contents_.end()) {
if (overwriteExisting)
contents_.erase(it);
else // Skip files that already exist.
continue;
}
contents = new CefZipFile(size);
data = contents->GetDataVector();
offset = 0;
// Read the file contents.
do {
offset += reader->ReadFile(&(*data)[offset], size - offset);
} while (offset < size && !reader->Eof());
DCHECK(offset == size);
reader->CloseFile();
count++;
// Add the file to the map.
contents_.insert(std::make_pair(name, contents.get()));
} while (reader->MoveToNextFile());
return count;
}
void CefZipArchive::Clear() {
base::AutoLock lock_scope(lock_);
contents_.clear();
}
size_t CefZipArchive::GetFileCount() {
base::AutoLock lock_scope(lock_);
return contents_.size();
}
bool CefZipArchive::HasFile(const CefString& fileName) {
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
base::AutoLock lock_scope(lock_);
FileMap::const_iterator it = contents_.find(CefString(str));
return (it != contents_.end());
}
CefRefPtr<CefZipArchive::File> CefZipArchive::GetFile(
const CefString& fileName) {
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
base::AutoLock lock_scope(lock_);
FileMap::const_iterator it = contents_.find(CefString(str));
if (it != contents_.end())
return it->second;
return NULL;
}
bool CefZipArchive::RemoveFile(const CefString& fileName) {
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
base::AutoLock lock_scope(lock_);
FileMap::iterator it = contents_.find(CefString(str));
if (it != contents_.end()) {
contents_.erase(it);
return true;
}
return false;
}
size_t CefZipArchive::GetFiles(FileMap& map) {
base::AutoLock lock_scope(lock_);
map = contents_;
return contents_.size();
}