Commit 1cc133a4 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add CallPDFiumWideStringBufferApiAndReturnOptional().

Add a variant of CallPDFiumWideStringBufferApi() that distinguishes
between API call failure and empty string return values.

Change-Id: I4ec2c1ed16f9109854493f184acd597c8b622196
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2322167Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792454}
parent 8be85143
......@@ -12,6 +12,7 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/numerics/safe_math.h"
#include "base/optional.h"
#include "base/strings/string16.h"
namespace chrome_pdf {
......@@ -103,19 +104,33 @@ template <class AdapterType,
class StringType,
typename BufferType,
typename ReturnType>
StringType CallPDFiumStringBufferApi(
base::Optional<StringType> CallPDFiumStringBufferApiAndReturnOptional(
base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
bool check_expected_size) {
StringType str;
ReturnType expected_size = api.Run(nullptr, 0);
if (expected_size > 0) {
if (expected_size == 0)
return base::nullopt;
StringType str;
AdapterType api_string_adapter(&str, expected_size, check_expected_size);
auto* data = reinterpret_cast<BufferType*>(api_string_adapter.GetData());
api_string_adapter.Close(api.Run(data, expected_size));
}
return str;
}
template <class AdapterType,
class StringType,
typename BufferType,
typename ReturnType>
StringType CallPDFiumStringBufferApi(
base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
bool check_expected_size) {
base::Optional<StringType> result =
CallPDFiumStringBufferApiAndReturnOptional<AdapterType, StringType>(
api, check_expected_size);
return result.value_or(StringType());
}
} // namespace internal
// Helper function to call PDFium APIs where the output buffer is expected to
......@@ -129,6 +144,18 @@ base::string16 CallPDFiumWideStringBufferApi(
api, check_expected_size);
}
// Variant of CallPDFiumWideStringBufferApi() that distinguishes between API
// call failures and empty string return values.
template <typename BufferType>
base::Optional<base::string16> CallPDFiumWideStringBufferApiAndReturnOptional(
base::RepeatingCallback<unsigned long(BufferType*, unsigned long)> api,
bool check_expected_size) {
using adapter_type = internal::PDFiumAPIStringBufferSizeInBytesAdapter;
return internal::CallPDFiumStringBufferApiAndReturnOptional<adapter_type,
base::string16>(
api, check_expected_size);
}
// Helper function to call PDFium APIs where the output buffer is expected to
// hold ASCII or UTF-8 data, and the buffer length is specified in bytes.
template <typename BufferType, typename ReturnType>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment