+static Frame* targetFrameForEditing(WebPage* page)
+{
+ Frame* frame = page->corePage()->focusController()->focusedOrMainFrame();
+ if (!frame)
+ return 0;
+
+ Editor* editor = frame->editor();
+ if (!editor->canEdit())
+ return 0;
+
+ if (editor->hasComposition()) {
+ // We should verify the parent node of this IME composition node are
+ // editable because JavaScript may delete a parent node of the composition
+ // node. In this case, WebKit crashes while deleting texts from the parent
+ // node, which doesn't exist any longer.
+ if (PassRefPtr<Range> range = editor->compositionRange()) {
+ Node* node = range->startContainer();
+ if (!node || !node->isContentEditable())
+ return 0;
+ }
+ }
+
+ return frame;
+}
+
+void WebPage::confirmComposition(const String& compositionString)
+{
+ Frame* targetFrame = targetFrameForEditing(this);
+ if (!targetFrame)
+ return;
+
+#if ENABLE(TIZEN_ISF_PORT)
+ if (m_prepareKeyDownEvent) {
+ m_keyPressCommands.append(adoptPtr(new ConfirmCompositionKeyPressCommand(compositionString)));
+ return;
+ }
+#endif
+
+ targetFrame->editor()->confirmComposition(compositionString);
+
+#if ENABLE(TIZEN_ISF_PORT)
+ m_page->editorClient()->respondToChangedSelection(targetFrame);
+#endif
+}
+
+void WebPage::setComposition(const String& compositionString, const Vector<WebCore::CompositionUnderline>& underlines, uint64_t cursorPosition)
+{
+ Frame* targetFrame = targetFrameForEditing(this);
+ if (!targetFrame)
+ return;
+
+#if ENABLE(TIZEN_ISF_PORT)
+ if (!targetFrame->editor()->hasComposition() && compositionString.isEmpty())
+ return;
+
+ if (m_prepareKeyDownEvent) {
+ m_keyPressCommands.append(adoptPtr(new SetCompositionKeyPressCommand(compositionString, underlines, cursorPosition)));
+ return;
+ }
+
+ if (targetFrame->selection()->rootEditableElement()) {
+ HTMLTextFormControlElement* textFormControl = toTextFormControl(targetFrame->selection()->rootEditableElement()->shadowAncestorNode());
+ if (textFormControl && textFormControl->maxLength() >= 0) {
+ unsigned availableLength = textFormControl->maxLength() - textFormControl->value().length();
+ if (targetFrame->editor()->hasComposition())
+ availableLength += (targetFrame->editor()->compositionEnd() - targetFrame->editor()->compositionStart());
+ if (!availableLength)
+ return;
+
+ if (availableLength < compositionString.length()) {
+ String newCompositionString = compositionString.substring(0, availableLength);
+ Vector<CompositionUnderline> newUnderlines;
+ size_t numUnderlines = underlines.size();
+ for (size_t index = 0; index < numUnderlines; ++index) {
+ if (underlines[index].startOffset < availableLength) {
+ newUnderlines.append(underlines[index]);
+ if (newUnderlines.last().endOffset > availableLength)
+ newUnderlines.last().endOffset = availableLength;
+ }
+ }
+ targetFrame->editor()->setComposition(newCompositionString, newUnderlines, cursorPosition, 0);
+ return;
+ }
+ }
+ }
+#endif
+
+ targetFrame->editor()->setComposition(compositionString, underlines, cursorPosition, 0);
+
+#if ENABLE(TIZEN_ISF_PORT)
+ m_page->editorClient()->respondToChangedSelection(targetFrame);
+#endif
+}
+
+void WebPage::cancelComposition()
+{
+ Frame* frame = m_page->focusController()->focusedOrMainFrame();
+ if (!frame)
+ return;
+
+ frame->editor()->cancelComposition();
+}
+