Commit afad3080 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

PageState: replace base::NullableString16 with Optional<base::string16>.

The long-term plan is to remove base::NullableString16 and just use
base::Optional<base::string16> directly.

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_site_isolation
Change-Id: I43b994de70262326b0ca148d888b4424f9d4a795
Reviewed-on: https://chromium-review.googlesource.com/699860
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarPatrick Noland <pnoland@google.com>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarCharlie Reis <creis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506970}
parent 051a012c
...@@ -109,7 +109,8 @@ scoped_refptr<ResourceRequestBody> FrameNavigationEntry::GetPostData( ...@@ -109,7 +109,8 @@ scoped_refptr<ResourceRequestBody> FrameNavigationEntry::GetPostData(
return nullptr; return nullptr;
*content_type = base::UTF16ToASCII( *content_type = base::UTF16ToASCII(
exploded_state.top.http_body.http_content_type.string()); exploded_state.top.http_body.http_content_type.value_or(
base::string16()));
return exploded_state.top.http_body.request_body; return exploded_state.top.http_body.request_body;
} }
......
...@@ -5524,9 +5524,11 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest, ...@@ -5524,9 +5524,11 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest,
ExplodedPageState exploded_state; ExplodedPageState exploded_state;
EXPECT_TRUE( EXPECT_TRUE(
DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state)); DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state));
EXPECT_EQ(url_a, GURL(exploded_state.top.url_string.string())); EXPECT_EQ(url_a,
GURL(exploded_state.top.url_string.value_or(base::string16())));
EXPECT_EQ(frame_url_a2, EXPECT_EQ(frame_url_a2,
GURL(exploded_state.top.children.at(0).url_string.string())); GURL(exploded_state.top.children.at(0).url_string.value_or(
base::string16())));
} }
// Start a provisional navigation, but abort it by going back before it commits. // Start a provisional navigation, but abort it by going back before it commits.
...@@ -5623,7 +5625,8 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest, ...@@ -5623,7 +5625,8 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest,
ExplodedPageState exploded_state; ExplodedPageState exploded_state;
EXPECT_TRUE( EXPECT_TRUE(
DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state)); DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state));
EXPECT_EQ(url_b, GURL(exploded_state.top.url_string.string())); EXPECT_EQ(url_b,
GURL(exploded_state.top.url_string.value_or(base::string16())));
EXPECT_EQ(0U, exploded_state.top.children.size()); EXPECT_EQ(0U, exploded_state.top.children.size());
// Go back and then forward to see if the PageState loads correctly. // Go back and then forward to see if the PageState loads correctly.
...@@ -5712,7 +5715,8 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest, ...@@ -5712,7 +5715,8 @@ IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest,
ExplodedPageState exploded_state; ExplodedPageState exploded_state;
EXPECT_TRUE( EXPECT_TRUE(
DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state)); DecodePageState(entry->GetPageState().ToEncodedData(), &exploded_state));
EXPECT_EQ(url_b, GURL(exploded_state.top.url_string.string())); EXPECT_EQ(url_b,
GURL(exploded_state.top.url_string.value_or(base::string16())));
// Go back and then forward to see if the PageState loads correctly. // Go back and then forward to see if the PageState loads correctly.
controller.GoBack(); controller.GoBack();
......
...@@ -41,14 +41,15 @@ int GetUniqueIDInConstructor() { ...@@ -41,14 +41,15 @@ int GetUniqueIDInConstructor() {
void RecursivelyGenerateFrameEntries( void RecursivelyGenerateFrameEntries(
const ExplodedFrameState& state, const ExplodedFrameState& state,
const std::vector<base::NullableString16>& referenced_files, const std::vector<base::Optional<base::string16>>& referenced_files,
NavigationEntryImpl::TreeNode* node) { NavigationEntryImpl::TreeNode* node) {
node->frame_entry = new FrameNavigationEntry( node->frame_entry = new FrameNavigationEntry(
UTF16ToUTF8(state.target.string()), state.item_sequence_number, UTF16ToUTF8(state.target.value_or(base::string16())),
state.document_sequence_number, nullptr, nullptr, state.item_sequence_number, state.document_sequence_number, nullptr,
GURL(state.url_string.string()), nullptr, GURL(state.url_string.value_or(base::string16())),
Referrer(GURL(state.referrer.string()), state.referrer_policy), "GET", Referrer(GURL(state.referrer.value_or(base::string16())),
-1); state.referrer_policy),
"GET", -1);
// Set a single-frame PageState on the entry. // Set a single-frame PageState on the entry.
ExplodedPageState page_state; ExplodedPageState page_state;
...@@ -70,7 +71,7 @@ void RecursivelyGenerateFrameEntries( ...@@ -70,7 +71,7 @@ void RecursivelyGenerateFrameEntries(
// Don't pass the file list to subframes, since that would result in multiple // Don't pass the file list to subframes, since that would result in multiple
// copies of it ending up in the combined list in GetPageState (via // copies of it ending up in the combined list in GetPageState (via
// RecursivelyGenerateFrameState). // RecursivelyGenerateFrameState).
std::vector<base::NullableString16> empty_file_list; std::vector<base::Optional<base::string16>> empty_file_list;
for (const ExplodedFrameState& child_state : state.children) { for (const ExplodedFrameState& child_state : state.children) {
node->children.push_back( node->children.push_back(
...@@ -83,7 +84,7 @@ void RecursivelyGenerateFrameEntries( ...@@ -83,7 +84,7 @@ void RecursivelyGenerateFrameEntries(
void RecursivelyGenerateFrameState( void RecursivelyGenerateFrameState(
NavigationEntryImpl::TreeNode* node, NavigationEntryImpl::TreeNode* node,
ExplodedFrameState* state, ExplodedFrameState* state,
std::vector<base::NullableString16>* referenced_files) { std::vector<base::Optional<base::string16>>* referenced_files) {
// The FrameNavigationEntry's PageState contains just the ExplodedFrameState // The FrameNavigationEntry's PageState contains just the ExplodedFrameState
// for that particular frame. // for that particular frame.
ExplodedPageState exploded_page_state; ExplodedPageState exploded_page_state;
...@@ -101,7 +102,7 @@ void RecursivelyGenerateFrameState( ...@@ -101,7 +102,7 @@ void RecursivelyGenerateFrameState(
referenced_files->reserve(referenced_files->size() + referenced_files->reserve(referenced_files->size() +
exploded_page_state.referenced_files.size()); exploded_page_state.referenced_files.size());
for (auto& file : exploded_page_state.referenced_files) for (auto& file : exploded_page_state.referenced_files)
referenced_files->push_back(file); referenced_files->emplace_back(file);
state->children.resize(node->children.size()); state->children.resize(node->children.size());
for (size_t i = 0; i < node->children.size(); ++i) { for (size_t i = 0; i < node->children.size(); ++i) {
...@@ -877,7 +878,8 @@ std::map<std::string, bool> NavigationEntryImpl::GetSubframeUniqueNames( ...@@ -877,7 +878,8 @@ std::map<std::string, bool> NavigationEntryImpl::GetSubframeUniqueNames(
if (DecodePageState(child->frame_entry->page_state().ToEncodedData(), if (DecodePageState(child->frame_entry->page_state().ToEncodedData(),
&exploded_page_state)) { &exploded_page_state)) {
ExplodedFrameState frame_state = exploded_page_state.top; ExplodedFrameState frame_state = exploded_page_state.top;
if (UTF16ToUTF8(frame_state.url_string.string()) == url::kAboutBlankURL) if (UTF16ToUTF8(frame_state.url_string.value_or(base::string16())) ==
url::kAboutBlankURL)
is_about_blank = true; is_about_blank = true;
} }
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include <limits> #include <limits>
#include "base/pickle.h" #include "base/pickle.h"
#include "base/strings/nullable_string16.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
...@@ -38,12 +37,13 @@ void AppendDataToRequestBody( ...@@ -38,12 +37,13 @@ void AppendDataToRequestBody(
void AppendFileRangeToRequestBody( void AppendFileRangeToRequestBody(
const scoped_refptr<ResourceRequestBody>& request_body, const scoped_refptr<ResourceRequestBody>& request_body,
const base::NullableString16& file_path, const base::Optional<base::string16>& file_path,
int file_start, int file_start,
int file_length, int file_length,
double file_modification_time) { double file_modification_time) {
request_body->AppendFileRange( request_body->AppendFileRange(
base::FilePath::FromUTF16Unsafe(file_path.string()), file_path ? base::FilePath::FromUTF16Unsafe(*file_path)
: base::FilePath(),
static_cast<uint64_t>(file_start), static_cast<uint64_t>(file_length), static_cast<uint64_t>(file_start), static_cast<uint64_t>(file_length),
base::Time::FromDoubleT(file_modification_time)); base::Time::FromDoubleT(file_modification_time));
} }
...@@ -70,17 +70,16 @@ void AppendBlobToRequestBody( ...@@ -70,17 +70,16 @@ void AppendBlobToRequestBody(
void AppendReferencedFilesFromHttpBody( void AppendReferencedFilesFromHttpBody(
const std::vector<ResourceRequestBody::Element>& elements, const std::vector<ResourceRequestBody::Element>& elements,
std::vector<base::NullableString16>* referenced_files) { std::vector<base::Optional<base::string16>>* referenced_files) {
for (size_t i = 0; i < elements.size(); ++i) { for (size_t i = 0; i < elements.size(); ++i) {
if (elements[i].type() == ResourceRequestBody::Element::TYPE_FILE) if (elements[i].type() == ResourceRequestBody::Element::TYPE_FILE)
referenced_files->push_back( referenced_files->emplace_back(elements[i].path().AsUTF16Unsafe());
base::NullableString16(elements[i].path().AsUTF16Unsafe(), false));
} }
} }
bool AppendReferencedFilesFromDocumentState( bool AppendReferencedFilesFromDocumentState(
const std::vector<base::NullableString16>& document_state, const std::vector<base::Optional<base::string16>>& document_state,
std::vector<base::NullableString16>* referenced_files) { std::vector<base::Optional<base::string16>>* referenced_files) {
if (document_state.empty()) if (document_state.empty())
return true; return true;
...@@ -100,7 +99,8 @@ bool AppendReferencedFilesFromDocumentState( ...@@ -100,7 +99,8 @@ bool AppendReferencedFilesFromDocumentState(
index++; // Skip over form key. index++; // Skip over form key.
size_t item_count; size_t item_count;
if (!base::StringToSizeT(document_state[index++].string(), &item_count)) if (!document_state[index] ||
!base::StringToSizeT(*document_state[index++], &item_count))
return false; return false;
while (item_count--) { while (item_count--) {
...@@ -108,24 +108,25 @@ bool AppendReferencedFilesFromDocumentState( ...@@ -108,24 +108,25 @@ bool AppendReferencedFilesFromDocumentState(
return false; return false;
index++; // Skip over name. index++; // Skip over name.
const base::NullableString16& type = document_state[index++]; const base::Optional<base::string16>& type = document_state[index++];
if (index >= document_state.size()) if (index >= document_state.size())
return false; return false;
size_t value_size; size_t value_size;
if (!base::StringToSizeT(document_state[index++].string(), &value_size)) if (!document_state[index] ||
!base::StringToSizeT(*document_state[index++], &value_size))
return false; return false;
if (index + value_size > document_state.size() || if (index + value_size > document_state.size() ||
index + value_size < index) // Check for overflow. index + value_size < index) // Check for overflow.
return false; return false;
if (base::EqualsASCII(type.string(), "file")) { if (type && base::EqualsASCII(*type, "file")) {
if (value_size != 2) if (value_size != 2)
return false; return false;
referenced_files->push_back(document_state[index++]); referenced_files->emplace_back(document_state[index++]);
index++; // Skip over display name. index++; // Skip over display name.
} else { } else {
index += value_size; index += value_size;
...@@ -137,7 +138,7 @@ bool AppendReferencedFilesFromDocumentState( ...@@ -137,7 +138,7 @@ bool AppendReferencedFilesFromDocumentState(
bool RecursivelyAppendReferencedFiles( bool RecursivelyAppendReferencedFiles(
const ExplodedFrameState& frame_state, const ExplodedFrameState& frame_state,
std::vector<base::NullableString16>* referenced_files) { std::vector<base::Optional<base::string16>>* referenced_files) {
if (frame_state.http_body.request_body != nullptr) { if (frame_state.http_body.request_body != nullptr) {
AppendReferencedFilesFromHttpBody( AppendReferencedFilesFromHttpBody(
*frame_state.http_body.request_body->elements(), referenced_files); *frame_state.http_body.request_body->elements(), referenced_files);
...@@ -306,25 +307,30 @@ std::string ReadStdString(SerializeObject* obj) { ...@@ -306,25 +307,30 @@ std::string ReadStdString(SerializeObject* obj) {
return std::string(); return std::string();
} }
// WriteString pickles the NullableString16 as <int length><char16* data>. // Pickles a base::string16 as <int length>:<char*16 data> tuple>.
// If length == -1, then the NullableString16 itself is null. Otherwise the void WriteString(const base::string16& str, SerializeObject* obj) {
// length is the number of char16 (not bytes) in the NullableString16. const base::char16* data = str.data();
void WriteString(const base::NullableString16& str, SerializeObject* obj) { size_t length_in_bytes = str.length() * sizeof(base::char16);
if (str.is_null()) {
CHECK_LT(length_in_bytes,
static_cast<size_t>(std::numeric_limits<int>::max()));
obj->pickle.WriteInt(length_in_bytes);
obj->pickle.WriteBytes(data, length_in_bytes);
}
// If str is a null optional, this simply pickles a length of -1. Otherwise,
// delegates to the base::string16 overload.
void WriteString(const base::Optional<base::string16>& str,
SerializeObject* obj) {
if (!str) {
obj->pickle.WriteInt(-1); obj->pickle.WriteInt(-1);
} else { } else {
const base::char16* data = str.string().data(); WriteString(*str, obj);
size_t length_in_bytes = str.string().length() * sizeof(base::char16);
CHECK_LT(length_in_bytes,
static_cast<size_t>(std::numeric_limits<int>::max()));
obj->pickle.WriteInt(length_in_bytes);
obj->pickle.WriteBytes(data, length_in_bytes);
} }
} }
// This reads a serialized NullableString16 from obj. If a string can't be // This reads a serialized base::Optional<base::string16> from obj. If a string
// read, NULL is returned. // can't be read, NULL is returned.
const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) {
int length_in_bytes; int length_in_bytes;
if (!obj->iter.ReadInt(&length_in_bytes)) { if (!obj->iter.ReadInt(&length_in_bytes)) {
...@@ -346,12 +352,13 @@ const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { ...@@ -346,12 +352,13 @@ const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) {
return reinterpret_cast<const base::char16*>(data); return reinterpret_cast<const base::char16*>(data);
} }
base::NullableString16 ReadString(SerializeObject* obj) { base::Optional<base::string16> ReadString(SerializeObject* obj) {
int num_chars; int num_chars;
const base::char16* chars = ReadStringNoCopy(obj, &num_chars); const base::char16* chars = ReadStringNoCopy(obj, &num_chars);
return chars ? base::Optional<base::string16> result;
base::NullableString16(base::string16(chars, num_chars), false) : if (chars)
base::NullableString16(); result.emplace(chars, num_chars);
return result;
} }
template <typename T> template <typename T>
...@@ -380,8 +387,8 @@ size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) { ...@@ -380,8 +387,8 @@ size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) {
} }
// Writes a Vector of strings into a SerializeObject for serialization. // Writes a Vector of strings into a SerializeObject for serialization.
void WriteStringVector( void WriteStringVector(const std::vector<base::Optional<base::string16>>& data,
const std::vector<base::NullableString16>& data, SerializeObject* obj) { SerializeObject* obj) {
WriteAndValidateVectorSize(data, obj); WriteAndValidateVectorSize(data, obj);
for (size_t i = 0; i < data.size(); ++i) { for (size_t i = 0; i < data.size(); ++i) {
WriteString(data[i], obj); WriteString(data[i], obj);
...@@ -389,9 +396,9 @@ void WriteStringVector( ...@@ -389,9 +396,9 @@ void WriteStringVector(
} }
void ReadStringVector(SerializeObject* obj, void ReadStringVector(SerializeObject* obj,
std::vector<base::NullableString16>* result) { std::vector<base::Optional<base::string16>>* result) {
size_t num_elements = size_t num_elements =
ReadAndValidateVectorSize(obj, sizeof(base::NullableString16)); ReadAndValidateVectorSize(obj, sizeof(base::Optional<base::string16>));
result->resize(num_elements); result->resize(num_elements);
for (size_t i = 0; i < num_elements; ++i) for (size_t i = 0; i < num_elements; ++i)
...@@ -409,8 +416,7 @@ void WriteResourceRequestBody(const ResourceRequestBody& request_body, ...@@ -409,8 +416,7 @@ void WriteResourceRequestBody(const ResourceRequestBody& request_body,
break; break;
case ResourceRequestBody::Element::TYPE_FILE: case ResourceRequestBody::Element::TYPE_FILE:
WriteInteger(blink::WebHTTPBody::Element::kTypeFile, obj); WriteInteger(blink::WebHTTPBody::Element::kTypeFile, obj);
WriteString( WriteString(element.path().AsUTF16Unsafe(), obj);
base::NullableString16(element.path().AsUTF16Unsafe(), false), obj);
WriteInteger64(static_cast<int64_t>(element.offset()), obj); WriteInteger64(static_cast<int64_t>(element.offset()), obj);
WriteInteger64(static_cast<int64_t>(element.length()), obj); WriteInteger64(static_cast<int64_t>(element.length()), obj);
WriteReal(element.expected_modification_time().ToDoubleT(), obj); WriteReal(element.expected_modification_time().ToDoubleT(), obj);
...@@ -451,7 +457,7 @@ void ReadResourceRequestBody( ...@@ -451,7 +457,7 @@ void ReadResourceRequestBody(
length); length);
} }
} else if (type == blink::WebHTTPBody::Element::kTypeFile) { } else if (type == blink::WebHTTPBody::Element::kTypeFile) {
base::NullableString16 file_path = ReadString(obj); base::Optional<base::string16> file_path = ReadString(obj);
int64_t file_start = ReadInteger64(obj); int64_t file_start = ReadInteger64(obj);
int64_t file_length = ReadInteger64(obj); int64_t file_length = ReadInteger64(obj);
double file_modification_time = ReadReal(obj); double file_modification_time = ReadReal(obj);
...@@ -536,10 +542,10 @@ void WriteFrameState( ...@@ -536,10 +542,10 @@ void WriteFrameState(
WriteInteger(state.scroll_restoration_type, obj); WriteInteger(state.scroll_restoration_type, obj);
bool has_state_object = !state.state_object.is_null(); bool has_state_object = state.state_object.has_value();
WriteBoolean(has_state_object, obj); WriteBoolean(has_state_object, obj);
if (has_state_object) if (has_state_object)
WriteString(state.state_object, obj); WriteString(*state.state_object, obj);
WriteHttpBody(state.http_body, obj); WriteHttpBody(state.http_body, obj);
...@@ -569,12 +575,9 @@ void ReadFrameState( ...@@ -569,12 +575,9 @@ void ReadFrameState(
ReadString(obj); // Skip obsolete original url string field. ReadString(obj); // Skip obsolete original url string field.
state->target = ReadString(obj); state->target = ReadString(obj);
if (obj->version < 25 && !state->target.is_null()) { if (obj->version < 25 && state->target) {
state->target = base::NullableString16( state->target = base::UTF8ToUTF16(UniqueNameHelper::UpdateLegacyNameFromV24(
base::UTF8ToUTF16(UniqueNameHelper::UpdateLegacyNameFromV24( base::UTF16ToUTF8(*state->target), unique_name_replacements));
base::UTF16ToUTF8(state->target.string()),
unique_name_replacements)),
false);
} }
if (obj->version < 15) { if (obj->version < 15) {
ReadString(obj); // Skip obsolete parent field. ReadString(obj); // Skip obsolete parent field.
...@@ -690,9 +693,7 @@ void ReadPageState(SerializeObject* obj, ExplodedPageState* state) { ...@@ -690,9 +693,7 @@ void ReadPageState(SerializeObject* obj, ExplodedPageState* state) {
if (obj->version == -1) { if (obj->version == -1) {
GURL url = ReadGURL(obj); GURL url = ReadGURL(obj);
// NOTE: GURL::possibly_invalid_spec() always returns valid UTF-8. // NOTE: GURL::possibly_invalid_spec() always returns valid UTF-8.
state->top.url_string = state->top.url_string = base::UTF8ToUTF16(url.possibly_invalid_spec());
base::NullableString16(
base::UTF8ToUTF16(url.possibly_invalid_spec()), false);
return; return;
} }
......
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/strings/nullable_string16.h" #include "base/optional.h"
#include "base/strings/string16.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/public/common/resource_request_body.h" #include "content/public/common/resource_request_body.h"
...@@ -24,7 +25,7 @@ ...@@ -24,7 +25,7 @@
namespace content { namespace content {
struct CONTENT_EXPORT ExplodedHttpBody { struct CONTENT_EXPORT ExplodedHttpBody {
base::NullableString16 http_content_type; base::Optional<base::string16> http_content_type;
scoped_refptr<ResourceRequestBody> request_body; scoped_refptr<ResourceRequestBody> request_body;
bool contains_passwords; bool contains_passwords;
...@@ -33,11 +34,11 @@ struct CONTENT_EXPORT ExplodedHttpBody { ...@@ -33,11 +34,11 @@ struct CONTENT_EXPORT ExplodedHttpBody {
}; };
struct CONTENT_EXPORT ExplodedFrameState { struct CONTENT_EXPORT ExplodedFrameState {
base::NullableString16 url_string; base::Optional<base::string16> url_string;
base::NullableString16 referrer; base::Optional<base::string16> referrer;
base::NullableString16 target; base::Optional<base::string16> target;
base::NullableString16 state_object; base::Optional<base::string16> state_object;
std::vector<base::NullableString16> document_state; std::vector<base::Optional<base::string16>> document_state;
blink::WebHistoryScrollRestorationType scroll_restoration_type; blink::WebHistoryScrollRestorationType scroll_restoration_type;
bool did_save_scroll_or_scale_state; bool did_save_scroll_or_scale_state;
gfx::PointF visual_viewport_scroll_offset; gfx::PointF visual_viewport_scroll_offset;
...@@ -63,7 +64,7 @@ struct CONTENT_EXPORT ExplodedPageState { ...@@ -63,7 +64,7 @@ struct CONTENT_EXPORT ExplodedPageState {
// extract referenced files from ExplodedHttpBody. |referenced_files| // extract referenced files from ExplodedHttpBody. |referenced_files|
// currently contains a list from all frames, but cannot be deserialized into // currently contains a list from all frames, but cannot be deserialized into
// the files referenced by each frame. See http://crbug.com/441966. // the files referenced by each frame. See http://crbug.com/441966.
std::vector<base::NullableString16> referenced_files; std::vector<base::Optional<base::string16>> referenced_files;
ExplodedFrameState top; ExplodedFrameState top;
ExplodedPageState(); ExplodedPageState();
......
...@@ -21,11 +21,6 @@ ...@@ -21,11 +21,6 @@
namespace content { namespace content {
namespace { namespace {
base::NullableString16 NS16(const char* s) {
return s ? base::NullableString16(base::ASCIIToUTF16(s), false) :
base::NullableString16();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
...@@ -99,15 +94,17 @@ class PageStateSerializationTest : public testing::Test { ...@@ -99,15 +94,17 @@ class PageStateSerializationTest : public testing::Test {
public: public:
void PopulateFrameState(ExplodedFrameState* frame_state) { void PopulateFrameState(ExplodedFrameState* frame_state) {
// Invent some data for the various fields. // Invent some data for the various fields.
frame_state->url_string = NS16("http://dev.chromium.org/"); frame_state->url_string = base::UTF8ToUTF16("http://dev.chromium.org/");
frame_state->referrer = NS16("https://www.google.com/search?q=dev.chromium.org"); frame_state->referrer =
base::UTF8ToUTF16("https://www.google.com/search?q=dev.chromium.org");
frame_state->referrer_policy = blink::kWebReferrerPolicyAlways; frame_state->referrer_policy = blink::kWebReferrerPolicyAlways;
frame_state->target = NS16("foo"); frame_state->target = base::UTF8ToUTF16("foo");
frame_state->state_object = NS16(NULL); frame_state->state_object = base::nullopt;
frame_state->document_state.push_back(NS16("1")); frame_state->document_state.push_back(base::UTF8ToUTF16("1"));
frame_state->document_state.push_back(NS16("q")); frame_state->document_state.push_back(base::UTF8ToUTF16("q"));
frame_state->document_state.push_back(NS16("text")); frame_state->document_state.push_back(base::UTF8ToUTF16("text"));
frame_state->document_state.push_back(NS16("dev.chromium.org")); frame_state->document_state.push_back(
base::UTF8ToUTF16("dev.chromium.org"));
frame_state->scroll_restoration_type = frame_state->scroll_restoration_type =
blink::kWebHistoryScrollRestorationManual; blink::kWebHistoryScrollRestorationManual;
frame_state->visual_viewport_scroll_offset = gfx::PointF(10, 15); frame_state->visual_viewport_scroll_offset = gfx::PointF(10, 15);
...@@ -117,12 +114,13 @@ class PageStateSerializationTest : public testing::Test { ...@@ -117,12 +114,13 @@ class PageStateSerializationTest : public testing::Test {
frame_state->page_scale_factor = 2.0; frame_state->page_scale_factor = 2.0;
} }
void PopulateHttpBody(ExplodedHttpBody* http_body, void PopulateHttpBody(
std::vector<base::NullableString16>* referenced_files) { ExplodedHttpBody* http_body,
std::vector<base::Optional<base::string16>>* referenced_files) {
http_body->request_body = new ResourceRequestBody(); http_body->request_body = new ResourceRequestBody();
http_body->request_body->set_identifier(12345); http_body->request_body->set_identifier(12345);
http_body->contains_passwords = false; http_body->contains_passwords = false;
http_body->http_content_type = NS16("text/foo"); http_body->http_content_type = base::UTF8ToUTF16("text/foo");
std::string test_body("foo"); std::string test_body("foo");
http_body->request_body->AppendBytes(test_body.data(), test_body.size()); http_body->request_body->AppendBytes(test_body.data(), test_body.size());
...@@ -131,18 +129,17 @@ class PageStateSerializationTest : public testing::Test { ...@@ -131,18 +129,17 @@ class PageStateSerializationTest : public testing::Test {
http_body->request_body->AppendFileRange(base::FilePath(path), 100, 1024, http_body->request_body->AppendFileRange(base::FilePath(path), 100, 1024,
base::Time::FromDoubleT(9999.0)); base::Time::FromDoubleT(9999.0));
referenced_files->push_back( referenced_files->emplace_back(path.AsUTF16Unsafe());
base::NullableString16(path.AsUTF16Unsafe(), false));
} }
void PopulateFrameStateForBackwardsCompatTest( void PopulateFrameStateForBackwardsCompatTest(
ExplodedFrameState* frame_state, ExplodedFrameState* frame_state,
bool is_child) { bool is_child) {
frame_state->url_string = NS16("http://chromium.org/"); frame_state->url_string = base::UTF8ToUTF16("http://chromium.org/");
frame_state->referrer = NS16("http://google.com/"); frame_state->referrer = base::UTF8ToUTF16("http://google.com/");
frame_state->referrer_policy = blink::kWebReferrerPolicyDefault; frame_state->referrer_policy = blink::kWebReferrerPolicyDefault;
if (!is_child) if (!is_child)
frame_state->target = NS16("target"); frame_state->target = base::UTF8ToUTF16("target");
frame_state->scroll_restoration_type = frame_state->scroll_restoration_type =
blink::kWebHistoryScrollRestorationAuto; blink::kWebHistoryScrollRestorationAuto;
frame_state->visual_viewport_scroll_offset = gfx::PointF(-1, -1); frame_state->visual_viewport_scroll_offset = gfx::PointF(-1, -1);
...@@ -151,18 +148,18 @@ class PageStateSerializationTest : public testing::Test { ...@@ -151,18 +148,18 @@ class PageStateSerializationTest : public testing::Test {
frame_state->document_sequence_number = 456; frame_state->document_sequence_number = 456;
frame_state->page_scale_factor = 2.0f; frame_state->page_scale_factor = 2.0f;
frame_state->document_state.push_back( frame_state->document_state.push_back(base::UTF8ToUTF16(
NS16("\n\r?% WebKit serialized form state version 8 \n\r=&")); "\n\r?% WebKit serialized form state version 8 \n\r=&"));
frame_state->document_state.push_back(NS16("form key")); frame_state->document_state.push_back(base::UTF8ToUTF16("form key"));
frame_state->document_state.push_back(NS16("1")); frame_state->document_state.push_back(base::UTF8ToUTF16("1"));
frame_state->document_state.push_back(NS16("foo")); frame_state->document_state.push_back(base::UTF8ToUTF16("foo"));
frame_state->document_state.push_back(NS16("file")); frame_state->document_state.push_back(base::UTF8ToUTF16("file"));
frame_state->document_state.push_back(NS16("2")); frame_state->document_state.push_back(base::UTF8ToUTF16("2"));
frame_state->document_state.push_back(NS16("file.txt")); frame_state->document_state.push_back(base::UTF8ToUTF16("file.txt"));
frame_state->document_state.push_back(NS16("displayName")); frame_state->document_state.push_back(base::UTF8ToUTF16("displayName"));
if (!is_child) { if (!is_child) {
frame_state->http_body.http_content_type = NS16("foo/bar"); frame_state->http_body.http_content_type = base::UTF8ToUTF16("foo/bar");
frame_state->http_body.request_body = new ResourceRequestBody(); frame_state->http_body.request_body = new ResourceRequestBody();
frame_state->http_body.request_body->set_identifier(789); frame_state->http_body.request_body->set_identifier(789);
...@@ -185,7 +182,7 @@ class PageStateSerializationTest : public testing::Test { ...@@ -185,7 +182,7 @@ class PageStateSerializationTest : public testing::Test {
} }
void PopulatePageStateForBackwardsCompatTest(ExplodedPageState* page_state) { void PopulatePageStateForBackwardsCompatTest(ExplodedPageState* page_state) {
page_state->referenced_files.push_back(NS16("file.txt")); page_state->referenced_files.push_back(base::UTF8ToUTF16("file.txt"));
PopulateFrameStateForBackwardsCompatTest(&page_state->top, false); PopulateFrameStateForBackwardsCompatTest(&page_state->top, false);
} }
......
...@@ -98,8 +98,7 @@ class TestFrameAdapter : public UniqueNameHelper::FrameAdapter { ...@@ -98,8 +98,7 @@ class TestFrameAdapter : public UniqueNameHelper::FrameAdapter {
// FrameState children is guaranteed to match the order of TestFrameAdapter // FrameState children is guaranteed to match the order of TestFrameAdapter
// children. // children.
void PopulateLegacyFrameState(ExplodedFrameState* frame_state) const { void PopulateLegacyFrameState(ExplodedFrameState* frame_state) const {
frame_state->target = frame_state->target = base::UTF8ToUTF16(GetLegacyName());
base::NullableString16(base::UTF8ToUTF16(GetLegacyName()), false);
frame_state->children.resize(children_.size()); frame_state->children.resize(children_.size());
for (size_t i = 0; i < children_.size(); ++i) for (size_t i = 0; i < children_.size(); ++i)
children_[i]->PopulateLegacyFrameState(&frame_state->children[i]); children_[i]->PopulateLegacyFrameState(&frame_state->children[i]);
...@@ -108,7 +107,8 @@ class TestFrameAdapter : public UniqueNameHelper::FrameAdapter { ...@@ -108,7 +107,8 @@ class TestFrameAdapter : public UniqueNameHelper::FrameAdapter {
// Recursively verify that FrameState and its children have matching unique // Recursively verify that FrameState and its children have matching unique
// names to this TestFrameAdapter. // names to this TestFrameAdapter.
void VerifyUpdatedFrameState(const ExplodedFrameState& frame_state) const { void VerifyUpdatedFrameState(const ExplodedFrameState& frame_state) const {
EXPECT_EQ(GetUniqueName(), base::UTF16ToUTF8(frame_state.target.string())); EXPECT_EQ(GetUniqueName(),
base::UTF16ToUTF8(frame_state.target.value_or(base::string16())));
ASSERT_EQ(children_.size(), frame_state.children.size()); ASSERT_EQ(children_.size(), frame_state.children.size());
for (size_t i = 0; i < children_.size(); ++i) { for (size_t i = 0; i < children_.size(); ++i) {
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
#include <stddef.h> #include <stddef.h>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/strings/nullable_string16.h" #include "base/optional.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/common/page_state_serialization.h" #include "content/common/page_state_serialization.h"
#include "content/public/common/resource_request_body.h" #include "content/public/common/resource_request_body.h"
...@@ -15,20 +16,16 @@ ...@@ -15,20 +16,16 @@
namespace content { namespace content {
namespace { namespace {
base::NullableString16 ToNullableString16(const std::string& utf8) { base::FilePath ToFilePath(const base::Optional<base::string16>& s) {
return base::NullableString16(base::UTF8ToUTF16(utf8), false); return s ? base::FilePath::FromUTF16Unsafe(*s) : base::FilePath();
} }
base::FilePath ToFilePath(const base::NullableString16& s) { void ToFilePathVector(const std::vector<base::Optional<base::string16>>& input,
return base::FilePath::FromUTF16Unsafe(s.string());
}
void ToFilePathVector(const std::vector<base::NullableString16>& input,
std::vector<base::FilePath>* output) { std::vector<base::FilePath>* output) {
output->clear(); output->clear();
output->reserve(input.size()); output->reserve(input.size());
for (size_t i = 0; i < input.size(); ++i) for (size_t i = 0; i < input.size(); ++i)
output->push_back(ToFilePath(input[i])); output->emplace_back(ToFilePath(input[i]));
} }
PageState ToPageState(const ExplodedPageState& state) { PageState ToPageState(const ExplodedPageState& state) {
...@@ -48,7 +45,7 @@ void RecursivelyRemoveScrollOffset(ExplodedFrameState* state) { ...@@ -48,7 +45,7 @@ void RecursivelyRemoveScrollOffset(ExplodedFrameState* state) {
} }
void RecursivelyRemoveReferrer(ExplodedFrameState* state) { void RecursivelyRemoveReferrer(ExplodedFrameState* state) {
state->referrer = base::NullableString16(); state->referrer.reset();
state->referrer_policy = blink::kWebReferrerPolicyDefault; state->referrer_policy = blink::kWebReferrerPolicyDefault;
for (std::vector<ExplodedFrameState>::iterator it = state->children.begin(); for (std::vector<ExplodedFrameState>::iterator it = state->children.begin();
it != state->children.end(); it != state->children.end();
...@@ -68,7 +65,7 @@ PageState PageState::CreateFromEncodedData(const std::string& data) { ...@@ -68,7 +65,7 @@ PageState PageState::CreateFromEncodedData(const std::string& data) {
PageState PageState::CreateFromURL(const GURL& url) { PageState PageState::CreateFromURL(const GURL& url) {
ExplodedPageState state; ExplodedPageState state;
state.top.url_string = ToNullableString16(url.possibly_invalid_spec()); state.top.url_string = base::UTF8ToUTF16(url.possibly_invalid_spec());
return ToPageState(state); return ToPageState(state);
} }
...@@ -81,7 +78,7 @@ PageState PageState::CreateForTesting( ...@@ -81,7 +78,7 @@ PageState PageState::CreateForTesting(
const base::FilePath* optional_body_file_path) { const base::FilePath* optional_body_file_path) {
ExplodedPageState state; ExplodedPageState state;
state.top.url_string = ToNullableString16(url.possibly_invalid_spec()); state.top.url_string = base::UTF8ToUTF16(url.possibly_invalid_spec());
if (optional_body_data || optional_body_file_path) { if (optional_body_data || optional_body_file_path) {
if (optional_body_data) { if (optional_body_data) {
...@@ -96,8 +93,8 @@ PageState PageState::CreateForTesting( ...@@ -96,8 +93,8 @@ PageState PageState::CreateForTesting(
*optional_body_file_path, *optional_body_file_path,
0, std::numeric_limits<uint64_t>::max(), 0, std::numeric_limits<uint64_t>::max(),
base::Time()); base::Time());
state.referenced_files.push_back(base::NullableString16( state.referenced_files.emplace_back(
optional_body_file_path->AsUTF16Unsafe(), false)); optional_body_file_path->AsUTF16Unsafe());
} }
state.top.http_body.contains_passwords = state.top.http_body.contains_passwords =
body_contains_password_data; body_contains_password_data;
...@@ -112,7 +109,7 @@ PageState PageState::CreateForTestingWithSequenceNumbers( ...@@ -112,7 +109,7 @@ PageState PageState::CreateForTestingWithSequenceNumbers(
int64_t item_sequence_number, int64_t item_sequence_number,
int64_t document_sequence_number) { int64_t document_sequence_number) {
ExplodedPageState page_state; ExplodedPageState page_state;
page_state.top.url_string = ToNullableString16(url.spec()); page_state.top.url_string = base::UTF8ToUTF16(url.spec());
page_state.top.item_sequence_number = item_sequence_number; page_state.top.item_sequence_number = item_sequence_number;
page_state.top.document_sequence_number = document_sequence_number; page_state.top.document_sequence_number = document_sequence_number;
......
...@@ -30,22 +30,23 @@ using blink::WebVector; ...@@ -30,22 +30,23 @@ using blink::WebVector;
namespace content { namespace content {
namespace { namespace {
void ToNullableString16Vector(const WebVector<WebString>& input, void ToOptionalString16Vector(
std::vector<base::NullableString16>* output) { const WebVector<WebString>& input,
std::vector<base::Optional<base::string16>>* output) {
output->reserve(output->size() + input.size()); output->reserve(output->size() + input.size());
for (size_t i = 0; i < input.size(); ++i) for (size_t i = 0; i < input.size(); ++i)
output->push_back(WebString::ToNullableString16(input[i])); output->emplace_back(WebString::ToOptionalString16(input[i]));
} }
void GenerateFrameStateFromItem(const WebHistoryItem& item, void GenerateFrameStateFromItem(const WebHistoryItem& item,
ExplodedFrameState* state) { ExplodedFrameState* state) {
state->url_string = WebString::ToNullableString16(item.UrlString()); state->url_string = WebString::ToOptionalString16(item.UrlString());
state->referrer = WebString::ToNullableString16(item.GetReferrer()); state->referrer = WebString::ToOptionalString16(item.GetReferrer());
state->referrer_policy = item.GetReferrerPolicy(); state->referrer_policy = item.GetReferrerPolicy();
state->target = WebString::ToNullableString16(item.Target()); state->target = WebString::ToOptionalString16(item.Target());
if (!item.StateObject().IsNull()) { if (!item.StateObject().IsNull()) {
state->state_object = state->state_object =
WebString::ToNullableString16(item.StateObject().ToString()); WebString::ToOptionalString16(item.StateObject().ToString());
} }
state->scroll_restoration_type = item.ScrollRestorationType(); state->scroll_restoration_type = item.ScrollRestorationType();
state->visual_viewport_scroll_offset = item.VisualViewportScrollOffset(); state->visual_viewport_scroll_offset = item.VisualViewportScrollOffset();
...@@ -54,10 +55,10 @@ void GenerateFrameStateFromItem(const WebHistoryItem& item, ...@@ -54,10 +55,10 @@ void GenerateFrameStateFromItem(const WebHistoryItem& item,
state->document_sequence_number = item.DocumentSequenceNumber(); state->document_sequence_number = item.DocumentSequenceNumber();
state->page_scale_factor = item.PageScaleFactor(); state->page_scale_factor = item.PageScaleFactor();
state->did_save_scroll_or_scale_state = item.DidSaveScrollOrScaleState(); state->did_save_scroll_or_scale_state = item.DidSaveScrollOrScaleState();
ToNullableString16Vector(item.GetDocumentState(), &state->document_state); ToOptionalString16Vector(item.GetDocumentState(), &state->document_state);
state->http_body.http_content_type = state->http_body.http_content_type =
WebString::ToNullableString16(item.HttpContentType()); WebString::ToOptionalString16(item.HttpContentType());
const WebHTTPBody& http_body = item.HttpBody(); const WebHTTPBody& http_body = item.HttpBody();
if (!http_body.IsNull()) { if (!http_body.IsNull()) {
state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body); state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body);
...@@ -65,22 +66,6 @@ void GenerateFrameStateFromItem(const WebHistoryItem& item, ...@@ -65,22 +66,6 @@ void GenerateFrameStateFromItem(const WebHistoryItem& item,
} }
} }
void RecursivelyGenerateFrameState(
HistoryEntry::HistoryNode* node,
ExplodedFrameState* state,
std::vector<base::NullableString16>* referenced_files) {
GenerateFrameStateFromItem(node->item(), state);
ToNullableString16Vector(node->item().GetReferencedFilePaths(),
referenced_files);
std::vector<HistoryEntry::HistoryNode*> children = node->children();
state->children.resize(children.size());
for (size_t i = 0; i < children.size(); ++i) {
RecursivelyGenerateFrameState(children[i], &state->children[i],
referenced_files);
}
}
void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
HistoryEntry::HistoryNode* node) { HistoryEntry::HistoryNode* node) {
WebHistoryItem item; WebHistoryItem item;
...@@ -88,13 +73,14 @@ void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, ...@@ -88,13 +73,14 @@ void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
item.SetURLString(WebString::FromUTF16(state.url_string)); item.SetURLString(WebString::FromUTF16(state.url_string));
item.SetReferrer(WebString::FromUTF16(state.referrer), state.referrer_policy); item.SetReferrer(WebString::FromUTF16(state.referrer), state.referrer_policy);
item.SetTarget(WebString::FromUTF16(state.target)); item.SetTarget(WebString::FromUTF16(state.target));
if (!state.state_object.is_null()) { if (state.state_object) {
item.SetStateObject(WebSerializedScriptValue::FromString( item.SetStateObject(WebSerializedScriptValue::FromString(
WebString::FromUTF16(state.state_object))); WebString::FromUTF16(*state.state_object)));
} }
WebVector<WebString> document_state(state.document_state.size()); WebVector<WebString> document_state(state.document_state.size());
std::transform(state.document_state.begin(), state.document_state.end(), std::transform(state.document_state.begin(), state.document_state.end(),
document_state.begin(), [](const base::NullableString16& s) { document_state.begin(),
[](const base::Optional<base::string16>& s) {
return WebString::FromUTF16(s); return WebString::FromUTF16(s);
}); });
item.SetDocumentState(document_state); item.SetDocumentState(document_state);
...@@ -128,19 +114,9 @@ void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, ...@@ -128,19 +114,9 @@ void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
} // namespace } // namespace
PageState HistoryEntryToPageState(HistoryEntry* entry) {
ExplodedPageState state;
RecursivelyGenerateFrameState(entry->root_history_node(), &state.top,
&state.referenced_files);
std::string encoded_data;
EncodePageState(state, &encoded_data);
return PageState::CreateFromEncodedData(encoded_data);
}
PageState SingleHistoryItemToPageState(const WebHistoryItem& item) { PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
ExplodedPageState state; ExplodedPageState state;
ToNullableString16Vector(item.GetReferencedFilePaths(), ToOptionalString16Vector(item.GetReferencedFilePaths(),
&state.referenced_files); &state.referenced_files);
GenerateFrameStateFromItem(item, &state.top); GenerateFrameStateFromItem(item, &state.top);
......
...@@ -106,12 +106,12 @@ std::string DumpFrameState(const ExplodedFrameState& frame_state, ...@@ -106,12 +106,12 @@ std::string DumpFrameState(const ExplodedFrameState& frame_state,
} }
std::string url = test_runner::NormalizeLayoutTestURL( std::string url = test_runner::NormalizeLayoutTestURL(
base::UTF16ToUTF8(frame_state.url_string.string())); base::UTF16ToUTF8(frame_state.url_string.value_or(base::string16())));
result.append(url); result.append(url);
const base::string16& target = frame_state.target.string(); DCHECK(frame_state.target);
if (!target.empty()) { if (!frame_state.target->empty()) {
result.append(" (in frame \""); result.append(" (in frame \"");
result.append(base::UTF16ToUTF8(target)); result.append(base::UTF16ToUTF8(*frame_state.target));
result.append("\")"); result.append("\")");
} }
result.append("\n"); result.append("\n");
...@@ -120,10 +120,10 @@ std::string DumpFrameState(const ExplodedFrameState& frame_state, ...@@ -120,10 +120,10 @@ std::string DumpFrameState(const ExplodedFrameState& frame_state,
std::sort(sorted_children.begin(), sorted_children.end(), std::sort(sorted_children.begin(), sorted_children.end(),
[](const ExplodedFrameState& lhs, const ExplodedFrameState& rhs) { [](const ExplodedFrameState& lhs, const ExplodedFrameState& rhs) {
// Child nodes should always have a target (aka unique name). // Child nodes should always have a target (aka unique name).
DCHECK(!lhs.target.string().empty()); DCHECK(lhs.target);
DCHECK(!rhs.target.string().empty()); DCHECK(rhs.target);
return base::CompareCaseInsensitiveASCII(lhs.target.string(), return base::CompareCaseInsensitiveASCII(*lhs.target,
rhs.target.string()) < 0; *rhs.target) < 0;
}); });
for (const auto& child : sorted_children) for (const auto& child : sorted_children)
result += DumpFrameState(child, indent + 4, false); result += DumpFrameState(child, indent + 4, false);
......
...@@ -103,6 +103,15 @@ WebString WebString::FromUTF16(const base::NullableString16& s) { ...@@ -103,6 +103,15 @@ WebString WebString::FromUTF16(const base::NullableString16& s) {
return string; return string;
} }
WebString WebString::FromUTF16(const base::Optional<base::string16>& s) {
WebString string;
if (!s)
string.Reset();
else
string.Assign(s->data(), s->length());
return string;
}
std::string WebString::Latin1() const { std::string WebString::Latin1() const {
String string(private_.Get()); String string(private_.Get());
......
...@@ -31,12 +31,13 @@ ...@@ -31,12 +31,13 @@
#ifndef WebString_h #ifndef WebString_h
#define WebString_h #define WebString_h
#include <string>
#include "WebCommon.h" #include "WebCommon.h"
#include "WebPrivatePtr.h" #include "WebPrivatePtr.h"
#include "base/optional.h"
#include "base/strings/latin1_string_conversions.h" #include "base/strings/latin1_string_conversions.h"
#include "base/strings/nullable_string16.h" #include "base/strings/nullable_string16.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include <string>
#if INSIDE_BLINK #if INSIDE_BLINK
#include "platform/wtf/Forward.h" #include "platform/wtf/Forward.h"
...@@ -56,6 +57,7 @@ namespace blink { ...@@ -56,6 +57,7 @@ namespace blink {
// * WebString::FromUTF8(const std::string& utf8) // * WebString::FromUTF8(const std::string& utf8)
// * WebString::FromUTF16(const base::string16& utf16) // * WebString::FromUTF16(const base::string16& utf16)
// * WebString::FromUTF16(const base::NullableString16& utf16) // * WebString::FromUTF16(const base::NullableString16& utf16)
// * WebString::FromUTF16(const base::Optional<base::string16>& utf16)
// //
// Similarly, use either of following methods to convert WebString to // Similarly, use either of following methods to convert WebString to
// ASCII, Latin1, UTF-8 or UTF-16: // ASCII, Latin1, UTF-8 or UTF-16:
...@@ -64,7 +66,8 @@ namespace blink { ...@@ -64,7 +66,8 @@ namespace blink {
// * webstring.Latin1() // * webstring.Latin1()
// * webstring.Utf8() // * webstring.Utf8()
// * webstring.Utf16() // * webstring.Utf16()
// * WebString::toNullableString16(webstring) // * WebString::ToNullableString16(webstring)
// * WebString::ToOptionalString16(webstring)
// //
// Note that if you need to convert the UTF8 string converted from WebString // Note that if you need to convert the UTF8 string converted from WebString
// back to WebString with FromUTF8() you may want to specify Strict // back to WebString with FromUTF8() you may want to specify Strict
...@@ -140,8 +143,15 @@ class WebString { ...@@ -140,8 +143,15 @@ class WebString {
BLINK_PLATFORM_EXPORT static WebString FromUTF16( BLINK_PLATFORM_EXPORT static WebString FromUTF16(
const base::NullableString16&); const base::NullableString16&);
BLINK_PLATFORM_EXPORT static WebString FromUTF16(
const base::Optional<base::string16>&);
static base::NullableString16 ToNullableString16(const WebString& s) { static base::NullableString16 ToNullableString16(const WebString& s) {
return base::NullableString16(s.Utf16(), s.IsNull()); return base::NullableString16(ToOptionalString16(s));
}
static base::Optional<base::string16> ToOptionalString16(const WebString& s) {
return s.IsNull() ? base::nullopt : base::make_optional(s.Utf16());
} }
BLINK_PLATFORM_EXPORT std::string Latin1() const; BLINK_PLATFORM_EXPORT std::string Latin1() const;
......
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