261fefadedc9d94db67b66e0a7383f5eab9b610c
[WebKit-https.git] / Source / WebCore / testing / Internals.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "Internals.h"
28
29 #include "CachedResourceLoader.h"
30 #include "ClientRect.h"
31 #include "Document.h"
32 #include "Element.h"
33 #include "ExceptionCode.h"
34 #include "InspectorController.h"
35 #include "MemoryCache.h"
36 #include "NodeRenderingContext.h"
37 #include "Page.h"
38 #include "RenderObject.h"
39 #include "RenderTreeAsText.h"
40 #include "Settings.h"
41 #include "ShadowContentElement.h"
42 #include "ShadowRoot.h"
43
44 namespace WebCore {
45
46 const char* Internals::internalsId = "internals";
47
48 PassRefPtr<Internals> Internals::create()
49 {
50     return adoptRef(new Internals);
51 }
52
53 Internals::~Internals()
54 {
55 }
56
57 Internals::Internals()
58 {
59 }
60
61 bool Internals::isPreloaded(Document* document, const String& url)
62 {
63     if (!document)
64         return false;
65
66     return document->cachedResourceLoader()->isPreloaded(url);
67 }
68
69 PassRefPtr<Element> Internals::createShadowContentElement(Document* document, ExceptionCode& ec)
70 {
71     if (!document) {
72         ec = INVALID_ACCESS_ERR;
73         return 0;
74     }
75
76     return ShadowContentElement::create(document);
77 }
78
79 Element* Internals::getElementByIdInShadowRoot(Node* shadowRoot, const String& id, ExceptionCode& ec)
80 {
81     if (!shadowRoot || !shadowRoot->isShadowRoot()) {
82         ec = INVALID_ACCESS_ERR;
83         return 0;
84     }
85     return toShadowRoot(shadowRoot)->getElementById(id);
86 }
87
88 String Internals::elementRenderTreeAsText(Element* element, ExceptionCode& ec)
89 {
90     if (!element) {
91         ec = INVALID_ACCESS_ERR;
92         return String();
93     }
94
95     String representation = externalRepresentation(element);
96     if (representation.isEmpty()) {
97         ec = INVALID_ACCESS_ERR;
98         return String();
99     }
100
101     return representation;
102 }
103
104 Node* Internals::ensureShadowRoot(Element* host, ExceptionCode& ec)
105 {
106     if (!host) {
107         ec = INVALID_ACCESS_ERR;
108         return 0;
109     }
110
111     return host->ensureShadowRoot();
112 }
113
114 Node* Internals::shadowRoot(Element* host, ExceptionCode& ec)
115 {
116     if (!host) {
117         ec = INVALID_ACCESS_ERR;
118         return 0;
119     }
120
121     return host->shadowRoot();
122 }
123
124 void Internals::removeShadowRoot(Element* host, ExceptionCode& ec)
125 {
126     if (!host) {
127         ec = INVALID_ACCESS_ERR;
128         return;
129     }
130
131     host->removeShadowRoot();
132 }
133
134 Element* Internals::includerFor(Node* node, ExceptionCode& ec)
135 {
136     if (!node) {
137         ec = INVALID_ACCESS_ERR;
138         return 0;
139     }
140
141     return NodeRenderingContext(node).includer();
142 }
143
144 String Internals::shadowPseudoId(Element* element, ExceptionCode& ec)
145 {
146     if (!element) {
147         ec = INVALID_ACCESS_ERR;
148         return String();
149     }
150
151     return element->shadowPseudoId().string();
152 }
153
154 void Internals::disableMemoryCache(bool disabled)
155 {
156     WebCore::memoryCache()->setDisabled(disabled);
157 }
158
159 #if ENABLE(INSPECTOR)
160 void Internals::setInspectorResourcesDataSizeLimits(Document* document, int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
161 {
162     if (!document || !document->page() || !document->page()->inspectorController()) {
163         ec = INVALID_ACCESS_ERR;
164         return;
165     }
166     document->page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
167 }
168 #endif
169
170 PassRefPtr<ClientRect> Internals::boundingBox(Element* element, ExceptionCode& ec)
171 {
172     if (!element) {
173         ec = INVALID_ACCESS_ERR;
174         return ClientRect::create();
175     }
176
177     element->document()->updateLayoutIgnorePendingStylesheets();
178     RenderObject* renderer = element->renderer();
179     if (!renderer)
180         return ClientRect::create();
181     return ClientRect::create(renderer->absoluteBoundingBoxRect());
182 }
183
184 void Internals::setForceCompositingMode(Document* document, bool enabled, ExceptionCode& ec)
185 {
186     if (!document || !document->settings()) {
187         ec = INVALID_ACCESS_ERR;
188         return;
189     }
190
191     document->settings()->setForceCompositingMode(enabled);
192 }
193
194 void Internals::reset(Document*)
195 {
196 // FIXME: Implement
197 }
198
199 }