ad01f3807c8729819291c85a6d27a28c9316c741
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
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 <v8.h>
31 #include "InternalProfilers.h"
32 #include "InternalRuntimeFlags.h"
33 #include "InternalSettings.h"
34 #include "LayerRect.h"
35 #include "LayerRectList.h"
36 #include "MallocStatistics.h"
37 #include "MockPagePopupDriver.h"
38 #include "RuntimeEnabledFeatures.h"
39 #include "TypeConversions.h"
40 #include "bindings/v8/ExceptionMessages.h"
41 #include "bindings/v8/ExceptionState.h"
42 #include "bindings/v8/ScriptFunction.h"
43 #include "bindings/v8/ScriptPromise.h"
44 #include "bindings/v8/ScriptPromiseResolver.h"
45 #include "bindings/v8/SerializedScriptValue.h"
46 #include "bindings/v8/V8ThrowException.h"
47 #include "core/animation/DocumentTimeline.h"
48 #include "core/css/StyleSheetContents.h"
49 #include "core/css/resolver/StyleResolver.h"
50 #include "core/css/resolver/StyleResolverStats.h"
51 #include "core/css/resolver/ViewportStyleResolver.h"
52 #include "core/dom/ClientRect.h"
53 #include "core/dom/ClientRectList.h"
54 #include "core/dom/DOMStringList.h"
55 #include "core/dom/Document.h"
56 #include "core/dom/DocumentMarker.h"
57 #include "core/dom/DocumentMarkerController.h"
58 #include "core/dom/Element.h"
59 #include "core/dom/ExceptionCode.h"
60 #include "core/dom/FullscreenElementStack.h"
61 #include "core/dom/NodeRenderStyle.h"
62 #include "core/dom/PseudoElement.h"
63 #include "core/dom/Range.h"
64 #include "core/dom/StaticNodeList.h"
65 #include "core/dom/TreeScope.h"
66 #include "core/dom/ViewportDescription.h"
67 #include "core/dom/WheelController.h"
68 #include "core/dom/shadow/ComposedTreeWalker.h"
69 #include "core/dom/shadow/ElementShadow.h"
70 #include "core/dom/shadow/SelectRuleFeatureSet.h"
71 #include "core/dom/shadow/ShadowRoot.h"
72 #include "core/editing/Editor.h"
73 #include "core/editing/PlainTextRange.h"
74 #include "core/editing/SpellCheckRequester.h"
75 #include "core/editing/SpellChecker.h"
76 #include "core/editing/SurroundingText.h"
77 #include "core/editing/TextIterator.h"
78 #include "core/fetch/MemoryCache.h"
79 #include "core/fetch/ResourceFetcher.h"
80 #include "core/frame/DOMPoint.h"
81 #include "core/frame/DOMWindow.h"
82 #include "core/frame/EventHandlerRegistry.h"
83 #include "core/frame/FrameView.h"
84 #include "core/frame/LocalFrame.h"
85 #include "core/frame/Settings.h"
86 #include "core/html/HTMLContentElement.h"
87 #include "core/html/HTMLIFrameElement.h"
88 #include "core/html/HTMLInputElement.h"
89 #include "core/html/HTMLMediaElement.h"
90 #include "core/html/HTMLSelectElement.h"
91 #include "core/html/HTMLTextAreaElement.h"
92 #include "core/html/forms/FormController.h"
93 #include "core/html/shadow/ShadowElementNames.h"
94 #include "core/html/shadow/TextControlInnerElements.h"
95 #include "core/inspector/InspectorClient.h"
96 #include "core/inspector/InspectorConsoleAgent.h"
97 #include "core/inspector/InspectorController.h"
98 #include "core/inspector/InspectorCounters.h"
99 #include "core/inspector/InspectorFrontendChannel.h"
100 #include "core/inspector/InspectorInstrumentation.h"
101 #include "core/inspector/InspectorOverlay.h"
102 #include "core/inspector/InstrumentingAgents.h"
103 #include "core/loader/FrameLoader.h"
104 #include "core/loader/HistoryItem.h"
105 #include "core/page/Chrome.h"
106 #include "core/page/ChromeClient.h"
107 #include "core/page/EventHandler.h"
108 #include "core/page/Page.h"
109 #include "core/page/PagePopupController.h"
110 #include "core/page/PrintContext.h"
111 #include "core/rendering/RenderLayer.h"
112 #include "core/rendering/RenderMenuList.h"
113 #include "core/rendering/RenderObject.h"
114 #include "core/rendering/RenderTreeAsText.h"
115 #include "core/rendering/RenderView.h"
116 #include "core/rendering/compositing/CompositedLayerMapping.h"
117 #include "core/rendering/compositing/RenderLayerCompositor.h"
118 #include "core/testing/GCObservation.h"
119 #include "core/workers/WorkerThread.h"
120 #include "platform/ColorChooser.h"
121 #include "platform/Cursor.h"
122 #include "platform/Language.h"
123 #include "platform/TraceEvent.h"
124 #include "platform/geometry/IntRect.h"
125 #include "platform/geometry/LayoutRect.h"
126 #include "platform/graphics/GraphicsLayer.h"
127 #include "platform/graphics/filters/FilterOperation.h"
128 #include "platform/graphics/filters/FilterOperations.h"
129 #include "platform/weborigin/SchemeRegistry.h"
130 #include "public/platform/Platform.h"
131 #include "public/platform/WebGraphicsContext3D.h"
132 #include "public/platform/WebGraphicsContext3DProvider.h"
133 #include "public/platform/WebLayer.h"
134 #include "wtf/InstanceCounter.h"
135 #include "wtf/PassOwnPtr.h"
136 #include "wtf/dtoa.h"
137 #include "wtf/text/StringBuffer.h"
138
139 namespace WebCore {
140
141 // FIXME: oilpan: These will be removed soon.
142 static MockPagePopupDriver* s_pagePopupDriver = 0;
143
144 using namespace HTMLNames;
145
146 static bool markerTypesFrom(const String& markerType, DocumentMarker::MarkerTypes& result)
147 {
148     if (markerType.isEmpty() || equalIgnoringCase(markerType, "all"))
149         result = DocumentMarker::AllMarkers();
150     else if (equalIgnoringCase(markerType, "Spelling"))
151         result =  DocumentMarker::Spelling;
152     else if (equalIgnoringCase(markerType, "Grammar"))
153         result =  DocumentMarker::Grammar;
154     else if (equalIgnoringCase(markerType, "TextMatch"))
155         result =  DocumentMarker::TextMatch;
156     else
157         return false;
158
159     return true;
160 }
161
162 static SpellCheckRequester* spellCheckRequester(Document* document)
163 {
164     if (!document || !document->frame())
165         return 0;
166     return &document->frame()->spellChecker().spellCheckRequester();
167 }
168
169 const char* Internals::internalsId = "internals";
170
171 PassRefPtrWillBeRawPtr<Internals> Internals::create(Document* document)
172 {
173     return adoptRefWillBeNoop(new Internals(document));
174 }
175
176 Internals::~Internals()
177 {
178 }
179
180 void Internals::resetToConsistentState(Page* page)
181 {
182     ASSERT(page);
183
184     page->setDeviceScaleFactor(1);
185     page->setIsCursorVisible(true);
186     page->setPageScaleFactor(1, IntPoint(0, 0));
187     TextRun::setAllowsRoundingHacks(false);
188     WebCore::overrideUserPreferredLanguages(Vector<AtomicString>());
189     delete s_pagePopupDriver;
190     s_pagePopupDriver = 0;
191     page->chrome().client().resetPagePopupDriver();
192     if (!page->mainFrame()->spellChecker().isContinuousSpellCheckingEnabled())
193         page->mainFrame()->spellChecker().toggleContinuousSpellChecking();
194     if (page->mainFrame()->editor().isOverwriteModeEnabled())
195         page->mainFrame()->editor().toggleOverwriteModeEnabled();
196
197     if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
198         scrollingCoordinator->reset();
199
200     page->mainFrame()->view()->clear();
201 }
202
203 Internals::Internals(Document* document)
204     : ContextLifecycleObserver(document)
205     , m_runtimeFlags(InternalRuntimeFlags::create())
206 {
207 }
208
209 Document* Internals::contextDocument() const
210 {
211     return toDocument(executionContext());
212 }
213
214 LocalFrame* Internals::frame() const
215 {
216     if (!contextDocument())
217         return 0;
218     return contextDocument()->frame();
219 }
220
221 InternalSettings* Internals::settings() const
222 {
223     Document* document = contextDocument();
224     if (!document)
225         return 0;
226     Page* page = document->page();
227     if (!page)
228         return 0;
229     return InternalSettings::from(*page);
230 }
231
232 InternalRuntimeFlags* Internals::runtimeFlags() const
233 {
234     return m_runtimeFlags.get();
235 }
236
237 InternalProfilers* Internals::profilers()
238 {
239     if (!m_profilers)
240         m_profilers = InternalProfilers::create();
241     return m_profilers.get();
242 }
243
244 unsigned Internals::workerThreadCount() const
245 {
246     return WorkerThread::workerThreadCount();
247 }
248
249 String Internals::address(Node* node)
250 {
251     char buf[32];
252     sprintf(buf, "%p", node);
253
254     return String(buf);
255 }
256
257 PassRefPtrWillBeRawPtr<GCObservation> Internals::observeGC(ScriptValue scriptValue)
258 {
259     v8::Handle<v8::Value> observedValue = scriptValue.v8Value();
260     ASSERT(!observedValue.IsEmpty());
261     if (observedValue->IsNull() || observedValue->IsUndefined()) {
262         V8ThrowException::throwTypeError("value to observe is null or undefined", v8::Isolate::GetCurrent());
263         return nullptr;
264     }
265
266     return GCObservation::create(observedValue);
267 }
268
269 unsigned Internals::updateStyleAndReturnAffectedElementCount(ExceptionState& exceptionState) const
270 {
271     Document* document = contextDocument();
272     if (!document) {
273         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
274         return 0;
275     }
276
277     unsigned beforeCount = document->styleEngine()->resolverAccessCount();
278     document->updateRenderTreeIfNeeded();
279     return document->styleEngine()->resolverAccessCount() - beforeCount;
280 }
281
282 unsigned Internals::needsLayoutCount(ExceptionState& exceptionState) const
283 {
284     LocalFrame* contextFrame = frame();
285     if (!contextFrame) {
286         exceptionState.throwDOMException(InvalidAccessError, "No context frame is available.");
287         return 0;
288     }
289
290     bool isPartial;
291     unsigned needsLayoutObjects;
292     unsigned totalObjects;
293     contextFrame->countObjectsNeedingLayout(needsLayoutObjects, totalObjects, isPartial);
294     return needsLayoutObjects;
295 }
296
297 bool Internals::isPreloaded(const String& url)
298 {
299     Document* document = contextDocument();
300     return document->fetcher()->isPreloaded(url);
301 }
302
303 bool Internals::isLoadingFromMemoryCache(const String& url)
304 {
305     if (!contextDocument())
306         return false;
307     Resource* resource = memoryCache()->resourceForURL(contextDocument()->completeURL(url));
308     return resource && resource->status() == Resource::Cached;
309 }
310
311 void Internals::crash()
312 {
313     CRASH();
314 }
315
316 void Internals::setStyleResolverStatsEnabled(bool enabled)
317 {
318     Document* document = contextDocument();
319     if (enabled)
320         document->ensureStyleResolver().enableStats(StyleResolver::ReportSlowStats);
321     else
322         document->ensureStyleResolver().disableStats();
323 }
324
325 String Internals::styleResolverStatsReport(ExceptionState& exceptionState) const
326 {
327     Document* document = contextDocument();
328     if (!document->ensureStyleResolver().stats()) {
329         exceptionState.throwDOMException(InvalidStateError, "Style resolver stats not enabled");
330         return String();
331     }
332     return document->ensureStyleResolver().stats()->report();
333 }
334
335 String Internals::styleResolverStatsTotalsReport(ExceptionState& exceptionState) const
336 {
337     Document* document = contextDocument();
338     if (!document->ensureStyleResolver().statsTotals()) {
339         exceptionState.throwDOMException(InvalidStateError, "Style resolver stats not enabled");
340         return String();
341     }
342     return document->ensureStyleResolver().statsTotals()->report();
343 }
344
345 bool Internals::isSharingStyle(Element* element1, Element* element2, ExceptionState& exceptionState) const
346 {
347     if (!element1 || !element2) {
348         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(element1 ? 2 : 1, "Element"));
349         return false;
350     }
351     return element1->renderStyle() == element2->renderStyle();
352 }
353
354 bool Internals::isValidContentSelect(Element* insertionPoint, ExceptionState& exceptionState)
355 {
356     if (!insertionPoint || !insertionPoint->isInsertionPoint()) {
357         exceptionState.throwDOMException(InvalidAccessError, "The insertion point provided is invalid.");
358         return false;
359     }
360
361     return isHTMLContentElement(*insertionPoint) && toHTMLContentElement(*insertionPoint).isSelectValid();
362 }
363
364 Node* Internals::treeScopeRootNode(Node* node, ExceptionState& exceptionState)
365 {
366     if (!node) {
367         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
368         return 0;
369     }
370
371     return &node->treeScope().rootNode();
372 }
373
374 Node* Internals::parentTreeScope(Node* node, ExceptionState& exceptionState)
375 {
376     if (!node) {
377         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
378         return 0;
379     }
380     const TreeScope* parentTreeScope = node->treeScope().parentTreeScope();
381     return parentTreeScope ? &parentTreeScope->rootNode() : 0;
382 }
383
384 bool Internals::hasSelectorForIdInShadow(Element* host, const AtomicString& idValue, ExceptionState& exceptionState)
385 {
386     if (!host || !host->shadow()) {
387         exceptionState.throwDOMException(InvalidAccessError, "The host element provided is invalid, or does not have a shadow.");
388         return 0;
389     }
390
391     return host->shadow()->ensureSelectFeatureSet().hasSelectorForId(idValue);
392 }
393
394 bool Internals::hasSelectorForClassInShadow(Element* host, const AtomicString& className, ExceptionState& exceptionState)
395 {
396     if (!host || !host->shadow()) {
397         exceptionState.throwDOMException(InvalidAccessError, "The host element provided is invalid, or does not have a shadow.");
398         return 0;
399     }
400
401     return host->shadow()->ensureSelectFeatureSet().hasSelectorForClass(className);
402 }
403
404 bool Internals::hasSelectorForAttributeInShadow(Element* host, const AtomicString& attributeName, ExceptionState& exceptionState)
405 {
406     if (!host || !host->shadow()) {
407         exceptionState.throwDOMException(InvalidAccessError, "The host element provided is invalid, or does not have a shadow.");
408         return 0;
409     }
410
411     return host->shadow()->ensureSelectFeatureSet().hasSelectorForAttribute(attributeName);
412 }
413
414 bool Internals::hasSelectorForPseudoClassInShadow(Element* host, const String& pseudoClass, ExceptionState& exceptionState)
415 {
416     if (!host || !host->shadow()) {
417         exceptionState.throwDOMException(InvalidAccessError, "The host element provided is invalid, or does not have a shadow.");
418         return 0;
419     }
420
421     const SelectRuleFeatureSet& featureSet = host->shadow()->ensureSelectFeatureSet();
422     if (pseudoClass == "checked")
423         return featureSet.hasSelectorForChecked();
424     if (pseudoClass == "enabled")
425         return featureSet.hasSelectorForEnabled();
426     if (pseudoClass == "disabled")
427         return featureSet.hasSelectorForDisabled();
428     if (pseudoClass == "indeterminate")
429         return featureSet.hasSelectorForIndeterminate();
430     if (pseudoClass == "link")
431         return featureSet.hasSelectorForLink();
432     if (pseudoClass == "target")
433         return featureSet.hasSelectorForTarget();
434     if (pseudoClass == "visited")
435         return featureSet.hasSelectorForVisited();
436
437     ASSERT_NOT_REACHED();
438     return false;
439 }
440
441 unsigned short Internals::compareTreeScopePosition(const Node* node1, const Node* node2, ExceptionState& exceptionState) const
442 {
443     if (!node1 || !node2) {
444         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(node1 ? 2 : 1, "Node"));
445         return 0;
446     }
447     const TreeScope* treeScope1 = node1->isDocumentNode() ? static_cast<const TreeScope*>(toDocument(node1)) :
448         node1->isShadowRoot() ? static_cast<const TreeScope*>(toShadowRoot(node1)) : 0;
449     const TreeScope* treeScope2 = node2->isDocumentNode() ? static_cast<const TreeScope*>(toDocument(node2)) :
450         node2->isShadowRoot() ? static_cast<const TreeScope*>(toShadowRoot(node2)) : 0;
451     if (!treeScope1 || !treeScope2) {
452         exceptionState.throwDOMException(InvalidAccessError, String::format("The %s node is neither a document node, nor a shadow root.", treeScope1 ? "second" : "first"));
453         return 0;
454     }
455     return treeScope1->comparePosition(*treeScope2);
456 }
457
458 unsigned Internals::numberOfActiveAnimations() const
459 {
460     LocalFrame* contextFrame = frame();
461     Document* document = contextFrame->document();
462     return document->timeline().numberOfActiveAnimationsForTesting() + document->transitionTimeline().numberOfActiveAnimationsForTesting();
463 }
464
465 void Internals::pauseAnimations(double pauseTime, ExceptionState& exceptionState)
466 {
467     if (pauseTime < 0) {
468         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::indexExceedsMinimumBound("pauseTime", pauseTime, 0.0));
469         return;
470     }
471
472     frame()->view()->updateLayoutAndStyleForPainting();
473     frame()->document()->timeline().pauseAnimationsForTesting(pauseTime);
474     frame()->document()->transitionTimeline().pauseAnimationsForTesting(pauseTime);
475 }
476
477 bool Internals::hasShadowInsertionPoint(const Node* root, ExceptionState& exceptionState) const
478 {
479     if (root && root->isShadowRoot())
480         return toShadowRoot(root)->containsShadowElements();
481
482     exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
483     return 0;
484 }
485
486 bool Internals::hasContentElement(const Node* root, ExceptionState& exceptionState) const
487 {
488     if (root && root->isShadowRoot())
489         return toShadowRoot(root)->containsContentElements();
490
491     exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
492     return 0;
493 }
494
495 size_t Internals::countElementShadow(const Node* root, ExceptionState& exceptionState) const
496 {
497     if (!root || !root->isShadowRoot()) {
498         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
499         return 0;
500     }
501     return toShadowRoot(root)->childShadowRootCount();
502 }
503
504 Node* Internals::nextSiblingByWalker(Node* node, ExceptionState& exceptionState)
505 {
506     if (!node) {
507         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
508         return 0;
509     }
510     ComposedTreeWalker walker(node);
511     walker.nextSibling();
512     return walker.get();
513 }
514
515 Node* Internals::firstChildByWalker(Node* node, ExceptionState& exceptionState)
516 {
517     if (!node) {
518         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
519         return 0;
520     }
521     ComposedTreeWalker walker(node);
522     walker.firstChild();
523     return walker.get();
524 }
525
526 Node* Internals::lastChildByWalker(Node* node, ExceptionState& exceptionState)
527 {
528     if (!node) {
529         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
530         return 0;
531     }
532     ComposedTreeWalker walker(node);
533     walker.lastChild();
534     return walker.get();
535 }
536
537 Node* Internals::nextNodeByWalker(Node* node, ExceptionState& exceptionState)
538 {
539     if (!node) {
540         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
541         return 0;
542     }
543     ComposedTreeWalker walker(node);
544     walker.next();
545     return walker.get();
546 }
547
548 Node* Internals::previousNodeByWalker(Node* node, ExceptionState& exceptionState)
549 {
550     if (!node) {
551         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
552         return 0;
553     }
554     ComposedTreeWalker walker(node);
555     walker.previous();
556     return walker.get();
557 }
558
559 String Internals::elementRenderTreeAsText(Element* element, ExceptionState& exceptionState)
560 {
561     if (!element) {
562         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
563         return String();
564     }
565
566     String representation = externalRepresentation(element);
567     if (representation.isEmpty()) {
568         exceptionState.throwDOMException(InvalidAccessError, "The element provided has no external representation.");
569         return String();
570     }
571
572     return representation;
573 }
574
575 size_t Internals::numberOfScopedHTMLStyleChildren(const Node* scope, ExceptionState& exceptionState) const
576 {
577     if (scope && (scope->isElementNode() || scope->isShadowRoot()))
578         return scope->numberOfScopedHTMLStyleChildren();
579
580     exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
581     return 0;
582 }
583
584 PassRefPtrWillBeRawPtr<CSSComputedStyleDeclaration> Internals::computedStyleIncludingVisitedInfo(Node* node, ExceptionState& exceptionState) const
585 {
586     if (!node) {
587         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
588         return nullptr;
589     }
590
591     bool allowVisitedStyle = true;
592     return CSSComputedStyleDeclaration::create(node, allowVisitedStyle);
593 }
594
595 ShadowRoot* Internals::shadowRoot(Element* host, ExceptionState& exceptionState)
596 {
597     // FIXME: Internals::shadowRoot() in tests should be converted to youngestShadowRoot() or oldestShadowRoot().
598     // https://bugs.webkit.org/show_bug.cgi?id=78465
599     return youngestShadowRoot(host, exceptionState);
600 }
601
602 ShadowRoot* Internals::youngestShadowRoot(Element* host, ExceptionState& exceptionState)
603 {
604     if (!host) {
605         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
606         return 0;
607     }
608
609     if (ElementShadow* shadow = host->shadow())
610         return shadow->youngestShadowRoot();
611     return 0;
612 }
613
614 ShadowRoot* Internals::oldestShadowRoot(Element* host, ExceptionState& exceptionState)
615 {
616     if (!host) {
617         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
618         return 0;
619     }
620
621     if (ElementShadow* shadow = host->shadow())
622         return shadow->oldestShadowRoot();
623     return 0;
624 }
625
626 ShadowRoot* Internals::youngerShadowRoot(Node* shadow, ExceptionState& exceptionState)
627 {
628     if (!shadow || !shadow->isShadowRoot()) {
629         exceptionState.throwDOMException(InvalidAccessError, "The node provided is not a valid shadow root.");
630         return 0;
631     }
632
633     return toShadowRoot(shadow)->youngerShadowRoot();
634 }
635
636 String Internals::shadowRootType(const Node* root, ExceptionState& exceptionState) const
637 {
638     if (!root || !root->isShadowRoot()) {
639         exceptionState.throwDOMException(InvalidAccessError, "The node provided is not a valid shadow root.");
640         return String();
641     }
642
643     switch (toShadowRoot(root)->type()) {
644     case ShadowRoot::UserAgentShadowRoot:
645         return String("UserAgentShadowRoot");
646     case ShadowRoot::AuthorShadowRoot:
647         return String("AuthorShadowRoot");
648     default:
649         ASSERT_NOT_REACHED();
650         return String("Unknown");
651     }
652 }
653
654 const AtomicString& Internals::shadowPseudoId(Element* element, ExceptionState& exceptionState)
655 {
656     if (!element) {
657         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
658         return nullAtom;
659     }
660
661     return element->shadowPseudoId();
662 }
663
664 void Internals::setShadowPseudoId(Element* element, const AtomicString& id, ExceptionState& exceptionState)
665 {
666     if (!element) {
667         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
668         return;
669     }
670
671     return element->setShadowPseudoId(id);
672 }
673
674 String Internals::visiblePlaceholder(Element* element)
675 {
676     if (element && isHTMLTextFormControlElement(*element)) {
677         if (toHTMLTextFormControlElement(element)->placeholderShouldBeVisible())
678             return toHTMLTextFormControlElement(element)->placeholderElement()->textContent();
679     }
680
681     return String();
682 }
683
684 void Internals::selectColorInColorChooser(Element* element, const String& colorValue)
685 {
686     ASSERT(element);
687     if (!isHTMLInputElement(*element))
688         return;
689     Color color;
690     if (!color.setFromString(colorValue))
691         return;
692     toHTMLInputElement(*element).selectColorInColorChooser(color);
693 }
694
695 bool Internals::hasAutofocusRequest(Document* document)
696 {
697     if (!document)
698         document = contextDocument();
699     return document->autofocusElement();
700 }
701
702 bool Internals::hasAutofocusRequest()
703 {
704     return hasAutofocusRequest(0);
705 }
706
707 Vector<String> Internals::formControlStateOfHistoryItem(ExceptionState& exceptionState)
708 {
709     HistoryItem* mainItem = frame()->loader().currentItem();
710     if (!mainItem) {
711         exceptionState.throwDOMException(InvalidAccessError, "No history item is available.");
712         return Vector<String>();
713     }
714     return mainItem->documentState();
715 }
716
717 void Internals::setFormControlStateOfHistoryItem(const Vector<String>& state, ExceptionState& exceptionState)
718 {
719     HistoryItem* mainItem = frame()->loader().currentItem();
720     if (!mainItem) {
721         exceptionState.throwDOMException(InvalidAccessError, "No history item is available.");
722         return;
723     }
724     mainItem->clearDocumentState();
725     mainItem->setDocumentState(state);
726 }
727
728 void Internals::setEnableMockPagePopup(bool enabled, ExceptionState& exceptionState)
729 {
730     Document* document = contextDocument();
731     if (!document || !document->page())
732         return;
733     Page* page = document->page();
734     if (!enabled) {
735         page->chrome().client().resetPagePopupDriver();
736         return;
737     }
738     if (!s_pagePopupDriver)
739         s_pagePopupDriver = MockPagePopupDriver::create(page->mainFrame()).leakPtr();
740     page->chrome().client().setPagePopupDriver(s_pagePopupDriver);
741 }
742
743 PassRefPtrWillBeRawPtr<PagePopupController> Internals::pagePopupController()
744 {
745     return s_pagePopupDriver ? s_pagePopupDriver->pagePopupController() : 0;
746 }
747
748 PassRefPtrWillBeRawPtr<ClientRect> Internals::unscaledViewportRect(ExceptionState& exceptionState)
749 {
750     Document* document = contextDocument();
751     if (!document || !document->view()) {
752         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's viewport cannot be retrieved." : "No context document can be obtained.");
753         return ClientRect::create();
754     }
755
756     return ClientRect::create(document->view()->visibleContentRect());
757 }
758
759 PassRefPtrWillBeRawPtr<ClientRect> Internals::absoluteCaretBounds(ExceptionState& exceptionState)
760 {
761     Document* document = contextDocument();
762     if (!document || !document->frame()) {
763         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "No context document can be obtained.");
764         return ClientRect::create();
765     }
766
767     return ClientRect::create(document->frame()->selection().absoluteCaretBounds());
768 }
769
770 PassRefPtrWillBeRawPtr<ClientRect> Internals::boundingBox(Element* element, ExceptionState& exceptionState)
771 {
772     if (!element) {
773         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
774         return ClientRect::create();
775     }
776
777     element->document().updateLayoutIgnorePendingStylesheets();
778     RenderObject* renderer = element->renderer();
779     if (!renderer)
780         return ClientRect::create();
781     return ClientRect::create(renderer->absoluteBoundingBoxRectIgnoringTransforms());
782 }
783
784 unsigned Internals::markerCountForNode(Node* node, const String& markerType, ExceptionState& exceptionState)
785 {
786     if (!node) {
787         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
788         return 0;
789     }
790
791     DocumentMarker::MarkerTypes markerTypes = 0;
792     if (!markerTypesFrom(markerType, markerTypes)) {
793         exceptionState.throwDOMException(SyntaxError, "The marker type provided ('" + markerType + "') is invalid.");
794         return 0;
795     }
796
797     return node->document().markers().markersFor(node, markerTypes).size();
798 }
799
800 unsigned Internals::activeMarkerCountForNode(Node* node, ExceptionState& exceptionState)
801 {
802     if (!node) {
803         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
804         return 0;
805     }
806
807     // Only TextMatch markers can be active.
808     DocumentMarker::MarkerType markerType = DocumentMarker::TextMatch;
809     Vector<DocumentMarker*> markers = node->document().markers().markersFor(node, markerType);
810
811     unsigned activeMarkerCount = 0;
812     for (Vector<DocumentMarker*>::iterator iter = markers.begin(); iter != markers.end(); ++iter) {
813         if ((*iter)->activeMatch())
814             activeMarkerCount++;
815     }
816
817     return activeMarkerCount;
818 }
819
820 DocumentMarker* Internals::markerAt(Node* node, const String& markerType, unsigned index, ExceptionState& exceptionState)
821 {
822     if (!node) {
823         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
824         return 0;
825     }
826
827     DocumentMarker::MarkerTypes markerTypes = 0;
828     if (!markerTypesFrom(markerType, markerTypes)) {
829         exceptionState.throwDOMException(SyntaxError, "The marker type provided ('" + markerType + "') is invalid.");
830         return 0;
831     }
832
833     Vector<DocumentMarker*> markers = node->document().markers().markersFor(node, markerTypes);
834     if (markers.size() <= index)
835         return 0;
836     return markers[index];
837 }
838
839 PassRefPtrWillBeRawPtr<Range> Internals::markerRangeForNode(Node* node, const String& markerType, unsigned index, ExceptionState& exceptionState)
840 {
841     DocumentMarker* marker = markerAt(node, markerType, index, exceptionState);
842     if (!marker)
843         return nullptr;
844     return Range::create(node->document(), node, marker->startOffset(), node, marker->endOffset());
845 }
846
847 String Internals::markerDescriptionForNode(Node* node, const String& markerType, unsigned index, ExceptionState& exceptionState)
848 {
849     DocumentMarker* marker = markerAt(node, markerType, index, exceptionState);
850     if (!marker)
851         return String();
852     return marker->description();
853 }
854
855 void Internals::addTextMatchMarker(const Range* range, bool isActive)
856 {
857     range->ownerDocument().updateLayoutIgnorePendingStylesheets();
858     range->ownerDocument().markers().addTextMatchMarker(range, isActive);
859 }
860
861 void Internals::setMarkersActive(Node* node, unsigned startOffset, unsigned endOffset, bool active, ExceptionState& exceptionState)
862 {
863     if (!node) {
864         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Node"));
865         return;
866     }
867
868     node->document().markers().setMarkersActive(node, startOffset, endOffset, active);
869 }
870
871 void Internals::setMarkedTextMatchesAreHighlighted(Document* document, bool highlight, ExceptionState&)
872 {
873     if (!document || !document->frame())
874         return;
875
876     document->frame()->editor().setMarkedTextMatchesAreHighlighted(highlight);
877 }
878
879 void Internals::setScrollViewPosition(Document* document, long x, long y, ExceptionState& exceptionState)
880 {
881     if (!document || !document->view()) {
882         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's view cannot be retrieved." : "The document provided is invalid.");
883         return;
884     }
885
886     FrameView* frameView = document->view();
887     bool constrainsScrollingToContentEdgeOldValue = frameView->constrainsScrollingToContentEdge();
888     bool scrollbarsSuppressedOldValue = frameView->scrollbarsSuppressed();
889
890     frameView->setConstrainsScrollingToContentEdge(false);
891     frameView->setScrollbarsSuppressed(false);
892     frameView->setScrollOffsetFromInternals(IntPoint(x, y));
893     frameView->setScrollbarsSuppressed(scrollbarsSuppressedOldValue);
894     frameView->setConstrainsScrollingToContentEdge(constrainsScrollingToContentEdgeOldValue);
895 }
896
897 String Internals::viewportAsText(Document* document, float, int availableWidth, int availableHeight, ExceptionState& exceptionState)
898 {
899     if (!document || !document->page()) {
900         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's page cannot be retrieved." : "The document provided is invalid.");
901         return String();
902     }
903
904     document->updateLayoutIgnorePendingStylesheets();
905
906     Page* page = document->page();
907
908     // Update initial viewport size.
909     IntSize initialViewportSize(availableWidth, availableHeight);
910     document->page()->mainFrame()->view()->setFrameRect(IntRect(IntPoint::zero(), initialViewportSize));
911
912     ViewportDescription description = page->viewportDescription();
913     PageScaleConstraints constraints = description.resolve(initialViewportSize, Length());
914
915     constraints.fitToContentsWidth(constraints.layoutSize.width(), availableWidth);
916
917     StringBuilder builder;
918
919     builder.appendLiteral("viewport size ");
920     builder.append(String::number(constraints.layoutSize.width()));
921     builder.append('x');
922     builder.append(String::number(constraints.layoutSize.height()));
923
924     builder.appendLiteral(" scale ");
925     builder.append(String::number(constraints.initialScale));
926     builder.appendLiteral(" with limits [");
927     builder.append(String::number(constraints.minimumScale));
928     builder.appendLiteral(", ");
929     builder.append(String::number(constraints.maximumScale));
930
931     builder.appendLiteral("] and userScalable ");
932     builder.append(description.userZoom ? "true" : "false");
933
934     return builder.toString();
935 }
936
937 bool Internals::wasLastChangeUserEdit(Element* textField, ExceptionState& exceptionState)
938 {
939     if (!textField) {
940         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
941         return false;
942     }
943
944     if (isHTMLInputElement(*textField))
945         return toHTMLInputElement(*textField).lastChangeWasUserEdit();
946
947     if (isHTMLTextAreaElement(*textField))
948         return toHTMLTextAreaElement(*textField).lastChangeWasUserEdit();
949
950     exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not a TEXTAREA.");
951     return false;
952 }
953
954 bool Internals::elementShouldAutoComplete(Element* element, ExceptionState& exceptionState)
955 {
956     if (!element) {
957         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
958         return false;
959     }
960
961     if (isHTMLInputElement(*element))
962         return toHTMLInputElement(*element).shouldAutocomplete();
963
964     exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not an INPUT.");
965     return false;
966 }
967
968 String Internals::suggestedValue(Element* element, ExceptionState& exceptionState)
969 {
970     if (!element) {
971         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
972         return String();
973     }
974
975     if (!element->isFormControlElement()) {
976         exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not a form control element.");
977         return String();
978     }
979
980     String suggestedValue;
981     if (isHTMLInputElement(*element))
982         suggestedValue = toHTMLInputElement(*element).suggestedValue();
983
984     if (isHTMLTextAreaElement(*element))
985         suggestedValue = toHTMLTextAreaElement(*element).suggestedValue();
986
987     if (isHTMLSelectElement(*element))
988         suggestedValue = toHTMLSelectElement(*element).suggestedValue();
989
990     return suggestedValue;
991 }
992
993 void Internals::setSuggestedValue(Element* element, const String& value, ExceptionState& exceptionState)
994 {
995     if (!element) {
996         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
997         return;
998     }
999
1000     if (!element->isFormControlElement()) {
1001         exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not a form control element.");
1002         return;
1003     }
1004
1005     if (isHTMLInputElement(*element))
1006         toHTMLInputElement(*element).setSuggestedValue(value);
1007
1008     if (isHTMLTextAreaElement(*element))
1009         toHTMLTextAreaElement(*element).setSuggestedValue(value);
1010
1011     if (isHTMLSelectElement(*element))
1012         toHTMLSelectElement(*element).setSuggestedValue(value);
1013 }
1014
1015 void Internals::setEditingValue(Element* element, const String& value, ExceptionState& exceptionState)
1016 {
1017     if (!element) {
1018         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1019         return;
1020     }
1021
1022     if (!isHTMLInputElement(*element)) {
1023         exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not an INPUT.");
1024         return;
1025     }
1026
1027     toHTMLInputElement(*element).setEditingValue(value);
1028 }
1029
1030 void Internals::setAutofilled(Element* element, bool enabled, ExceptionState& exceptionState)
1031 {
1032     if (!element->isFormControlElement()) {
1033         exceptionState.throwDOMException(InvalidNodeTypeError, "The element provided is not a form control element.");
1034         return;
1035     }
1036     toHTMLFormControlElement(element)->setAutofilled(enabled);
1037 }
1038
1039 void Internals::scrollElementToRect(Element* element, long x, long y, long w, long h, ExceptionState& exceptionState)
1040 {
1041     if (!element || !element->document().view()) {
1042         exceptionState.throwDOMException(InvalidNodeTypeError, element ? "No view can be obtained from the provided element's document." : ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1043         return;
1044     }
1045     FrameView* frameView = element->document().view();
1046     frameView->scrollElementToRect(element, IntRect(x, y, w, h));
1047 }
1048
1049 PassRefPtrWillBeRawPtr<Range> Internals::rangeFromLocationAndLength(Element* scope, int rangeLocation, int rangeLength, ExceptionState& exceptionState)
1050 {
1051     if (!scope) {
1052         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1053         return nullptr;
1054     }
1055
1056     // TextIterator depends on Layout information, make sure layout it up to date.
1057     scope->document().updateLayoutIgnorePendingStylesheets();
1058
1059     return PlainTextRange(rangeLocation, rangeLocation + rangeLength).createRange(*scope);
1060 }
1061
1062 unsigned Internals::locationFromRange(Element* scope, const Range* range, ExceptionState& exceptionState)
1063 {
1064     if (!scope || !range) {
1065         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(scope ? 2 : 1, scope ? "Range" : "Element"));
1066         return 0;
1067     }
1068
1069     // PlainTextRange depends on Layout information, make sure layout it up to date.
1070     scope->document().updateLayoutIgnorePendingStylesheets();
1071
1072     return PlainTextRange::create(*scope, *range).start();
1073 }
1074
1075 unsigned Internals::lengthFromRange(Element* scope, const Range* range, ExceptionState& exceptionState)
1076 {
1077     if (!scope || !range) {
1078         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(scope ? 2 : 1, scope ? "Range" : "Element"));
1079         return 0;
1080     }
1081
1082     // PlainTextRange depends on Layout information, make sure layout it up to date.
1083     scope->document().updateLayoutIgnorePendingStylesheets();
1084
1085     return PlainTextRange::create(*scope, *range).length();
1086 }
1087
1088 String Internals::rangeAsText(const Range* range, ExceptionState& exceptionState)
1089 {
1090     if (!range) {
1091         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Range"));
1092         return String();
1093     }
1094
1095     return range->text();
1096 }
1097
1098 PassRefPtrWillBeRawPtr<DOMPoint> Internals::touchPositionAdjustedToBestClickableNode(long x, long y, long width, long height, Document* document, ExceptionState& exceptionState)
1099 {
1100     if (!document || !document->frame()) {
1101         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1102         return nullptr;
1103     }
1104
1105     document->updateLayout();
1106
1107     IntSize radius(width / 2, height / 2);
1108     IntPoint point(x + radius.width(), y + radius.height());
1109
1110     Node* targetNode;
1111     IntPoint adjustedPoint;
1112
1113     bool foundNode = document->frame()->eventHandler().bestClickableNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1114     if (foundNode)
1115         return DOMPoint::create(adjustedPoint.x(), adjustedPoint.y());
1116
1117     return nullptr;
1118 }
1119
1120 Node* Internals::touchNodeAdjustedToBestClickableNode(long x, long y, long width, long height, Document* document, ExceptionState& exceptionState)
1121 {
1122     if (!document || !document->frame()) {
1123         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1124         return 0;
1125     }
1126
1127     document->updateLayout();
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 PassRefPtrWillBeRawPtr<DOMPoint> Internals::touchPositionAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document* document, ExceptionState& exceptionState)
1139 {
1140     if (!document || !document->frame()) {
1141         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1142         return nullptr;
1143     }
1144
1145     document->updateLayout();
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
1153     bool foundNode = document->frame()->eventHandler().bestContextMenuNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1154     if (foundNode)
1155         return DOMPoint::create(adjustedPoint.x(), adjustedPoint.y());
1156
1157     return DOMPoint::create(x, y);
1158 }
1159
1160 Node* Internals::touchNodeAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document* document, ExceptionState& exceptionState)
1161 {
1162     if (!document || !document->frame()) {
1163         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1164         return 0;
1165     }
1166
1167     document->updateLayout();
1168
1169     IntSize radius(width / 2, height / 2);
1170     IntPoint point(x + radius.width(), y + radius.height());
1171
1172     Node* targetNode = 0;
1173     IntPoint adjustedPoint;
1174     document->frame()->eventHandler().bestContextMenuNodeForTouchPoint(point, radius, adjustedPoint, targetNode);
1175     return targetNode;
1176 }
1177
1178 PassRefPtrWillBeRawPtr<ClientRect> Internals::bestZoomableAreaForTouchPoint(long x, long y, long width, long height, Document* document, ExceptionState& exceptionState)
1179 {
1180     if (!document || !document->frame()) {
1181         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1182         return nullptr;
1183     }
1184
1185     document->updateLayout();
1186
1187     IntSize radius(width / 2, height / 2);
1188     IntPoint point(x + radius.width(), y + radius.height());
1189
1190     Node* targetNode;
1191     IntRect zoomableArea;
1192     bool foundNode = document->frame()->eventHandler().bestZoomableAreaForTouchPoint(point, radius, zoomableArea, targetNode);
1193     if (foundNode)
1194         return ClientRect::create(zoomableArea);
1195
1196     return nullptr;
1197 }
1198
1199
1200 int Internals::lastSpellCheckRequestSequence(Document* document, ExceptionState& exceptionState)
1201 {
1202     SpellCheckRequester* requester = spellCheckRequester(document);
1203
1204     if (!requester) {
1205         exceptionState.throwDOMException(InvalidAccessError, "No spell check requestor can be obtained for the provided document.");
1206         return -1;
1207     }
1208
1209     return requester->lastRequestSequence();
1210 }
1211
1212 int Internals::lastSpellCheckProcessedSequence(Document* document, ExceptionState& exceptionState)
1213 {
1214     SpellCheckRequester* requester = spellCheckRequester(document);
1215
1216     if (!requester) {
1217         exceptionState.throwDOMException(InvalidAccessError, "No spell check requestor can be obtained for the provided document.");
1218         return -1;
1219     }
1220
1221     return requester->lastProcessedSequence();
1222 }
1223
1224 Vector<AtomicString> Internals::userPreferredLanguages() const
1225 {
1226     return WebCore::userPreferredLanguages();
1227 }
1228
1229 // Optimally, the bindings generator would pass a Vector<AtomicString> here but
1230 // this is not supported yet.
1231 void Internals::setUserPreferredLanguages(const Vector<String>& languages)
1232 {
1233     Vector<AtomicString> atomicLanguages;
1234     for (size_t i = 0; i < languages.size(); ++i)
1235         atomicLanguages.append(AtomicString(languages[i]));
1236     WebCore::overrideUserPreferredLanguages(atomicLanguages);
1237 }
1238
1239 unsigned Internals::activeDOMObjectCount(Document* document, ExceptionState& exceptionState)
1240 {
1241     if (!document) {
1242         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1243         return 0;
1244     }
1245
1246     return document->activeDOMObjectCount();
1247 }
1248
1249 static unsigned eventHandlerCount(Document& document, EventHandlerRegistry::EventHandlerClass handlerClass)
1250 {
1251     if (!document.frameHost())
1252         return 0;
1253     EventHandlerRegistry* registry = &document.frameHost()->eventHandlerRegistry();
1254     unsigned count = 0;
1255     const EventTargetSet* targets = registry->eventHandlerTargets(handlerClass);
1256     if (targets) {
1257         for (EventTargetSet::const_iterator iter = targets->begin(); iter != targets->end(); ++iter)
1258             count += iter->value;
1259     }
1260     return count;
1261 }
1262
1263 unsigned Internals::wheelEventHandlerCount(Document* document, ExceptionState& exceptionState)
1264 {
1265     if (!document) {
1266         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1267         return 0;
1268     }
1269
1270     return WheelController::from(*document)->wheelEventHandlerCount();
1271 }
1272
1273 unsigned Internals::scrollEventHandlerCount(Document* document, ExceptionState& exceptionState)
1274 {
1275     if (!document) {
1276         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1277         return 0;
1278     }
1279
1280     return eventHandlerCount(*document, EventHandlerRegistry::ScrollEvent);
1281 }
1282
1283 unsigned Internals::touchEventHandlerCount(Document* document, ExceptionState& exceptionState)
1284 {
1285     if (!document) {
1286         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1287         return 0;
1288     }
1289
1290     const TouchEventTargetSet* touchHandlers = document->touchEventTargets();
1291     if (!touchHandlers)
1292         return 0;
1293
1294     unsigned count = 0;
1295     for (TouchEventTargetSet::const_iterator iter = touchHandlers->begin(); iter != touchHandlers->end(); ++iter)
1296         count += iter->value;
1297     return count;
1298 }
1299
1300 static RenderLayer* findRenderLayerForGraphicsLayer(RenderLayer* searchRoot, GraphicsLayer* graphicsLayer, IntSize* layerOffset, String* layerType)
1301 {
1302     *layerOffset = IntSize();
1303     if (searchRoot->hasCompositedLayerMapping() && graphicsLayer == searchRoot->compositedLayerMapping()->mainGraphicsLayer()) {
1304         CompositedLayerMappingPtr compositedLayerMapping = searchRoot->compositedLayerMapping();
1305         LayoutSize offset = compositedLayerMapping->contentOffsetInCompositingLayer();
1306         *layerOffset = IntSize(offset.width(), offset.height());
1307         return searchRoot;
1308     }
1309
1310     GraphicsLayer* layerForScrolling = searchRoot->scrollableArea() ? searchRoot->scrollableArea()->layerForScrolling() : 0;
1311     if (graphicsLayer == layerForScrolling) {
1312         *layerType = "scrolling";
1313         return searchRoot;
1314     }
1315
1316     if (searchRoot->compositingState() == PaintsIntoGroupedBacking) {
1317         GraphicsLayer* squashingLayer = searchRoot->groupedMapping()->squashingLayer();
1318         if (graphicsLayer == squashingLayer) {
1319             *layerType ="squashing";
1320             *layerOffset = -searchRoot->offsetFromSquashingLayerOrigin();
1321             return searchRoot;
1322         }
1323     }
1324
1325     GraphicsLayer* layerForHorizontalScrollbar = searchRoot->scrollableArea() ? searchRoot->scrollableArea()->layerForHorizontalScrollbar() : 0;
1326     if (graphicsLayer == layerForHorizontalScrollbar) {
1327         *layerType = "horizontalScrollbar";
1328         return searchRoot;
1329     }
1330
1331     GraphicsLayer* layerForVerticalScrollbar = searchRoot->scrollableArea() ? searchRoot->scrollableArea()->layerForVerticalScrollbar() : 0;
1332     if (graphicsLayer == layerForVerticalScrollbar) {
1333         *layerType = "verticalScrollbar";
1334         return searchRoot;
1335     }
1336
1337     GraphicsLayer* layerForScrollCorner = searchRoot->scrollableArea() ? searchRoot->scrollableArea()->layerForScrollCorner() : 0;
1338     if (graphicsLayer == layerForScrollCorner) {
1339         *layerType = "scrollCorner";
1340         return searchRoot;
1341     }
1342
1343     // Search right to left to increase the chances that we'll choose the top-most layers in a
1344     // grouped mapping for squashing.
1345     for (RenderLayer* child = searchRoot->lastChild(); child; child = child->previousSibling()) {
1346         RenderLayer* foundLayer = findRenderLayerForGraphicsLayer(child, graphicsLayer, layerOffset, layerType);
1347         if (foundLayer)
1348             return foundLayer;
1349     }
1350
1351     return 0;
1352 }
1353
1354 // Given a vector of rects, merge those that are adjacent, leaving empty rects
1355 // in the place of no longer used slots. This is intended to simplify the list
1356 // of rects returned by an SkRegion (which have been split apart for sorting
1357 // purposes). No attempt is made to do this efficiently (eg. by relying on the
1358 // sort criteria of SkRegion).
1359 static void mergeRects(blink::WebVector<blink::WebRect>& rects)
1360 {
1361     for (size_t i = 0; i < rects.size(); ++i) {
1362         if (rects[i].isEmpty())
1363             continue;
1364         bool updated;
1365         do {
1366             updated = false;
1367             for (size_t j = i+1; j < rects.size(); ++j) {
1368                 if (rects[j].isEmpty())
1369                     continue;
1370                 // Try to merge rects[j] into rects[i] along the 4 possible edges.
1371                 if (rects[i].y == rects[j].y && rects[i].height == rects[j].height) {
1372                     if (rects[i].x + rects[i].width == rects[j].x) {
1373                         rects[i].width += rects[j].width;
1374                         rects[j] = blink::WebRect();
1375                         updated = true;
1376                     } else if (rects[i].x == rects[j].x + rects[j].width) {
1377                         rects[i].x = rects[j].x;
1378                         rects[i].width += rects[j].width;
1379                         rects[j] = blink::WebRect();
1380                         updated = true;
1381                     }
1382                 } else if (rects[i].x == rects[j].x && rects[i].width == rects[j].width) {
1383                     if (rects[i].y + rects[i].height == rects[j].y) {
1384                         rects[i].height += rects[j].height;
1385                         rects[j] = blink::WebRect();
1386                         updated = true;
1387                     } else if (rects[i].y == rects[j].y + rects[j].height) {
1388                         rects[i].y = rects[j].y;
1389                         rects[i].height += rects[j].height;
1390                         rects[j] = blink::WebRect();
1391                         updated = true;
1392                     }
1393                 }
1394             }
1395         } while (updated);
1396     }
1397 }
1398
1399 static void accumulateLayerRectList(RenderLayerCompositor* compositor, GraphicsLayer* graphicsLayer, LayerRectList* rects)
1400 {
1401     blink::WebVector<blink::WebRect> layerRects = graphicsLayer->platformLayer()->touchEventHandlerRegion();
1402     if (!layerRects.isEmpty()) {
1403         mergeRects(layerRects);
1404         String layerType;
1405         IntSize layerOffset;
1406         RenderLayer* renderLayer = findRenderLayerForGraphicsLayer(compositor->rootRenderLayer(), graphicsLayer, &layerOffset, &layerType);
1407         Node* node = renderLayer ? renderLayer->renderer()->node() : 0;
1408         for (size_t i = 0; i < layerRects.size(); ++i) {
1409             if (!layerRects[i].isEmpty()) {
1410                 rects->append(node, layerType, layerOffset.width(), layerOffset.height(), ClientRect::create(layerRects[i]));
1411             }
1412         }
1413     }
1414
1415     size_t numChildren = graphicsLayer->children().size();
1416     for (size_t i = 0; i < numChildren; ++i)
1417         accumulateLayerRectList(compositor, graphicsLayer->children()[i], rects);
1418 }
1419
1420 PassRefPtrWillBeRawPtr<LayerRectList> Internals::touchEventTargetLayerRects(Document* document, ExceptionState& exceptionState)
1421 {
1422     if (!document || !document->view() || !document->page() || document != contextDocument()) {
1423         exceptionState.throwDOMException(InvalidAccessError, "The document provided is invalid.");
1424         return nullptr;
1425     }
1426
1427     // Do any pending layout and compositing update (which may call touchEventTargetRectsChange) to ensure this
1428     // really takes any previous changes into account.
1429     forceCompositingUpdate(document, exceptionState);
1430     if (exceptionState.hadException())
1431         return nullptr;
1432
1433     if (RenderView* view = document->renderView()) {
1434         if (RenderLayerCompositor* compositor = view->compositor()) {
1435             if (GraphicsLayer* rootLayer = compositor->rootGraphicsLayer()) {
1436                 RefPtrWillBeRawPtr<LayerRectList> rects = LayerRectList::create();
1437                 accumulateLayerRectList(compositor, rootLayer, rects.get());
1438                 return rects;
1439             }
1440         }
1441     }
1442
1443     return nullptr;
1444 }
1445
1446 PassRefPtr<NodeList> Internals::nodesFromRect(Document* document, int centerX, int centerY, unsigned topPadding, unsigned rightPadding,
1447     unsigned bottomPadding, unsigned leftPadding, bool ignoreClipping, bool allowShadowContent, bool allowChildFrameContent, ExceptionState& exceptionState) const
1448 {
1449     if (!document || !document->frame() || !document->frame()->view()) {
1450         exceptionState.throwDOMException(InvalidAccessError, "No view can be obtained from the provided document.");
1451         return nullptr;
1452     }
1453
1454     LocalFrame* frame = document->frame();
1455     FrameView* frameView = document->view();
1456     RenderView* renderView = document->renderView();
1457
1458     if (!renderView)
1459         return nullptr;
1460
1461     float zoomFactor = frame->pageZoomFactor();
1462     LayoutPoint point = roundedLayoutPoint(FloatPoint(centerX * zoomFactor + frameView->scrollX(), centerY * zoomFactor + frameView->scrollY()));
1463
1464     HitTestRequest::HitTestRequestType hitType = HitTestRequest::ReadOnly | HitTestRequest::Active;
1465     if (ignoreClipping)
1466         hitType |= HitTestRequest::IgnoreClipping;
1467     if (!allowShadowContent)
1468         hitType |= HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent;
1469     if (allowChildFrameContent)
1470         hitType |= HitTestRequest::AllowChildFrameContent;
1471
1472     HitTestRequest request(hitType);
1473
1474     // When ignoreClipping is false, this method returns null for coordinates outside of the viewport.
1475     if (!request.ignoreClipping() && !frameView->visibleContentRect().intersects(HitTestLocation::rectForPoint(point, topPadding, rightPadding, bottomPadding, leftPadding)))
1476         return nullptr;
1477
1478     Vector<RefPtr<Node> > matches;
1479
1480     // Need padding to trigger a rect based hit test, but we want to return a NodeList
1481     // so we special case this.
1482     if (!topPadding && !rightPadding && !bottomPadding && !leftPadding) {
1483         HitTestResult result(point);
1484         renderView->hitTest(request, result);
1485         if (result.innerNode())
1486             matches.append(result.innerNode()->deprecatedShadowAncestorNode());
1487     } else {
1488         HitTestResult result(point, topPadding, rightPadding, bottomPadding, leftPadding);
1489         renderView->hitTest(request, result);
1490         copyToVector(result.rectBasedTestResult(), matches);
1491     }
1492
1493     return StaticNodeList::adopt(matches);
1494 }
1495
1496 void Internals::emitInspectorDidBeginFrame(int frameId)
1497 {
1498     contextDocument()->page()->inspectorController().didBeginFrame(frameId);
1499 }
1500
1501 void Internals::emitInspectorDidCancelFrame()
1502 {
1503     contextDocument()->page()->inspectorController().didCancelFrame();
1504 }
1505
1506 bool Internals::hasSpellingMarker(Document* document, int from, int length, ExceptionState&)
1507 {
1508     if (!document || !document->frame())
1509         return 0;
1510
1511     return document->frame()->spellChecker().selectionStartHasMarkerFor(DocumentMarker::Spelling, from, length);
1512 }
1513
1514 void Internals::setContinuousSpellCheckingEnabled(bool enabled, ExceptionState&)
1515 {
1516     if (!contextDocument() || !contextDocument()->frame())
1517         return;
1518
1519     if (enabled != contextDocument()->frame()->spellChecker().isContinuousSpellCheckingEnabled())
1520         contextDocument()->frame()->spellChecker().toggleContinuousSpellChecking();
1521 }
1522
1523 bool Internals::isOverwriteModeEnabled(Document* document, ExceptionState&)
1524 {
1525     if (!document || !document->frame())
1526         return 0;
1527
1528     return document->frame()->editor().isOverwriteModeEnabled();
1529 }
1530
1531 void Internals::toggleOverwriteModeEnabled(Document* document, ExceptionState&)
1532 {
1533     if (!document || !document->frame())
1534         return;
1535
1536     document->frame()->editor().toggleOverwriteModeEnabled();
1537 }
1538
1539 unsigned Internals::numberOfLiveNodes() const
1540 {
1541     return InspectorCounters::counterValue(InspectorCounters::NodeCounter);
1542 }
1543
1544 unsigned Internals::numberOfLiveDocuments() const
1545 {
1546     return InspectorCounters::counterValue(InspectorCounters::DocumentCounter);
1547 }
1548
1549 String Internals::dumpRefCountedInstanceCounts() const
1550 {
1551     return WTF::dumpRefCountedInstanceCounts();
1552 }
1553
1554 Vector<String> Internals::consoleMessageArgumentCounts(Document* document) const
1555 {
1556     InstrumentingAgents* instrumentingAgents = instrumentationForPage(document->page());
1557     if (!instrumentingAgents)
1558         return Vector<String>();
1559     InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent();
1560     if (!consoleAgent)
1561         return Vector<String>();
1562     Vector<unsigned> counts = consoleAgent->consoleMessageArgumentCounts();
1563     Vector<String> result(counts.size());
1564     for (size_t i = 0; i < counts.size(); i++)
1565         result[i] = String::number(counts[i]);
1566     return result;
1567 }
1568
1569 Vector<unsigned long> Internals::setMemoryCacheCapacities(unsigned long minDeadBytes, unsigned long maxDeadBytes, unsigned long totalBytes)
1570 {
1571     Vector<unsigned long> result;
1572     result.append(memoryCache()->minDeadCapacity());
1573     result.append(memoryCache()->maxDeadCapacity());
1574     result.append(memoryCache()->capacity());
1575     memoryCache()->setCapacities(minDeadBytes, maxDeadBytes, totalBytes);
1576     return result;
1577 }
1578
1579 void Internals::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionState& exceptionState)
1580 {
1581     Page* page = contextDocument()->frame()->page();
1582     if (!page) {
1583         exceptionState.throwDOMException(InvalidAccessError, "No page can be obtained from the current context document.");
1584         return;
1585     }
1586     page->inspectorController().setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
1587 }
1588
1589 bool Internals::hasGrammarMarker(Document* document, int from, int length, ExceptionState&)
1590 {
1591     if (!document || !document->frame())
1592         return 0;
1593
1594     return document->frame()->spellChecker().selectionStartHasMarkerFor(DocumentMarker::Grammar, from, length);
1595 }
1596
1597 unsigned Internals::numberOfScrollableAreas(Document* document, ExceptionState&)
1598 {
1599     unsigned count = 0;
1600     LocalFrame* frame = document->frame();
1601     if (frame->view()->scrollableAreas())
1602         count += frame->view()->scrollableAreas()->size();
1603
1604     for (LocalFrame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
1605         if (child->view() && child->view()->scrollableAreas())
1606             count += child->view()->scrollableAreas()->size();
1607     }
1608
1609     return count;
1610 }
1611
1612 bool Internals::isPageBoxVisible(Document* document, int pageNumber, ExceptionState& exceptionState)
1613 {
1614     if (!document) {
1615         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1616         return false;
1617     }
1618
1619     return document->isPageBoxVisible(pageNumber);
1620 }
1621
1622 String Internals::layerTreeAsText(Document* document, ExceptionState& exceptionState) const
1623 {
1624     return layerTreeAsText(document, 0, exceptionState);
1625 }
1626
1627 String Internals::elementLayerTreeAsText(Element* element, ExceptionState& exceptionState) const
1628 {
1629     if (!element) {
1630         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1631         return String();
1632     }
1633
1634     FrameView* frameView = element->document().view();
1635     frameView->updateLayoutAndStyleForPainting();
1636
1637     return elementLayerTreeAsText(element, 0, exceptionState);
1638 }
1639
1640 bool Internals::scrollsWithRespectTo(Element* element1, Element* element2, ExceptionState& exceptionState)
1641 {
1642     if (!element1 || !element2) {
1643         exceptionState.throwDOMException(InvalidAccessError, String::format("The %s element provided is invalid.", element1 ? "second" : "first"));
1644         return 0;
1645     }
1646
1647     element1->document().updateLayout();
1648
1649     RenderObject* renderer1 = element1->renderer();
1650     RenderObject* renderer2 = element2->renderer();
1651     if (!renderer1 || !renderer1->isBox()) {
1652         exceptionState.throwDOMException(InvalidAccessError, renderer1 ? "The first provided element's renderer is not a box." : "The first provided element has no renderer.");
1653         return 0;
1654     }
1655     if (!renderer2 || !renderer2->isBox()) {
1656         exceptionState.throwDOMException(InvalidAccessError, renderer2 ? "The second provided element's renderer is not a box." : "The second provided element has no renderer.");
1657         return 0;
1658     }
1659
1660     RenderLayer* layer1 = toRenderBox(renderer1)->layer();
1661     RenderLayer* layer2 = toRenderBox(renderer2)->layer();
1662     if (!layer1 || !layer2) {
1663         exceptionState.throwDOMException(InvalidAccessError, String::format("No render layer can be obtained from the %s provided element.", layer1 ? "second" : "first"));
1664         return 0;
1665     }
1666
1667     return layer1->scrollsWithRespectTo(layer2);
1668 }
1669
1670 bool Internals::isUnclippedDescendant(Element* element, ExceptionState& exceptionState)
1671 {
1672     if (!element) {
1673         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1674         return 0;
1675     }
1676
1677     element->document().view()->updateLayoutAndStyleForPainting();
1678
1679     RenderObject* renderer = element->renderer();
1680     if (!renderer || !renderer->isBox()) {
1681         exceptionState.throwDOMException(InvalidAccessError, renderer ? "The provided element's renderer is not a box." : "The provided element has no renderer.");
1682         return 0;
1683     }
1684
1685     RenderLayer* layer = toRenderBox(renderer)->layer();
1686     if (!layer) {
1687         exceptionState.throwDOMException(InvalidAccessError, "No render layer can be obtained from the provided element.");
1688         return 0;
1689     }
1690
1691     return layer->isUnclippedDescendant();
1692 }
1693
1694 String Internals::layerTreeAsText(Document* document, unsigned flags, ExceptionState& exceptionState) const
1695 {
1696     if (!document || !document->frame()) {
1697         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1698         return String();
1699     }
1700
1701     document->view()->updateLayoutAndStyleForPainting();
1702
1703     return document->frame()->layerTreeAsText(flags);
1704 }
1705
1706 String Internals::elementLayerTreeAsText(Element* element, unsigned flags, ExceptionState& exceptionState) const
1707 {
1708     if (!element) {
1709         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1710         return String();
1711     }
1712
1713     element->document().updateLayout();
1714
1715     RenderObject* renderer = element->renderer();
1716     if (!renderer || !renderer->isBox()) {
1717         exceptionState.throwDOMException(InvalidAccessError, renderer ? "The provided element's renderer is not a box." : "The provided element has no renderer.");
1718         return String();
1719     }
1720
1721     RenderLayer* layer = toRenderBox(renderer)->layer();
1722     if (!layer
1723         || !layer->hasCompositedLayerMapping()
1724         || !layer->compositedLayerMapping()->mainGraphicsLayer()) {
1725         // Don't raise exception in these cases which may be normally used in tests.
1726         return String();
1727     }
1728
1729     return layer->compositedLayerMapping()->mainGraphicsLayer()->layerTreeAsText(flags);
1730 }
1731
1732 static RenderLayer* getRenderLayerForElement(Element* element, ExceptionState& exceptionState)
1733 {
1734     if (!element) {
1735         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1736         return 0;
1737     }
1738
1739     RenderObject* renderer = element->renderer();
1740     if (!renderer || !renderer->isBox()) {
1741         exceptionState.throwDOMException(InvalidAccessError, renderer ? "The provided element's renderer is not a box." : "The provided element has no renderer.");
1742         return 0;
1743     }
1744
1745     RenderLayer* layer = toRenderBox(renderer)->layer();
1746     if (!layer) {
1747         exceptionState.throwDOMException(InvalidAccessError, "No render layer can be obtained from the provided element.");
1748         return 0;
1749     }
1750
1751     return layer;
1752 }
1753
1754 void Internals::setNeedsCompositedScrolling(Element* element, unsigned needsCompositedScrolling, ExceptionState& exceptionState)
1755 {
1756     if (!element) {
1757         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1758         return;
1759     }
1760
1761     element->document().updateLayout();
1762
1763     if (RenderLayer* layer = getRenderLayerForElement(element, exceptionState))
1764         layer->scrollableArea()->setForceNeedsCompositedScrolling(static_cast<ForceNeedsCompositedScrollingMode>(needsCompositedScrolling));
1765 }
1766
1767 String Internals::repaintRectsAsText(Document* document, ExceptionState& exceptionState) const
1768 {
1769     if (!document || !document->frame()) {
1770         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1771         return String();
1772     }
1773
1774     return document->frame()->trackedRepaintRectsAsText();
1775 }
1776
1777 PassRefPtrWillBeRawPtr<ClientRectList> Internals::repaintRects(Element* element, ExceptionState& exceptionState) const
1778 {
1779     if (!element) {
1780         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
1781         return nullptr;
1782     }
1783
1784     element->document().frame()->view()->updateLayoutAndStyleForPainting();
1785
1786     if (RenderLayer* layer = getRenderLayerForElement(element, exceptionState)) {
1787         if (layer->compositingState() == PaintsIntoOwnBacking) {
1788             OwnPtr<Vector<FloatRect> > rects = layer->collectTrackedRepaintRects();
1789             ASSERT(rects.get());
1790             Vector<FloatQuad> quads(rects->size());
1791             for (size_t i = 0; i < rects->size(); ++i)
1792                 quads[i] = FloatRect(rects->at(i));
1793             return ClientRectList::create(quads);
1794         }
1795     }
1796
1797     exceptionState.throwDOMException(InvalidAccessError, "The provided element is not composited.");
1798     return nullptr;
1799 }
1800
1801 String Internals::scrollingStateTreeAsText(Document* document, ExceptionState& exceptionState) const
1802 {
1803     return String();
1804 }
1805
1806 String Internals::mainThreadScrollingReasons(Document* document, ExceptionState& exceptionState) const
1807 {
1808     if (!document || !document->frame()) {
1809         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1810         return String();
1811     }
1812
1813     document->frame()->view()->updateLayoutAndStyleForPainting();
1814
1815     Page* page = document->page();
1816     if (!page)
1817         return String();
1818
1819     return page->mainThreadScrollingReasonsAsText();
1820 }
1821
1822 PassRefPtrWillBeRawPtr<ClientRectList> Internals::nonFastScrollableRects(Document* document, ExceptionState& exceptionState) const
1823 {
1824     if (!document || !document->frame()) {
1825         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
1826         return nullptr;
1827     }
1828
1829     Page* page = document->page();
1830     if (!page)
1831         return nullptr;
1832
1833     return page->nonFastScrollableRects(document->frame());
1834 }
1835
1836 void Internals::garbageCollectDocumentResources(Document* document, ExceptionState& exceptionState) const
1837 {
1838     if (!document) {
1839         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
1840         return;
1841     }
1842     ResourceFetcher* fetcher = document->fetcher();
1843     if (!fetcher)
1844         return;
1845     fetcher->garbageCollectDocumentResources();
1846 }
1847
1848 void Internals::evictAllResources() const
1849 {
1850     memoryCache()->evictResources();
1851 }
1852
1853 void Internals::allowRoundingHacks() const
1854 {
1855     TextRun::setAllowsRoundingHacks(true);
1856 }
1857
1858 String Internals::counterValue(Element* element)
1859 {
1860     if (!element)
1861         return String();
1862
1863     return counterValueForElement(element);
1864 }
1865
1866 int Internals::pageNumber(Element* element, float pageWidth, float pageHeight)
1867 {
1868     if (!element)
1869         return 0;
1870
1871     return PrintContext::pageNumberForElement(element, FloatSize(pageWidth, pageHeight));
1872 }
1873
1874 Vector<String> Internals::iconURLs(Document* document, int iconTypesMask) const
1875 {
1876     Vector<IconURL> iconURLs = document->iconURLs(iconTypesMask);
1877     Vector<String> array;
1878
1879     Vector<IconURL>::const_iterator iter(iconURLs.begin());
1880     for (; iter != iconURLs.end(); ++iter)
1881         array.append(iter->m_iconURL.string());
1882
1883     return array;
1884 }
1885
1886 Vector<String> Internals::shortcutIconURLs(Document* document) const
1887 {
1888     return iconURLs(document, Favicon);
1889 }
1890
1891 Vector<String> Internals::allIconURLs(Document* document) const
1892 {
1893     return iconURLs(document, Favicon | TouchIcon | TouchPrecomposedIcon);
1894 }
1895
1896 int Internals::numberOfPages(float pageWidth, float pageHeight)
1897 {
1898     if (!frame())
1899         return -1;
1900
1901     return PrintContext::numberOfPages(frame(), FloatSize(pageWidth, pageHeight));
1902 }
1903
1904 String Internals::pageProperty(String propertyName, int pageNumber, ExceptionState& exceptionState) const
1905 {
1906     if (!frame()) {
1907         exceptionState.throwDOMException(InvalidAccessError, "No frame is available.");
1908         return String();
1909     }
1910
1911     return PrintContext::pageProperty(frame(), propertyName.utf8().data(), pageNumber);
1912 }
1913
1914 String Internals::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft, ExceptionState& exceptionState) const
1915 {
1916     if (!frame()) {
1917         exceptionState.throwDOMException(InvalidAccessError, "No frame is available.");
1918         return String();
1919     }
1920
1921     return PrintContext::pageSizeAndMarginsInPixels(frame(), pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft);
1922 }
1923
1924 void Internals::setDeviceScaleFactor(float scaleFactor, ExceptionState& exceptionState)
1925 {
1926     Document* document = contextDocument();
1927     if (!document || !document->page()) {
1928         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's page cannot be retrieved." : "No context document can be obtained.");
1929         return;
1930     }
1931     Page* page = document->page();
1932     page->setDeviceScaleFactor(scaleFactor);
1933 }
1934
1935 void Internals::setIsCursorVisible(Document* document, bool isVisible, ExceptionState& exceptionState)
1936 {
1937     if (!document || !document->page()) {
1938         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's page cannot be retrieved." : "No context document can be obtained.");
1939         return;
1940     }
1941     document->page()->setIsCursorVisible(isVisible);
1942 }
1943
1944 void Internals::webkitWillEnterFullScreenForElement(Document* document, Element* element)
1945 {
1946     if (!document)
1947         return;
1948     FullscreenElementStack::from(*document).webkitWillEnterFullScreenForElement(element);
1949 }
1950
1951 void Internals::webkitDidEnterFullScreenForElement(Document* document, Element* element)
1952 {
1953     if (!document)
1954         return;
1955     FullscreenElementStack::from(*document).webkitDidEnterFullScreenForElement(element);
1956 }
1957
1958 void Internals::webkitWillExitFullScreenForElement(Document* document, Element* element)
1959 {
1960     if (!document)
1961         return;
1962     FullscreenElementStack::from(*document).webkitWillExitFullScreenForElement(element);
1963 }
1964
1965 void Internals::webkitDidExitFullScreenForElement(Document* document, Element* element)
1966 {
1967     if (!document)
1968         return;
1969     FullscreenElementStack::from(*document).webkitDidExitFullScreenForElement(element);
1970 }
1971
1972 void Internals::mediaPlayerRequestFullscreen(HTMLMediaElement* mediaElement)
1973 {
1974     mediaElement->mediaPlayerRequestFullscreen();
1975 }
1976
1977 void Internals::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
1978 {
1979     SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(scheme);
1980 }
1981
1982 void Internals::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
1983 {
1984     SchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(scheme);
1985 }
1986
1987 PassRefPtrWillBeRawPtr<MallocStatistics> Internals::mallocStatistics() const
1988 {
1989     return MallocStatistics::create();
1990 }
1991
1992 PassRefPtrWillBeRawPtr<TypeConversions> Internals::typeConversions() const
1993 {
1994     return TypeConversions::create();
1995 }
1996
1997 Vector<String> Internals::getReferencedFilePaths() const
1998 {
1999     return frame()->loader().currentItem()->getReferencedFilePaths();
2000 }
2001
2002 void Internals::startTrackingRepaints(Document* document, ExceptionState& exceptionState)
2003 {
2004     if (!document || !document->view()) {
2005         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's view cannot be retrieved." : "The document provided is invalid.");
2006         return;
2007     }
2008
2009     FrameView* frameView = document->view();
2010     frameView->updateLayoutAndStyleForPainting();
2011     frameView->setTracksRepaints(true);
2012 }
2013
2014 void Internals::stopTrackingRepaints(Document* document, ExceptionState& exceptionState)
2015 {
2016     if (!document || !document->view()) {
2017         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's view cannot be retrieved." : "The document provided is invalid.");
2018         return;
2019     }
2020
2021     FrameView* frameView = document->view();
2022     frameView->updateLayoutAndStyleForPainting();
2023     frameView->setTracksRepaints(false);
2024 }
2025
2026 void Internals::updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks(ExceptionState& exceptionState)
2027 {
2028     updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks(0, exceptionState);
2029 }
2030
2031 void Internals::updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks(Node* node, ExceptionState& exceptionState)
2032 {
2033     Document* document;
2034     if (!node) {
2035         document = contextDocument();
2036     } else if (node->isDocumentNode()) {
2037         document = toDocument(node);
2038     } else if (isHTMLIFrameElement(*node)) {
2039         document = toHTMLIFrameElement(*node).contentDocument();
2040     } else {
2041         exceptionState.throwTypeError("The node provided is neither a document nor an IFrame.");
2042         return;
2043     }
2044     document->updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasksSynchronously);
2045 }
2046
2047 PassRefPtrWillBeRawPtr<ClientRectList> Internals::draggableRegions(Document* document, ExceptionState& exceptionState)
2048 {
2049     return annotatedRegions(document, true, exceptionState);
2050 }
2051
2052 PassRefPtrWillBeRawPtr<ClientRectList> Internals::nonDraggableRegions(Document* document, ExceptionState& exceptionState)
2053 {
2054     return annotatedRegions(document, false, exceptionState);
2055 }
2056
2057 PassRefPtrWillBeRawPtr<ClientRectList> Internals::annotatedRegions(Document* document, bool draggable, ExceptionState& exceptionState)
2058 {
2059     if (!document || !document->view()) {
2060         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's view cannot be retrieved." : "The document provided is invalid.");
2061         return ClientRectList::create();
2062     }
2063
2064     document->updateLayout();
2065     document->view()->updateAnnotatedRegions();
2066     Vector<AnnotatedRegionValue> regions = document->annotatedRegions();
2067
2068     Vector<FloatQuad> quads;
2069     for (size_t i = 0; i < regions.size(); ++i) {
2070         if (regions[i].draggable == draggable)
2071             quads.append(FloatQuad(regions[i].bounds));
2072     }
2073     return ClientRectList::create(quads);
2074 }
2075
2076 static const char* cursorTypeToString(Cursor::Type cursorType)
2077 {
2078     switch (cursorType) {
2079     case Cursor::Pointer: return "Pointer";
2080     case Cursor::Cross: return "Cross";
2081     case Cursor::Hand: return "Hand";
2082     case Cursor::IBeam: return "IBeam";
2083     case Cursor::Wait: return "Wait";
2084     case Cursor::Help: return "Help";
2085     case Cursor::EastResize: return "EastResize";
2086     case Cursor::NorthResize: return "NorthResize";
2087     case Cursor::NorthEastResize: return "NorthEastResize";
2088     case Cursor::NorthWestResize: return "NorthWestResize";
2089     case Cursor::SouthResize: return "SouthResize";
2090     case Cursor::SouthEastResize: return "SouthEastResize";
2091     case Cursor::SouthWestResize: return "SouthWestResize";
2092     case Cursor::WestResize: return "WestResize";
2093     case Cursor::NorthSouthResize: return "NorthSouthResize";
2094     case Cursor::EastWestResize: return "EastWestResize";
2095     case Cursor::NorthEastSouthWestResize: return "NorthEastSouthWestResize";
2096     case Cursor::NorthWestSouthEastResize: return "NorthWestSouthEastResize";
2097     case Cursor::ColumnResize: return "ColumnResize";
2098     case Cursor::RowResize: return "RowResize";
2099     case Cursor::MiddlePanning: return "MiddlePanning";
2100     case Cursor::EastPanning: return "EastPanning";
2101     case Cursor::NorthPanning: return "NorthPanning";
2102     case Cursor::NorthEastPanning: return "NorthEastPanning";
2103     case Cursor::NorthWestPanning: return "NorthWestPanning";
2104     case Cursor::SouthPanning: return "SouthPanning";
2105     case Cursor::SouthEastPanning: return "SouthEastPanning";
2106     case Cursor::SouthWestPanning: return "SouthWestPanning";
2107     case Cursor::WestPanning: return "WestPanning";
2108     case Cursor::Move: return "Move";
2109     case Cursor::VerticalText: return "VerticalText";
2110     case Cursor::Cell: return "Cell";
2111     case Cursor::ContextMenu: return "ContextMenu";
2112     case Cursor::Alias: return "Alias";
2113     case Cursor::Progress: return "Progress";
2114     case Cursor::NoDrop: return "NoDrop";
2115     case Cursor::Copy: return "Copy";
2116     case Cursor::None: return "None";
2117     case Cursor::NotAllowed: return "NotAllowed";
2118     case Cursor::ZoomIn: return "ZoomIn";
2119     case Cursor::ZoomOut: return "ZoomOut";
2120     case Cursor::Grab: return "Grab";
2121     case Cursor::Grabbing: return "Grabbing";
2122     case Cursor::Custom: return "Custom";
2123     }
2124
2125     ASSERT_NOT_REACHED();
2126     return "UNKNOWN";
2127 }
2128
2129 String Internals::getCurrentCursorInfo(Document* document, ExceptionState& exceptionState)
2130 {
2131     if (!document || !document->frame()) {
2132         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "The document provided is invalid.");
2133         return String();
2134     }
2135
2136     Cursor cursor = document->frame()->eventHandler().currentMouseCursor();
2137
2138     StringBuilder result;
2139     result.append("type=");
2140     result.append(cursorTypeToString(cursor.type()));
2141     result.append(" hotSpot=");
2142     result.appendNumber(cursor.hotSpot().x());
2143     result.append(",");
2144     result.appendNumber(cursor.hotSpot().y());
2145     if (cursor.image()) {
2146         IntSize size = cursor.image()->size();
2147         result.append(" image=");
2148         result.appendNumber(size.width());
2149         result.append("x");
2150         result.appendNumber(size.height());
2151     }
2152     if (cursor.imageScaleFactor() != 1) {
2153         result.append(" scale=");
2154         NumberToStringBuffer buffer;
2155         result.append(numberToFixedPrecisionString(cursor.imageScaleFactor(), 8, buffer, true));
2156     }
2157
2158     return result.toString();
2159 }
2160
2161 PassRefPtr<ArrayBuffer> Internals::serializeObject(PassRefPtr<SerializedScriptValue> value) const
2162 {
2163     String stringValue = value->toWireString();
2164     RefPtr<ArrayBuffer> buffer = ArrayBuffer::createUninitialized(stringValue.length(), sizeof(UChar));
2165     stringValue.copyTo(static_cast<UChar*>(buffer->data()), 0, stringValue.length());
2166     return buffer.release();
2167 }
2168
2169 PassRefPtr<SerializedScriptValue> Internals::deserializeBuffer(PassRefPtr<ArrayBuffer> buffer) const
2170 {
2171     String value(static_cast<const UChar*>(buffer->data()), buffer->byteLength() / sizeof(UChar));
2172     return SerializedScriptValue::createFromWire(value);
2173 }
2174
2175 void Internals::forceReload(bool endToEnd)
2176 {
2177     frame()->loader().reload(endToEnd ? EndToEndReload : NormalReload);
2178 }
2179
2180 PassRefPtrWillBeRawPtr<ClientRect> Internals::selectionBounds(ExceptionState& exceptionState)
2181 {
2182     Document* document = contextDocument();
2183     if (!document || !document->frame()) {
2184         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's frame cannot be retrieved." : "No context document can be obtained.");
2185         return nullptr;
2186     }
2187
2188     return ClientRect::create(document->frame()->selection().bounds());
2189 }
2190
2191 String Internals::markerTextForListItem(Element* element, ExceptionState& exceptionState)
2192 {
2193     if (!element) {
2194         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
2195         return String();
2196     }
2197     return WebCore::markerTextForListItem(element);
2198 }
2199
2200 String Internals::getImageSourceURL(Element* element, ExceptionState& exceptionState)
2201 {
2202     if (!element) {
2203         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
2204         return String();
2205     }
2206     return element->imageSourceURL();
2207 }
2208
2209 String Internals::baseURL(Document* document, ExceptionState& exceptionState)
2210 {
2211     if (!document) {
2212         exceptionState.throwDOMException(InvalidAccessError, "No context document is available.");
2213         return String();
2214     }
2215
2216     return document->baseURL().string();
2217 }
2218
2219 bool Internals::isSelectPopupVisible(Node* node)
2220 {
2221     ASSERT(node);
2222     if (!isHTMLSelectElement(*node))
2223         return false;
2224
2225     HTMLSelectElement& select = toHTMLSelectElement(*node);
2226
2227     RenderObject* renderer = select.renderer();
2228     if (!renderer->isMenuList())
2229         return false;
2230
2231     RenderMenuList* menuList = toRenderMenuList(renderer);
2232     return menuList->popupIsVisible();
2233 }
2234
2235 bool Internals::loseSharedGraphicsContext3D()
2236 {
2237     OwnPtr<blink::WebGraphicsContext3DProvider> sharedProvider = adoptPtr(blink::Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
2238     if (!sharedProvider)
2239         return false;
2240     blink::WebGraphicsContext3D* sharedContext = sharedProvider->context3d();
2241     sharedContext->loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_EXT, GL_INNOCENT_CONTEXT_RESET_EXT);
2242     // To prevent tests that call loseSharedGraphicsContext3D from being
2243     // flaky, we call finish so that the context is guaranteed to be lost
2244     // synchronously (i.e. before returning).
2245     sharedContext->finish();
2246     return true;
2247 }
2248
2249 void Internals::forceCompositingUpdate(Document* document, ExceptionState& exceptionState)
2250 {
2251     // Hit when running content_shell with --expose-internals-for-testing.
2252     DisableCompositingQueryAsserts disabler;
2253
2254     if (!document || !document->renderView()) {
2255         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's render view cannot be retrieved." : "The document provided is invalid.");
2256         return;
2257     }
2258
2259     document->frame()->view()->updateLayoutAndStyleForPainting();
2260 }
2261
2262 bool Internals::isCompositorFramePending(Document* document, ExceptionState& exceptionState)
2263 {
2264     if (!document || !document->renderView()) {
2265         exceptionState.throwDOMException(InvalidAccessError, document ? "The document's render view cannot be retrieved." : "The document provided is invalid.");
2266         return false;
2267     }
2268
2269     return document->page()->chrome().client().isCompositorFramePending();
2270 }
2271
2272 void Internals::setZoomFactor(float factor)
2273 {
2274     frame()->setPageZoomFactor(factor);
2275 }
2276
2277 void Internals::setShouldRevealPassword(Element* element, bool reveal, ExceptionState& exceptionState)
2278 {
2279     if (!isHTMLInputElement(element)) {
2280         exceptionState.throwDOMException(InvalidAccessError, ExceptionMessages::argumentNullOrIncorrectType(1, "Element"));
2281         return;
2282     }
2283
2284     return toHTMLInputElement(*element).setShouldRevealPassword(reveal);
2285 }
2286
2287 namespace {
2288
2289 class AddOneFunction : public ScriptFunction {
2290 public:
2291     static PassOwnPtr<ScriptFunction> create(ExecutionContext* context)
2292     {
2293         return adoptPtr(new AddOneFunction(toIsolate(context)));
2294     }
2295
2296 private:
2297     AddOneFunction(v8::Isolate* isolate)
2298         : ScriptFunction(isolate)
2299     {
2300     }
2301
2302     virtual ScriptValue call(ScriptValue value) OVERRIDE
2303     {
2304         v8::Local<v8::Value> v8Value = value.v8Value();
2305         v8::Isolate* isolate = value.isolate();
2306         ASSERT(v8Value->IsNumber());
2307         int intValue = v8Value.As<v8::Integer>()->Value();
2308         ScriptValue result  = ScriptValue(v8::Integer::New(isolate, intValue + 1), isolate);
2309         return result;
2310     }
2311 };
2312
2313 } // namespace
2314
2315 ScriptPromise Internals::createPromise(ScriptState* scriptState)
2316 {
2317     return ScriptPromiseResolver::create(scriptState)->promise();
2318 }
2319
2320 ScriptPromise Internals::createResolvedPromise(ScriptState* scriptState, ScriptValue value)
2321 {
2322     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
2323     ScriptPromise promise = resolver->promise();
2324     resolver->resolve(value);
2325     return promise;
2326 }
2327
2328 ScriptPromise Internals::createRejectedPromise(ScriptState* scriptState, ScriptValue value)
2329 {
2330     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
2331     ScriptPromise promise = resolver->promise();
2332     resolver->reject(value);
2333     return promise;
2334 }
2335
2336 ScriptPromise Internals::addOneToPromise(ExecutionContext* context, ScriptPromise promise)
2337 {
2338     return promise.then(AddOneFunction::create(context));
2339 }
2340
2341 void Internals::trace(Visitor* visitor)
2342 {
2343     visitor->trace(m_runtimeFlags);
2344     visitor->trace(m_profilers);
2345 }
2346
2347 void Internals::setValueForUser(Element* element, const String& value)
2348 {
2349     toHTMLInputElement(element)->setValueForUser(value);
2350 }
2351
2352 String Internals::textSurroundingNode(Node* node, int x, int y, unsigned long maxLength)
2353 {
2354     if (!node)
2355         return String();
2356     blink::WebPoint point(x, y);
2357     SurroundingText surroundingText(VisiblePosition(node->renderer()->positionForPoint(static_cast<IntPoint>(point))), maxLength);
2358     return surroundingText.content();
2359 }
2360
2361 }