This source file includes following definitions.
- urlFromFrame
- isASingleWord
- selectMisspelledWord
- IsWhiteSpaceOrPunctuation
- selectMisspellingAsync
- showContextMenu
- clearContextMenu
- populateSubMenuItems
- populateCustomMenuItems
#include "config.h"
#include "ContextMenuClientImpl.h"
#include "CSSPropertyNames.h"
#include "HTMLNames.h"
#include "WebContextMenuData.h"
#include "WebDataSourceImpl.h"
#include "WebFormElement.h"
#include "WebFrameClient.h"
#include "WebFrameImpl.h"
#include "WebMenuItemInfo.h"
#include "WebPlugin.h"
#include "WebPluginContainerImpl.h"
#include "WebSearchableFormData.h"
#include "WebSpellCheckClient.h"
#include "WebViewClient.h"
#include "WebViewImpl.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/css/CSSStyleDeclaration.h"
#include "core/dom/Document.h"
#include "core/dom/DocumentMarkerController.h"
#include "core/editing/Editor.h"
#include "core/editing/SpellChecker.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLMediaElement.h"
#include "core/html/HTMLPlugInElement.h"
#include "core/html/MediaError.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/HistoryItem.h"
#include "core/page/ContextMenuController.h"
#include "core/page/EventHandler.h"
#include "core/frame/FrameView.h"
#include "core/page/Page.h"
#include "core/frame/Settings.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/RenderWidget.h"
#include "platform/ContextMenu.h"
#include "platform/Widget.h"
#include "platform/text/TextBreakIterator.h"
#include "platform/weborigin/KURL.h"
#include "public/platform/WebPoint.h"
#include "public/platform/WebString.h"
#include "public/platform/WebURL.h"
#include "public/platform/WebURLResponse.h"
#include "public/platform/WebVector.h"
#include "wtf/text/WTFString.h"
using namespace WebCore;
namespace blink {
static WebURL urlFromFrame(LocalFrame* frame)
{
if (frame) {
DocumentLoader* dl = frame->loader().documentLoader();
if (dl) {
WebDataSource* ds = WebDataSourceImpl::fromDocumentLoader(dl);
if (ds)
return ds->hasUnreachableURL() ? ds->unreachableURL() : ds->request().url();
}
}
return WebURL();
}
static bool isASingleWord(const String& text)
{
TextBreakIterator* it = wordBreakIterator(text, 0, text.length());
return it && it->next() == static_cast<int>(text.length());
}
static String selectMisspelledWord(LocalFrame* selectedFrame)
{
String misspelledWord = selectedFrame->selectedText().stripWhiteSpace();
if (!misspelledWord.isEmpty()) {
if (!isASingleWord(misspelledWord))
return String();
return misspelledWord;
}
HitTestResult hitTestResult = selectedFrame->eventHandler().
hitTestResultAtPoint(selectedFrame->page()->contextMenuController().hitTestResult().pointInInnerNodeFrame());
Node* innerNode = hitTestResult.innerNode();
VisiblePosition pos(innerNode->renderer()->positionForPoint(
hitTestResult.localPoint()));
if (pos.isNull())
return misspelledWord;
WebFrameImpl::selectWordAroundPosition(selectedFrame, pos);
misspelledWord = selectedFrame->selectedText().stripWhiteSpace();
#if OS(MACOSX)
if (misspelledWord.isEmpty())
selectedFrame->selection().setSelection(VisibleSelection(pos));
#else
selectedFrame->selection().setSelection(VisibleSelection(pos));
#endif
return misspelledWord;
}
static bool IsWhiteSpaceOrPunctuation(UChar c)
{
return isSpaceOrNewline(c) || WTF::Unicode::isPunct(c);
}
static String selectMisspellingAsync(LocalFrame* selectedFrame, DocumentMarker& marker)
{
VisibleSelection selection = selectedFrame->selection().selection();
if (!selection.isCaretOrRange())
return String();
RefPtrWillBeRawPtr<Range> selectionRange = selection.toNormalizedRange();
Vector<DocumentMarker*> markers = selectedFrame->document()->markers().markersInRange(selectionRange.get(), DocumentMarker::MisspellingMarkers());
if (markers.size() != 1)
return String();
marker = *markers[0];
RefPtrWillBeRawPtr<Range> markerRange = selectionRange->cloneRange(ASSERT_NO_EXCEPTION);
markerRange->setStart(markerRange->startContainer(), marker.startOffset());
markerRange->setEnd(markerRange->endContainer(), marker.endOffset());
if (markerRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation) != selectionRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation))
return String();
return markerRange->text();
}
void ContextMenuClientImpl::showContextMenu(const WebCore::ContextMenu* defaultMenu)
{
if (!m_webView->contextMenuAllowed())
return;
HitTestResult r = m_webView->page()->contextMenuController().hitTestResult();
LocalFrame* selectedFrame = r.innerNodeFrame();
WebContextMenuData data;
IntPoint mousePoint = selectedFrame->view()->contentsToWindow(r.roundedPointInInnerNodeFrame());
mousePoint.scale(m_webView->pageScaleFactor(), m_webView->pageScaleFactor());
data.mousePosition = mousePoint;
data.editFlags = WebContextMenuData::CanDoNone;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canUndo())
data.editFlags |= WebContextMenuData::CanUndo;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canRedo())
data.editFlags |= WebContextMenuData::CanRedo;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canCut())
data.editFlags |= WebContextMenuData::CanCut;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canCopy())
data.editFlags |= WebContextMenuData::CanCopy;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canPaste())
data.editFlags |= WebContextMenuData::CanPaste;
if (toLocalFrame(m_webView->focusedWebCoreFrame())->editor().canDelete())
data.editFlags |= WebContextMenuData::CanDelete;
data.editFlags |= WebContextMenuData::CanSelectAll;
data.editFlags |= WebContextMenuData::CanTranslate;
data.linkURL = r.absoluteLinkURL();
if (!r.absoluteImageURL().isEmpty()) {
data.srcURL = r.absoluteImageURL();
data.mediaType = WebContextMenuData::MediaTypeImage;
data.mediaFlags |= WebContextMenuData::MediaCanPrint;
} else if (!r.absoluteMediaURL().isEmpty()) {
data.srcURL = r.absoluteMediaURL();
HTMLMediaElement* mediaElement = toHTMLMediaElement(r.innerNonSharedNode());
if (isHTMLVideoElement(*mediaElement))
data.mediaType = WebContextMenuData::MediaTypeVideo;
else if (isHTMLAudioElement(*mediaElement))
data.mediaType = WebContextMenuData::MediaTypeAudio;
if (mediaElement->error())
data.mediaFlags |= WebContextMenuData::MediaInError;
if (mediaElement->paused())
data.mediaFlags |= WebContextMenuData::MediaPaused;
if (mediaElement->muted())
data.mediaFlags |= WebContextMenuData::MediaMuted;
if (mediaElement->loop())
data.mediaFlags |= WebContextMenuData::MediaLoop;
if (mediaElement->supportsSave())
data.mediaFlags |= WebContextMenuData::MediaCanSave;
if (mediaElement->hasAudio())
data.mediaFlags |= WebContextMenuData::MediaHasAudio;
if (mediaElement->hasVideo() && !mediaElement->isFullscreen())
data.mediaFlags |= WebContextMenuData::MediaCanToggleControls;
if (mediaElement->controls())
data.mediaFlags |= WebContextMenuData::MediaControls;
} else if (isHTMLObjectElement(*r.innerNonSharedNode()) || isHTMLEmbedElement(*r.innerNonSharedNode())) {
RenderObject* object = r.innerNonSharedNode()->renderer();
if (object && object->isWidget()) {
Widget* widget = toRenderWidget(object)->widget();
if (widget && widget->isPluginContainer()) {
data.mediaType = WebContextMenuData::MediaTypePlugin;
WebPluginContainerImpl* plugin = toWebPluginContainerImpl(widget);
WebString text = plugin->plugin()->selectionAsText();
if (!text.isEmpty()) {
data.selectedText = text;
data.editFlags |= WebContextMenuData::CanCopy;
}
data.editFlags &= ~WebContextMenuData::CanTranslate;
data.linkURL = plugin->plugin()->linkAtPosition(data.mousePosition);
if (plugin->plugin()->supportsPaginatedPrint())
data.mediaFlags |= WebContextMenuData::MediaCanPrint;
HTMLPlugInElement* pluginElement = toHTMLPlugInElement(r.innerNonSharedNode());
data.srcURL = pluginElement->document().completeURL(pluginElement->url());
data.mediaFlags |= WebContextMenuData::MediaCanSave;
if (plugin->plugin()->canRotateView())
data.mediaFlags |= WebContextMenuData::MediaCanRotate;
}
}
}
data.hasImageContents =
(data.mediaType == WebContextMenuData::MediaTypeImage)
&& r.image() && !(r.image()->isNull());
if (selectedFrame->document()->loader())
data.frameEncoding = selectedFrame->document()->encodingName();
data.pageURL = urlFromFrame(m_webView->mainFrameImpl()->frame());
if (selectedFrame != m_webView->mainFrameImpl()->frame()) {
data.frameURL = urlFromFrame(selectedFrame);
RefPtr<HistoryItem> historyItem = selectedFrame->loader().currentItem();
if (historyItem)
data.frameHistoryItem = WebHistoryItem(historyItem);
}
if (r.isSelected()) {
if (!isHTMLInputElement(*r.innerNonSharedNode()) || !toHTMLInputElement(r.innerNonSharedNode())->isPasswordField())
data.selectedText = selectedFrame->selectedText().stripWhiteSpace();
}
if (r.isContentEditable()) {
data.isEditable = true;
#if ENABLE(INPUT_SPEECH)
if (isHTMLInputElement(*r.innerNonSharedNode()))
data.isSpeechInputEnabled = toHTMLInputElement(r.innerNonSharedNode())->isSpeechEnabled();
#endif
if (selectedFrame->settings() && selectedFrame->settings()->asynchronousSpellCheckingEnabled()) {
DocumentMarker marker;
data.misspelledWord = selectMisspellingAsync(selectedFrame, marker);
data.misspellingHash = marker.hash();
if (marker.description().length()) {
Vector<String> suggestions;
marker.description().split('\n', suggestions);
data.dictionarySuggestions = suggestions;
} else if (m_webView->spellCheckClient()) {
int misspelledOffset, misspelledLength;
m_webView->spellCheckClient()->spellCheck(data.misspelledWord, misspelledOffset, misspelledLength, &data.dictionarySuggestions);
}
} else {
data.isSpellCheckingEnabled =
toLocalFrame(m_webView->focusedWebCoreFrame())->spellChecker().isContinuousSpellCheckingEnabled();
if (toLocalFrame(m_webView->focusedWebCoreFrame())->spellChecker().isSpellCheckingEnabledInFocusedNode()) {
data.misspelledWord = selectMisspelledWord(selectedFrame);
if (m_webView->spellCheckClient()) {
int misspelledOffset, misspelledLength;
m_webView->spellCheckClient()->spellCheck(
data.misspelledWord, misspelledOffset, misspelledLength,
&data.dictionarySuggestions);
if (!misspelledLength)
data.misspelledWord.reset();
}
}
}
HTMLFormElement* form = selectedFrame->selection().currentForm();
if (form && isHTMLInputElement(*r.innerNonSharedNode())) {
HTMLInputElement& selectedElement = toHTMLInputElement(*r.innerNonSharedNode());
WebSearchableFormData ws = WebSearchableFormData(WebFormElement(form), WebInputElement(&selectedElement));
if (ws.url().isValid())
data.keywordURL = ws.url();
}
}
if (selectedFrame->editor().selectionHasStyle(CSSPropertyDirection, "ltr") != FalseTriState)
data.writingDirectionLeftToRight |= WebContextMenuData::CheckableMenuItemChecked;
if (selectedFrame->editor().selectionHasStyle(CSSPropertyDirection, "rtl") != FalseTriState)
data.writingDirectionRightToLeft |= WebContextMenuData::CheckableMenuItemChecked;
DocumentLoader* dl = selectedFrame->loader().documentLoader();
WebDataSource* ds = WebDataSourceImpl::fromDocumentLoader(dl);
if (ds)
data.securityInfo = ds->response().securityInfo();
data.referrerPolicy = static_cast<WebReferrerPolicy>(selectedFrame->document()->referrerPolicy());
populateCustomMenuItems(defaultMenu, &data);
data.node = r.innerNonSharedNode();
WebFrameImpl* selectedWebFrame = WebFrameImpl::fromFrame(selectedFrame);
if (selectedWebFrame->client())
selectedWebFrame->client()->showContextMenu(data);
}
void ContextMenuClientImpl::clearContextMenu()
{
HitTestResult r = m_webView->page()->contextMenuController().hitTestResult();
LocalFrame* selectedFrame = r.innerNodeFrame();
if (!selectedFrame)
return;
WebFrameImpl* selectedWebFrame = WebFrameImpl::fromFrame(selectedFrame);
if (selectedWebFrame->client())
selectedWebFrame->client()->clearContextMenu();
}
static void populateSubMenuItems(const Vector<ContextMenuItem>& inputMenu, WebVector<WebMenuItemInfo>& subMenuItems)
{
Vector<WebMenuItemInfo> subItems;
for (size_t i = 0; i < inputMenu.size(); ++i) {
const ContextMenuItem* inputItem = &inputMenu.at(i);
if (inputItem->action() < ContextMenuItemBaseCustomTag || inputItem->action() > ContextMenuItemLastCustomTag)
continue;
WebMenuItemInfo outputItem;
outputItem.label = inputItem->title();
outputItem.enabled = inputItem->enabled();
outputItem.checked = inputItem->checked();
outputItem.action = static_cast<unsigned>(inputItem->action() - ContextMenuItemBaseCustomTag);
switch (inputItem->type()) {
case ActionType:
outputItem.type = WebMenuItemInfo::Option;
break;
case CheckableActionType:
outputItem.type = WebMenuItemInfo::CheckableOption;
break;
case SeparatorType:
outputItem.type = WebMenuItemInfo::Separator;
break;
case SubmenuType:
outputItem.type = WebMenuItemInfo::SubMenu;
populateSubMenuItems(inputItem->subMenuItems(), outputItem.subMenuItems);
break;
}
subItems.append(outputItem);
}
WebVector<WebMenuItemInfo> outputItems(subItems.size());
for (size_t i = 0; i < subItems.size(); ++i)
outputItems[i] = subItems[i];
subMenuItems.swap(outputItems);
}
void ContextMenuClientImpl::populateCustomMenuItems(const WebCore::ContextMenu* defaultMenu, WebContextMenuData* data)
{
populateSubMenuItems(defaultMenu->items(), data->customItems);
}
}