cef/libcef/cef_string.c
Marshall Greenblatt c295931b1e - Move frame-related methods from CefBrowser into a new CefFrame class.
- Add CefBrowser::Get*Frame() methods for retrieving the appropriate CefFrame instance.
- Add a CefFrame attribute to CefHandler callback methods where appropriate.
- Add support for V8 JavaScript extensions and values via CefV8Value and CefV8Handler.  Native C++ and user-defined JavaScript object hierarchies may now be created and accessed using the CEF API.
- Remove the CefHandler and CefVariant classes and related CefBrowser methods that have been obsoleted by the addition of CEF V8 support.
- Add the CefRegisterExtension() function for registering system-wide V8 extensions.
- Add the CefHandler::HandleJSBinding() callback method for attaching V8 values to the global frame JavaScript object.  This method replaces the previous technique of calling CefBrowser::AddJSHandler().
- Add new wrapper template methods for simplifying DLL wrapper implementations.
- Move cef_string* files from libcef_dll to libcef so that projects can link libcef statically without errors.
- Fix crashes when CEF exits due to object constructors being executed on non-UI threads if the application is closed while a page is still loading.
- Update the cefclient project to reflect changes and demonstrate the new APIs.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@26 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
2009-05-28 00:31:21 +00:00

164 lines
3.9 KiB
C

// 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 "include/cef_string.h"
#include <limits.h>
#include <malloc.h>
#include <string.h>
typedef unsigned long dword_t;
CEF_EXPORT size_t cef_string_length(cef_string_t str)
{
dword_t* ptr;
if(!str)
return 0;
// The string length, in bytes, is placed in a dword_t immediately proceeding
// the string value.
ptr = (dword_t*)str;
ptr--;
return (size_t)(*ptr / sizeof(wchar_t));
}
CEF_EXPORT cef_string_t cef_string_alloc(const wchar_t* str)
{
if(!str)
return NULL;
return cef_string_alloc_length(str, wcslen(str));
}
CEF_EXPORT cef_string_t cef_string_alloc_length(const wchar_t* str,
size_t len)
{
dword_t size, *ptr;
wchar_t* newstr;
// Check that the size can fit in a dword_t.
if(len >= (UINT_MAX - sizeof(wchar_t) - sizeof(dword_t)) / sizeof(wchar_t))
return NULL;
// Get the size of the string in bytes.
size = sizeof(wchar_t) * len;
// Allocate the new buffer including space for the proceeding dword_t size
// value and the terminating nul.
ptr = (dword_t*)malloc(sizeof(dword_t) + size + sizeof(wchar_t));
if(!ptr)
return NULL;
// Set the size as the first value in the newly allocated memory and
// increment to the string location.
*ptr = size;
ptr++;
if(str != NULL)
{
// Copy the string to the buffer.
memcpy(ptr, str, size);
}
else
{
// Initialize the string to zeros.
memset(ptr, 0, size);
}
newstr = (wchar_t*)ptr;
// Nul-terminate the string.
newstr[len] = '\0';
return (cef_string_t)newstr;
}
CEF_EXPORT int cef_string_realloc(cef_string_t* oldstr, const wchar_t* newstr)
{
if(!oldstr)
return 0;
// Free the old string.
cef_string_free(*oldstr);
// Copy the new string.
*oldstr = cef_string_alloc(newstr);
return 1;
}
CEF_EXPORT int cef_string_realloc_length(cef_string_t* oldstr,
const wchar_t* newstr,
size_t len)
{
if(!oldstr)
return 0;
// Check that the size can fit in a dword_t.
if(len >= (UINT_MAX - sizeof(wchar_t) - sizeof(dword_t)) / sizeof(wchar_t))
return 0;
if(*oldstr)
{
dword_t newsize, *oldptr, *newptr;
// Get the new size of the string in bytes.
newsize = sizeof(wchar_t) * len;
// Adjust the pointer to account for the dword_t immediately proceeding the
// string value.
oldptr = (dword_t*)*oldstr;
oldptr--;
// Re-allocate the buffer including space for the proceeding dword_t size
// value and the terminating nul.
newptr = (dword_t*)realloc(
oldptr, sizeof(dword_t) + newsize + sizeof(wchar_t));
if(!newptr)
return 0;
// Set the size as the first value in the newly allocated memory and
// increment to the string location.
*newptr = newsize;
newptr++;
// Set the string pointer to the beginning on the string in the newly
// allocated memory.
*oldstr = (cef_string_t)newptr;
if(newstr)
{
// Copy the new string value. Use of memmove() ensures that any
// overlapping region in the old string will be copied before being
// overwritten.
memmove(*oldstr, newstr, newsize);
// Nul-terminate the string.
*oldstr[len] = '\0';
}
}
else
{
// Allocate the string.
*oldstr = cef_string_alloc_length(newstr, len);
}
return 1;
}
CEF_EXPORT void cef_string_free(cef_string_t str)
{
dword_t* ptr;
if(!str)
return;
// The size is placed in a dword_t immediately proceeding the string value.
ptr = (dword_t*)str;
ptr--;
free(ptr);
}