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