Expose zip archive reading support (issue #30).

Move ClientReadHandler to CefByteReadHandler in cef_wrapper.h.
Add support for the time_t data type to cef_parser.py.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@125 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2010-10-24 16:41:21 +00:00
parent 1911b23bf5
commit 5a4c8f5a13
20 changed files with 1492 additions and 99 deletions

View File

@@ -0,0 +1,67 @@
// 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/cef_wrapper.h"
#include "base/logging.h"
CefByteReadHandler::CefByteReadHandler(const unsigned char* bytes, size_t size,
CefRefPtr<CefBase> source)
: bytes_(bytes), size_(size), offset_(0), source_(source)
{
}
size_t CefByteReadHandler::Read(void* ptr, size_t size, size_t n)
{
Lock();
size_t s = (size_ - offset_) / size;
size_t ret = min(n, s);
memcpy(ptr, bytes_ + offset_, ret * size);
offset_ += ret * size;
Unlock();
return ret;
}
int CefByteReadHandler::Seek(long offset, int whence)
{
int rv = -1L;
Lock();
switch(whence) {
case SEEK_CUR:
if(offset_ + offset > size_)
break;
offset_ += offset;
rv = offset_;
break;
case SEEK_END:
if(offset > static_cast<long>(size_))
break;
offset_ = size_ - offset;
rv = offset_;
case SEEK_SET:
if(offset > static_cast<long>(size_))
break;
offset_ = offset;
rv = offset_;
break;
}
Unlock();
return rv;
}
long CefByteReadHandler::Tell()
{
Lock();
long rv = offset_;
Unlock();
return rv;
}
int CefByteReadHandler::Eof()
{
Lock();
int rv = (offset_ >= size_);
Unlock();
return rv;
}

View File

@@ -0,0 +1,163 @@
// 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/cef_wrapper.h"
#include "base/logging.h"
#include <algorithm>
namespace {
class CefZipFile : public CefThreadSafeBase<CefZipArchive::File>
{
public:
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_;
};
} // namespace
// CefZipArchive implementation
CefZipArchive::CefZipArchive()
{
}
CefZipArchive::~CefZipArchive()
{
}
size_t CefZipArchive::Load(CefRefPtr<CefStreamReader> stream,
bool overwriteExisting)
{
AutoLock lock_scope(this);
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(L""))
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<std::wstring, CefRefPtr<File> >(name, contents.get()));
} while (reader->MoveToNextFile());
return count;
}
void CefZipArchive::Clear()
{
AutoLock lock_scope(this);
contents_.empty();
}
size_t CefZipArchive::GetFileCount()
{
AutoLock lock_scope(this);
return contents_.size();
}
bool CefZipArchive::HasFile(const std::wstring& fileName)
{
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
AutoLock lock_scope(this);
FileMap::const_iterator it = contents_.find(str);
return (it != contents_.end());
}
CefRefPtr<CefZipArchive::File> CefZipArchive::GetFile(
const std::wstring& fileName)
{
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
AutoLock lock_scope(this);
FileMap::const_iterator it = contents_.find(str);
if (it != contents_.end())
return it->second;
return NULL;
}
bool CefZipArchive::RemoveFile(const std::wstring& fileName)
{
std::wstring str = fileName;
std::transform(str.begin(), str.end(), str.begin(), towlower);
AutoLock lock_scope(this);
FileMap::iterator it = contents_.find(str);
if (it != contents_.end()) {
contents_.erase(it);
return true;
}
return false;
}
size_t CefZipArchive::GetFiles(FileMap& map)
{
AutoLock lock_scope(this);
map = contents_;
return contents_.size();
}

View File

@@ -22,6 +22,7 @@
#include "libcef_dll/ctocpp/stream_writer_ctocpp.h"
#include "libcef_dll/ctocpp/v8value_ctocpp.h"
#include "libcef_dll/ctocpp/xml_reader_ctocpp.h"
#include "libcef_dll/ctocpp/zip_reader_ctocpp.h"
bool CefInitialize(bool multi_threaded_message_loop,
@@ -52,6 +53,7 @@ void CefShutdown()
DCHECK(CefStreamWriterCToCpp::DebugObjCt == 0);
DCHECK(CefV8ValueCToCpp::DebugObjCt == 0);
DCHECK(CefXmlReaderCToCpp::DebugObjCt == 0);
DCHECK(CefZipReaderCToCpp::DebugObjCt == 0);
#endif // _DEBUG
}