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