From 2b7e69d20028828a87a17b9a1f619a7c7fca9fbb Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 21 Aug 2009 17:41:09 +0000 Subject: [PATCH] libcef: - Add CefReadHandler and CefWriteHandler classes for creating streams handled by the client. cefclient: - Add a CefReadHandler test. - Move tests out of cefclient.cpp and into separate implementation files. git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@39 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- include/cef.h | 56 ++ include/cef_capi.h | 57 ++ libcef/stream_impl.cc | 37 +- libcef/stream_impl.h | 62 ++- libcef_dll/cpptoc/read_handler_cpptoc.cc | 73 +++ libcef_dll/cpptoc/read_handler_cpptoc.h | 34 ++ libcef_dll/cpptoc/stream_reader_cpptoc.cc | 11 + libcef_dll/cpptoc/stream_writer_cpptoc.cc | 32 ++ libcef_dll/cpptoc/write_handler_cpptoc.cc | 74 +++ libcef_dll/cpptoc/write_handler_cpptoc.h | 35 ++ libcef_dll/ctocpp/read_handler_ctocpp.cc | 56 ++ libcef_dll/ctocpp/read_handler_ctocpp.h | 43 ++ libcef_dll/ctocpp/stream_reader_ctocpp.cc | 11 + libcef_dll/ctocpp/stream_writer_ctocpp.cc | 33 ++ libcef_dll/ctocpp/write_handler_ctocpp.cc | 56 ++ libcef_dll/ctocpp/write_handler_ctocpp.h | 44 ++ libcef_dll/libcef_dll.cc | 4 + libcef_dll/libcef_dll.vcproj | 48 ++ libcef_dll/wrapper/libcef_dll_wrapper.cc | 4 + libcef_dll/wrapper/libcef_dll_wrapper.vcproj | 16 + tests/cefclient/binding_test.cpp | 174 ++++++ tests/cefclient/binding_test.h | 14 + tests/cefclient/cefclient.cpp | 532 +------------------ tests/cefclient/cefclient.vcproj | 48 ++ tests/cefclient/extension_test.cpp | 112 ++++ tests/cefclient/extension_test.h | 12 + tests/cefclient/plugin_test.cpp | 40 ++ tests/cefclient/plugin_test.h | 12 + tests/cefclient/resource_util.cpp | 91 ++++ tests/cefclient/resource_util.h | 39 ++ tests/cefclient/scheme_test.cpp | 134 +++++ tests/cefclient/scheme_test.h | 12 + tests/cefclient/string_util.cpp | 84 +++ tests/cefclient/string_util.h | 18 + 34 files changed, 1592 insertions(+), 516 deletions(-) create mode 100644 libcef_dll/cpptoc/read_handler_cpptoc.cc create mode 100644 libcef_dll/cpptoc/read_handler_cpptoc.h create mode 100644 libcef_dll/cpptoc/write_handler_cpptoc.cc create mode 100644 libcef_dll/cpptoc/write_handler_cpptoc.h create mode 100644 libcef_dll/ctocpp/read_handler_ctocpp.cc create mode 100644 libcef_dll/ctocpp/read_handler_ctocpp.h create mode 100644 libcef_dll/ctocpp/write_handler_ctocpp.cc create mode 100644 libcef_dll/ctocpp/write_handler_ctocpp.h create mode 100644 tests/cefclient/binding_test.cpp create mode 100644 tests/cefclient/binding_test.h create mode 100644 tests/cefclient/extension_test.cpp create mode 100644 tests/cefclient/extension_test.h create mode 100644 tests/cefclient/plugin_test.cpp create mode 100644 tests/cefclient/plugin_test.h create mode 100644 tests/cefclient/resource_util.cpp create mode 100644 tests/cefclient/resource_util.h create mode 100644 tests/cefclient/scheme_test.cpp create mode 100644 tests/cefclient/scheme_test.h create mode 100644 tests/cefclient/string_util.cpp create mode 100644 tests/cefclient/string_util.h diff --git a/include/cef.h b/include/cef.h index f53252871..fca62d555 100644 --- a/include/cef.h +++ b/include/cef.h @@ -745,6 +745,30 @@ public: }; +// Interface the client can implement to provide a custom stream reader. +/*--cef(source=client)--*/ +class CefReadHandler : public CefBase +{ +public: + // Read raw binary data. + /*--cef()--*/ + virtual size_t Read(void* ptr, size_t size, size_t n) =0; + + // Seek to the specified offset position. |whence| may be any one of + // SEEK_CUR, SEEK_END or SEEK_SET. + /*--cef()--*/ + virtual int Seek(long offset, int whence) =0; + + // Return the current offset position. + /*--cef()--*/ + virtual long Tell() =0; + + // Return non-zero if at end of file. + /*--cef()--*/ + virtual int Eof() =0; +}; + + // Class used to read data from a stream. /*--cef(source=library)--*/ class CefStreamReader : public CefBase @@ -755,6 +779,8 @@ public: static CefRefPtr CreateForFile(const std::wstring& fileName); /*--cef()--*/ static CefRefPtr CreateForData(void* data, size_t size); + /*--cef()--*/ + static CefRefPtr CreateForHandler(CefRefPtr handler); // Read raw binary data. /*--cef()--*/ @@ -775,11 +801,41 @@ public: }; +// Interface the client can implement to provide a custom stream writer. +/*--cef(source=client)--*/ +class CefWriteHandler : public CefBase +{ +public: + // Write raw binary data. + /*--cef()--*/ + virtual size_t Write(const void* ptr, size_t size, size_t n) =0; + + // Seek to the specified offset position. |whence| may be any one of + // SEEK_CUR, SEEK_END or SEEK_SET. + /*--cef()--*/ + virtual int Seek(long offset, int whence) =0; + + // Return the current offset position. + /*--cef()--*/ + virtual long Tell() =0; + + // Flush the stream. + /*--cef()--*/ + virtual int Flush() =0; +}; + + // Class used to write data to a stream. /*--cef(source=library)--*/ class CefStreamWriter : public CefBase { public: + // Create a new CefStreamWriter object. + /*--cef()--*/ + static CefRefPtr CreateForFile(const std::wstring& fileName); + /*--cef()--*/ + static CefRefPtr CreateForHandler(CefRefPtr handler); + // Write raw binary data. /*--cef()--*/ virtual size_t Write(const void* ptr, size_t size, size_t n) =0; diff --git a/include/cef_capi.h b/include/cef_capi.h index 6c8db308f..8c8729d3f 100644 --- a/include/cef_capi.h +++ b/include/cef_capi.h @@ -600,6 +600,30 @@ typedef struct _cef_post_data_element_t CEF_EXPORT cef_post_data_element_t* cef_post_data_element_create(); +// Structure the client can implement to provide a custom stream reader. +typedef struct _cef_read_handler_t +{ + // Base structure. + cef_base_t base; + + // Read raw binary data. + size_t (CEF_CALLBACK *read)(struct _cef_read_handler_t* self, void* ptr, + size_t size, size_t n); + + // Seek to the specified offset position. |whence| may be any one of SEEK_CUR, + // SEEK_END or SEEK_SET. + int (CEF_CALLBACK *seek)(struct _cef_read_handler_t* self, long offset, + int whence); + + // Return the current offset position. + long (CEF_CALLBACK *tell)(struct _cef_read_handler_t* self); + + // Return non-zero if at end of file. + int (CEF_CALLBACK *eof)(struct _cef_read_handler_t* self); + +} cef_read_handler_t; + + // Structure used to read data from a stream. typedef struct _cef_stream_reader_t { @@ -629,6 +653,32 @@ CEF_EXPORT cef_stream_reader_t* cef_stream_reader_create_for_file( const wchar_t* fileName); CEF_EXPORT cef_stream_reader_t* cef_stream_reader_create_for_data(void* data, size_t size); +CEF_EXPORT cef_stream_reader_t* cef_stream_reader_create_for_handler( + cef_read_handler_t* handler); + + +// Structure the client can implement to provide a custom stream writer. +typedef struct _cef_write_handler_t +{ + // Base structure. + cef_base_t base; + + // Write raw binary data. + size_t (CEF_CALLBACK *write)(struct _cef_write_handler_t* self, + const void* ptr, size_t size, size_t n); + + // Seek to the specified offset position. |whence| may be any one of SEEK_CUR, + // SEEK_END or SEEK_SET. + int (CEF_CALLBACK *seek)(struct _cef_write_handler_t* self, long offset, + int whence); + + // Return the current offset position. + long (CEF_CALLBACK *tell)(struct _cef_write_handler_t* self); + + // Flush the stream. + int (CEF_CALLBACK *flush)(struct _cef_write_handler_t* self); + +} cef_write_handler_t; // Structure used to write data to a stream. @@ -655,6 +705,13 @@ typedef struct _cef_stream_writer_t } cef_stream_writer_t; +// Create a new cef_stream_writer_t object. +CEF_EXPORT cef_stream_writer_t* cef_stream_writer_create_for_file( + const wchar_t* fileName); +CEF_EXPORT cef_stream_writer_t* cef_stream_writer_create_for_handler( + cef_write_handler_t* handler); + + // Structure that should be implemented to handle V8 function calls. typedef struct _cef_v8handler_t { diff --git a/libcef/stream_impl.cc b/libcef/stream_impl.cc index 0ba81acee..bb6039b3b 100644 --- a/libcef/stream_impl.cc +++ b/libcef/stream_impl.cc @@ -10,7 +10,8 @@ // Static functions -CefRefPtr CefStreamReader::CreateForFile(const std::wstring& fileName) +CefRefPtr CefStreamReader::CreateForFile( + const std::wstring& fileName) { CefRefPtr reader; FILE *f = _wfopen(fileName.c_str(), L"rb"); @@ -19,7 +20,8 @@ CefRefPtr CefStreamReader::CreateForFile(const std::wstring& fi return reader; } -CefRefPtr CefStreamReader::CreateForData(void* data, size_t size) +CefRefPtr CefStreamReader::CreateForData(void* data, + size_t size) { DCHECK(data != NULL); DCHECK(size > 0); @@ -29,6 +31,37 @@ CefRefPtr CefStreamReader::CreateForData(void* data, size_t siz return reader; } +CefRefPtr CefStreamReader::CreateForHandler( + CefRefPtr handler) +{ + DCHECK(handler.get()); + CefRefPtr reader; + if(handler.get()) + reader = new CefHandlerReader(handler); + return reader; +} + +CefRefPtr CefStreamWriter::CreateForFile( + const std::wstring& fileName) +{ + DCHECK(!fileName.empty()); + CefRefPtr writer; + FILE* file = _wfopen(fileName.c_str(), L"wb"); + if(file) + writer = new CefFileWriter(file, true); + return writer; +} + +CefRefPtr CefStreamWriter::CreateForHandler( + CefRefPtr handler) +{ + DCHECK(handler.get()); + CefRefPtr writer; + if(handler.get()) + writer = new CefHandlerWriter(handler); + return writer; +} + // CefFileReader diff --git a/libcef/stream_impl.h b/libcef/stream_impl.h index ca582f78d..9fd095606 100644 --- a/libcef/stream_impl.h +++ b/libcef/stream_impl.h @@ -13,7 +13,7 @@ class CefFileReader : public CefThreadSafeBase { public: CefFileReader(FILE* file, bool close); - ~CefFileReader(); + virtual ~CefFileReader(); virtual size_t Read(void* ptr, size_t size, size_t n); virtual int Seek(long offset, int whence); @@ -30,7 +30,7 @@ class CefFileWriter : public CefThreadSafeBase { public: CefFileWriter(FILE* file, bool close); - ~CefFileWriter(); + virtual ~CefFileWriter(); virtual size_t Write(const void* ptr, size_t size, size_t n); virtual int Seek(long offset, int whence); @@ -47,7 +47,7 @@ class CefBytesReader : public CefThreadSafeBase { public: CefBytesReader(void* data, long datasize, bool copy); - ~CefBytesReader(); + virtual ~CefBytesReader(); virtual size_t Read(void* ptr, size_t size, size_t n); virtual int Seek(long offset, int whence); @@ -71,7 +71,7 @@ class CefBytesWriter : public CefThreadSafeBase { public: CefBytesWriter(size_t grow); - ~CefBytesWriter(); + virtual ~CefBytesWriter(); virtual size_t Write(const void* ptr, size_t size, size_t n); virtual int Seek(long offset, int whence); @@ -92,4 +92,58 @@ protected: size_t offset_; }; +// Implementation of CefStreamReader for handlers. +class CefHandlerReader : public CefThreadSafeBase +{ +public: + CefHandlerReader(CefRefPtr handler) : handler_(handler) {} + + virtual size_t Read(void* ptr, size_t size, size_t n) + { + return handler_->Read(ptr, size, n); + } + virtual int Seek(long offset, int whence) + { + return handler_->Seek(offset, whence); + } + virtual long Tell() + { + return handler_->Tell(); + } + virtual int Eof() + { + return handler_->Eof(); + } + +protected: + CefRefPtr handler_; +}; + +// Implementation of CefStreamWriter for handlers. +class CefHandlerWriter : public CefThreadSafeBase +{ +public: + CefHandlerWriter(CefRefPtr handler) : handler_(handler) {} + + virtual size_t Write(const void* ptr, size_t size, size_t n) + { + return handler_->Write(ptr, size, n); + } + virtual int Seek(long offset, int whence) + { + return handler_->Seek(offset, whence); + } + virtual long Tell() + { + return handler_->Tell(); + } + virtual int Flush() + { + return handler_->Flush(); + } + +protected: + CefRefPtr handler_; +}; + #endif // _STREAM_IMPL_H diff --git a/libcef_dll/cpptoc/read_handler_cpptoc.cc b/libcef_dll/cpptoc/read_handler_cpptoc.cc new file mode 100644 index 000000000..f8ebdb771 --- /dev/null +++ b/libcef_dll/cpptoc/read_handler_cpptoc.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// A portion of this file was generated by the CEF translator tool. When +// making changes by hand only do so within the body of existing function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#include "../precompiled_libcef.h" +#include "cpptoc/read_handler_cpptoc.h" + + +// MEMBER FUNCTIONS - Body may be edited by hand. + +size_t CEF_CALLBACK read_handler_read(struct _cef_read_handler_t* self, + void* ptr, size_t size, size_t n) +{ + DCHECK(self); + if(!self) + return 0; + + return CefReadHandlerCppToC::Get(self)->Read(ptr, size, n); +} + +int CEF_CALLBACK read_handler_seek(struct _cef_read_handler_t* self, + long offset, int whence) +{ + DCHECK(self); + if(!self) + return 0; + + return CefReadHandlerCppToC::Get(self)->Seek(offset, whence); +} + +long CEF_CALLBACK read_handler_tell(struct _cef_read_handler_t* self) +{ + DCHECK(self); + if(!self) + return 0; + + return CefReadHandlerCppToC::Get(self)->Tell(); +} + +int CEF_CALLBACK read_handler_eof(struct _cef_read_handler_t* self) +{ + DCHECK(self); + if(!self) + return 0; + + return CefReadHandlerCppToC::Get(self)->Eof(); +} + + +// CONSTRUCTOR - Do not edit by hand. + +CefReadHandlerCppToC::CefReadHandlerCppToC(CefReadHandler* cls) + : CefCppToC(cls) +{ + struct_.struct_.read = read_handler_read; + struct_.struct_.seek = read_handler_seek; + struct_.struct_.tell = read_handler_tell; + struct_.struct_.eof = read_handler_eof; +} + +#ifdef _DEBUG +long CefCppToC::DebugObjCt = 0; +#endif + diff --git a/libcef_dll/cpptoc/read_handler_cpptoc.h b/libcef_dll/cpptoc/read_handler_cpptoc.h new file mode 100644 index 000000000..fc1256082 --- /dev/null +++ b/libcef_dll/cpptoc/read_handler_cpptoc.h @@ -0,0 +1,34 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool and should not edited +// by hand. See the translator.README.txt file in the tools directory for +// more information. +// +#ifndef _READHANDLER_CPPTOC_H +#define _READHANDLER_CPPTOC_H + +#ifndef USING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed wrapper-side only") +#else // USING_CEF_SHARED + +#include "cef.h" +#include "cef_capi.h" +#include "cpptoc.h" + +// Wrap a C++ class with a C structure. +// This class may be instantiated and accessed wrapper-side only. +class CefReadHandlerCppToC + : public CefCppToC +{ +public: + CefReadHandlerCppToC(CefReadHandler* cls); + virtual ~CefReadHandlerCppToC() {} +}; + +#endif // USING_CEF_SHARED +#endif // _READHANDLER_CPPTOC_H + diff --git a/libcef_dll/cpptoc/stream_reader_cpptoc.cc b/libcef_dll/cpptoc/stream_reader_cpptoc.cc index 799eb957e..0b6a1b4db 100644 --- a/libcef_dll/cpptoc/stream_reader_cpptoc.cc +++ b/libcef_dll/cpptoc/stream_reader_cpptoc.cc @@ -12,6 +12,7 @@ #include "../precompiled_libcef.h" #include "cpptoc/stream_reader_cpptoc.h" +#include "ctocpp/read_handler_ctocpp.h" // GLOBAL FUNCTIONS - Body may be edited by hand. @@ -37,6 +38,16 @@ CEF_EXPORT cef_stream_reader_t* cef_stream_reader_create_for_data(void* data, return NULL; } +CEF_EXPORT cef_stream_reader_t* cef_stream_reader_create_for_handler( + cef_read_handler_t* handler) +{ + CefRefPtr impl = + CefStreamReader::CreateForHandler(CefReadHandlerCToCpp::Wrap(handler)); + if(impl.get()) + return CefStreamReaderCppToC::Wrap(impl); + return NULL; +} + // MEMBER FUNCTIONS - Body may be edited by hand. diff --git a/libcef_dll/cpptoc/stream_writer_cpptoc.cc b/libcef_dll/cpptoc/stream_writer_cpptoc.cc index 4a72ac9a8..c120b4cd8 100644 --- a/libcef_dll/cpptoc/stream_writer_cpptoc.cc +++ b/libcef_dll/cpptoc/stream_writer_cpptoc.cc @@ -12,6 +12,38 @@ #include "../precompiled_libcef.h" #include "cpptoc/stream_writer_cpptoc.h" +#include "ctocpp/write_handler_ctocpp.h" + + +// GLOBAL FUNCTIONS - Body may be edited by hand. + +CEF_EXPORT cef_stream_writer_t* cef_stream_writer_create_for_file( + const wchar_t* fileName) +{ + DCHECK(fileName); + if(!fileName) + return NULL; + + std::wstring fileNameStr = fileName; + CefRefPtr impl = CefStreamWriter::CreateForFile(fileName); + if(impl.get()) + return CefStreamWriterCppToC::Wrap(impl); + return NULL; +} + +CEF_EXPORT cef_stream_writer_t* cef_stream_writer_create_for_handler( + cef_write_handler_t* handler) +{ + DCHECK(handler); + if(!handler) + return NULL; + + CefRefPtr impl = + CefStreamWriter::CreateForHandler(CefWriteHandlerCToCpp::Wrap(handler)); + if(impl.get()) + return CefStreamWriterCppToC::Wrap(impl); + return NULL; +} // MEMBER FUNCTIONS - Body may be edited by hand. diff --git a/libcef_dll/cpptoc/write_handler_cpptoc.cc b/libcef_dll/cpptoc/write_handler_cpptoc.cc new file mode 100644 index 000000000..ab68a52c2 --- /dev/null +++ b/libcef_dll/cpptoc/write_handler_cpptoc.cc @@ -0,0 +1,74 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// A portion of this file was generated by the CEF translator tool. When +// making changes by hand only do so within the body of existing function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#include "../precompiled_libcef.h" +#include "cpptoc/write_handler_cpptoc.h" + + +// MEMBER FUNCTIONS - Body may be edited by hand. + +size_t CEF_CALLBACK write_handler_write(struct _cef_write_handler_t* self, + const void* ptr, size_t size, size_t n) +{ + DCHECK(self); + if(!self) + return 0; + + return CefWriteHandlerCppToC::Get(self)->Write(ptr, size, n); +} + +int CEF_CALLBACK write_handler_seek(struct _cef_write_handler_t* self, + long offset, int whence) +{ + DCHECK(self); + if(!self) + return 0; + + return CefWriteHandlerCppToC::Get(self)->Seek(offset, whence); +} + +long CEF_CALLBACK write_handler_tell(struct _cef_write_handler_t* self) +{ + DCHECK(self); + if(!self) + return 0; + + return CefWriteHandlerCppToC::Get(self)->Tell(); +} + +int CEF_CALLBACK write_handler_flush(struct _cef_write_handler_t* self) +{ + DCHECK(self); + if(!self) + return 0; + + return CefWriteHandlerCppToC::Get(self)->Flush(); +} + + +// CONSTRUCTOR - Do not edit by hand. + +CefWriteHandlerCppToC::CefWriteHandlerCppToC(CefWriteHandler* cls) + : CefCppToC( + cls) +{ + struct_.struct_.write = write_handler_write; + struct_.struct_.seek = write_handler_seek; + struct_.struct_.tell = write_handler_tell; + struct_.struct_.flush = write_handler_flush; +} + +#ifdef _DEBUG +long CefCppToC::DebugObjCt = 0; +#endif + diff --git a/libcef_dll/cpptoc/write_handler_cpptoc.h b/libcef_dll/cpptoc/write_handler_cpptoc.h new file mode 100644 index 000000000..c67bbf335 --- /dev/null +++ b/libcef_dll/cpptoc/write_handler_cpptoc.h @@ -0,0 +1,35 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool and should not edited +// by hand. See the translator.README.txt file in the tools directory for +// more information. +// +#ifndef _WRITEHANDLER_CPPTOC_H +#define _WRITEHANDLER_CPPTOC_H + +#ifndef USING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed wrapper-side only") +#else // USING_CEF_SHARED + +#include "cef.h" +#include "cef_capi.h" +#include "cpptoc.h" + +// Wrap a C++ class with a C structure. +// This class may be instantiated and accessed wrapper-side only. +class CefWriteHandlerCppToC + : public CefCppToC +{ +public: + CefWriteHandlerCppToC(CefWriteHandler* cls); + virtual ~CefWriteHandlerCppToC() {} +}; + +#endif // USING_CEF_SHARED +#endif // _WRITEHANDLER_CPPTOC_H + diff --git a/libcef_dll/ctocpp/read_handler_ctocpp.cc b/libcef_dll/ctocpp/read_handler_ctocpp.cc new file mode 100644 index 000000000..409449b49 --- /dev/null +++ b/libcef_dll/ctocpp/read_handler_ctocpp.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// A portion of this file was generated by the CEF translator tool. When +// making changes by hand only do so within the body of existing static and +// virtual method implementations. See the translator.README.txt file in the +// tools directory for more information. +// + +#include "../precompiled_libcef.h" +#include "ctocpp/read_handler_ctocpp.h" + + +// VIRTUAL METHODS - Body may be edited by hand. + +size_t CefReadHandlerCToCpp::Read(void* ptr, size_t size, size_t n) +{ + if(CEF_MEMBER_MISSING(struct_, read)) + return 0; + + return struct_->read(struct_, ptr, size, n); +} + +int CefReadHandlerCToCpp::Seek(long offset, int whence) +{ + if(CEF_MEMBER_MISSING(struct_, seek)) + return 0; + + return struct_->seek(struct_, offset, whence); +} + +long CefReadHandlerCToCpp::Tell() +{ + if(CEF_MEMBER_MISSING(struct_, tell)) + return 0; + + return struct_->tell(struct_); +} + +int CefReadHandlerCToCpp::Eof() +{ + if(CEF_MEMBER_MISSING(struct_, eof)) + return 0; + + return struct_->eof(struct_); +} + + +#ifdef _DEBUG +long CefCToCpp::DebugObjCt = 0; +#endif + diff --git a/libcef_dll/ctocpp/read_handler_ctocpp.h b/libcef_dll/ctocpp/read_handler_ctocpp.h new file mode 100644 index 000000000..8aa6535f1 --- /dev/null +++ b/libcef_dll/ctocpp/read_handler_ctocpp.h @@ -0,0 +1,43 @@ +// Copyright (c) 2009 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. +// +// ------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool and should not edited +// by hand. See the translator.README.txt file in the tools directory for +// more information. +// + +#ifndef _READHANDLER_CTOCPP_H +#define _READHANDLER_CTOCPP_H + +#ifndef BUILDING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed DLL-side only") +#else // BUILDING_CEF_SHARED + +#include "cef.h" +#include "cef_capi.h" +#include "ctocpp.h" + +// Wrap a C structure with a C++ class. +// This class may be instantiated and accessed DLL-side only. +class CefReadHandlerCToCpp + : public CefCToCpp +{ +public: + CefReadHandlerCToCpp(cef_read_handler_t* str) + : CefCToCpp( + str) {} + virtual ~CefReadHandlerCToCpp() {} + + // CefReadHandler methods + virtual size_t Read(void* ptr, size_t size, size_t n); + virtual int Seek(long offset, int whence); + virtual long Tell(); + virtual int Eof(); +}; + +#endif // BUILDING_CEF_SHARED +#endif // _READHANDLER_CTOCPP_H + diff --git a/libcef_dll/ctocpp/stream_reader_ctocpp.cc b/libcef_dll/ctocpp/stream_reader_ctocpp.cc index 1e8d58fa3..0748ad4a2 100644 --- a/libcef_dll/ctocpp/stream_reader_ctocpp.cc +++ b/libcef_dll/ctocpp/stream_reader_ctocpp.cc @@ -11,6 +11,7 @@ // #include "../precompiled_libcef.h" +#include "cpptoc/read_handler_cpptoc.h" #include "ctocpp/stream_reader_ctocpp.h" @@ -35,6 +36,16 @@ CefRefPtr CefStreamReader::CreateForData(void* data, return NULL; } +CefRefPtr CefStreamReader::CreateForHandler( + CefRefPtr handler) +{ + cef_stream_reader_t* impl = + cef_stream_reader_create_for_handler(CefReadHandlerCppToC::Wrap(handler)); + if(impl) + return CefStreamReaderCToCpp::Wrap(impl); + return NULL; +} + // VIRTUAL METHODS - Body may be edited by hand. diff --git a/libcef_dll/ctocpp/stream_writer_ctocpp.cc b/libcef_dll/ctocpp/stream_writer_ctocpp.cc index b6c0a3eef..1204a6f2b 100644 --- a/libcef_dll/ctocpp/stream_writer_ctocpp.cc +++ b/libcef_dll/ctocpp/stream_writer_ctocpp.cc @@ -11,9 +11,42 @@ // #include "../precompiled_libcef.h" +#include "cpptoc/write_handler_cpptoc.h" #include "ctocpp/stream_writer_ctocpp.h" +// STATIC METHODS - Body may be edited by hand. + +CefRefPtr CefStreamWriter::CreateForFile( + const std::wstring& fileName) +{ + DCHECK(!fileName.empty()); + if(fileName.empty()) + return NULL; + + cef_stream_writer_t* impl = + cef_stream_writer_create_for_file(fileName.c_str()); + if(impl) + return CefStreamWriterCToCpp::Wrap(impl); + return NULL; +} + +CefRefPtr CefStreamWriter::CreateForHandler( + CefRefPtr handler) +{ + DCHECK(handler.get()); + if(!handler.get()) + return NULL; + + cef_stream_writer_t* impl = + cef_stream_writer_create_for_handler( + CefWriteHandlerCppToC::Wrap(handler)); + if(impl) + return CefStreamWriterCToCpp::Wrap(impl); + return NULL; +} + + // VIRTUAL METHODS - Body may be edited by hand. size_t CefStreamWriterCToCpp::Write(const void* ptr, size_t size, size_t n) diff --git a/libcef_dll/ctocpp/write_handler_ctocpp.cc b/libcef_dll/ctocpp/write_handler_ctocpp.cc new file mode 100644 index 000000000..ca459786a --- /dev/null +++ b/libcef_dll/ctocpp/write_handler_ctocpp.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2009 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. +// +// --------------------------------------------------------------------------- +// +// A portion of this file was generated by the CEF translator tool. When +// making changes by hand only do so within the body of existing static and +// virtual method implementations. See the translator.README.txt file in the +// tools directory for more information. +// + +#include "../precompiled_libcef.h" +#include "ctocpp/write_handler_ctocpp.h" + + +// VIRTUAL METHODS - Body may be edited by hand. + +size_t CefWriteHandlerCToCpp::Write(const void* ptr, size_t size, size_t n) +{ + if(CEF_MEMBER_MISSING(struct_, write)) + return 0; + + return struct_->write(struct_, ptr, size, n); +} + +int CefWriteHandlerCToCpp::Seek(long offset, int whence) +{ + if(CEF_MEMBER_MISSING(struct_, seek)) + return 0; + + return struct_->seek(struct_, offset, whence); +} + +long CefWriteHandlerCToCpp::Tell() +{ + if(CEF_MEMBER_MISSING(struct_, tell)) + return 0; + + return struct_->tell(struct_); +} + +int CefWriteHandlerCToCpp::Flush() +{ + if(CEF_MEMBER_MISSING(struct_, flush)) + return 0; + + return struct_->flush(struct_); +} + + +#ifdef _DEBUG +long CefCToCpp::DebugObjCt = 0; +#endif + diff --git a/libcef_dll/ctocpp/write_handler_ctocpp.h b/libcef_dll/ctocpp/write_handler_ctocpp.h new file mode 100644 index 000000000..c477561f9 --- /dev/null +++ b/libcef_dll/ctocpp/write_handler_ctocpp.h @@ -0,0 +1,44 @@ +// Copyright (c) 2009 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. +// +// ------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool and should not edited +// by hand. See the translator.README.txt file in the tools directory for +// more information. +// + +#ifndef _WRITEHANDLER_CTOCPP_H +#define _WRITEHANDLER_CTOCPP_H + +#ifndef BUILDING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed DLL-side only") +#else // BUILDING_CEF_SHARED + +#include "cef.h" +#include "cef_capi.h" +#include "ctocpp.h" + +// Wrap a C structure with a C++ class. +// This class may be instantiated and accessed DLL-side only. +class CefWriteHandlerCToCpp + : public CefCToCpp +{ +public: + CefWriteHandlerCToCpp(cef_write_handler_t* str) + : CefCToCpp( + str) {} + virtual ~CefWriteHandlerCToCpp() {} + + // CefWriteHandler methods + virtual size_t Write(const void* ptr, size_t size, size_t n); + virtual int Seek(long offset, int whence); + virtual long Tell(); + virtual int Flush(); +}; + +#endif // BUILDING_CEF_SHARED +#endif // _WRITEHANDLER_CTOCPP_H + diff --git a/libcef_dll/libcef_dll.cc b/libcef_dll/libcef_dll.cc index cd4e175b7..868743ec7 100644 --- a/libcef_dll/libcef_dll.cc +++ b/libcef_dll/libcef_dll.cc @@ -16,9 +16,11 @@ #include "cpptoc/stream_writer_cpptoc.h" #include "cpptoc/v8value_cpptoc.h" #include "ctocpp/handler_ctocpp.h" +#include "ctocpp/read_handler_ctocpp.h" #include "ctocpp/scheme_handler_ctocpp.h" #include "ctocpp/scheme_handler_factory_ctocpp.h" #include "ctocpp/v8handler_ctocpp.h" +#include "ctocpp/write_handler_ctocpp.h" #include "base/string_util.h" @@ -45,9 +47,11 @@ CEF_EXPORT void cef_shutdown() DCHECK(CefStreamWriterCppToC::DebugObjCt == 0); DCHECK(CefV8ValueCppToC::DebugObjCt == 0); DCHECK(CefHandlerCToCpp::DebugObjCt == 0); + DCHECK(CefReadHandlerCToCpp::DebugObjCt == 0); DCHECK(CefSchemeHandlerCToCpp::DebugObjCt == 0); DCHECK(CefSchemeHandlerFactoryCToCpp::DebugObjCt == 0); DCHECK(CefV8HandlerCToCpp::DebugObjCt == 0); + DCHECK(CefWriteHandlerCToCpp::DebugObjCt == 0); #endif // _DEBUG } diff --git a/libcef_dll/libcef_dll.vcproj b/libcef_dll/libcef_dll.vcproj index ac52f35c4..4b831bd55 100644 --- a/libcef_dll/libcef_dll.vcproj +++ b/libcef_dll/libcef_dll.vcproj @@ -430,6 +430,30 @@ RelativePath=".\ctocpp\handler_ctocpp.h" > + + + + + + + + + + @@ -502,6 +526,30 @@ RelativePath=".\ctocpp\v8handler_ctocpp.h" > + + + + + + + + + + + + + + @@ -165,6 +173,14 @@ RelativePath="..\cpptoc\v8handler_cpptoc.h" > + + + + + + +// Implementation of the V8 handler class for the "window.cef_test.Dump" +// function. +class ClientV8FunctionHandler : public CefThreadSafeBase +{ +public: + ClientV8FunctionHandler() {} + virtual ~ClientV8FunctionHandler() {} + + // Execute with the specified argument list and return value. Return true if + // the method was handled. + virtual bool Execute(const std::wstring& name, + CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + std::wstring& exception) + { + if(name == L"Dump") + { + // The "Dump" function will return a human-readable dump of the input + // arguments. + std::wstringstream stream; + size_t i; + + for(i = 0; i < arguments.size(); ++i) + { + stream << L"arg[" << i << L"] = "; + PrintValue(arguments[i], stream, 0); + stream << L"\n"; + } + + retval = CefV8Value::CreateString(stream.str()); + return true; + } + else if(name == L"Call") + { + // The "Call" function will execute a function to get an object and then + // return the result of calling a function belonging to that object. The + // first arument is the function that will return an object and the second + // argument is the function that will be called on that returned object. + int argSize = arguments.size(); + if(argSize < 2 || !arguments[0]->IsFunction() + || !arguments[1]->IsString()) + return false; + + CefV8ValueList argList; + + // Execute the function stored in the first argument to retrieve an + // object. + CefRefPtr objectPtr; + if(!arguments[0]->ExecuteFunction(object, argList, objectPtr, exception)) + return false; + // Verify that the returned value is an object. + if(!objectPtr.get() || !objectPtr->IsObject()) + return false; + + // Retrieve the member function specified by name in the second argument + // from the object. + CefRefPtr funcPtr = + objectPtr->GetValue(arguments[1]->GetStringValue()); + // Verify that the returned value is a function. + if(!funcPtr.get() || !funcPtr->IsFunction()) + return false; + + // Pass any additional arguments on to the member function. + for(int i = 2; i < argSize; ++i) + argList.push_back(arguments[i]); + + // Execute the member function. + return funcPtr->ExecuteFunction(arguments[0], argList, retval, exception); + } + return false; + } + + // Simple function for formatted output of a V8 value. + void PrintValue(CefRefPtr value, std::wstringstream &stream, + int indent) + { + std::wstringstream indent_stream; + for(int i = 0; i < indent; ++i) + indent_stream << L" "; + std::wstring indent_str = indent_stream.str(); + + if(value->IsUndefined()) + stream << L"(undefined)"; + else if(value->IsNull()) + stream << L"(null)"; + else if(value->IsBool()) + stream << L"(bool) " << (value->GetBoolValue() ? L"true" : L"false"); + else if(value->IsInt()) + stream << L"(int) " << value->GetIntValue(); + else if(value->IsDouble()) + stream << L"(double) " << value->GetDoubleValue(); + else if(value->IsString()) + stream << L"(string) " << value->GetStringValue().c_str(); + else if(value->IsFunction()) + stream << L"(function) " << value->GetFunctionName().c_str(); + else if(value->IsArray()) { + stream << L"(array) ["; + int len = value->GetArrayLength(); + for(int i = 0; i < len; ++i) { + stream << L"\n " << indent_str.c_str() << i << L" = "; + PrintValue(value->GetValue(i), stream, indent+1); + } + stream << L"\n" << indent_str.c_str() << L"]"; + } else if(value->IsObject()) { + stream << L"(object) ["; + std::vector keys; + if(value->GetKeys(keys)) { + for(size_t i = 0; i < keys.size(); ++i) { + stream << L"\n " << indent_str.c_str() << keys[i].c_str() << L" = "; + PrintValue(value->GetValue(keys[i]), stream, indent+1); + } + } + stream << L"\n" << indent_str.c_str() << L"]"; + } + } +}; + + +void InitBindingTest(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr object) +{ + // Create the new V8 object. + CefRefPtr testObjPtr = CefV8Value::CreateObject(NULL); + // Add the new V8 object to the global window object with the name + // "cef_test". + object->SetValue(L"cef_test", testObjPtr); + + // Create an instance of ClientV8FunctionHandler as the V8 handler. + CefRefPtr handlerPtr = new ClientV8FunctionHandler(); + + // Add a new V8 function to the cef_test object with the name "Dump". + testObjPtr->SetValue(L"Dump", + CefV8Value::CreateFunction(L"Dump", handlerPtr)); + // Add a new V8 function to the cef_test object with the name "Call". + testObjPtr->SetValue(L"Call", + CefV8Value::CreateFunction(L"Call", handlerPtr)); +} + +void RunBindingTest(CefRefPtr browser) +{ + std::wstring html = + L"ClientV8FunctionHandler says:
"
+    L""
+    L"
"; + browser->GetMainFrame()->LoadString(html, L"about:blank"); +} diff --git a/tests/cefclient/binding_test.h b/tests/cefclient/binding_test.h new file mode 100644 index 000000000..f8a1d0079 --- /dev/null +++ b/tests/cefclient/binding_test.h @@ -0,0 +1,14 @@ +// Copyright (c) 2009 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. + +#pragma once +#include "cef.h" + +// Add the V8 bindings. +void InitBindingTest(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr object); + +// Run the test. +void RunBindingTest(CefRefPtr browser); diff --git a/tests/cefclient/cefclient.cpp b/tests/cefclient/cefclient.cpp index e56a4edcf..72d33566f 100644 --- a/tests/cefclient/cefclient.cpp +++ b/tests/cefclient/cefclient.cpp @@ -2,11 +2,15 @@ // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. - #include "stdafx.h" -#include "cefclient.h" -#include "clientplugin.h" #include "cef.h" +#include "cefclient.h" +#include "binding_test.h" +#include "extension_test.h" +#include "plugin_test.h" +#include "resource_util.h" +#include "scheme_test.h" +#include "string_util.h" #include @@ -30,284 +34,6 @@ BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); -// Convert a std::string to a std::wstring -std::wstring StringToWString(const std::string& s) -{ - wchar_t* wch; - UINT bytes = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size()+1, NULL, 0); - wch = new wchar_t[bytes]; - if(wch) - bytes = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size()+1, wch, bytes); - std::wstring str = wch; - delete [] wch; - return str; -} - -// Convert a std::wstring to a std::string -std::string WStringToString(const std::wstring& s) -{ - char* ch; - UINT bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), s.size()+1, NULL, 0, - NULL, NULL); - ch = new char[bytes]; - if(ch) - bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), s.size()+1, ch, bytes, - NULL, NULL); - std::string str = ch; - delete [] ch; - return str; -} - -// Load a resource of type BINARY -bool LoadBinaryResource(int binaryId, DWORD &dwSize, LPBYTE &pBytes) -{ - HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(binaryId), - MAKEINTRESOURCE(256)); - if(hRes) - { - HGLOBAL hGlob = LoadResource(hInst, hRes); - if(hGlob) - { - dwSize = SizeofResource(hInst, hRes); - pBytes = (LPBYTE)LockResource(hGlob); - if(dwSize > 0 && pBytes) - return true; - } - } - - return false; -} - -// Dump the contents of the request into a string. -void DumpRequestContents(CefRefPtr request, std::wstring& str) -{ - std::wstringstream ss; - - ss << L"URL: " << request->GetURL(); - ss << L"\nMethod: " << request->GetMethod(); - - CefRequest::HeaderMap headerMap; - request->GetHeaderMap(headerMap); - if(headerMap.size() > 0) { - ss << L"\nHeaders:"; - CefRequest::HeaderMap::const_iterator it = headerMap.begin(); - for(; it != headerMap.end(); ++it) { - ss << L"\n\t" << (*it).first << L": " << (*it).second; - } - } - - CefRefPtr postData = request->GetPostData(); - if(postData.get()) { - CefPostData::ElementVector elements; - postData->GetElements(elements); - if(elements.size() > 0) { - ss << L"\nPost Data:"; - CefRefPtr element; - CefPostData::ElementVector::const_iterator it = elements.begin(); - for(; it != elements.end(); ++it) { - element = (*it); - if(element->GetType() == PDE_TYPE_BYTES) { - // the element is composed of bytes - ss << L"\n\tBytes: "; - if(element->GetBytesCount() == 0) - ss << L"(empty)"; - else { - // retrieve the data. - size_t size = element->GetBytesCount(); - char* bytes = new char[size]; - element->GetBytes(size, bytes); - ss << StringToWString(std::string(bytes, size)); - delete [] bytes; - } - } else if(element->GetType() == PDE_TYPE_FILE) { - ss << L"\n\tFile: " << element->GetFile(); - } - } - } - } - - str = ss.str(); -} - -#ifndef min -#define min(a,b) ((a)<(b)?(a):(b)) -#endif - - -// Implementation of the V8 handler class for the "cef.test" extension. -class ClientV8ExtensionHandler : public CefThreadSafeBase -{ -public: - ClientV8ExtensionHandler() : test_param_(L"An initial string value.") {} - virtual ~ClientV8ExtensionHandler() {} - - // Execute with the specified argument list and return value. Return true if - // the method was handled. - virtual bool Execute(const std::wstring& name, - CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - std::wstring& exception) - { - if(name == L"SetTestParam") - { - // Handle the SetTestParam native function by saving the string argument - // into the local member. - if(arguments.size() != 1 || !arguments[0]->IsString()) - return false; - - test_param_ = arguments[0]->GetStringValue(); - return true; - } - else if(name == L"GetTestParam") - { - // Handle the GetTestParam native function by returning the local member - // value. - retval = CefV8Value::CreateString(test_param_); - return true; - } - else if(name == L"GetTestObject") - { - // Handle the GetTestObject native function by creating and returning a - // new V8 object. - retval = CefV8Value::CreateObject(NULL); - // Add a string parameter to the new V8 object. - retval->SetValue(L"param", CefV8Value::CreateString( - L"Retrieving a parameter on a native object succeeded.")); - // Add a function to the new V8 object. - retval->SetValue(L"GetMessage", - CefV8Value::CreateFunction(L"GetMessage", this)); - return true; - } - else if(name == L"GetMessage") - { - // Handle the GetMessage object function by returning a string. - retval = CefV8Value::CreateString( - L"Calling a function on a native object succeeded."); - return true; - } - return false; - } - -private: - std::wstring test_param_; -}; - - -// Implementation of the schema handler for client:// requests. -class ClientSchemeHandler : public CefThreadSafeBase -{ -public: - ClientSchemeHandler() : size_(0), offset_(0), bytes_(NULL) {} - - // Process the request. All response generation should take place in this - // method. If there is no response set |response_length| to zero and - // ReadResponse() will not be called. If the response length is not known then - // set |response_length| to -1 and ReadResponse() will be called until it - // returns false or until the value of |bytes_read| is set to 0. Otherwise, - // set |response_length| to a positive value and ReadResponse() will be called - // until it returns false, the value of |bytes_read| is set to 0 or the - // specified number of bytes have been read. If there is a response set - // |mime_type| to the mime type for the response. - virtual bool ProcessRequest(CefRefPtr request, - std::wstring& mime_type, int* response_length) - { - bool handled = false; - - Lock(); - std::wstring url = request->GetURL(); - if(wcsstr(url.c_str(), L"handler.html") != NULL) { - // Build the response html - html_ = "Client Scheme Handler" - "This contents of this page page are served by the " - "ClientSchemeHandler class handling the client:// protocol." - "
You should see an image:" - "
";
-      
-      // Output a string representation of the request
-      std::wstring dump;
-      DumpRequestContents(request, dump);
-      html_.append(WStringToString(dump));
-
-      html_.append("

Try the test form:" - "
" - "" - "" - "" - "
"); - - handled = true; - size_ = html_.size(); - bytes_ = (LPBYTE)html_.c_str(); - - // Set the resulting mime type - mime_type = L"text/html"; - } - else if(wcsstr(url.c_str(), L"client.gif") != NULL) { - // Load the response image - if(LoadBinaryResource(IDS_LOGO, size_, bytes_)) { - handled = true; - // Set the resulting mime type - mime_type = L"image/jpg"; - } - } - - // Set the resulting response length - *response_length = size_; - Unlock(); - - return handled; - } - - // Cancel processing of the request. - virtual void Cancel() - { - } - - // Copy up to |bytes_to_read| bytes into |data_out|. If the copy succeeds - // set |bytes_read| to the number of bytes copied and return true. If the - // copy fails return false and ReadResponse() will not be called again. - virtual bool ReadResponse(void* data_out, int bytes_to_read, - int* bytes_read) - { - bool has_data = false; - *bytes_read = 0; - - Lock(); - - if(offset_ < size_) { - // Copy the next block of data into the buffer. - int transfer_size = min(bytes_to_read, static_cast(size_ - offset_)); - memcpy(data_out, bytes_ + offset_, transfer_size); - offset_ += transfer_size; - - *bytes_read = transfer_size; - has_data = true; - } - - Unlock(); - - return has_data; - } - -private: - DWORD size_, offset_; - LPBYTE bytes_; - std::string html_; -}; - -// Implementation of the factory for for creating schema handlers. -class ClientSchemeHandlerFactory : - public CefThreadSafeBase -{ -public: - // Return a new scheme handler instance to handle the request. - virtual CefRefPtr Create() - { - return new ClientSchemeHandler(); - } -}; - // Program entry point function. int APIENTRY _tWinMain(HINSTANCE hInstance, @@ -327,52 +53,15 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, CefInitialize(true, std::wstring()); #endif - // Structure providing information about the client plugin. - CefPluginInfo plugin_info; - plugin_info.display_name = L"Client Plugin"; - plugin_info.unique_name = L"client_plugin"; - plugin_info.version = L"1, 0, 0, 1"; - plugin_info.description = L"My Example Client Plugin"; + // Register the internal client plugin. + InitPluginTest(); - CefPluginMimeType mime_type; - mime_type.mime_type = L"application/x-client-plugin"; - mime_type.file_extensions.push_back(L"*"); - plugin_info.mime_types.push_back(mime_type); - - plugin_info.np_getentrypoints = NP_GetEntryPoints; - plugin_info.np_initialize = NP_Initialize; - plugin_info.np_shutdown = NP_Shutdown; - - // Register the internal client plugin - CefRegisterPlugin(plugin_info); - - // Register a V8 extension with the below JavaScript code that calls native - // methods implemented in ClientV8ExtensionHandler. - std::wstring code = L"var cef;" - L"if (!cef)" - L" cef = {};" - L"if (!cef.test)" - L" cef.test = {};" - L"(function() {" - L" cef.test.__defineGetter__('test_param', function() {" - L" native function GetTestParam();" - L" return GetTestParam();" - L" });" - L" cef.test.__defineSetter__('test_param', function(b) {" - L" native function SetTestParam();" - L" if(b) SetTestParam(b);" - L" });" - L" cef.test.test_object = function() {" - L" native function GetTestObject();" - L" return GetTestObject();" - L" };" - L"})();"; - CefRegisterExtension(L"v8/test", code, new ClientV8ExtensionHandler()); - - // Register the scheme handler factory for requests using the client:// - // protocol in the tests domain. - CefRegisterScheme(L"client", L"tests", new ClientSchemeHandlerFactory()); + // Register the V8 extension handler. + InitExtensionTest(); + // Register the scheme handler. + InitSchemeTest(); + MSG msg; HACCEL hAccelTable; @@ -410,8 +99,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, return (int) msg.wParam; } - - // // FUNCTION: MyRegisterClass() // @@ -477,123 +164,6 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) return TRUE; } -// Implementation of the V8 handler class for the "window.cef_test.Dump" -// function. -class ClientV8FunctionHandler : public CefThreadSafeBase -{ -public: - ClientV8FunctionHandler() {} - virtual ~ClientV8FunctionHandler() {} - - // Execute with the specified argument list and return value. Return true if - // the method was handled. - virtual bool Execute(const std::wstring& name, - CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - std::wstring& exception) - { - if(name == L"Dump") - { - // The "Dump" function will return a human-readable dump of the input - // arguments. - std::wstringstream stream; - size_t i; - - for(i = 0; i < arguments.size(); ++i) - { - stream << L"arg[" << i << L"] = "; - PrintValue(arguments[i], stream, 0); - stream << L"\n"; - } - - retval = CefV8Value::CreateString(stream.str()); - return true; - } - else if(name == L"Call") - { - // The "Call" function will execute a function to get an object and then - // return the result of calling a function belonging to that object. The - // first arument is the function that will return an object and the second - // argument is the function that will be called on that returned object. - int argSize = arguments.size(); - if(argSize < 2 || !arguments[0]->IsFunction() - || !arguments[1]->IsString()) - return false; - - CefV8ValueList argList; - - // Execute the function stored in the first argument to retrieve an - // object. - CefRefPtr objectPtr; - if(!arguments[0]->ExecuteFunction(object, argList, objectPtr, exception)) - return false; - // Verify that the returned value is an object. - if(!objectPtr.get() || !objectPtr->IsObject()) - return false; - - // Retrieve the member function specified by name in the second argument - // from the object. - CefRefPtr funcPtr = - objectPtr->GetValue(arguments[1]->GetStringValue()); - // Verify that the returned value is a function. - if(!funcPtr.get() || !funcPtr->IsFunction()) - return false; - - // Pass any additional arguments on to the member function. - for(int i = 2; i < argSize; ++i) - argList.push_back(arguments[i]); - - // Execute the member function. - return funcPtr->ExecuteFunction(arguments[0], argList, retval, exception); - } - return false; - } - - // Simple function for formatted output of a V8 value. - void PrintValue(CefRefPtr value, std::wstringstream &stream, - int indent) - { - std::wstringstream indent_stream; - for(int i = 0; i < indent; ++i) - indent_stream << L" "; - std::wstring indent_str = indent_stream.str(); - - if(value->IsUndefined()) - stream << L"(undefined)"; - else if(value->IsNull()) - stream << L"(null)"; - else if(value->IsBool()) - stream << L"(bool) " << (value->GetBoolValue() ? L"true" : L"false"); - else if(value->IsInt()) - stream << L"(int) " << value->GetIntValue(); - else if(value->IsDouble()) - stream << L"(double) " << value->GetDoubleValue(); - else if(value->IsString()) - stream << L"(string) " << value->GetStringValue().c_str(); - else if(value->IsFunction()) - stream << L"(function) " << value->GetFunctionName().c_str(); - else if(value->IsArray()) { - stream << L"(array) ["; - int len = value->GetArrayLength(); - for(int i = 0; i < len; ++i) { - stream << L"\n " << indent_str.c_str() << i << L" = "; - PrintValue(value->GetValue(i), stream, indent+1); - } - stream << L"\n" << indent_str.c_str() << L"]"; - } else if(value->IsObject()) { - stream << L"(object) ["; - std::vector keys; - if(value->GetKeys(keys)) { - for(size_t i = 0; i < keys.size(); ++i) { - stream << L"\n " << indent_str.c_str() << keys[i].c_str() << L" = "; - PrintValue(value->GetValue(keys[i]), stream, indent+1); - } - } - stream << L"\n" << indent_str.c_str() << L"]"; - } - } -}; // Client implementation of the browser handler class class ClientHandler : public CefThreadSafeBase @@ -784,7 +354,8 @@ public: DWORD dwSize; LPBYTE pBytes; if(LoadBinaryResource(IDS_LOGO, dwSize, pBytes)) { - resourceStream = CefStreamReader::CreateForData(pBytes, dwSize); + resourceStream = CefStreamReader::CreateForHandler( + new ClientReadHandler(pBytes, dwSize)); mimeType = L"image/jpg"; } } @@ -912,21 +483,8 @@ public: CefRefPtr frame, CefRefPtr object) { - // Create the new V8 object. - CefRefPtr testObjPtr = CefV8Value::CreateObject(NULL); - // Add the new V8 object to the global window object with the name - // "cef_test". - object->SetValue(L"cef_test", testObjPtr); - - // Create an instance of ClientV8FunctionHandler as the V8 handler. - CefRefPtr handlerPtr = new ClientV8FunctionHandler(); - - // Add a new V8 function to the cef_test object with the name "Dump". - testObjPtr->SetValue(L"Dump", - CefV8Value::CreateFunction(L"Dump", handlerPtr)); - // Add a new V8 function to the cef_test object with the name "Call". - testObjPtr->SetValue(L"Call", - CefV8Value::CreateFunction(L"Call", handlerPtr)); + // Add the V8 bindings. + InitBindingTest(browser, frame, object); return RV_HANDLED; } @@ -1162,50 +720,11 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; case ID_TESTS_JAVASCRIPT_HANDLER: // Test the V8 function handler if(browser.get()) - { - std::wstring html = - L"ClientV8FunctionHandler says:
"
-              L""
-              L"
"; - browser->GetMainFrame()->LoadString(html, L"about:blank"); - } + RunBindingTest(browser); return 0; case ID_TESTS_JAVASCRIPT_HANDLER2: // Test the V8 extension handler if(browser.get()) - { - std::wstring html = - L"ClientV8ExtensionHandler says:
"
-              L""
-              L"
"; - browser->GetMainFrame()->LoadString(html, L"about:blank"); - } + RunExtensionTest(browser); return 0; case ID_TESTS_JAVASCRIPT_EXECUTE: // Test execution of javascript if(browser.get()) @@ -1217,14 +736,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; case ID_TESTS_PLUGIN: // Test the custom plugin if(browser.get()) - { - std::wstring html = - L"Client Plugin:
" - L"" - L""; - browser->GetMainFrame()->LoadString(html, L"about:blank"); - } + RunPluginTest(browser); return 0; case ID_TESTS_POPUP: // Test a popup window if(browser.get()) @@ -1265,7 +777,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; case ID_TESTS_SCHEME_HANDLER: // Test the scheme handler if(browser.get()) - browser->GetMainFrame()->LoadURL(L"client://tests/handler.html"); + RunSchemeTest(browser); return 0; } } diff --git a/tests/cefclient/cefclient.vcproj b/tests/cefclient/cefclient.vcproj index 8d20a8e1c..ed6fde850 100644 --- a/tests/cefclient/cefclient.vcproj +++ b/tests/cefclient/cefclient.vcproj @@ -169,6 +169,14 @@ >
+ + + + @@ -189,10 +197,42 @@ RelativePath=".\clientplugin.h" > + + + + + + + + + + + + + + + + @@ -217,6 +257,14 @@ RelativePath=".\stdafx.h" > + + + + diff --git a/tests/cefclient/extension_test.cpp b/tests/cefclient/extension_test.cpp new file mode 100644 index 000000000..ea9e407f2 --- /dev/null +++ b/tests/cefclient/extension_test.cpp @@ -0,0 +1,112 @@ +// Copyright (c) 2008-2009 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 "stdafx.h" +#include "extension_test.h" + + +// Implementation of the V8 handler class for the "cef.test" extension. +class ClientV8ExtensionHandler : public CefThreadSafeBase +{ +public: + ClientV8ExtensionHandler() : test_param_(L"An initial string value.") {} + virtual ~ClientV8ExtensionHandler() {} + + // Execute with the specified argument list and return value. Return true if + // the method was handled. + virtual bool Execute(const std::wstring& name, + CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + std::wstring& exception) + { + if(name == L"SetTestParam") + { + // Handle the SetTestParam native function by saving the string argument + // into the local member. + if(arguments.size() != 1 || !arguments[0]->IsString()) + return false; + + test_param_ = arguments[0]->GetStringValue(); + return true; + } + else if(name == L"GetTestParam") + { + // Handle the GetTestParam native function by returning the local member + // value. + retval = CefV8Value::CreateString(test_param_); + return true; + } + else if(name == L"GetTestObject") + { + // Handle the GetTestObject native function by creating and returning a + // new V8 object. + retval = CefV8Value::CreateObject(NULL); + // Add a string parameter to the new V8 object. + retval->SetValue(L"param", CefV8Value::CreateString( + L"Retrieving a parameter on a native object succeeded.")); + // Add a function to the new V8 object. + retval->SetValue(L"GetMessage", + CefV8Value::CreateFunction(L"GetMessage", this)); + return true; + } + else if(name == L"GetMessage") + { + // Handle the GetMessage object function by returning a string. + retval = CefV8Value::CreateString( + L"Calling a function on a native object succeeded."); + return true; + } + return false; + } + +private: + std::wstring test_param_; +}; + + +void InitExtensionTest() +{ + // Register a V8 extension with the below JavaScript code that calls native + // methods implemented in ClientV8ExtensionHandler. + std::wstring code = L"var cef;" + L"if (!cef)" + L" cef = {};" + L"if (!cef.test)" + L" cef.test = {};" + L"(function() {" + L" cef.test.__defineGetter__('test_param', function() {" + L" native function GetTestParam();" + L" return GetTestParam();" + L" });" + L" cef.test.__defineSetter__('test_param', function(b) {" + L" native function SetTestParam();" + L" if(b) SetTestParam(b);" + L" });" + L" cef.test.test_object = function() {" + L" native function GetTestObject();" + L" return GetTestObject();" + L" };" + L"})();"; + CefRegisterExtension(L"v8/test", code, new ClientV8ExtensionHandler()); +} + +void RunExtensionTest(CefRefPtr browser) +{ + std::wstring html = + L"ClientV8ExtensionHandler says:
"
+    L""
+    L"
"; + browser->GetMainFrame()->LoadString(html, L"about:blank"); +} diff --git a/tests/cefclient/extension_test.h b/tests/cefclient/extension_test.h new file mode 100644 index 000000000..732334793 --- /dev/null +++ b/tests/cefclient/extension_test.h @@ -0,0 +1,12 @@ +// Copyright (c) 2009 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. + +#pragma once +#include "cef.h" + +// Register the V8 extension handler. +void InitExtensionTest(); + +// Run the test. +void RunExtensionTest(CefRefPtr browser); diff --git a/tests/cefclient/plugin_test.cpp b/tests/cefclient/plugin_test.cpp new file mode 100644 index 000000000..8665b2c29 --- /dev/null +++ b/tests/cefclient/plugin_test.cpp @@ -0,0 +1,40 @@ +// Copyright (c) 2008-2009 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 "stdafx.h" +#include "plugin_test.h" +#include "clientplugin.h" + + +void InitPluginTest() +{ + // Structure providing information about the client plugin. + CefPluginInfo plugin_info; + plugin_info.display_name = L"Client Plugin"; + plugin_info.unique_name = L"client_plugin"; + plugin_info.version = L"1, 0, 0, 1"; + plugin_info.description = L"My Example Client Plugin"; + + CefPluginMimeType mime_type; + mime_type.mime_type = L"application/x-client-plugin"; + mime_type.file_extensions.push_back(L"*"); + plugin_info.mime_types.push_back(mime_type); + + plugin_info.np_getentrypoints = NP_GetEntryPoints; + plugin_info.np_initialize = NP_Initialize; + plugin_info.np_shutdown = NP_Shutdown; + + // Register the internal client plugin + CefRegisterPlugin(plugin_info); +} + +void RunPluginTest(CefRefPtr browser) +{ + std::wstring html = + L"Client Plugin:
" + L"" + L""; + browser->GetMainFrame()->LoadString(html, L"about:blank"); +} diff --git a/tests/cefclient/plugin_test.h b/tests/cefclient/plugin_test.h new file mode 100644 index 000000000..28916d978 --- /dev/null +++ b/tests/cefclient/plugin_test.h @@ -0,0 +1,12 @@ +// Copyright (c) 2009 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. + +#pragma once +#include "cef.h" + +// Register the internal client plugin. +void InitPluginTest(); + +// Run the test. +void RunPluginTest(CefRefPtr browser); diff --git a/tests/cefclient/resource_util.cpp b/tests/cefclient/resource_util.cpp new file mode 100644 index 000000000..0f54610ea --- /dev/null +++ b/tests/cefclient/resource_util.cpp @@ -0,0 +1,91 @@ +// Copyright (c) 2008-2009 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 "stdafx.h" +#include "resource_util.h" + + +bool LoadBinaryResource(int binaryId, DWORD &dwSize, LPBYTE &pBytes) +{ + extern HINSTANCE hInst; + HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(binaryId), + MAKEINTRESOURCE(256)); + if(hRes) + { + HGLOBAL hGlob = LoadResource(hInst, hRes); + if(hGlob) + { + dwSize = SizeofResource(hInst, hRes); + pBytes = (LPBYTE)LockResource(hGlob); + if(dwSize > 0 && pBytes) + return true; + } + } + + return false; +} + + +// ClientReadHandler implementation + +ClientReadHandler::ClientReadHandler(LPBYTE pBytes, DWORD dwSize) + : bytes_(pBytes), size_(dwSize), offset_(0) {} + +size_t ClientReadHandler::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 ClientReadHandler::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 > (int)size_) { + break; + } + offset_ = size_ - offset; + rv = offset_; + case SEEK_SET: + if(offset > (int)size_) { + break; + } + offset_ = offset; + rv = offset_; + break; + } + Unlock(); + + return rv; +} + +long ClientReadHandler::Tell() +{ + Lock(); + long rv = offset_; + Unlock(); + return rv; +} + +int ClientReadHandler::Eof() +{ + Lock(); + int rv = (offset_ >= size_); + Unlock(); + return rv; +} diff --git a/tests/cefclient/resource_util.h b/tests/cefclient/resource_util.h new file mode 100644 index 000000000..7498aa2e5 --- /dev/null +++ b/tests/cefclient/resource_util.h @@ -0,0 +1,39 @@ +// Copyright (c) 2009 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. + +#pragma once + +#include "cef.h" + +#ifndef min +#define min(a,b) ((a)<(b)?(a):(b)) +#endif + +// Load a resource of type BINARY +bool LoadBinaryResource(int binaryId, DWORD &dwSize, LPBYTE &pBytes); + + +// Implementation of the stream read handler for reading in-memory data. +class ClientReadHandler : public CefThreadSafeBase +{ +public: + ClientReadHandler(LPBYTE pBytes, DWORD dwSize); + + // Read raw binary data. + virtual size_t Read(void* ptr, size_t size, size_t n); + + // Seek to the specified offset position. |whence| may be any one of + // SEEK_CUR, SEEK_END or SEEK_SET. + virtual int Seek(long offset, int whence); + + // Return the current offset position. + virtual long Tell(); + + // Return non-zero if at end of file. + virtual int Eof(); + +private: + LPBYTE bytes_; + DWORD size_, offset_; +}; diff --git a/tests/cefclient/scheme_test.cpp b/tests/cefclient/scheme_test.cpp new file mode 100644 index 000000000..81bc19f0d --- /dev/null +++ b/tests/cefclient/scheme_test.cpp @@ -0,0 +1,134 @@ +// Copyright (c) 2009 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 "stdafx.h" +#include "scheme_test.h" +#include "string_util.h" +#include "resource_util.h" +#include "resource.h" + + +// Implementation of the schema handler for client:// requests. +class ClientSchemeHandler : public CefThreadSafeBase +{ +public: + ClientSchemeHandler() : size_(0), offset_(0), bytes_(NULL) {} + + // Process the request. All response generation should take place in this + // method. If there is no response set |response_length| to zero and + // ReadResponse() will not be called. If the response length is not known then + // set |response_length| to -1 and ReadResponse() will be called until it + // returns false or until the value of |bytes_read| is set to 0. Otherwise, + // set |response_length| to a positive value and ReadResponse() will be called + // until it returns false, the value of |bytes_read| is set to 0 or the + // specified number of bytes have been read. If there is a response set + // |mime_type| to the mime type for the response. + virtual bool ProcessRequest(CefRefPtr request, + std::wstring& mime_type, int* response_length) + { + bool handled = false; + + Lock(); + std::wstring url = request->GetURL(); + if(wcsstr(url.c_str(), L"handler.html") != NULL) { + // Build the response html + html_ = "Client Scheme Handler" + "This contents of this page page are served by the " + "ClientSchemeHandler class handling the client:// protocol." + "
You should see an image:" + "
";
+      
+      // Output a string representation of the request
+      std::wstring dump;
+      DumpRequestContents(request, dump);
+      html_.append(WStringToString(dump));
+
+      html_.append("

Try the test form:" + "
" + "" + "" + "" + "
"); + + handled = true; + size_ = html_.size(); + bytes_ = (LPBYTE)html_.c_str(); + + // Set the resulting mime type + mime_type = L"text/html"; + } + else if(wcsstr(url.c_str(), L"client.gif") != NULL) { + // Load the response image + if(LoadBinaryResource(IDS_LOGO, size_, bytes_)) { + handled = true; + // Set the resulting mime type + mime_type = L"image/jpg"; + } + } + + // Set the resulting response length + *response_length = size_; + Unlock(); + + return handled; + } + + // Cancel processing of the request. + virtual void Cancel() + { + } + + // Copy up to |bytes_to_read| bytes into |data_out|. If the copy succeeds + // set |bytes_read| to the number of bytes copied and return true. If the + // copy fails return false and ReadResponse() will not be called again. + virtual bool ReadResponse(void* data_out, int bytes_to_read, + int* bytes_read) + { + bool has_data = false; + *bytes_read = 0; + + Lock(); + + if(offset_ < size_) { + // Copy the next block of data into the buffer. + int transfer_size = min(bytes_to_read, static_cast(size_ - offset_)); + memcpy(data_out, bytes_ + offset_, transfer_size); + offset_ += transfer_size; + + *bytes_read = transfer_size; + has_data = true; + } + + Unlock(); + + return has_data; + } + +private: + DWORD size_, offset_; + LPBYTE bytes_; + std::string html_; +}; + +// Implementation of the factory for for creating schema handlers. +class ClientSchemeHandlerFactory : + public CefThreadSafeBase +{ +public: + // Return a new scheme handler instance to handle the request. + virtual CefRefPtr Create() + { + return new ClientSchemeHandler(); + } +}; + +void InitSchemeTest() +{ + CefRegisterScheme(L"client", L"tests", new ClientSchemeHandlerFactory()); +} + +void RunSchemeTest(CefRefPtr browser) +{ + browser->GetMainFrame()->LoadURL(L"client://tests/handler.html"); +} diff --git a/tests/cefclient/scheme_test.h b/tests/cefclient/scheme_test.h new file mode 100644 index 000000000..1fe9daa97 --- /dev/null +++ b/tests/cefclient/scheme_test.h @@ -0,0 +1,12 @@ +// Copyright (c) 2009 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. + +#pragma once +#include "cef.h" + +// Register the scheme handler. +void InitSchemeTest(); + +// Run the test. +void RunSchemeTest(CefRefPtr browser); diff --git a/tests/cefclient/string_util.cpp b/tests/cefclient/string_util.cpp new file mode 100644 index 000000000..b7bbb67be --- /dev/null +++ b/tests/cefclient/string_util.cpp @@ -0,0 +1,84 @@ +// Copyright (c) 2009 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 "stdafx.h" +#include "string_util.h" +#include + + +std::wstring StringToWString(const std::string& s) +{ + wchar_t* wch; + UINT bytes = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size()+1, NULL, 0); + wch = new wchar_t[bytes]; + if(wch) + bytes = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size()+1, wch, bytes); + std::wstring str = wch; + delete [] wch; + return str; +} + +std::string WStringToString(const std::wstring& s) +{ + char* ch; + UINT bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), s.size()+1, NULL, 0, + NULL, NULL); + ch = new char[bytes]; + if(ch) + bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), s.size()+1, ch, bytes, + NULL, NULL); + std::string str = ch; + delete [] ch; + return str; +} + +void DumpRequestContents(CefRefPtr request, std::wstring& str) +{ + std::wstringstream ss; + + ss << L"URL: " << request->GetURL(); + ss << L"\nMethod: " << request->GetMethod(); + + CefRequest::HeaderMap headerMap; + request->GetHeaderMap(headerMap); + if(headerMap.size() > 0) { + ss << L"\nHeaders:"; + CefRequest::HeaderMap::const_iterator it = headerMap.begin(); + for(; it != headerMap.end(); ++it) { + ss << L"\n\t" << (*it).first << L": " << (*it).second; + } + } + + CefRefPtr postData = request->GetPostData(); + if(postData.get()) { + CefPostData::ElementVector elements; + postData->GetElements(elements); + if(elements.size() > 0) { + ss << L"\nPost Data:"; + CefRefPtr element; + CefPostData::ElementVector::const_iterator it = elements.begin(); + for(; it != elements.end(); ++it) { + element = (*it); + if(element->GetType() == PDE_TYPE_BYTES) { + // the element is composed of bytes + ss << L"\n\tBytes: "; + if(element->GetBytesCount() == 0) + ss << L"(empty)"; + else { + // retrieve the data. + size_t size = element->GetBytesCount(); + char* bytes = new char[size]; + element->GetBytes(size, bytes); + ss << StringToWString(std::string(bytes, size)); + delete [] bytes; + } + } else if(element->GetType() == PDE_TYPE_FILE) { + ss << L"\n\tFile: " << element->GetFile(); + } + } + } + } + + str = ss.str(); +} diff --git a/tests/cefclient/string_util.h b/tests/cefclient/string_util.h new file mode 100644 index 000000000..ca349234c --- /dev/null +++ b/tests/cefclient/string_util.h @@ -0,0 +1,18 @@ +// Copyright (c) 2009 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. + +#pragma once + +#include "cef.h" +#include + + +// Convert a std::string to a std::wstring +std::wstring StringToWString(const std::string& s); + +// Convert a std::wstring to a std::string +std::string WStringToString(const std::wstring& s); + +// Dump the contents of the request into a string. +void DumpRequestContents(CefRefPtr request, std::wstring& str);