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