// Copyright © 2013 The CefSharp 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 #include #include "vcclr_local.h" #include "include\cef_v8.h" using namespace System; using namespace System::Collections::Generic; using namespace System::Diagnostics; namespace CefSharp { namespace Internals { private class StringUtils { public: /// /// Converts an unmanaged string to a (managed) .NET string. /// /// The string that should be converted. /// A .NET string. [DebuggerStepThrough] static String^ StringUtils::ToClr(const cef_string_t& cefStr) { return gcnew String(cefStr.str); } /// /// Converts an unmanaged string to a (managed) .NET string. /// /// The string that should be converted. /// A .NET string. [DebuggerStepThrough] static String^ StringUtils::ToClr(const CefString& cefStr) { return gcnew String(cefStr.c_str()); } /// /// Converts an unmanaged vector of strings to a (managed) .NET List of strings. /// /// The vector of strings that should be converted. /// A .NET List of strings. [DebuggerStepThrough] static List^ ToClr(const std::vector& cefStr) { auto result = gcnew List(); for each(CefString s in cefStr) { result->Add(StringUtils::ToClr(s)); } return result; } /// /// Converts a .NET string to native (unmanaged) format. Note that this method does not allocate a new copy of the // string, but rather returns a pointer to the memory in the existing managed String object. /// /// The string that should be converted. /// An unmanaged representation of the provided string, or an empty string if the input string is a nullptr. [DebuggerStepThrough] static CefString ToNative(String^ str) { if (str == nullptr) { return CefString(); } pin_ptr pStr = PtrToStringChars(str); CefString cefStr(pStr); return cefStr; } /// /// Converts a .NET List of strings to native (unmanaged) format. /// /// The List of strings that should be converted. /// An unmanaged representation of the provided List of strings, or an empty List if the input is a nullptr. [DebuggerStepThrough] static std::vector ToNative(IEnumerable^ str) { if (str == nullptr) { return std::vector(); } std::vector result = std::vector(); for each (String^ s in str) { result.push_back(StringUtils::ToNative(s)); } return result; } /// /// Assigns the provided cef_string_t object from the given .NET string. /// /// The cef_string_t that should be updated. /// The .NET string whose value should be used to update cefStr. [DebuggerStepThrough] static void StringUtils::AssignNativeFromClr(cef_string_t& cefStr, String^ str) { cef_string_clear(&cefStr); if (str != nullptr) { pin_ptr pStr = PtrToStringChars(str); cef_string_copy(pStr, str->Length, &cefStr); } } /// /// Creates a detailed expection string from a provided Cef V8 exception. /// /// The exception which will be used as base for the message [DebuggerStepThrough] static CefString CreateExceptionString(CefRefPtr exception) { if (exception.get()) { std::wstringstream logMessageBuilder; logMessageBuilder << exception->GetMessage().c_str() << L"\n@ "; if (!exception->GetScriptResourceName().empty()) { logMessageBuilder << exception->GetScriptResourceName().c_str(); } logMessageBuilder << L":" << exception->GetLineNumber() << L":" << exception->GetStartColumn(); return CefString(logMessageBuilder.str()); } return "Exception occured but the Cef V8 exception is null"; } }; } }