From ecdfd467f84da9bb6ef57eb4fcb2778fdef5e07a Mon Sep 17 00:00:00 2001 From: Vladimir Kharitonov Date: Tue, 16 May 2023 13:22:17 +0300 Subject: [PATCH] Update CefRange type to match gfx::Range (fixes #3422) --- include/cef_api_hash.h | 8 ++-- include/internal/cef_types.h | 4 +- include/internal/cef_types_wrappers.h | 7 +++- .../cefclient/browser/osr_ime_handler_win.cc | 35 ++++++++-------- tests/cefclient/browser/osr_ime_handler_win.h | 4 +- tests/cefclient/browser/osr_window_win.cc | 5 +-- .../browser/text_input_client_osr_mac.mm | 40 ++++++++++--------- tests/ceftests/os_rendering_unittest.cc | 18 ++++----- tests/ceftests/views/textfield_unittest.cc | 20 +++++----- 9 files changed, 74 insertions(+), 67 deletions(-) diff --git a/include/cef_api_hash.h b/include/cef_api_hash.h index b92e267ff..77a0dbcdd 100644 --- a/include/cef_api_hash.h +++ b/include/cef_api_hash.h @@ -42,13 +42,13 @@ // way that may cause binary incompatibility with other builds. The universal // hash value will change if any platform is affected whereas the platform hash // values will change only if that particular platform is affected. -#define CEF_API_HASH_UNIVERSAL "cd16e1ab288ddddc4c13c67b05aed49a47db6e7f" +#define CEF_API_HASH_UNIVERSAL "a3f241333b0d0bdcb3b3ea7c2b003ff146019b54" #if defined(OS_WIN) -#define CEF_API_HASH_PLATFORM "d80ddfd6897163f83a0acdc2a3d3e627916a1365" +#define CEF_API_HASH_PLATFORM "22809c1dd1e83a467160065822e4c5a2aa1a6f74" #elif defined(OS_MAC) -#define CEF_API_HASH_PLATFORM "d19854c113f1549904ff4055f3a38901cac6af68" +#define CEF_API_HASH_PLATFORM "4366603b6ce969025e27b7d1834028df8e6ffbe4" #elif defined(OS_LINUX) -#define CEF_API_HASH_PLATFORM "0ba21a432d3b28ab79a0319a11a6e113ff1fcb07" +#define CEF_API_HASH_PLATFORM "d1f8e80f361ae3d013add51dcb1449341be9eff1" #endif #ifdef __cplusplus diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index 2ca9d7d1a..73ad9ccad 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -3000,8 +3000,8 @@ typedef enum { /// Structure representing a range. /// typedef struct _cef_range_t { - int from; - int to; + uint32_t from; + uint32_t to; } cef_range_t; /// diff --git a/include/internal/cef_types_wrappers.h b/include/internal/cef_types_wrappers.h index c1153275c..9c7701e7e 100644 --- a/include/internal/cef_types_wrappers.h +++ b/include/internal/cef_types_wrappers.h @@ -215,7 +215,12 @@ class CefRange : public cef_range_t { public: CefRange() : cef_range_t{} {} CefRange(const cef_range_t& r) : cef_range_t(r) {} - CefRange(int from, int to) : cef_range_t{from, to} {} + CefRange(uint32_t from, uint32_t to) : cef_range_t{from, to} {} + + static CefRange InvalidRange() { + return CefRange(std::numeric_limits::max(), + std::numeric_limits::max()); + } void Set(int from_val, int to_val) { from = from_val, to = to_val; } }; diff --git a/tests/cefclient/browser/osr_ime_handler_win.cc b/tests/cefclient/browser/osr_ime_handler_win.cc index 3f0921cfd..1571cd4a7 100644 --- a/tests/cefclient/browser/osr_ime_handler_win.cc +++ b/tests/cefclient/browser/osr_ime_handler_win.cc @@ -35,12 +35,13 @@ bool IsSelectionAttribute(char attribute) { // to get the target range that's selected by the user in the current // composition string. void GetCompositionSelectionRange(HIMC imc, - int* target_start, - int* target_end) { - int attribute_size = ::ImmGetCompositionString(imc, GCS_COMPATTR, nullptr, 0); + uint32_t* target_start, + uint32_t* target_end) { + uint32_t attribute_size = + ::ImmGetCompositionString(imc, GCS_COMPATTR, nullptr, 0); if (attribute_size > 0) { - int start = 0; - int end = 0; + uint32_t start = 0; + uint32_t end = 0; std::vector attribute_data(attribute_size); ::ImmGetCompositionString(imc, GCS_COMPATTR, &attribute_data[0], @@ -65,13 +66,13 @@ void GetCompositionSelectionRange(HIMC imc, // underlines information of the current composition string. void GetCompositionUnderlines( HIMC imc, - int target_start, - int target_end, + uint32_t target_start, + uint32_t target_end, std::vector& underlines) { int clause_size = ::ImmGetCompositionString(imc, GCS_COMPCLAUSE, nullptr, 0); int clause_length = clause_size / sizeof(uint32); if (clause_length) { - std::vector clause_data(clause_length); + std::vector clause_data(clause_length); ::ImmGetCompositionString(imc, GCS_COMPCLAUSE, &clause_data[0], clause_size); @@ -99,7 +100,7 @@ OsrImeHandlerWin::OsrImeHandlerWin(HWND hwnd) : is_composing_(false), input_language_id_(LANG_USER_DEFAULT), system_caret_(false), - cursor_index_(-1), + cursor_index_(std::numeric_limits::max()), hwnd_(hwnd) { ime_rect_ = {-1, -1, 0, 0}; } @@ -157,10 +158,10 @@ void OsrImeHandlerWin::MoveImeWindow() { } CefRect rc = ime_rect_; - int location = cursor_index_; + uint32_t location = cursor_index_; // If location is not specified fall back to the composition range start. - if (location == -1) { + if (location == std::numeric_limits::max()) { location = composition_range_.from; } @@ -169,7 +170,7 @@ void OsrImeHandlerWin::MoveImeWindow() { location -= composition_range_.from; } - if (location < static_cast(composition_bounds_.size())) { + if (location < composition_bounds_.size()) { rc = composition_bounds_[location]; } else { return; @@ -240,7 +241,7 @@ void OsrImeHandlerWin::CleanupComposition() { void OsrImeHandlerWin::ResetComposition() { // Reset the composition status. is_composing_ = false; - cursor_index_ = -1; + cursor_index_ = std::numeric_limits::max(); } void OsrImeHandlerWin::GetCompositionInfo( @@ -253,11 +254,11 @@ void OsrImeHandlerWin::GetCompositionInfo( // convert them into underlines and selection range respectively. underlines.clear(); - int length = static_cast(composition_text.length()); + uint32_t length = static_cast(composition_text.length()); // Find out the range selected by the user. - int target_start = length; - int target_end = length; + uint32_t target_start = length; + uint32_t target_end = length; if (lparam & GCS_COMPATTR) { GetCompositionSelectionRange(imc, &target_start, &target_end); } @@ -386,7 +387,7 @@ void OsrImeHandlerWin::EnableIME() { ::ImmAssociateContextEx(hwnd_, nullptr, IACE_DEFAULT); } -void OsrImeHandlerWin::UpdateCaretPosition(int index) { +void OsrImeHandlerWin::UpdateCaretPosition(uint32_t index) { // Save the caret position. cursor_index_ = index; // Move the IME window. diff --git a/tests/cefclient/browser/osr_ime_handler_win.h b/tests/cefclient/browser/osr_ime_handler_win.h index 875898506..de5b92fa8 100644 --- a/tests/cefclient/browser/osr_ime_handler_win.h +++ b/tests/cefclient/browser/osr_ime_handler_win.h @@ -60,7 +60,7 @@ class OsrImeHandlerWin { virtual void CancelIME(); // Updates the IME caret position of the given window. - void UpdateCaretPosition(int index); + void UpdateCaretPosition(uint32_t index); // Updates the composition range. |selected_range| is the range of characters // that have been selected. |character_bounds| is the bounds of each character @@ -100,7 +100,7 @@ class OsrImeHandlerWin { CefRect ime_rect_; // The current cursor index in composition string. - int cursor_index_; + uint32_t cursor_index_; // The composition range in the string. This may be used to determine the // offset in composition bounds. diff --git a/tests/cefclient/browser/osr_window_win.cc b/tests/cefclient/browser/osr_window_win.cc index 913510903..ed800525e 100644 --- a/tests/cefclient/browser/osr_window_win.cc +++ b/tests/cefclient/browser/osr_window_win.cc @@ -418,8 +418,7 @@ void OsrWindowWin::OnIMEComposition(UINT message, // Send the text to the browser. The |replacement_range| and // |relative_cursor_pos| params are not used on Windows, so provide // default invalid values. - browser_->GetHost()->ImeCommitText(cTextStr, - CefRange(UINT32_MAX, UINT32_MAX), 0); + browser_->GetHost()->ImeCommitText(cTextStr, CefRange::InvalidRange(), 0); ime_handler_->ResetComposition(); // Continue reading the composition string - Japanese IMEs send both // GCS_RESULTSTR and GCS_COMPSTR. @@ -433,7 +432,7 @@ void OsrWindowWin::OnIMEComposition(UINT message, // Send the composition string to the browser. The |replacement_range| // param is not used on Windows, so provide a default invalid value. browser_->GetHost()->ImeSetComposition( - cTextStr, underlines, CefRange(UINT32_MAX, UINT32_MAX), + cTextStr, underlines, CefRange::InvalidRange(), CefRange(composition_start, static_cast(composition_start + cTextStr.length()))); diff --git a/tests/cefclient/browser/text_input_client_osr_mac.mm b/tests/cefclient/browser/text_input_client_osr_mac.mm index 8e3e902ee..34d08e9ab 100644 --- a/tests/cefclient/browser/text_input_client_osr_mac.mm +++ b/tests/cefclient/browser/text_input_client_osr_mac.mm @@ -45,11 +45,12 @@ void ExtractUnderlines(NSAttributedString* string, color = CefColorFromNSColor( [colorAttr colorUsingColorSpaceName:NSDeviceRGBColorSpace]); } - cef_composition_underline_t line = {{static_cast(range.location), - static_cast(NSMaxRange(range))}, - color, - 0, - [style intValue] > 1}; + cef_composition_underline_t line = { + {static_cast(range.location), + static_cast(NSMaxRange(range))}, + color, + 0, + [style intValue] > 1}; underlines->push_back(line); } i = static_cast(range.location + range.length); @@ -109,8 +110,8 @@ extern NSString* NSTextInputReplacementRangeAttributeName; if (handlingKeyDown_) { textToBeInserted_.append([im_text UTF8String]); } else { - cef_range_t range = {static_cast(replacementRange.location), - static_cast(NSMaxRange(replacementRange))}; + cef_range_t range = {static_cast(replacementRange.location), + static_cast(NSMaxRange(replacementRange))}; browser_->GetHost()->ImeCommitText([im_text UTF8String], range, 0); } @@ -131,7 +132,7 @@ extern NSString* NSTextInputReplacementRangeAttributeName; BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]]; NSString* im_text = isAttributedString ? [aString string] : aString; - int length = static_cast([im_text length]); + uint32_t length = [im_text length]; // |markedRange_| will get set in a callback from ImeSetComposition(). selectedRange_ = newSelRange; @@ -154,13 +155,14 @@ extern NSString* NSTextInputReplacementRangeAttributeName; // ongoing composition when we send empty text. if (handlingKeyDown_) { setMarkedTextReplacementRange_ = { - static_cast(replacementRange.location), - static_cast(NSMaxRange(replacementRange))}; + static_cast(replacementRange.location), + static_cast(NSMaxRange(replacementRange))}; } else if (!handlingKeyDown_) { - CefRange replacement_range(static_cast(replacementRange.location), - static_cast(NSMaxRange(replacementRange))); - CefRange selection_range(static_cast(newSelRange.location), - static_cast(NSMaxRange(newSelRange))); + CefRange replacement_range( + static_cast(replacementRange.location), + static_cast(NSMaxRange(replacementRange))); + CefRange selection_range(static_cast(newSelRange.location), + static_cast(NSMaxRange(newSelRange))); browser_->GetHost()->ImeSetComposition(markedText_, underlines_, replacement_range, selection_range); @@ -268,7 +270,7 @@ extern NSString* NSTextInputReplacementRangeAttributeName; textToBeInserted_.clear(); markedText_.clear(); underlines_.clear(); - setMarkedTextReplacementRange_ = CefRange(UINT32_MAX, UINT32_MAX); + setMarkedTextReplacementRange_ = CefRange::InvalidRange(); unmarkTextCalled_ = NO; } @@ -306,7 +308,7 @@ extern NSString* NSTextInputReplacementRangeAttributeName; if (textToBeInserted_.length() > ((hasMarkedText_ || oldHasMarkedText_) ? 0u : 1u)) { browser_->GetHost()->ImeCommitText(textToBeInserted_, - CefRange(UINT32_MAX, UINT32_MAX), 0); + CefRange::InvalidRange(), 0); textToBeInserted_.clear(); } @@ -317,8 +319,8 @@ extern NSString* NSTextInputReplacementRangeAttributeName; // |selectedRange_| is the range being selected inside the marked text. browser_->GetHost()->ImeSetComposition( markedText_, underlines_, setMarkedTextReplacementRange_, - CefRange(static_cast(selectedRange_.location), - static_cast(NSMaxRange(selectedRange_)))); + CefRange(static_cast(selectedRange_.location), + static_cast(NSMaxRange(selectedRange_)))); } else if (oldHasMarkedText_ && !hasMarkedText_ && !textInserted) { // There was no marked text or inserted text. Complete or cancel the // composition. @@ -329,7 +331,7 @@ extern NSString* NSTextInputReplacementRangeAttributeName; } } - setMarkedTextReplacementRange_ = CefRange(UINT32_MAX, UINT32_MAX); + setMarkedTextReplacementRange_ = CefRange::InvalidRange(); } - (void)ChangeCompositionRange:(CefRange)range diff --git a/tests/ceftests/os_rendering_unittest.cc b/tests/ceftests/os_rendering_unittest.cc index 9d554255f..e8c566e72 100644 --- a/tests/ceftests/os_rendering_unittest.cc +++ b/tests/ceftests/os_rendering_unittest.cc @@ -1097,8 +1097,8 @@ class OSRTestHandler : public RoutingTestHandler, const CefRange& range, const CefRenderHandler::RectList& bounds) override { if (test_type_ == OSR_TEST_IME_SET_COMPOSITION && started()) { - EXPECT_EQ(range.from, 0); - EXPECT_EQ(range.to, 1); + EXPECT_EQ(range.from, 0U); + EXPECT_EQ(range.to, 1U); EXPECT_EQ(1U, bounds.size()); DestroySucceededTestSoon(); } @@ -1586,7 +1586,7 @@ class OSRTestHandler : public RoutingTestHandler, // This text should be honored instead of 'ka' added via key events CefString markedText("osrimecommit"); - CefRange range(0, static_cast(markedText.length())); + CefRange range(0, static_cast(markedText.length())); browser->GetHost()->ImeCommitText(markedText, range, 0); ClickButtonToNavigate(browser); @@ -1621,12 +1621,12 @@ class OSRTestHandler : public RoutingTestHandler, std::vector underlines; // Use a thin black underline by default. - CefRange range(0, static_cast(markedText.length())); + CefRange range(0, static_cast(markedText.length())); cef_composition_underline_t line = {range, 0xFF000000, 0, false}; underlines.push_back(line); - CefRange replacement_range(0, static_cast(markedText.length())); - CefRange selection_range(0, static_cast(markedText.length())); + CefRange replacement_range(0, static_cast(markedText.length())); + CefRange selection_range(0, static_cast(markedText.length())); // Composition should be updated browser->GetHost()->ImeSetComposition(markedText, underlines, @@ -1646,12 +1646,12 @@ class OSRTestHandler : public RoutingTestHandler, std::vector underlines; // Use a thin black underline by default. - CefRange range(0, static_cast(markedText.length())); + CefRange range(0, static_cast(markedText.length())); cef_composition_underline_t line = {range, 0xFF000000, 0, false}; underlines.push_back(line); - CefRange replacement_range(0, static_cast(markedText.length())); - CefRange selection_range(0, static_cast(markedText.length())); + CefRange replacement_range(0, static_cast(markedText.length())); + CefRange selection_range(0, static_cast(markedText.length())); // This should update composition range and // trigger the compositionRangeChanged callback diff --git a/tests/ceftests/views/textfield_unittest.cc b/tests/ceftests/views/textfield_unittest.cc index 8c08fbfc7..07181d60e 100644 --- a/tests/ceftests/views/textfield_unittest.cc +++ b/tests/ceftests/views/textfield_unittest.cc @@ -64,12 +64,12 @@ void RunTextfieldContents(CefRefPtr window) { // Test select range. EXPECT_FALSE(textfield->HasSelection()); - EXPECT_EQ( - CefRange(static_cast(cursor_pos), static_cast(cursor_pos)), - textfield->GetSelectedRange()); - textfield->SelectRange(CefRange(0, static_cast(cursor_pos))); + EXPECT_EQ(CefRange(static_cast(cursor_pos), + static_cast(cursor_pos)), + textfield->GetSelectedRange()); + textfield->SelectRange(CefRange(0, static_cast(cursor_pos))); EXPECT_TRUE(textfield->HasSelection()); - EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), + EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), textfield->GetSelectedRange()); EXPECT_STREQ(kText, textfield->GetSelectedText().ToString().c_str()); EXPECT_EQ(cursor_pos, textfield->GetCursorPosition()); @@ -89,23 +89,23 @@ void RunTextfieldContents(CefRefPtr window) { EXPECT_TRUE(textfield->HasSelection()); cursor_pos = sizeof(kReplaceText) + sizeof(kAppendText) - 2; - EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), + EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), textfield->GetSelectedRange()); EXPECT_EQ(cursor_pos, textfield->GetCursorPosition()); // Test clear selection. textfield->ClearSelection(); EXPECT_FALSE(textfield->HasSelection()); - EXPECT_EQ( - CefRange(static_cast(cursor_pos), static_cast(cursor_pos)), - textfield->GetSelectedRange()); + EXPECT_EQ(CefRange(static_cast(cursor_pos), + static_cast(cursor_pos)), + textfield->GetSelectedRange()); EXPECT_EQ(cursor_pos, textfield->GetCursorPosition()); // Test selection with command. EXPECT_TRUE(textfield->IsCommandEnabled(CEF_TFC_SELECT_ALL)); textfield->ExecuteCommand(CEF_TFC_SELECT_ALL); EXPECT_TRUE(textfield->HasSelection()); - EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), + EXPECT_EQ(CefRange(0, static_cast(cursor_pos)), textfield->GetSelectedRange()); EXPECT_EQ(cursor_pos, textfield->GetCursorPosition());