Internals.cpp 9.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "Internals.h"

29
#include "CachedResourceLoader.h"
30
#include "ClientRect.h"
31
#include "Document.h"
32
#include "DocumentMarkerController.h"
33
34
#include "Element.h"
#include "ExceptionCode.h"
35
36
37
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLTextAreaElement.h"
38
#include "InspectorController.h"
39
#include "MemoryCache.h"
40
#include "NodeRenderingContext.h"
41
#include "Page.h"
42
#include "Range.h"
43
#include "RenderObject.h"
44
#include "RenderTreeAsText.h"
45
#include "Settings.h"
46
#include "ShadowContentElement.h"
47
#include "ShadowRoot.h"
48

49
50
51
52
#if ENABLE(INPUT_COLOR)
#include "ColorChooser.h"
#endif

53
54
namespace WebCore {

55
56
const char* Internals::internalsId = "internals";

57
58
59
60
61
62
63
64
65
66
PassRefPtr<Internals> Internals::create()
{
    return adoptRef(new Internals);
}

Internals::~Internals()
{
}

Internals::Internals()
67
68
    : passwordEchoDurationInSecondsBackedUp(false)
    , passwordEchoEnabledBackedUp(false)
69
70
71
{
}

72
73
74
75
76
77
78
79
bool Internals::isPreloaded(Document* document, const String& url)
{
    if (!document)
        return false;

    return document->cachedResourceLoader()->isPreloaded(url);
}

80
81
82
83
84
85
86
87
88
89
PassRefPtr<Element> Internals::createShadowContentElement(Document* document, ExceptionCode& ec)
{
    if (!document) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }

    return ShadowContentElement::create(document);
}

90
91
92
93
94
95
96
97
98
Element* Internals::getElementByIdInShadowRoot(Node* shadowRoot, const String& id, ExceptionCode& ec)
{
    if (!shadowRoot || !shadowRoot->isShadowRoot()) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }
    return toShadowRoot(shadowRoot)->getElementById(id);
}

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
String Internals::elementRenderTreeAsText(Element* element, ExceptionCode& ec)
{
    if (!element) {
        ec = INVALID_ACCESS_ERR;
        return String();
    }

    String representation = externalRepresentation(element);
    if (representation.isEmpty()) {
        ec = INVALID_ACCESS_ERR;
        return String();
    }

    return representation;
}

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Node* Internals::ensureShadowRoot(Element* host, ExceptionCode& ec)
{
    if (!host) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }

    return host->ensureShadowRoot();
}

Node* Internals::shadowRoot(Element* host, ExceptionCode& ec)
{
    if (!host) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }

    return host->shadowRoot();
}

void Internals::removeShadowRoot(Element* host, ExceptionCode& ec)
{
    if (!host) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    host->removeShadowRoot();
}

145
146
147
148
149
150
151
152
153
154
Element* Internals::includerFor(Node* node, ExceptionCode& ec)
{
    if (!node) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }

    return NodeRenderingContext(node).includer();
}

155
156
157
158
159
160
161
162
163
164
String Internals::shadowPseudoId(Element* element, ExceptionCode& ec)
{
    if (!element) {
        ec = INVALID_ACCESS_ERR;
        return String();
    }

    return element->shadowPseudoId().string();
}

165
166
167
168
169
void Internals::disableMemoryCache(bool disabled)
{
    WebCore::memoryCache()->setDisabled(disabled);
}

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#if ENABLE(INPUT_COLOR)
bool Internals::connectColorChooserClient(Element* element)
{
    if (!element->hasTagName(HTMLNames::inputTag))
        return false;
    HTMLInputElement* inputElement = element->toInputElement();
    if (!inputElement)
        return false;
    return inputElement->connectToColorChooser();
}

void Internals::selectColorInColorChooser(const String& colorValue)
{
    ColorChooser::chooser()->colorSelected(Color(colorValue));
}
#endif

187
188
189
190
191
192
193
194
195
196
197
#if ENABLE(INSPECTOR)
void Internals::setInspectorResourcesDataSizeLimits(Document* document, int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
{
    if (!document || !document->page() || !document->page()->inspectorController()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }
    document->page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
}
#endif

198
199
200
201
202
203
204
205
206
207
208
209
210
211
PassRefPtr<ClientRect> Internals::boundingBox(Element* element, ExceptionCode& ec)
{
    if (!element) {
        ec = INVALID_ACCESS_ERR;
        return ClientRect::create();
    }

    element->document()->updateLayoutIgnorePendingStylesheets();
    RenderObject* renderer = element->renderer();
    if (!renderer)
        return ClientRect::create();
    return ClientRect::create(renderer->absoluteBoundingBoxRect());
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
unsigned Internals::markerCountForNode(Node* node, ExceptionCode& ec)
{
    if (!node) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }

    return node->document()->markers()->markersFor(node).size();
}

PassRefPtr<Range> Internals::markerRangeForNode(Node* node, unsigned index, ExceptionCode& ec)
{
    if (!node) {
        ec = INVALID_ACCESS_ERR;
        return 0;
    }
    
    Vector<DocumentMarker*> markers = node->document()->markers()->markersFor(node);
    if (markers.size() <= index)
        return 0;
    return Range::create(node->document(), node, markers[index]->startOffset(), node, markers[index]->endOffset());
}

235
236
237
238
239
240
241
242
243
244
void Internals::setForceCompositingMode(Document* document, bool enabled, ExceptionCode& ec)
{
    if (!document || !document->settings()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    document->settings()->setForceCompositingMode(enabled);
}

245
void Internals::setPasswordEchoEnabled(Document* document, bool enabled, ExceptionCode& ec)
246
{
247
248
249
250
251
252
    if (!document || !document->settings()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    if (!passwordEchoEnabledBackedUp) {
253
        passwordEchoEnabledBackup = document->settings()->passwordEchoEnabled();
254
255
256
257
258
259
260
261
262
263
264
265
266
        passwordEchoEnabledBackedUp = true;
    }
    document->settings()->setPasswordEchoEnabled(enabled);
}

void Internals::setPasswordEchoDurationInSeconds(Document* document, double durationInSeconds, ExceptionCode& ec)
{
    if (!document || !document->settings()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    if (!passwordEchoDurationInSecondsBackedUp) {
267
        passwordEchoDurationInSecondsBackup = document->settings()->passwordEchoDurationInSeconds();
268
269
270
271
272
273
274
275
276
277
        passwordEchoDurationInSecondsBackedUp = true;
    }
    document->settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
}

void Internals::reset(Document* document)
{
    if (!document || !document->settings())
        return;

278
    if (passwordEchoDurationInSecondsBackedUp) {
279
        document->settings()->setPasswordEchoDurationInSeconds(passwordEchoDurationInSecondsBackup);
280
281
        passwordEchoDurationInSecondsBackedUp = false;
    }
282

283
284
285
286
    if (passwordEchoEnabledBackedUp) {
        document->settings()->setPasswordEchoEnabled(passwordEchoEnabledBackup);
        passwordEchoEnabledBackedUp = false;
    }
287
288
}

289
290
291
292
293
294
295
bool Internals::wasLastChangeUserEdit(Element* textField, ExceptionCode& ec)
{
    if (!textField) {
        ec = INVALID_ACCESS_ERR;
        return false;
    }

296
297
    if (HTMLInputElement* inputElement = textField->toInputElement())
        return inputElement->lastChangeWasUserEdit();
298

299
300
    // FIXME: We should be using hasTagName instead but Windows port doesn't link QualifiedNames properly.
    if (textField->tagName() == "TEXTAREA")
301
302
303
304
305
306
        return static_cast<HTMLTextAreaElement*>(textField)->lastChangeWasUserEdit();

    ec = INVALID_NODE_TYPE_ERR;
    return false;
}

307
String Internals::suggestedValue(Element* element, ExceptionCode& ec)
308
{
309
    if (!element) {
310
311
312
313
        ec = INVALID_ACCESS_ERR;
        return String();
    }

314
315
    HTMLInputElement* inputElement = element->toInputElement();
    if (!inputElement) {
316
317
318
319
        ec = INVALID_NODE_TYPE_ERR;
        return String();
    }

320
    return inputElement->suggestedValue();
321
322
}

323
void Internals::setSuggestedValue(Element* element, const String& value, ExceptionCode& ec)
324
{
325
    if (!element) {
326
327
328
329
        ec = INVALID_ACCESS_ERR;
        return;
    }

330
331
    HTMLInputElement* inputElement = element->toInputElement();
    if (!inputElement) {
332
333
334
335
        ec = INVALID_NODE_TYPE_ERR;
        return;
    }

336
    inputElement->setSuggestedValue(value);
337
338
}

339
}