Skip to content

Commit

Permalink
Refactor RevealItem to replace RVItem for IPC messaging
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264065
rdar://117822336

Reviewed by Megan Gardner.

The only RVItems that RevealItem every serialized for IPC were created internally with text and a range.
This patch changes serializing the RVItem to simply serializing the text and range.

* Source/WebCore/editing/cocoa/DictionaryLookup.mm:
* Source/WebKit/Shared/Cocoa/RevealItem.h:
(WebKit::RevealItemRange::RevealItemRange):
(WebKit::RevealItem::text const):
(WebKit::RevealItem::selectedRange const):
(WebKit::RevealItem::item const): Deleted.
* Source/WebKit/Shared/Cocoa/RevealItem.mm:
(WebKit::RevealItemRange::RevealItemRange):
(WebKit::RevealItem::RevealItem):
(WebKit::RevealItem::highlightRange const):
(WebKit::RevealItem::item const):
* Source/WebKit/Shared/Cocoa/RevealItem.serialization.in:
* Source/WebKit/WebProcess/WebPage/WebPage.h:
* Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::revealItemForCurrentSelection):

Canonical link: https://commits.webkit.org/270131@main
  • Loading branch information
beidson committed Nov 2, 2023
1 parent 81e5846 commit 413e473
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
4 changes: 1 addition & 3 deletions Source/WebCore/editing/cocoa/DictionaryLookup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,7 @@ static void expandSelectionByCharacters(PDFSelection *selection, NSInteger numbe
auto fullPlainTextString = [selectionForLookup string];
auto rangeToPass = NSMakeRange(charactersAddedBeforeStart, 0);

auto item = adoptNS([PAL::allocRVItemInstance() initWithText:fullPlainTextString selectedRange:rangeToPass]);
NSRange extractedRange = item.get().highlightRange;

NSRange extractedRange = adoptNS([PAL::allocRVItemInstance() initWithText:fullPlainTextString selectedRange:rangeToPass]).get().highlightRange;
if (extractedRange.location == NSNotFound)
return { selection.string, nil };

Expand Down
36 changes: 28 additions & 8 deletions Source/WebKit/Shared/Cocoa/RevealItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,44 @@

#if ENABLE(REVEAL)

#import "ArgumentCoders.h"

#import <wtf/RetainPtr.h>

OBJC_CLASS RVItem;

typedef struct _NSRange NSRange;
typedef unsigned long NSUInteger;

namespace WebKit {

struct RevealItemRange {
RevealItemRange() = default;
RevealItemRange(NSRange);
RevealItemRange(NSUInteger loc, NSUInteger len)
: location(loc)
, length(len)
{
}

NSUInteger location { 0 };
NSUInteger length { 0 };
};

class RevealItem {

public:
RevealItem() = default;
RevealItem(RetainPtr<RVItem>&&);

RVItem *item() const { return m_item.get(); }
RevealItem(const String& text, RevealItemRange selectedRange);

const String& text() const { return m_text; }
const RevealItemRange& selectedRange() const { return m_selectedRange; }
NSRange highlightRange() const;

RVItem *item() const;

private:
friend struct IPC::ArgumentCoder<RevealItem, void>;
RetainPtr<RVItem> m_item;
String m_text;
RevealItemRange m_selectedRange;

mutable RetainPtr<RVItem> m_item;
};

}
Expand Down
25 changes: 22 additions & 3 deletions Source/WebKit/Shared/Cocoa/RevealItem.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@

#if ENABLE(REVEAL)

RevealItem::RevealItem(RetainPtr<RVItem>&& item)
: m_item { WTFMove(item) }
RevealItemRange::RevealItemRange(NSRange range)
: location(range.location)
, length(range.length)
{
}

#endif
RevealItem::RevealItem(const String& text, RevealItemRange selectedRange)
: m_text(text)
, m_selectedRange(selectedRange)
{
}

NSRange RevealItem::highlightRange() const
{
return item().highlightRange;
}

RVItem *RevealItem::item() const
{
if (!m_item)
m_item = adoptNS([PAL::allocRVItemInstance() initWithText:m_text selectedRange:NSMakeRange(m_selectedRange.location, m_selectedRange.length)]);
return m_item.get();
}

#endif // ENABLE(REVEAL)

} // namespace WebKit
10 changes: 8 additions & 2 deletions Source/WebKit/Shared/Cocoa/RevealItem.serialization.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

headers: <pal/cocoa/RevealSoftLink.h>
webkit_platform_headers: "RevealItem.h" <pal/cocoa/RevealSoftLink.h>

#if ENABLE(REVEAL)

[WebKitPlatform, CustomHeader] struct WebKit::RevealItemRange {
NSUInteger location;
NSUInteger length;
}

class WebKit::RevealItem {
[SecureCodingAllowed=[PAL::getRVItemClass()]] RetainPtr<RVItem> m_item;
String text();
WebKit::RevealItemRange selectedRange();
};

#endif
5 changes: 2 additions & 3 deletions Source/WebKit/WebProcess/WebPage/WebPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ OBJC_CLASS NSDictionary;
OBJC_CLASS NSObject;
OBJC_CLASS PDFDocument;
OBJC_CLASS PDFSelection;
OBJC_CLASS RVItem;
OBJC_CLASS WKAccessibilityWebPageObject;
#endif

Expand Down Expand Up @@ -877,8 +876,8 @@ class WebPage : public API::ObjectImpl<API::Object::Type::BundlePage>, public IP
void updateSelectionWithExtentPointAndBoundary(const WebCore::IntPoint&, WebCore::TextGranularity, bool isInteractingWithFocusedElement, CompletionHandler<void(bool)>&&);

#if ENABLE(REVEAL)
RetainPtr<RVItem> revealItemForCurrentSelection();
void requestRVItemInCurrentSelectedRange(CompletionHandler<void(const WebKit::RevealItem&)>&&);
RevealItem revealItemForCurrentSelection();
void requestRVItemInCurrentSelectedRange(CompletionHandler<void(const RevealItem&)>&&);
void prepareSelectionForContextMenuWithLocationInView(WebCore::IntPoint, CompletionHandler<void(bool, const RevealItem&)>&&);
#endif
void willInsertFinalDictationResult();
Expand Down
7 changes: 3 additions & 4 deletions Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2434,11 +2434,10 @@ static inline bool rectIsTooBigForSelection(const IntRect& blockRect, const Loca
}

#if ENABLE(REVEAL)
RetainPtr<RVItem> WebPage::revealItemForCurrentSelection()
RevealItem WebPage::revealItemForCurrentSelection()
{
Ref frame = CheckedRef(m_page->focusController())->focusedOrMainFrame();
auto selection = frame->selection().selection();
RetainPtr<RVItem> item;
if (!selection.isNone()) {
std::optional<SimpleRange> fullCharacterRange;
if (selection.isRange()) {
Expand All @@ -2453,11 +2452,11 @@ static inline bool rectIsTooBigForSelection(const IntRect& blockRect, const Loca
if (fullCharacterRange) {
auto selectionRange = NSMakeRange(characterCount(*makeSimpleRange(fullCharacterRange->start, selectionStart)), characterCount(*makeSimpleRange(selectionStart, selectionEnd)));
String itemString = plainText(*fullCharacterRange);
item = adoptNS([PAL::allocRVItemInstance() initWithText:itemString selectedRange:selectionRange]);
return { itemString, selectionRange };
}
}
}
return item;
return { };
}

void WebPage::requestRVItemInCurrentSelectedRange(CompletionHandler<void(const WebKit::RevealItem&)>&& completionHandler)
Expand Down

0 comments on commit 413e473

Please sign in to comment.