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