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