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