0410c681a10b734536a6487856989e7750026508
[WebKit-https.git] / Source / WebCore / testing / Internals.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "Internals.h"
29
30 #include "AnimationController.h"
31 #include "BackForwardController.h"
32 #include "CachedResourceLoader.h"
33 #include "Chrome.h"
34 #include "ChromeClient.h"
35 #include "ClientRect.h"
36 #include "ClientRectList.h"
37 #include "ComposedShadowTreeWalker.h"
38 #include "ContentDistributor.h"
39 #include "Cursor.h"
40 #include "DOMStringList.h"
41 #include "DOMWindow.h"
42 #include "Document.h"
43 #include "DocumentMarker.h"
44 #include "DocumentMarkerController.h"
45 #include "Editor.h"
46 #include "Element.h"
47 #include "ElementShadow.h"
48 #include "EventHandler.h"
49 #include "ExceptionCode.h"
50 #include "FormController.h"
51 #include "Frame.h"
52 #include "FrameLoader.h"
53 #include "FrameView.h"
54 #include "HTMLContentElement.h"
55 #include "HTMLInputElement.h"
56 #include "HTMLNames.h"
57 #include "HTMLSelectElement.h"
58 #include "HTMLTextAreaElement.h"
59 #include "HistoryController.h"
60 #include "HistoryItem.h"
61 #include "InspectorClient.h"
62 #include "InspectorConsoleAgent.h"
63 #include "InspectorController.h"
64 #include "InspectorCounters.h"
65 #include "InspectorFrontendChannel.h"
66 #include "InspectorFrontendClientLocal.h"
67 #include "InspectorInstrumentation.h"
68 #include "InspectorOverlay.h"
69 #include "InstrumentingAgents.h"
70 #include "InternalSettings.h"
71 #include "IntRect.h"
72 #include "Language.h"
73 #include "MallocStatistics.h"
74 #include "MemoryCache.h"
75 #include "MockPagePopupDriver.h"
76 #include "NodeRenderingContext.h"
77 #include "Page.h"
78 #include "PrintContext.h"
79 #include "PseudoElement.h"
80 #include "Range.h"
81 #include "RenderMenuList.h"
82 #include "RenderObject.h"
83 #include "RenderTreeAsText.h"
84 #include "RenderView.h"
85 #include "RuntimeEnabledFeatures.h"
86 #include "SchemeRegistry.h"
87 #include "ScrollingCoordinator.h"
88 #include "SerializedScriptValue.h"
89 #include "Settings.h"
90 #include "ShadowRoot.h"
91 #include "SpellChecker.h"
92 #include "StaticNodeList.h"
93 #include "StyleSheetContents.h"
94 #include "TextIterator.h"
95 #include "TreeScope.h"
96 #include "TypeConversions.h"
97 #include "ViewportArguments.h"
98 #include "WorkerThread.h"
99 #include <wtf/text/CString.h>
100 #include <wtf/text/StringBuffer.h>
101
102 #if ENABLE(INPUT_TYPE_COLOR)
103 #include "ColorChooser.h"
104 #endif
105
106 #if ENABLE(BATTERY_STATUS)
107 #include "BatteryController.h"
108 #endif
109
110 #if ENABLE(NETWORK_INFO)
111 #include "NetworkInfo.h"
112 #include "NetworkInfoController.h"
113 #endif
114
115 #if ENABLE(PROXIMITY_EVENTS)
116 #include "DeviceProximityController.h"
117 #endif
118
119 #if ENABLE(PAGE_POPUP)
120 #include "PagePopupController.h"
121 #endif
122
123 #if ENABLE(TOUCH_ADJUSTMENT)
124 #include "WebKitPoint.h"
125 #endif
126
127 #if ENABLE(MOUSE_CURSOR_SCALE)
128 #include <wtf/dtoa.h>
129 #endif
130
131 #if ENABLE(ENCRYPTED_MEDIA_V2)
132 #include "CDM.h"
133 #include "MockCDM.h"
134 #endif
135
136 #if ENABLE(VIDEO_TRACK)
137 #include "CaptionUserPreferences.h"
138 #include "PageGroup.h"
139 #endif
140
141 #if ENABLE(VIDEO)
142 #include "HTMLMediaElement.h"
143 #include "TimeRanges.h"
144 #endif
145
146 #if ENABLE(SPEECH_SYNTHESIS)
147 #include "DOMWindowSpeechSynthesis.h"
148 #include "PlatformSpeechSynthesizerMock.h"
149 #include "SpeechSynthesis.h"
150 #endif
151
152 namespace WebCore {
153
154 #if ENABLE(PAGE_POPUP)
155 static MockPagePopupDriver* s_pagePopupDriver = 0;
156 #endif
157
158 using namespace HTMLNames;
159
160 #if ENABLE(INSPECTOR)
161 class InspectorFrontendClientDummy : public InspectorFrontendClientLocal {
162 public:
163     InspectorFrontendClientDummy(InspectorController*, Page*);
164     virtual ~InspectorFrontendClientDummy() { }
165     virtual void attachWindow(DockSide) OVERRIDE { }
166     virtual void detachWindow() OVERRIDE { }
167
168     virtual String localizedStringsURL() OVERRIDE { return String(); }
169
170     virtual void bringToFront() OVERRIDE { }
171     virtual void closeWindow() OVERRIDE { }
172
173     virtual void inspectedURLChanged(const String&) OVERRIDE { }
174
175 protected:
176     virtual void setAttachedWindowHeight(unsigned) OVERRIDE { }
177     virtual void setAttachedWindowWidth(unsigned) OVERRIDE { }
178     virtual void setToolbarHeight(unsigned) OVERRIDE { }
179 };
180
181 InspectorFrontendClientDummy::InspectorFrontendClientDummy(InspectorController* controller, Page* page)
182     : InspectorFrontendClientLocal(controller, page, adoptPtr(new InspectorFrontendClientLocal::Settings()))
183 {
184 }
185
186 class InspectorFrontendChannelDummy : public InspectorFrontendChannel {
187 public:
188     explicit InspectorFrontendChannelDummy(Page*);
189     virtual ~InspectorFrontendChannelDummy() { }
190     virtual bool sendMessageToFrontend(const String& message) OVERRIDE;
191
192 private:
193     Page* m_frontendPage;
194 };
195
196 InspectorFrontendChannelDummy::InspectorFrontendChannelDummy(Page* page)
197     : m_frontendPage(page)
198 {
199 }
200
201 bool InspectorFrontendChannelDummy::sendMessageToFrontend(const String& message)
202 {
203     return InspectorClient::doDispatchMessageOnFrontendPage(m_frontendPage, message);
204 }
205 #endif // ENABLE(INSPECTOR)
206
207 static bool markerTypesFrom(const String& markerType, DocumentMarker::MarkerTypes& result)
208 {
209     if (markerType.isEmpty() || equalIgnoringCase(markerType, "all"))
210         result = DocumentMarker::AllMarkers();
211     else if (equalIgnoringCase(markerType, "Spelling"))
212         result =  DocumentMarker::Spelling;
213     else if (equalIgnoringCase(markerType, "Grammar"))
214         result =  DocumentMarker::Grammar;
215     else if (equalIgnoringCase(markerType, "TextMatch"))
216         result =  DocumentMarker::TextMatch;
217     else if (equalIgnoringCase(markerType, "Replacement"))
218         result =  DocumentMarker::Replacement;
219     else if (equalIgnoringCase(markerType, "CorrectionIndicator"))
220         result =  DocumentMarker::CorrectionIndicator;
221     else if (equalIgnoringCase(markerType, "RejectedCorrection"))
222         result =  DocumentMarker::RejectedCorrection;
223     else if (equalIgnoringCase(markerType, "Autocorrected"))
224         result =  DocumentMarker::Autocorrected;
225     else if (equalIgnoringCase(markerType, "SpellCheckingExemption"))
226         result =  DocumentMarker::SpellCheckingExemption;
227     else if (equalIgnoringCase(markerType, "DeletedAutocorrection"))
228         result =  DocumentMarker::DeletedAutocorrection;
229     else if (equalIgnoringCase(markerType, "DictationAlternatives"))
230         result =  DocumentMarker::DictationAlternatives;
231     else
232         return false;
233
234     return true;
235 }
236
237 static SpellChecker* spellchecker(Document* document)
238 {
239     if (!document || !document->frame())
240         return 0;
241
242     return document->frame()->editor().spellChecker();
243 }
244
245 const char* Internals::internalsId = "internals";
246
247 PassRefPtr<Internals> Internals::create(Document* document)
248 {
249     return adoptRef(new Internals(document));
250 }
251
252 Internals::~Internals()
253 {
254 }
255
256 void Internals::resetToConsistentState(Page* page)
257 {
258     ASSERT(page);
259
260     page->setPageScaleFactor(1, IntPoint(0, 0));
261     page->setPagination(Pagination());
262
263 #if USE(ACCELERATED_COMPOSITING)
264     FrameView* mainFrameView = page->mainFrame()->view();
265     if (mainFrameView) {
266         mainFrameView->setHeaderHeight(0);
267         mainFrameView->setFooterHeight(0);
268     }
269 #endif
270     TextRun::setAllowsRoundingHacks(false);
271     WebCore::overrideUserPreferredLanguages(Vector<String>());
272     WebCore::Settings::setUsesOverlayScrollbars(false);
273 #if ENABLE(PAGE_POPUP)
274     delete s_pagePopupDriver;
275     s_pagePopupDriver = 0;
276     page->chrome().client()->resetPagePopupDriver();
277 #endif
278 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
279     if (page->inspectorController())
280         page->inspectorController()->setProfilerEnabled(false);
281 #endif
282 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
283     page->group().captionPreferences()->setCaptionsStyleSheetOverride(emptyString());
284     page->group().captionPreferences()->setTestingMode(false);
285 #endif
286     if (!page->mainFrame()->editor().isContinuousSpellCheckingEnabled())
287         page->mainFrame()->editor().toggleContinuousSpellChecking();
288
289 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
290     if (page->mainFrame()->editor().isAutomaticQuoteSubstitutionEnabled())
291         page->mainFrame()->editor().toggleAutomaticQuoteSubstitution();
292     if (page->mainFrame()->editor().isAutomaticLinkDetectionEnabled())
293         page->mainFrame()->editor().toggleAutomaticLinkDetection();
294     if (page->mainFrame()->editor().isAutomaticDashSubstitutionEnabled())
295         page->mainFrame()->editor().toggleAutomaticDashSubstitution();
296     if (page->mainFrame()->editor().isAutomaticTextReplacementEnabled())
297         page->mainFrame()->editor().toggleAutomaticTextReplacement();
298     if (!page->mainFrame()->editor().isAutomaticSpellingCorrectionEnabled())
299         page->mainFrame()->editor().toggleAutomaticSpellingCorrection();
300 #endif
301
302     if (page->mainFrame()->editor().isOverwriteModeEnabled())
303         page->mainFrame()->editor().toggleOverwriteModeEnabled();
304 }
305
306 Internals::Internals(Document* document)
307     : ContextDestructionObserver(document)
308 {
309 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
310     if (document && document->page())
311         document->page()->group().captionPreferences()->setTestingMode(true);
312 #endif
313 }
314
315 Document* Internals::contextDocument() const
316 {
317     return toDocument(scriptExecutionContext());
318 }
319
320 Frame* Internals::frame() const
321 {
322     if (!contextDocument())
323         return 0;
324     return contextDocument()->frame();
325 }
326
327 InternalSettings* Internals::settings() const
328 {
329     Document* document = contextDocument();
330     if (!document)
331         return 0;
332     Page* page = document->page();
333     if (!page)
334         return 0;
335     return InternalSettings::from(page);
336 }
337
338 unsigned Internals::workerThreadCount() const
339 {
340 #if ENABLE(WORKERS)
341     return WorkerThread::workerThreadCount();
342 #else
343     return 0;
344 #endif
345 }
346
347 String Internals::address(Node* node)
348 {
349     char buf[32];
350     sprintf(buf, "%p", node);
351
352     return String(buf);
353 }
354
355 bool Internals::isPreloaded(const String& url)
356 {
357     Document* document = contextDocument();
358     return document->cachedResourceLoader()->isPreloaded(url);
359 }
360
361 bool Internals::isLoadingFromMemoryCache(const String& url)
362 {
363     if (!contextDocument())
364         return false;
365     CachedResource* resource = memoryCache()->resourceForURL(contextDocument()->completeURL(url));
366     return resource && resource->status() == CachedResource::Cached;
367 }
368
369 PassRefPtr<Element> Internals::createContentElement(ExceptionCode& ec)
370 {
371     Document* document = contextDocument();
372     if (!document) {
373         ec = INVALID_ACCESS_ERR;
374         return 0;
375     }
376
377 #if ENABLE(SHADOW_DOM)
378     return HTMLContentElement::create(document);
379 #else
380     return 0;
381 #endif
382 }
383
384 bool Internals::isValidContentSelect(Element* insertionPoint, ExceptionCode& ec)
385 {
386     if (!insertionPoint || !insertionPoint->isInsertionPoint()) {
387         ec = INVALID_ACCESS_ERR;
388         return false;
389     }
390
391 #if ENABLE(SHADOW_DOM)
392     return isHTMLContentElement(insertionPoint) && toHTMLContentElement(insertionPoint)->isSelectValid();
393 #else
394     return false;
395 #endif
396 }
397
398 Node* Internals::treeScopeRootNode(Node* node, ExceptionCode& ec)
399 {
400     if (!node) {
401         ec = INVALID_ACCESS_ERR;
402         return 0;
403     }
404
405     return node->treeScope()->rootNode();
406 }
407
408 Node* Internals::parentTreeScope(Node* node, ExceptionCode& ec)
409 {
410     if (!node) {
411         ec = INVALID_ACCESS_ERR;
412         return 0;
413     }
414     const TreeScope* parentTreeScope = node->treeScope()->parentTreeScope();
415     return parentTreeScope ? parentTreeScope->rootNode() : 0;
416 }
417
418 unsigned Internals::numberOfActiveAnimations() const
419 {
420     Frame* contextFrame = frame();
421     if (AnimationController* controller = contextFrame->animation())
422         return controller->numberOfActiveAnimations(contextFrame->document());
423     return 0;
424 }
425
426 bool Internals::animationsAreSuspended(Document* document, ExceptionCode& ec) const
427 {
428     if (!document || !document->frame()) {
429         ec = INVALID_ACCESS_ERR;
430         return false;
431     }
432
433     AnimationController* controller = document->frame()->animation();
434     if (!controller)
435         return false;
436
437     return controller->isSuspended();
438 }
439
440 void Internals::suspendAnimations(Document* document, ExceptionCode& ec) const
441 {
442     if (!document || !document->frame()) {
443         ec = INVALID_ACCESS_ERR;
444         return;
445     }
446
447     AnimationController* controller = document->frame()->animation();
448     if (!controller)
449         return;
450
451     controller->suspendAnimations();
452 }
453
454 void Internals::resumeAnimations(Document* document, ExceptionCode& ec) const
455 {
456     if (!document || !document->frame()) {
457         ec = INVALID_ACCESS_ERR;
458         return;
459     }
460
461     AnimationController* controller = document->frame()->animation();
462     if (!controller)
463         return;
464
465     controller->resumeAnimations();
466 }
467
468 bool Internals::pauseAnimationAtTimeOnElement(const String& animationName, double pauseTime, Element* element, ExceptionCode& ec)
469 {
470     if (!element || pauseTime < 0) {
471         ec = INVALID_ACCESS_ERR;
472         return false;
473     }
474     AnimationController* controller = frame()->animation();
475     return controller->pauseAnimationAtTime(element->renderer(), AtomicString(animationName), pauseTime);
476 }
477
478 bool Internals::pauseAnimationAtTimeOnPseudoElement(const String& animationName, double pauseTime, Element* element, const String& pseudoId, ExceptionCode& ec)
479 {
480     if (!element || pauseTime < 0) {
481         ec = INVALID_ACCESS_ERR;
482         return false;
483     }
484
485     if (pseudoId != "before" && pseudoId != "after") {
486         ec = INVALID_ACCESS_ERR;
487         return false;
488     }
489
490     PseudoElement* pseudoElement = element->pseudoElement(pseudoId == "before" ? BEFORE : AFTER);
491     if (!pseudoElement) {
492         ec = INVALID_ACCESS_ERR;
493         return false;
494     }
495
496     return frame()->animation()->pauseAnimationAtTime(pseudoElement->renderer(), AtomicString(animationName), pauseTime);
497 }
498
499 bool Internals::pauseTransitionAtTimeOnElement(const String& propertyName, double pauseTime, Element* element, ExceptionCode& ec)
500 {
501     if (!element || pauseTime < 0) {
502         ec = INVALID_ACCESS_ERR;
503         return false;
504     }
505     AnimationController* controller = frame()->animation();
506     return controller->pauseTransitionAtTime(element->renderer(), propertyName, pauseTime);
507 }
508
509 bool Internals::pauseTransitionAtTimeOnPseudoElement(const String& property, double pauseTime, Element* element, const String& pseudoId, ExceptionCode& ec)
510 {
511     if (!element || pauseTime < 0) {
512         ec = INVALID_ACCESS_ERR;
513         return false;
514     }
515
516     if (pseudoId != "before" && pseudoId != "after") {
517         ec = INVALID_ACCESS_ERR;
518         return false;
519     }
520
521     PseudoElement* pseudoElement = element->pseudoElement(pseudoId == "before" ? BEFORE : AFTER);
522     if (!pseudoElement) {
523         ec = INVALID_ACCESS_ERR;
524         return false;
525     }
526
527     return frame()->animation()->pauseTransitionAtTime(pseudoElement->renderer(), property, pauseTime);
528 }
529
530 bool Internals::attached(Node* node, ExceptionCode& ec)
531 {
532     if (!node) {
533         ec = INVALID_ACCESS_ERR;
534         return false;
535     }
536
537     return node->attached();
538 }
539
540 Node* Internals::nextSiblingByWalker(Node* node, ExceptionCode& ec)
541 {
542     if (!node) {
543         ec = INVALID_ACCESS_ERR;
544         return 0;
545     }
546     ComposedShadowTreeWalker walker(node);
547     walker.nextSibling();
548     return walker.get();
549 }
550
551 Node* Internals::firstChildByWalker(Node* node, ExceptionCode& ec)
552 {
553     if (!node) {
554         ec = INVALID_ACCESS_ERR;
555         return 0;
556     }
557     ComposedShadowTreeWalker walker(node);
558     walker.firstChild();
559     return walker.get();
560 }
561
562 Node* Internals::lastChildByWalker(Node* node, ExceptionCode& ec)
563 {
564     if (!node) {
565         ec = INVALID_ACCESS_ERR;
566         return 0;
567     }
568     ComposedShadowTreeWalker walker(node);
569     walker.lastChild();
570     return walker.get();
571 }
572
573 Node* Internals::nextNodeByWalker(Node* node, ExceptionCode& ec)
574 {
575     if (!node) {
576         ec = INVALID_ACCESS_ERR;
577         return 0;
578     }
579     ComposedShadowTreeWalker walker(node);
580     walker.next();
581     return walker.get();
582 }
583
584 Node* Internals::previousNodeByWalker(Node* node, ExceptionCode& ec)
585 {
586     if (!node) {
587         ec = INVALID_ACCESS_ERR;
588         return 0;
589     }
590     ComposedShadowTreeWalker walker(node);
591     walker.previous();
592     return walker.get();
593 }
594
595 String Internals::elementRenderTreeAsText(Element* element, ExceptionCode& ec)
596 {
597     if (!element) {
598         ec = INVALID_ACCESS_ERR;
599         return String();
600     }
601
602     String representation = externalRepresentation(element);
603     if (representation.isEmpty()) {
604         ec = INVALID_ACCESS_ERR;
605         return String();
606     }
607
608     return representation;
609 }
610
611 size_t Internals::numberOfScopedHTMLStyleChildren(const Node* scope, ExceptionCode& ec) const
612 {
613     if (scope && (scope->isElementNode() || scope->isShadowRoot()))
614 #if ENABLE(STYLE_SCOPED)
615         return scope->numberOfScopedHTMLStyleChildren();
616 #else
617         return 0;
618 #endif
619
620     ec = INVALID_ACCESS_ERR;
621     return 0;
622 }
623
624 PassRefPtr<CSSComputedStyleDeclaration> Internals::computedStyleIncludingVisitedInfo(Node* node, ExceptionCode& ec) const
625 {
626     if (!node) {
627         ec = INVALID_ACCESS_ERR;
628         return 0;
629     }
630
631     bool allowVisitedStyle = true;
632     return CSSComputedStyleDeclaration::create(node, allowVisitedStyle);
633 }
634
635 Internals::ShadowRootIfShadowDOMEnabledOrNode* Internals::ensureShadowRoot(Element* host, ExceptionCode& ec)
636 {
637     if (!host) {
638         ec = INVALID_ACCESS_ERR;
639         return 0;
640     }
641
642     if (ElementShadow* shadow = host->shadow())
643         return shadow->shadowRoot();
644
645     return host->createShadowRoot(ec).get();
646 }
647
648 Internals::ShadowRootIfShadowDOMEnabledOrNode* Internals::createShadowRoot(Element* host, ExceptionCode& ec)
649 {
650     if (!host) {
651         ec = INVALID_ACCESS_ERR;
652         return 0;
653     }
654     return host->createShadowRoot(ec).get();
655 }
656
657 Internals::ShadowRootIfShadowDOMEnabledOrNode* Internals::shadowRoot(Element* host, ExceptionCode& ec)
658 {
659     if (!host) {
660         ec = INVALID_ACCESS_ERR;
661         return 0;
662     }
663     if (ElementShadow* shadow = host->shadow())
664         return shadow->shadowRoot();
665     return 0;
666 }
667
668 String Internals::shadowRootType(const Node* root, ExceptionCode& ec) const
669 {
670     if (!root || !root->isShadowRoot()) {
671         ec = INVALID_ACCESS_ERR;
672         return String();
673     }
674
675     switch (toShadowRoot(root)->type()) {
676     case ShadowRoot::UserAgentShadowRoot:
677         return String("UserAgentShadowRoot");
678     case ShadowRoot::AuthorShadowRoot:
679         return String("AuthorShadowRoot");
680     default:
681         ASSERT_NOT_REACHED();
682         return String("Unknown");
683     }
684 }
685
686 Element* Internals::includerFor(Node* node, ExceptionCode& ec)
687 {
688     if (!node) {
689         ec = INVALID_ACCESS_ERR;
690         return 0;
691     }
692
693     return NodeRenderingContext(node).insertionPoint();
694 }
695
696 String Internals::shadowPseudoId(Element* element, ExceptionCode& ec)
697 {
698     if (!element) {
699         ec = INVALID_ACCESS_ERR;
700         return String();
701     }
702
703     return element->shadowPseudoId().string();
704 }
705
706 void Internals::setShadowPseudoId(Element* element, const String& id, ExceptionCode& ec)
707 {
708     if (!element) {
709         ec = INVALID_ACCESS_ERR;
710         return;
711     }
712
713     return element->setPseudo(id);
714 }
715
716 String Internals::visiblePlaceholder(Element* element)
717 {
718     if (element && isHTMLTextFormControlElement(element)) {
719         if (toHTMLTextFormControlElement(element)->placeholderShouldBeVisible())
720             return toHTMLTextFormControlElement(element)->placeholderElement()->textContent();
721     }
722
723     return String();
724 }
725
726 #if ENABLE(INPUT_TYPE_COLOR)
727 void Internals::selectColorInColorChooser(Element* element, const String& colorValue)
728 {
729     if (!element->hasTagName(inputTag))
730         return;
731     HTMLInputElement* inputElement = element->toInputElement();
732     if (!inputElement)
733         return;
734     inputElement->selectColorInColorChooser(Color(colorValue));
735 }
736 #endif
737
738 Vector<String> Internals::formControlStateOfPreviousHistoryItem(ExceptionCode& ec)
739 {
740     HistoryItem* mainItem = frame()->loader()->history()->previousItem();
741     if (!mainItem) {
742         ec = INVALID_ACCESS_ERR;
743         return Vector<String>();
744     }
745     String uniqueName = frame()->tree()->uniqueName();
746     if (mainItem->target() != uniqueName && !mainItem->childItemWithTarget(uniqueName)) {
747         ec = INVALID_ACCESS_ERR;
748         return Vector<String>();
749     }
750     return mainItem->target() == uniqueName ? mainItem->documentState() : mainItem->childItemWithTarget(uniqueName)->documentState();
751 }
752
753 void Internals::setFormControlStateOfPreviousHistoryItem(const Vector<String>& state, ExceptionCode& ec)
754 {
755     HistoryItem* mainItem = frame()->loader()->history()->previousItem();
756     if (!mainItem) {
757         ec = INVALID_ACCESS_ERR;
758         return;
759     }
760     String uniqueName = frame()->tree()->uniqueName();
761     if (mainItem->target() == uniqueName)
762         mainItem->setDocumentState(state);
763     else if (HistoryItem* subItem = mainItem->childItemWithTarget(uniqueName))
764         subItem->setDocumentState(state);
765     else
766         ec = INVALID_ACCESS_ERR;
767 }
768
769 #if ENABLE(SPEECH_SYNTHESIS)
770 void Internals::enableMockSpeechSynthesizer()
771 {
772     Document* document = contextDocument();
773     if (!document || !document->domWindow())
774         return;
775     SpeechSynthesis* synthesis = DOMWindowSpeechSynthesis::speechSynthesis(document->domWindow());
776     if (!synthesis)
777         return;
778     
779     synthesis->setPlatformSynthesizer(PlatformSpeechSynthesizerMock::create(synthesis));
780 }
781 #endif
782     
783 void Internals::setEnableMockPagePopup(bool enabled, ExceptionCode& ec)
784 {
785 #if ENABLE(PAGE_POPUP)
786     Document* document = contextDocument();
787     if (!document || !document->page())
788         return;
789     Page* page = document->page();
790     if (!enabled) {
791         page->chrome().client()->resetPagePopupDriver();
792         return;
793     }
794     if (!s_pagePopupDriver)
795         s_pagePopupDriver = MockPagePopupDriver::create(page->mainFrame()).leakPtr();
796     page->chrome().client()->setPagePopupDriver(s_pagePopupDriver);
797 #else
798     UNUSED_PARAM(enabled);
799     UNUSED_PARAM(ec);
800 #endif
801 }
802
803 #if ENABLE(PAGE_POPUP)
804 PassRefPtr<PagePopupController> Internals::pagePopupController()
805 {
806     return s_pagePopupDriver ? s_pagePopupDriver->pagePopupController() : 0;
807 }
808 #endif
809
810 PassRefPtr<ClientRect> Internals::absoluteCaretBounds(ExceptionCode& ec)
811 {
812     Document* document = contextDocument();
813     if (!document || !document->frame() || !document->frame()->selection()) {
814         ec = INVALID_ACCESS_ERR;
815         return ClientRect::create();
816     }
817
818     return ClientRect::create(document->frame()->selection()->absoluteCaretBounds());
819 }
820
821 PassRefPtr<ClientRect> Internals::boundingBox(Element* element, ExceptionCode& ec)
822 {
823     if (!element) {
824         ec = INVALID_ACCESS_ERR;
825         return ClientRect::create();
826     }
827
828     element->document()->updateLayoutIgnorePendingStylesheets();
829     RenderObject* renderer = element->renderer();
830     if (!renderer)
831         return ClientRect::create();
832     return ClientRect::create(renderer->absoluteBoundingBoxRectIgnoringTransforms());
833 }
834
835 PassRefPtr<ClientRectList> Internals::inspectorHighlightRects(Document* document, ExceptionCode& ec)
836 {
837 #if ENABLE(INSPECTOR)
838     if (!document || !document->page() || !document->page()->inspectorController()) {
839         ec = INVALID_ACCESS_ERR;
840         return ClientRectList::create();
841     }
842
843     Highlight highlight;
844     document->page()->inspectorController()->getHighlight(&highlight);
845     return ClientRectList::create(highlight.quads);
846 #else
847     UNUSED_PARAM(document);
848     UNUSED_PARAM(ec);
849     return ClientRectList::create();
850 #endif
851 }
852
853 unsigned Internals::markerCountForNode(Node* node, const String& markerType, ExceptionCode& ec)
854 {
855     if (!node) {
856         ec = INVALID_ACCESS_ERR;
857         return 0;
858     }
859
860     DocumentMarker::MarkerTypes markerTypes = 0;
861     if (!markerTypesFrom(markerType, markerTypes)) {
862         ec = SYNTAX_ERR;
863         return 0;
864     }
865
866     return node->document()->markers()->markersFor(node, markerTypes).size();
867 }
868
869 DocumentMarker* Internals::markerAt(Node* node, const String& markerType, unsigned index, ExceptionCode& ec)
870 {
871     if (!node) {
872         ec = INVALID_ACCESS_ERR;
873         return 0;
874     }
875
876     DocumentMarker::MarkerTypes markerTypes = 0;
877     if (!markerTypesFrom(markerType, markerTypes)) {
878         ec = SYNTAX_ERR;
879         return 0;
880     }
881
882     Vector<DocumentMarker*> markers = node->document()->markers()->markersFor(node, markerTypes);
883     if (markers.size() <= index)
884         return 0;
885     return markers[index];
886 }
887
888 PassRefPtr<Range> Internals::markerRangeForNode(Node* node, const String& markerType, unsigned index, ExceptionCode& ec)
889 {
890     DocumentMarker* marker = markerAt(node, markerType, index, ec);
891     if (!marker)
892         return 0;
893     return Range::create(node->document(), node, marker->startOffset(), node, marker->endOffset());
894 }
895
896 String Internals::markerDescriptionForNode(Node* node, const String& markerType, unsigned index, ExceptionCode& ec)
897 {
898     DocumentMarker* marker = markerAt(node, markerType, index, ec);
899     if (!marker)
900         return String();
901     return marker->description();
902 }
903
904 void Internals::addTextMatchMarker(const Range* range, bool isActive)
905 {
906     range->ownerDocument()->updateLayoutIgnorePendingStylesheets();
907     range->ownerDocument()->markers()->addTextMatchMarker(range, isActive);
908 }
909
910 void Internals::setScrollViewPosition(Document* document, long x, long y, ExceptionCode& ec)
911 {
912     if (!document || !document->view()) {
913         ec = INVALID_ACCESS_ERR;
914         return;
915     }
916
917     FrameView* frameView = document->view();
918     bool constrainsScrollingToContentEdgeOldValue = frameView->constrainsScrollingToContentEdge();
919     bool scrollbarsSuppressedOldValue = frameView->scrollbarsSuppressed();
920
921     frameView->setConstrainsScrollingToContentEdge(false);
922     frameView->setScrollbarsSuppressed(false);
923     frameView->setScrollOffsetFromInternals(IntPoint(x, y));
924     frameView->setScrollbarsSuppressed(scrollbarsSuppressedOldValue);
925     frameView->setConstrainsScrollingToContentEdge(constrainsScrollingToContentEdgeOldValue);
926 }
927
928 void Internals::setPagination(Document* document, const String& mode, int gap, int pageLength, ExceptionCode& ec)
929 {
930     if (!document || !document->page()) {
931         ec = INVALID_ACCESS_ERR;
932         return;
933     }
934     Page* page = document->page();
935
936     Pagination pagination;
937     if (mode == "Unpaginated")
938         pagination.mode = Pagination::Unpaginated;
939     else if (mode == "LeftToRightPaginated")
940         pagination.mode = Pagination::LeftToRightPaginated;
941     else if (mode == "RightToLeftPaginated")
942         pagination.mode = Pagination::RightToLeftPaginated;
943     else if (mode == "TopToBottomPaginated")
944         pagination.mode = Pagination::TopToBottomPaginated;
945     else if (mode == "BottomToTopPaginated")
946         pagination.mode = Pagination::BottomToTopPaginated;
947     else {
948         ec = SYNTAX_ERR;
949         return;
950     }
951
952     pagination.gap = gap;
953     pagination.pageLength = pageLength;
954     page->setPagination(pagination);
955 }
956
957 String Internals::configurationForViewport(Document* document, float devicePixelRatio, int deviceWidth, int deviceHeight, int availableWidth, int availableHeight, ExceptionCode& ec)
958 {
959     if (!document || !document->page()) {
960         ec = INVALID_ACCESS_ERR;
961         return String();
962     }
963     Page* page = document->page();
964
965     const int defaultLayoutWidthForNonMobilePages = 980;
966
967     ViewportArguments arguments = page->viewportArguments();
968     ViewportAttributes attributes = computeViewportAttributes(arguments, defaultLayoutWidthForNonMobilePages, deviceWidth, deviceHeight, devicePixelRatio, IntSize(availableWidth, availableHeight));
969     restrictMinimumScaleFactorToViewportSize(attributes, IntSize(availableWidth, availableHeight), devicePixelRatio);
970     restrictScaleFactorToInitialScaleIfNotUserScalable(attributes);
971
972     return "viewport size " + String::number(attributes.layoutSize.width()) + "x" + String::number(attributes.layoutSize.height()) + " scale " + String::number(attributes.initialScale) + " with limits [" + String::number(attributes.minimumScale) + ", " + String::number(attributes.maximumScale) + "] and userScalable " + (attributes.userScalable ? "true" : "false");
973 }
974
975 bool Internals::wasLastChangeUserEdit(Element* textField, ExceptionCode& ec)
976 {
977     if (!textField) {
978         ec = INVALID_ACCESS_ERR;
979         return false;
980     }
981
982     if (HTMLInputElement* inputElement = textField->toInputElement())
983         return inputElement->lastChangeWasUserEdit();
984
985     // FIXME: We should be using hasTagName instead but Windows port doesn't link QualifiedNames properly.
986     if (textField->tagName() == "TEXTAREA")
987         return static_cast<HTMLTextAreaElement*>(textField)->lastChangeWasUserEdit();
988
989     ec = INVALID_NODE_TYPE_ERR;
990     return false;
991 }
992
993 bool Internals::elementShouldAutoComplete(Element* element, ExceptionCode& ec)
994 {
995     if (!element) {
996         ec = INVALID_ACCESS_ERR;
997         return false;
998     }
999
1000     if (HTMLInputElement* inputElement = element->toInputElement())
1001         return inputElement->shouldAutocomplete();
1002
1003     ec = INVALID_NODE_TYPE_ERR;
1004     return false;
1005 }
1006
1007 String Internals::suggestedValue(Element* element, ExceptionCode& ec)
1008 {
1009     if (!element) {
1010         ec = INVALID_ACCESS_ERR;
1011         return String();
1012     }
1013
1014     HTMLInputElement* inputElement = element->toInputElement();
1015     if (!inputElement) {
1016         ec = INVALID_NODE_TYPE_ERR;
1017         return String();
1018     }
1019
1020     return inputElement->suggestedValue();
1021 }
1022
1023 void Internals::setSuggestedValue(Element* element, const String& value, ExceptionCode& ec)
1024 {
1025     if (!element) {
1026         ec = INVALID_ACCESS_ERR;
1027         return;
1028     }
1029
1030     HTMLInputElement* inputElement = element->toInputElement();
1031     if (!inputElement) {
1032         ec = INVALID_NODE_TYPE_ERR;
1033         return;
1034     }
1035
1036     inputElement->setSuggestedValue(value);
1037 }
1038
1039 void Internals::setEditingValue(Element* element, const String& value, ExceptionCode& ec)
1040 {
1041     if (!element) {
1042         ec = INVALID_ACCESS_ERR;
1043         return;
1044     }
1045
1046     HTMLInputElement* inputElement = element->toInputElement();
1047     if (!inputElement) {
1048         ec = INVALID_NODE_TYPE_ERR;
1049         return;
1050     }
1051
1052     inputElement->setEditingValue(value);
1053 }
1054
1055 void Internals::setAutofilled(Element* element, bool enabled, ExceptionCode& ec)
1056 {
1057     HTMLInputElement* inputElement = element->toInputElement();
1058     if (!inputElement) {
1059         ec = INVALID_ACCESS_ERR;
1060         return;
1061     }
1062     inputElement->setAutofilled(enabled);
1063 }
1064
1065 void Internals::scrollElementToRect(Element* element, long x, long y, long w, long h, ExceptionCode& ec)
1066 {
1067     if (!element || !element->document() || !element->document()->view()) {
1068         ec = INVALID_ACCESS_ERR;
1069         return;
1070     }
1071     FrameView* frameView = element->document()->view();
1072     frameView->scrollElementToRect(element, IntRect(x, y, w, h));
1073 }
1074
1075 void Internals::paintControlTints(Document* document, ExceptionCode& ec)
1076 {
1077     if (!document || !document->view()) {
1078         ec = INVALID_ACCESS_ERR;
1079         return;
1080     }
1081
1082     FrameView* frameView = document->view();
1083     frameView->paintControlTints();
1084 }
1085
1086 PassRefPtr<Range> Internals::rangeFromLocationAndLength(Element* scope, int rangeLocation, int rangeLength, ExceptionCode& ec)
1087 {
1088     if (!scope) {
1089         ec = INVALID_ACCESS_ERR;
1090         return 0;
1091     }
1092
1093     return TextIterator::rangeFromLocationAndLength(scope, rangeLocation, rangeLength);
1094 }
1095
1096 unsigned Internals::locationFromRange(Element* scope, const Range* range, ExceptionCode& ec)
1097 {
1098     if (!scope || !range) {
1099         ec = INVALID_ACCESS_ERR;
1100         return 0;
1101     }
1102
1103     size_t location = 0;
1104     size_t unusedLength = 0;
1105     TextIterator::getLocationAndLengthFromRange(scope, range, location, unusedLength);
1106     return location;
1107 }
1108
1109 unsigned Internals::lengthFromRange(Element* scope, const Range* range, ExceptionCode& ec)
1110 {
1111     if (!scope || !range) {
1112         ec = INVALID_ACCESS_ERR;
1113         return 0;
1114     }
1115
1116     size_t unusedLocation = 0;
1117     size_t length = 0;
1118     TextIterator::getLocationAndLengthFromRange(scope, range, unusedLocation, length);
1119     return length;
1120 }
1121
1122 String Internals::rangeAsText(const Range* range, ExceptionCode& ec)
1123 {
1124     if (!range) {
1125         ec = INVALID_ACCESS_ERR;
1126         return String();
1127     }
1128
1129     return range->text();
1130 }
1131
1132 void Internals::setDelegatesScrolling(bool enabled, Document* document, ExceptionCode& ec)
1133 {
1134     // Delegate scrolling is valid only on mainframe's view.
1135     if (!document || !document->view() || !document->page() || document->page()->mainFrame() != document->frame()) {
1136         ec = INVALID_ACCESS_ERR;
1137         return;
1138     }
1139
1140     document->view()->setDelegatesScrolling(enabled);
1141 }
1142
1143 #if ENABLE(TOUCH_ADJUSTMENT)
1144 PassRefPtr<WebKitPoint> Internals::touchPositionAdjustedToBestClickableNode(long x, long y, long width, long height, Document* document, ExceptionCode& ec)
1145 {
1146     if (!document || !document->frame()) {
1147         ec = INVALID_ACCESS_ERR;
1148         return 0;
1149     }
1150
1151     IntSize radius(width / 2, height / 2);
1152     IntPoint point(x + radius.width(), y + radius.height());
1153
1154     Node* targetNode;
1155     IntPoint adjustedPoint;
1156
1157     bool foundNode = document->frame()->eventHandler()->bestClickableNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1158     if (foundNode)
1159         return WebKitPoint::create(adjustedPoint.x(), adjustedPoint.y());
1160
1161     return 0;
1162 }
1163
1164 Node* Internals::touchNodeAdjustedToBestClickableNode(long x, long y, long width, long height, Document* document, ExceptionCode& ec)
1165 {
1166     if (!document || !document->frame()) {
1167         ec = INVALID_ACCESS_ERR;
1168         return 0;
1169     }
1170
1171     IntSize radius(width / 2, height / 2);
1172     IntPoint point(x + radius.width(), y + radius.height());
1173
1174     Node* targetNode;
1175     IntPoint adjustedPoint;
1176     document->frame()->eventHandler()->bestClickableNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1177     return targetNode;
1178 }
1179
1180 PassRefPtr<WebKitPoint> Internals::touchPositionAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document* document, ExceptionCode& ec)
1181 {
1182     if (!document || !document->frame()) {
1183         ec = INVALID_ACCESS_ERR;
1184         return 0;
1185     }
1186
1187     IntSize radius(width / 2, height / 2);
1188     IntPoint point(x + radius.width(), y + radius.height());
1189
1190     Node* targetNode = 0;
1191     IntPoint adjustedPoint;
1192
1193     bool foundNode = document->frame()->eventHandler()->bestContextMenuNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1194     if (foundNode)
1195         return WebKitPoint::create(adjustedPoint.x(), adjustedPoint.y());
1196
1197     return WebKitPoint::create(x, y);
1198 }
1199
1200 Node* Internals::touchNodeAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document* document, ExceptionCode& ec)
1201 {
1202     if (!document || !document->frame()) {
1203         ec = INVALID_ACCESS_ERR;
1204         return 0;
1205     }
1206
1207     IntSize radius(width / 2, height / 2);
1208     IntPoint point(x + radius.width(), y + radius.height());
1209
1210     Node* targetNode = 0;
1211     IntPoint adjustedPoint;
1212     document->frame()->eventHandler()->bestContextMenuNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1213     return targetNode;
1214 }
1215
1216 PassRefPtr<ClientRect> Internals::bestZoomableAreaForTouchPoint(long x, long y, long width, long height, Document* document, ExceptionCode& ec)
1217 {
1218     if (!document || !document->frame()) {
1219         ec = INVALID_ACCESS_ERR;
1220         return 0;
1221     }
1222
1223     IntSize radius(width / 2, height / 2);
1224     IntPoint point(x + radius.width(), y + radius.height());
1225
1226     Node* targetNode;
1227     IntRect zoomableArea;
1228     bool foundNode = document->frame()->eventHandler()->bestZoomableAreaForTouchPoint(point, radius, zoomableArea, targetNode);
1229     if (foundNode)
1230         return ClientRect::create(zoomableArea);
1231
1232     return 0;
1233 }
1234 #endif
1235
1236
1237 int Internals::lastSpellCheckRequestSequence(Document* document, ExceptionCode& ec)
1238 {
1239     SpellChecker* checker = spellchecker(document);
1240
1241     if (!checker) {
1242         ec = INVALID_ACCESS_ERR;
1243         return -1;
1244     }
1245
1246     return checker->lastRequestSequence();
1247 }
1248
1249 int Internals::lastSpellCheckProcessedSequence(Document* document, ExceptionCode& ec)
1250 {
1251     SpellChecker* checker = spellchecker(document);
1252
1253     if (!checker) {
1254         ec = INVALID_ACCESS_ERR;
1255         return -1;
1256     }
1257
1258     return checker->lastProcessedSequence();
1259 }
1260
1261 Vector<String> Internals::userPreferredLanguages() const
1262 {
1263     return WebCore::userPreferredLanguages();
1264 }
1265
1266 void Internals::setUserPreferredLanguages(const Vector<String>& languages)
1267 {
1268     WebCore::overrideUserPreferredLanguages(languages);
1269 }
1270
1271 unsigned Internals::wheelEventHandlerCount(Document* document, ExceptionCode& ec)
1272 {
1273     if (!document) {
1274         ec = INVALID_ACCESS_ERR;
1275         return 0;
1276     }
1277
1278     return document->wheelEventHandlerCount();
1279 }
1280
1281 unsigned Internals::touchEventHandlerCount(Document* document, ExceptionCode& ec)
1282 {
1283     if (!document) {
1284         ec = INVALID_ACCESS_ERR;
1285         return 0;
1286     }
1287
1288     const TouchEventTargetSet* touchHandlers = document->touchEventTargets();
1289     if (!touchHandlers)
1290         return 0;
1291
1292     unsigned count = 0;
1293     for (TouchEventTargetSet::const_iterator iter = touchHandlers->begin(); iter != touchHandlers->end(); ++iter)
1294         count += iter->value;
1295     return count;
1296 }
1297
1298 #if ENABLE(TOUCH_EVENT_TRACKING)
1299 PassRefPtr<ClientRectList> Internals::touchEventTargetClientRects(Document* document, ExceptionCode& ec)
1300 {
1301     if (!document || !document->view() || !document->page()) {
1302         ec = INVALID_ACCESS_ERR;
1303         return 0;
1304     }
1305     if (!document->page()->scrollingCoordinator())
1306         return ClientRectList::create();
1307
1308     document->updateLayoutIgnorePendingStylesheets();
1309
1310     Vector<IntRect> absoluteRects;
1311     document->page()->scrollingCoordinator()->computeAbsoluteTouchEventTargetRects(document, absoluteRects);
1312     Vector<FloatQuad> absoluteQuads(absoluteRects.size());
1313
1314     for (size_t i = 0; i < absoluteRects.size(); ++i)
1315         absoluteQuads[i] = FloatQuad(absoluteRects[i]);
1316
1317     return ClientRectList::create(absoluteQuads);
1318 }
1319 #endif
1320
1321 PassRefPtr<NodeList> Internals::nodesFromRect(Document* document, int centerX, int centerY, unsigned topPadding, unsigned rightPadding,
1322     unsigned bottomPadding, unsigned leftPadding, bool ignoreClipping, bool allowShadowContent, bool allowChildFrameContent, ExceptionCode& ec) const
1323 {
1324     if (!document || !document->frame() || !document->frame()->view()) {
1325         ec = INVALID_ACCESS_ERR;
1326         return 0;
1327     }
1328
1329     Frame* frame = document->frame();
1330     FrameView* frameView = document->view();
1331     RenderView* renderView = document->renderView();
1332     if (!renderView)
1333         return 0;
1334
1335     float zoomFactor = frame->pageZoomFactor();
1336     LayoutPoint point = roundedLayoutPoint(FloatPoint(centerX * zoomFactor + frameView->scrollX(), centerY * zoomFactor + frameView->scrollY()));
1337
1338     HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active;
1339     if (ignoreClipping)
1340         hitType |= HitTestRequest::IgnoreClipping;
1341     if (!allowShadowContent)
1342         hitType |= HitTestRequest::DisallowShadowContent;
1343     if (allowChildFrameContent)
1344         hitType |= HitTestRequest::AllowChildFrameContent;
1345
1346     HitTestRequest request(hitType);
1347
1348     // When ignoreClipping is false, this method returns null for coordinates outside of the viewport.
1349     if (!request.ignoreClipping() && !frameView->visibleContentRect().intersects(HitTestLocation::rectForPoint(point, topPadding, rightPadding, bottomPadding, leftPadding)))
1350         return 0;
1351
1352     Vector<RefPtr<Node> > matches;
1353
1354     // Need padding to trigger a rect based hit test, but we want to return a NodeList
1355     // so we special case this.
1356     if (!topPadding && !rightPadding && !bottomPadding && !leftPadding) {
1357         HitTestResult result(point);
1358         renderView->hitTest(request, result);
1359         if (result.innerNode())
1360             matches.append(result.innerNode()->deprecatedShadowAncestorNode());
1361     } else {
1362         HitTestResult result(point, topPadding, rightPadding, bottomPadding, leftPadding);
1363         renderView->hitTest(request, result);
1364         copyToVector(result.rectBasedTestResult(), matches);
1365     }
1366
1367     return StaticNodeList::adopt(matches);
1368 }
1369
1370 void Internals::emitInspectorDidBeginFrame()
1371 {
1372 #if ENABLE(INSPECTOR)
1373     InspectorController* inspectorController = contextDocument()->frame()->page()->inspectorController();
1374     inspectorController->didBeginFrame();
1375 #endif
1376 }
1377
1378 void Internals::emitInspectorDidCancelFrame()
1379 {
1380 #if ENABLE(INSPECTOR)
1381     InspectorController* inspectorController = contextDocument()->frame()->page()->inspectorController();
1382     inspectorController->didCancelFrame();
1383 #endif
1384 }
1385
1386 void Internals::setBatteryStatus(Document* document, const String& eventType, bool charging, double chargingTime, double dischargingTime, double level, ExceptionCode& ec)
1387 {
1388     if (!document || !document->page()) {
1389         ec = INVALID_ACCESS_ERR;
1390         return;
1391     }
1392
1393 #if ENABLE(BATTERY_STATUS)
1394     BatteryController::from(document->page())->didChangeBatteryStatus(eventType, BatteryStatus::create(charging, chargingTime, dischargingTime, level));
1395 #else
1396     UNUSED_PARAM(eventType);
1397     UNUSED_PARAM(charging);
1398     UNUSED_PARAM(chargingTime);
1399     UNUSED_PARAM(dischargingTime);
1400     UNUSED_PARAM(level);
1401 #endif
1402 }
1403
1404 void Internals::setNetworkInformation(Document* document, const String& eventType, double bandwidth, bool metered, ExceptionCode& ec)
1405 {
1406     if (!document || !document->page()) {
1407         ec = INVALID_ACCESS_ERR;
1408         return;
1409     }
1410
1411 #if ENABLE(NETWORK_INFO)
1412     NetworkInfoController::from(document->page())->didChangeNetworkInformation(eventType, NetworkInfo::create(bandwidth, metered));
1413 #else
1414     UNUSED_PARAM(eventType);
1415     UNUSED_PARAM(bandwidth);
1416     UNUSED_PARAM(metered);
1417 #endif
1418 }
1419
1420 void Internals::setDeviceProximity(Document* document, const String& eventType, double value, double min, double max, ExceptionCode& ec)
1421 {
1422     if (!document || !document->page()) {
1423         ec = INVALID_ACCESS_ERR;
1424         return;
1425     }
1426
1427 #if ENABLE(PROXIMITY_EVENTS)
1428     DeviceProximityController::from(document->page())->didChangeDeviceProximity(value, min, max);
1429 #else
1430     UNUSED_PARAM(eventType);
1431     UNUSED_PARAM(value);
1432     UNUSED_PARAM(min);
1433     UNUSED_PARAM(max);
1434 #endif
1435 }
1436
1437 bool Internals::hasSpellingMarker(Document* document, int from, int length, ExceptionCode&)
1438 {
1439     if (!document || !document->frame())
1440         return 0;
1441
1442     return document->frame()->editor().selectionStartHasMarkerFor(DocumentMarker::Spelling, from, length);
1443 }
1444     
1445 bool Internals::hasAutocorrectedMarker(Document* document, int from, int length, ExceptionCode&)
1446 {
1447     if (!document || !document->frame())
1448         return 0;
1449     
1450     return document->frame()->editor().selectionStartHasMarkerFor(DocumentMarker::Autocorrected, from, length);
1451 }
1452
1453 void Internals::setContinuousSpellCheckingEnabled(bool enabled, ExceptionCode&)
1454 {
1455     if (!contextDocument() || !contextDocument()->frame())
1456         return;
1457
1458     if (enabled != contextDocument()->frame()->editor().isContinuousSpellCheckingEnabled())
1459         contextDocument()->frame()->editor().toggleContinuousSpellChecking();
1460 }
1461
1462 void Internals::setAutomaticQuoteSubstitutionEnabled(bool enabled, ExceptionCode&)
1463 {
1464     if (!contextDocument() || !contextDocument()->frame())
1465         return;
1466
1467 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1468     if (enabled != contextDocument()->frame()->editor().isAutomaticQuoteSubstitutionEnabled())
1469         contextDocument()->frame()->editor().toggleAutomaticQuoteSubstitution();
1470 #else
1471     UNUSED_PARAM(enabled);
1472 #endif
1473 }
1474
1475 void Internals::setAutomaticLinkDetectionEnabled(bool enabled, ExceptionCode&)
1476 {
1477     if (!contextDocument() || !contextDocument()->frame())
1478         return;
1479
1480 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1481     if (enabled != contextDocument()->frame()->editor().isAutomaticLinkDetectionEnabled())
1482         contextDocument()->frame()->editor().toggleAutomaticLinkDetection();
1483 #else
1484     UNUSED_PARAM(enabled);
1485 #endif
1486 }
1487
1488 void Internals::setAutomaticDashSubstitutionEnabled(bool enabled, ExceptionCode&)
1489 {
1490     if (!contextDocument() || !contextDocument()->frame())
1491         return;
1492
1493 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1494     if (enabled != contextDocument()->frame()->editor().isAutomaticDashSubstitutionEnabled())
1495         contextDocument()->frame()->editor().toggleAutomaticDashSubstitution();
1496 #else
1497     UNUSED_PARAM(enabled);
1498 #endif
1499 }
1500
1501 void Internals::setAutomaticTextReplacementEnabled(bool enabled, ExceptionCode&)
1502 {
1503     if (!contextDocument() || !contextDocument()->frame())
1504         return;
1505
1506 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1507     if (enabled != contextDocument()->frame()->editor().isAutomaticTextReplacementEnabled())
1508         contextDocument()->frame()->editor().toggleAutomaticTextReplacement();
1509 #else
1510     UNUSED_PARAM(enabled);
1511 #endif
1512 }
1513
1514 void Internals::setAutomaticSpellingCorrectionEnabled(bool enabled, ExceptionCode&)
1515 {
1516     if (!contextDocument() || !contextDocument()->frame())
1517         return;
1518
1519 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1520     if (enabled != contextDocument()->frame()->editor().isAutomaticSpellingCorrectionEnabled())
1521         contextDocument()->frame()->editor().toggleAutomaticSpellingCorrection();
1522 #else
1523     UNUSED_PARAM(enabled);
1524 #endif
1525 }
1526
1527 bool Internals::isOverwriteModeEnabled(Document* document, ExceptionCode&)
1528 {
1529     if (!document || !document->frame())
1530         return 0;
1531
1532     return document->frame()->editor().isOverwriteModeEnabled();
1533 }
1534
1535 void Internals::toggleOverwriteModeEnabled(Document* document, ExceptionCode&)
1536 {
1537     if (!document || !document->frame())
1538         return;
1539
1540     document->frame()->editor().toggleOverwriteModeEnabled();
1541 }
1542
1543 #if ENABLE(INSPECTOR)
1544 unsigned Internals::numberOfLiveNodes() const
1545 {
1546     return InspectorCounters::counterValue(InspectorCounters::NodeCounter);
1547 }
1548
1549 unsigned Internals::numberOfLiveDocuments() const
1550 {
1551     return InspectorCounters::counterValue(InspectorCounters::DocumentCounter);
1552 }
1553
1554 Vector<String> Internals::consoleMessageArgumentCounts(Document* document) const
1555 {
1556     InstrumentingAgents* instrumentingAgents = instrumentationForPage(document->page());
1557     if (!instrumentingAgents)
1558         return Vector<String>();
1559     InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent();
1560     if (!consoleAgent)
1561         return Vector<String>();
1562     Vector<unsigned> counts = consoleAgent->consoleMessageArgumentCounts();
1563     Vector<String> result(counts.size());
1564     for (size_t i = 0; i < counts.size(); i++)
1565         result[i] = String::number(counts[i]);
1566     return result;
1567 }
1568
1569 PassRefPtr<DOMWindow> Internals::openDummyInspectorFrontend(const String& url)
1570 {
1571     Page* page = contextDocument()->frame()->page();
1572     ASSERT(page);
1573
1574     DOMWindow* window = page->mainFrame()->document()->domWindow();
1575     ASSERT(window);
1576
1577     m_frontendWindow = window->open(url, "", "", window, window);
1578     ASSERT(m_frontendWindow);
1579
1580     Page* frontendPage = m_frontendWindow->document()->page();
1581     ASSERT(frontendPage);
1582
1583     OwnPtr<InspectorFrontendClientDummy> frontendClient = adoptPtr(new InspectorFrontendClientDummy(page->inspectorController(), frontendPage));
1584
1585     frontendPage->inspectorController()->setInspectorFrontendClient(frontendClient.release());
1586
1587     m_frontendChannel = adoptPtr(new InspectorFrontendChannelDummy(frontendPage));
1588
1589     page->inspectorController()->connectFrontend(m_frontendChannel.get());
1590
1591     return m_frontendWindow;
1592 }
1593
1594 void Internals::closeDummyInspectorFrontend()
1595 {
1596     Page* page = contextDocument()->frame()->page();
1597     ASSERT(page);
1598     ASSERT(m_frontendWindow);
1599
1600     page->inspectorController()->disconnectFrontend();
1601
1602     m_frontendChannel.release();
1603
1604     m_frontendWindow->close(m_frontendWindow->scriptExecutionContext());
1605     m_frontendWindow.release();
1606 }
1607
1608 void Internals::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
1609 {
1610     Page* page = contextDocument()->frame()->page();
1611     if (!page || !page->inspectorController()) {
1612         ec = INVALID_ACCESS_ERR;
1613         return;
1614     }
1615     page->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
1616 }
1617
1618 void Internals::setJavaScriptProfilingEnabled(bool enabled, ExceptionCode& ec)
1619 {
1620     Page* page = contextDocument()->frame()->page();
1621     if (!page || !page->inspectorController()) {
1622         ec = INVALID_ACCESS_ERR;
1623         return;
1624     }
1625
1626     page->inspectorController()->setProfilerEnabled(enabled);
1627 }
1628 #endif // ENABLE(INSPECTOR)
1629
1630 bool Internals::hasGrammarMarker(Document* document, int from, int length, ExceptionCode&)
1631 {
1632     if (!document || !document->frame())
1633         return 0;
1634
1635     return document->frame()->editor().selectionStartHasMarkerFor(DocumentMarker::Grammar, from, length);
1636 }
1637
1638 unsigned Internals::numberOfScrollableAreas(Document* document, ExceptionCode&)
1639 {
1640     unsigned count = 0;
1641     Frame* frame = document->frame();
1642     if (frame->view()->scrollableAreas())
1643         count += frame->view()->scrollableAreas()->size();
1644
1645     for (Frame* child = frame->tree()->firstChild(); child; child = child->tree()->nextSibling()) {
1646         if (child->view() && child->view()->scrollableAreas())
1647             count += child->view()->scrollableAreas()->size();
1648     }
1649
1650     return count;
1651 }
1652     
1653 bool Internals::isPageBoxVisible(Document* document, int pageNumber, ExceptionCode& ec)
1654 {
1655     if (!document) {
1656         ec = INVALID_ACCESS_ERR;
1657         return false;
1658     }
1659
1660     return document->isPageBoxVisible(pageNumber);
1661 }
1662
1663 String Internals::layerTreeAsText(Document* document, ExceptionCode& ec) const
1664 {
1665     return layerTreeAsText(document, 0, ec);
1666 }
1667
1668 String Internals::layerTreeAsText(Document* document, unsigned flags, ExceptionCode& ec) const
1669 {
1670     if (!document || !document->frame()) {
1671         ec = INVALID_ACCESS_ERR;
1672         return String();
1673     }
1674
1675     LayerTreeFlags layerTreeFlags = 0;
1676     if (flags & LAYER_TREE_INCLUDES_VISIBLE_RECTS)
1677         layerTreeFlags |= LayerTreeFlagsIncludeVisibleRects;
1678     if (flags & LAYER_TREE_INCLUDES_TILE_CACHES)
1679         layerTreeFlags |= LayerTreeFlagsIncludeTileCaches;
1680     if (flags & LAYER_TREE_INCLUDES_REPAINT_RECTS)
1681         layerTreeFlags |= LayerTreeFlagsIncludeRepaintRects;
1682     if (flags & LAYER_TREE_INCLUDES_PAINTING_PHASES)
1683         layerTreeFlags |= LayerTreeFlagsIncludePaintingPhases;
1684
1685     return document->frame()->layerTreeAsText(layerTreeFlags);
1686 }
1687
1688 String Internals::repaintRectsAsText(Document* document, ExceptionCode& ec) const
1689 {
1690     if (!document || !document->frame()) {
1691         ec = INVALID_ACCESS_ERR;
1692         return String();
1693     }
1694
1695     return document->frame()->trackedRepaintRectsAsText();
1696 }
1697
1698 String Internals::scrollingStateTreeAsText(Document* document, ExceptionCode& ec) const
1699 {
1700     if (!document || !document->frame()) {
1701         ec = INVALID_ACCESS_ERR;
1702         return String();
1703     }
1704
1705     Page* page = document->page();
1706     if (!page)
1707         return String();
1708
1709     return page->scrollingStateTreeAsText();
1710 }
1711
1712 String Internals::mainThreadScrollingReasons(Document* document, ExceptionCode& ec) const
1713 {
1714     if (!document || !document->frame()) {
1715         ec = INVALID_ACCESS_ERR;
1716         return String();
1717     }
1718
1719     Page* page = document->page();
1720     if (!page)
1721         return String();
1722
1723     return page->mainThreadScrollingReasonsAsText();
1724 }
1725
1726 PassRefPtr<ClientRectList> Internals::nonFastScrollableRects(Document* document, ExceptionCode& ec) const
1727 {
1728     if (!document || !document->frame()) {
1729         ec = INVALID_ACCESS_ERR;
1730         return 0;
1731     }
1732
1733     Page* page = document->page();
1734     if (!page)
1735         return 0;
1736
1737     return page->nonFastScrollableRects(document->frame());
1738 }
1739
1740 void Internals::garbageCollectDocumentResources(Document* document, ExceptionCode& ec) const
1741 {
1742     if (!document) {
1743         ec = INVALID_ACCESS_ERR;
1744         return;
1745     }
1746
1747     CachedResourceLoader* cachedResourceLoader = document->cachedResourceLoader();
1748     if (!cachedResourceLoader)
1749         return;
1750     cachedResourceLoader->garbageCollectDocumentResources();
1751 }
1752
1753 void Internals::allowRoundingHacks() const
1754 {
1755     TextRun::setAllowsRoundingHacks(true);
1756 }
1757
1758 void Internals::insertAuthorCSS(Document* document, const String& css) const
1759 {
1760     RefPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(document);
1761     parsedSheet->setIsUserStyleSheet(false);
1762     parsedSheet->parseString(css);
1763     document->styleSheetCollection()->addAuthorSheet(parsedSheet);
1764 }
1765
1766 void Internals::insertUserCSS(Document* document, const String& css) const
1767 {
1768     RefPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(document);
1769     parsedSheet->setIsUserStyleSheet(true);
1770     parsedSheet->parseString(css);
1771     document->styleSheetCollection()->addUserSheet(parsedSheet);
1772 }
1773
1774 String Internals::counterValue(Element* element)
1775 {
1776     if (!element)
1777         return String();
1778
1779     return counterValueForElement(element);
1780 }
1781
1782 int Internals::pageNumber(Element* element, float pageWidth, float pageHeight)
1783 {
1784     if (!element)
1785         return 0;
1786
1787     return PrintContext::pageNumberForElement(element, FloatSize(pageWidth, pageHeight));
1788 }
1789
1790 Vector<String> Internals::iconURLs(Document* document, int iconTypesMask) const
1791 {
1792     Vector<IconURL> iconURLs = document->iconURLs(iconTypesMask);
1793     Vector<String> array;
1794
1795     Vector<IconURL>::const_iterator iter(iconURLs.begin());
1796     for (; iter != iconURLs.end(); ++iter)
1797         array.append(iter->m_iconURL.string());
1798
1799     return array;
1800 }
1801
1802 Vector<String> Internals::shortcutIconURLs(Document* document) const
1803 {
1804     return iconURLs(document, Favicon);
1805 }
1806
1807 Vector<String> Internals::allIconURLs(Document* document) const
1808 {
1809     return iconURLs(document, Favicon | TouchIcon | TouchPrecomposedIcon);
1810 }
1811
1812 int Internals::numberOfPages(float pageWidth, float pageHeight)
1813 {
1814     if (!frame())
1815         return -1;
1816
1817     return PrintContext::numberOfPages(frame(), FloatSize(pageWidth, pageHeight));
1818 }
1819
1820 String Internals::pageProperty(String propertyName, int pageNumber, ExceptionCode& ec) const
1821 {
1822     if (!frame()) {
1823         ec = INVALID_ACCESS_ERR;
1824         return String();
1825     }
1826
1827     return PrintContext::pageProperty(frame(), propertyName.utf8().data(), pageNumber);
1828 }
1829
1830 String Internals::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft, ExceptionCode& ec) const
1831 {
1832     if (!frame()) {
1833         ec = INVALID_ACCESS_ERR;
1834         return String();
1835     }
1836
1837     return PrintContext::pageSizeAndMarginsInPixels(frame(), pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft);
1838 }
1839
1840 void Internals::setPageScaleFactor(float scaleFactor, int x, int y, ExceptionCode& ec)
1841 {
1842     Document* document = contextDocument();
1843     if (!document || !document->page()) {
1844         ec = INVALID_ACCESS_ERR;
1845         return;
1846     }
1847     Page* page = document->page();
1848     page->setPageScaleFactor(scaleFactor, IntPoint(x, y));
1849 }
1850
1851 void Internals::setHeaderHeight(Document* document, float height)
1852 {
1853     if (!document || !document->view())
1854         return;
1855 #if USE(ACCELERATED_COMPOSITING)
1856     FrameView* frameView = document->view();
1857     frameView->setHeaderHeight(height);
1858 #endif
1859 }
1860
1861 void Internals::setFooterHeight(Document* document, float height)
1862 {
1863     if (!document || !document->view())
1864         return;
1865 #if USE(ACCELERATED_COMPOSITING)
1866     FrameView* frameView = document->view();
1867     frameView->setFooterHeight(height);
1868 #endif
1869 }
1870
1871 #if ENABLE(FULLSCREEN_API)
1872 void Internals::webkitWillEnterFullScreenForElement(Document* document, Element* element)
1873 {
1874     if (!document)
1875         return;
1876     document->webkitWillEnterFullScreenForElement(element);
1877 }
1878
1879 void Internals::webkitDidEnterFullScreenForElement(Document* document, Element* element)
1880 {
1881     if (!document)
1882         return;
1883     document->webkitDidEnterFullScreenForElement(element);
1884 }
1885
1886 void Internals::webkitWillExitFullScreenForElement(Document* document, Element* element)
1887 {
1888     if (!document)
1889         return;
1890     document->webkitWillExitFullScreenForElement(element);
1891 }
1892
1893 void Internals::webkitDidExitFullScreenForElement(Document* document, Element* element)
1894 {
1895     if (!document)
1896         return;
1897     document->webkitDidExitFullScreenForElement(element);
1898 }
1899 #endif
1900
1901 void Internals::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
1902 {
1903     SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(scheme);
1904 }
1905
1906 void Internals::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
1907 {
1908     SchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(scheme);
1909 }
1910
1911 PassRefPtr<MallocStatistics> Internals::mallocStatistics() const
1912 {
1913     return MallocStatistics::create();
1914 }
1915
1916 PassRefPtr<TypeConversions> Internals::typeConversions() const
1917 {
1918     return TypeConversions::create();
1919 }
1920
1921 Vector<String> Internals::getReferencedFilePaths() const
1922 {
1923     frame()->loader()->history()->saveDocumentAndScrollState();
1924     return FormController::getReferencedFilePaths(frame()->loader()->history()->currentItem()->documentState());
1925 }
1926
1927 void Internals::startTrackingRepaints(Document* document, ExceptionCode& ec)
1928 {
1929     if (!document || !document->view()) {
1930         ec = INVALID_ACCESS_ERR;
1931         return;
1932     }
1933
1934     FrameView* frameView = document->view();
1935     frameView->setTracksRepaints(true);
1936 }
1937
1938 void Internals::stopTrackingRepaints(Document* document, ExceptionCode& ec)
1939 {
1940     if (!document || !document->view()) {
1941         ec = INVALID_ACCESS_ERR;
1942         return;
1943     }
1944
1945     FrameView* frameView = document->view();
1946     frameView->setTracksRepaints(false);
1947 }
1948
1949 #if USE(LAZY_NATIVE_CURSOR)
1950 static const char* cursorTypeToString(Cursor::Type cursorType)
1951 {
1952     switch (cursorType) {
1953     case Cursor::Pointer: return "Pointer";
1954     case Cursor::Cross: return "Cross";
1955     case Cursor::Hand: return "Hand";
1956     case Cursor::IBeam: return "IBeam";
1957     case Cursor::Wait: return "Wait";
1958     case Cursor::Help: return "Help";
1959     case Cursor::EastResize: return "EastResize";
1960     case Cursor::NorthResize: return "NorthResize";
1961     case Cursor::NorthEastResize: return "NorthEastResize";
1962     case Cursor::NorthWestResize: return "NorthWestResize";
1963     case Cursor::SouthResize: return "SouthResize";
1964     case Cursor::SouthEastResize: return "SouthEastResize";
1965     case Cursor::SouthWestResize: return "SouthWestResize";
1966     case Cursor::WestResize: return "WestResize";
1967     case Cursor::NorthSouthResize: return "NorthSouthResize";
1968     case Cursor::EastWestResize: return "EastWestResize";
1969     case Cursor::NorthEastSouthWestResize: return "NorthEastSouthWestResize";
1970     case Cursor::NorthWestSouthEastResize: return "NorthWestSouthEastResize";
1971     case Cursor::ColumnResize: return "ColumnResize";
1972     case Cursor::RowResize: return "RowResize";
1973     case Cursor::MiddlePanning: return "MiddlePanning";
1974     case Cursor::EastPanning: return "EastPanning";
1975     case Cursor::NorthPanning: return "NorthPanning";
1976     case Cursor::NorthEastPanning: return "NorthEastPanning";
1977     case Cursor::NorthWestPanning: return "NorthWestPanning";
1978     case Cursor::SouthPanning: return "SouthPanning";
1979     case Cursor::SouthEastPanning: return "SouthEastPanning";
1980     case Cursor::SouthWestPanning: return "SouthWestPanning";
1981     case Cursor::WestPanning: return "WestPanning";
1982     case Cursor::Move: return "Move";
1983     case Cursor::VerticalText: return "VerticalText";
1984     case Cursor::Cell: return "Cell";
1985     case Cursor::ContextMenu: return "ContextMenu";
1986     case Cursor::Alias: return "Alias";
1987     case Cursor::Progress: return "Progress";
1988     case Cursor::NoDrop: return "NoDrop";
1989     case Cursor::Copy: return "Copy";
1990     case Cursor::None: return "None";
1991     case Cursor::NotAllowed: return "NotAllowed";
1992     case Cursor::ZoomIn: return "ZoomIn";
1993     case Cursor::ZoomOut: return "ZoomOut";
1994     case Cursor::Grab: return "Grab";
1995     case Cursor::Grabbing: return "Grabbing";
1996     case Cursor::Custom: return "Custom";
1997     }
1998
1999     ASSERT_NOT_REACHED();
2000     return "UNKNOWN";
2001 }
2002 #endif
2003
2004 String Internals::getCurrentCursorInfo(Document* document, ExceptionCode& ec)
2005 {
2006     if (!document || !document->frame()) {
2007         ec = INVALID_ACCESS_ERR;
2008         return String();
2009     }
2010
2011     Cursor cursor = document->frame()->eventHandler()->currentMouseCursor();
2012
2013 #if USE(LAZY_NATIVE_CURSOR)
2014     StringBuilder result;
2015     result.append("type=");
2016     result.append(cursorTypeToString(cursor.type()));
2017     result.append(" hotSpot=");
2018     result.appendNumber(cursor.hotSpot().x());
2019     result.append(",");
2020     result.appendNumber(cursor.hotSpot().y());
2021     if (cursor.image()) {
2022         IntSize size = cursor.image()->size();
2023         result.append(" image=");
2024         result.appendNumber(size.width());
2025         result.append("x");
2026         result.appendNumber(size.height());
2027     }
2028 #if ENABLE(MOUSE_CURSOR_SCALE)
2029     if (cursor.imageScaleFactor() != 1) {
2030         result.append(" scale=");
2031         NumberToStringBuffer buffer;
2032         result.append(numberToFixedPrecisionString(cursor.imageScaleFactor(), 8, buffer, true));
2033     }
2034 #endif
2035     return result.toString();
2036 #else
2037     return "FAIL: Cursor details not available on this platform.";
2038 #endif
2039 }
2040
2041 PassRefPtr<ArrayBuffer> Internals::serializeObject(PassRefPtr<SerializedScriptValue> value) const
2042 {
2043     Vector<uint8_t> bytes = value->data();
2044     return ArrayBuffer::create(bytes.data(), bytes.size());
2045 }
2046
2047 PassRefPtr<SerializedScriptValue> Internals::deserializeBuffer(PassRefPtr<ArrayBuffer> buffer) const
2048 {
2049     Vector<uint8_t> bytes;
2050     bytes.append(static_cast<const uint8_t*>(buffer->data()), buffer->byteLength());
2051     return SerializedScriptValue::adopt(bytes);
2052 }
2053
2054 void Internals::setUsesOverlayScrollbars(bool enabled)
2055 {
2056     WebCore::Settings::setUsesOverlayScrollbars(enabled);
2057 }
2058
2059 void Internals::forceReload(bool endToEnd)
2060 {
2061     frame()->loader()->reload(endToEnd);
2062 }
2063
2064 #if ENABLE(ENCRYPTED_MEDIA_V2)
2065 void Internals::initializeMockCDM()
2066 {
2067     CDM::registerCDMFactory(MockCDM::create, MockCDM::supportsKeySytem);
2068 }
2069 #endif
2070
2071 String Internals::markerTextForListItem(Element* element, ExceptionCode& ec)
2072 {
2073     if (!element) {
2074         ec = INVALID_ACCESS_ERR;
2075         return String();
2076     }
2077     return WebCore::markerTextForListItem(element);
2078 }
2079
2080 String Internals::getImageSourceURL(Element* element, ExceptionCode& ec)
2081 {
2082     if (!element) {
2083         ec = INVALID_ACCESS_ERR;
2084         return String();
2085     }
2086     return element->imageSourceURL();
2087 }
2088
2089 #if ENABLE(VIDEO)
2090 void Internals::simulateAudioInterruption(Node* node)
2091 {
2092 #if USE(GSTREAMER)
2093     HTMLMediaElement* element = toMediaElement(node);
2094     element->player()->simulateAudioInterruption();
2095 #else
2096     UNUSED_PARAM(node);
2097 #endif
2098 }
2099 #endif
2100
2101 bool Internals::isSelectPopupVisible(Node* node)
2102 {
2103     if (!isHTMLSelectElement(node))
2104         return false;
2105
2106     HTMLSelectElement* select = toHTMLSelectElement(node);
2107
2108     RenderObject* renderer = select->renderer();
2109     if (!renderer->isMenuList())
2110         return false;
2111
2112     RenderMenuList* menuList = toRenderMenuList(renderer);
2113     return menuList->popupIsVisible();
2114 }
2115
2116 String Internals::captionsStyleSheetOverride(ExceptionCode& ec)
2117 {
2118     Document* document = contextDocument();
2119     if (!document || !document->page()) {
2120         ec = INVALID_ACCESS_ERR;
2121         return emptyString();
2122     }
2123
2124 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
2125     return document->page()->group().captionPreferences()->captionsStyleSheetOverride();
2126 #else
2127     return emptyString();
2128 #endif
2129 }
2130
2131 void Internals::setCaptionsStyleSheetOverride(const String& override, ExceptionCode& ec)
2132 {
2133     Document* document = contextDocument();
2134     if (!document || !document->page()) {
2135         ec = INVALID_ACCESS_ERR;
2136         return;
2137     }
2138
2139 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
2140     document->page()->group().captionPreferences()->setCaptionsStyleSheetOverride(override);
2141 #endif
2142 }
2143
2144 void Internals::setPrimaryAudioTrackLanguageOverride(const String& language, ExceptionCode& ec)
2145 {
2146     Document* document = contextDocument();
2147     if (!document || !document->page()) {
2148         ec = INVALID_ACCESS_ERR;
2149         return;
2150     }
2151
2152 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
2153     document->page()->group().captionPreferences()->setPrimaryAudioTrackLanguageOverride(language);
2154 #else
2155     UNUSED_PARAM(language);
2156 #endif
2157 }
2158
2159 void Internals::setCaptionDisplayMode(const String& mode, ExceptionCode& ec)
2160 {
2161     Document* document = contextDocument();
2162     if (!document || !document->page()) {
2163         ec = INVALID_ACCESS_ERR;
2164         return;
2165     }
2166     
2167 #if ENABLE(VIDEO_TRACK) && !PLATFORM(WIN)
2168     CaptionUserPreferences* captionPreferences = document->page()->group().captionPreferences();
2169     
2170     if (equalIgnoringCase(mode, "Automatic"))
2171         captionPreferences->setCaptionDisplayMode(CaptionUserPreferences::Automatic);
2172     else if (equalIgnoringCase(mode, "ForcedOnly"))
2173         captionPreferences->setCaptionDisplayMode(CaptionUserPreferences::ForcedOnly);
2174     else if (equalIgnoringCase(mode, "AlwaysOn"))
2175         captionPreferences->setCaptionDisplayMode(CaptionUserPreferences::AlwaysOn);
2176     else
2177         ec = SYNTAX_ERR;
2178 #else
2179     UNUSED_PARAM(mode);
2180 #endif
2181 }
2182
2183 #if ENABLE(VIDEO)
2184 PassRefPtr<TimeRanges> Internals::createTimeRanges(Float32Array* startTimes, Float32Array* endTimes)
2185 {
2186     ASSERT(startTimes && endTimes);
2187     ASSERT(startTimes->length() == endTimes->length());
2188     RefPtr<TimeRanges> ranges = TimeRanges::create();
2189
2190     unsigned count = std::min(startTimes->length(), endTimes->length());
2191     for (unsigned i = 0; i < count; ++i)
2192         ranges->add(startTimes->item(i), endTimes->item(i));
2193     return ranges;
2194 }
2195
2196 double Internals::closestTimeToTimeRanges(double time, TimeRanges* ranges)
2197 {
2198     return ranges->nearest(time);
2199 }
2200 #endif
2201
2202 PassRefPtr<ClientRect> Internals::selectionBounds(ExceptionCode& ec)
2203 {
2204     Document* document = contextDocument();
2205     if (!document || !document->frame() || !document->frame()->selection()) {
2206         ec = INVALID_ACCESS_ERR;
2207         return ClientRect::create();
2208     }
2209
2210     return ClientRect::create(document->frame()->selection()->bounds());
2211 }
2212
2213 }