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