diff options
Diffstat (limited to 'Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm')
-rw-r--r-- | Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm | 128 |
1 files changed, 113 insertions, 15 deletions
diff --git a/Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm b/Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm index 71bbf78..4cae5aa 100644 --- a/Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm +++ b/Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm @@ -42,6 +42,7 @@ #import <WebCore/KeyboardEvent.h> #import <WebCore/Page.h> #import <WebCore/PlatformKeyboardEvent.h> +#import <WebCore/ResourceHandle.h> #import <WebCore/ScrollView.h> #import <WebCore/TextIterator.h> #import <WebCore/WindowsKeyboardCodes.h> @@ -163,29 +164,29 @@ void WebPage::getMarkedRange(uint64_t& location, uint64_t& length) getLocationAndLengthFromRange(frame->editor()->compositionRange().get(), location, length); } -static Range *characterRangeAtPoint(Frame* frame, const IntPoint point) +static PassRefPtr<Range> characterRangeAtPoint(Frame* frame, const IntPoint& point) { VisiblePosition position = frame->visiblePositionForPoint(point); if (position.isNull()) - return NULL; + return 0; VisiblePosition previous = position.previous(); if (previous.isNotNull()) { - Range *previousCharacterRange = makeRange(previous, position).get(); - NSRect rect = frame->editor()->firstRectForRange(previousCharacterRange); - if (NSPointInRect(point, rect)) - return previousCharacterRange; + RefPtr<Range> previousCharacterRange = makeRange(previous, position); + IntRect rect = frame->editor()->firstRectForRange(previousCharacterRange.get()); + if (rect.contains(point)) + return previousCharacterRange.release(); } VisiblePosition next = position.next(); if (next.isNotNull()) { - Range *nextCharacterRange = makeRange(position, next).get(); - NSRect rect = frame->editor()->firstRectForRange(nextCharacterRange); - if (NSPointInRect(point, rect)) - return nextCharacterRange; + RefPtr<Range> nextCharacterRange = makeRange(position, next); + IntRect rect = frame->editor()->firstRectForRange(nextCharacterRange.get()); + if (rect.contains(point)) + return nextCharacterRange.release(); } - return NULL; + return 0; } void WebPage::characterIndexForPoint(IntPoint point, uint64_t& index) @@ -198,12 +199,12 @@ void WebPage::characterIndexForPoint(IntPoint point, uint64_t& index) HitTestResult result = frame->eventHandler()->hitTestResultAtPoint(point, false); frame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document()->frame() : m_page->focusController()->focusedOrMainFrame(); - Range *range = characterRangeAtPoint(frame, result.point()); + RefPtr<Range> range = characterRangeAtPoint(frame, result.point()); if (!range) return; uint64_t length; - getLocationAndLengthFromRange(range, index, length); + getLocationAndLengthFromRange(range.get(), index, length); } static PassRefPtr<Range> convertToRange(Frame* frame, NSRange nsrange) @@ -241,6 +242,84 @@ void WebPage::firstRectForCharacterRange(uint64_t location, uint64_t length, Web resultRect = frame->view()->contentsToWindow(rect); } +void WebPage::performDictionaryLookupAtLocation(const FloatPoint& floatPoint) +{ + Frame* frame = m_page->mainFrame(); + if (!frame) + return; + + // Find the frame the point is over. + IntPoint point = roundedIntPoint(floatPoint); + + HitTestResult result = frame->eventHandler()->hitTestResultAtPoint(point, false); + frame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document()->frame() : m_page->focusController()->focusedOrMainFrame(); + + // Figure out if there are any characters under the point. + RefPtr<Range> characterRange = characterRangeAtPoint(frame, frame->view()->windowToContents(point)); + if (!characterRange) + return; + + // Grab the currently selected text. + RefPtr<Range> selectedRange = m_page->focusController()->focusedOrMainFrame()->selection()->selection().toNormalizedRange(); + + // Use the selected text if the point was anywhere in it. Assertain this by seeing if either character range + // the mouse is over is contained by the selection range. + if (characterRange && selectedRange) { + ExceptionCode ec = 0; + selectedRange->compareBoundaryPoints(Range::START_TO_START, characterRange.get(), ec); + if (!ec) { + if (selectedRange->isPointInRange(characterRange->startContainer(), characterRange->startOffset(), ec)) { + if (!ec) + characterRange = selectedRange; + } + } + } + + if (!characterRange) + return; + + // Ensure we have whole words. + VisibleSelection selection(characterRange.get()); + selection.expandUsingGranularity(WordGranularity); + + RefPtr<Range> finalRange = selection.toNormalizedRange(); + if (!finalRange) + return; + + performDictionaryLookupForRange(DictionaryPopupInfo::HotKey, frame, finalRange.get()); +} + +void WebPage::performDictionaryLookupForRange(DictionaryPopupInfo::Type type, Frame* frame, Range* range) +{ + String rangeText = range->text(); + if (rangeText.stripWhiteSpace().isEmpty()) + return; + + RenderObject* renderer = range->startContainer()->renderer(); + RenderStyle* style = renderer->style(); + NSFont *font = style->font().primaryFont()->getNSFont(); + if (!font) + return; + + CFDictionaryRef fontDescriptorAttributes = (CFDictionaryRef)[[font fontDescriptor] fontAttributes]; + if (!fontDescriptorAttributes) + return; + + Vector<FloatQuad> quads; + range->textQuads(quads); + if (quads.isEmpty()) + return; + + IntRect rangeRect = frame->view()->contentsToWindow(quads[0].enclosingBoundingBox()); + + DictionaryPopupInfo dictionaryPopupInfo; + dictionaryPopupInfo.type = type; + dictionaryPopupInfo.origin = FloatPoint(rangeRect.x(), rangeRect.y()); + dictionaryPopupInfo.fontInfo.fontAttributeDictionary = fontDescriptorAttributes; + + send(Messages::WebPageProxy::DidPerformDictionaryLookup(rangeText, dictionaryPopupInfo)); +} + static inline void scroll(Page* page, ScrollDirection direction, ScrollGranularity granularity) { page->focusController()->focusedOrMainFrame()->eventHandler()->scrollRecursively(direction, granularity); @@ -349,6 +428,13 @@ void WebPage::registerUIProcessAccessibilityTokens(const CoreIPC::DataReference& #endif } +void WebPage::writeSelectionToPasteboard(const String& pasteboardName, const Vector<String>& pasteboardTypes, bool& result) +{ + Frame* frame = m_page->focusController()->focusedOrMainFrame(); + frame->editor()->writeSelectionToPasteboard(pasteboardName, pasteboardTypes); + result = true; +} + AccessibilityWebPageObject* WebPage::accessibilityRemoteObject() { return m_mockAccessibilityElement.get(); @@ -358,7 +444,13 @@ bool WebPage::platformHasLocalDataForURL(const WebCore::KURL& url) { NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setValue:(NSString*)userAgent() forHTTPHeaderField:@"User-Agent"]; - NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; + NSCachedURLResponse *cachedResponse; +#if USE(CFURLSTORAGESESSIONS) + if (CFURLStorageSessionRef storageSession = ResourceHandle::privateBrowsingStorageSession()) + cachedResponse = WKCachedResponseForRequest(storageSession, request); + else +#endif + cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; [request release]; return cachedResponse; @@ -368,7 +460,13 @@ String WebPage::cachedResponseMIMETypeForURL(const WebCore::KURL& url) { NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setValue:(NSString*)userAgent() forHTTPHeaderField:@"User-Agent"]; - NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; + NSCachedURLResponse *cachedResponse; +#if USE(CFURLSTORAGESESSIONS) + if (CFURLStorageSessionRef storageSession = ResourceHandle::privateBrowsingStorageSession()) + cachedResponse = WKCachedResponseForRequest(storageSession, request); + else +#endif + cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; [request release]; return [[cachedResponse response] MIMEType]; |