Internals.cpp 10.8 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
#include "FrameView.h"
36 37 38
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLTextAreaElement.h"
39
#include "InspectorController.h"
40
#include "IntRect.h"
41
#include "MemoryCache.h"
42
#include "NodeRenderingContext.h"
43
#include "Page.h"
44
#include "Range.h"
45
#include "RenderObject.h"
46
#include "RenderTreeAsText.h"
47
#include "Settings.h"
48
#include "ShadowContentElement.h"
49
#include "ShadowRoot.h"
50

51 52 53 54
#if ENABLE(INPUT_COLOR)
#include "ColorChooser.h"
#endif

55 56
namespace WebCore {

57 58
const char* Internals::internalsId = "internals";

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

Internals::~Internals()
{
}

Internals::Internals()
69 70
    : passwordEchoDurationInSecondsBackedUp(false)
    , passwordEchoEnabledBackedUp(false)
71 72 73
{
}

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

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

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

    return ShadowContentElement::create(document);
}

92 93 94 95 96 97 98 99 100
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);
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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;
}

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 145 146
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();
}

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

    return NodeRenderingContext(node).includer();
}

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

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

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

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#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

189 190 191 192 193 194 195 196 197 198 199
#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

200 201 202 203 204 205 206 207 208 209 210 211 212 213
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());
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
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());
}

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

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

247 248 249 250 251 252 253 254 255 256
void Internals::setZoomAnimatorScale(Document *document, double scale, ExceptionCode& ec)
{
    if (!document || !document->settings()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    document->settings()->setZoomAnimatorScale(scale);
}

257
void Internals::setPasswordEchoEnabled(Document* document, bool enabled, ExceptionCode& ec)
258
{
259 260 261 262 263 264
    if (!document || !document->settings()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    if (!passwordEchoEnabledBackedUp) {
265
        passwordEchoEnabledBackup = document->settings()->passwordEchoEnabled();
266 267 268 269 270 271 272 273 274 275 276 277 278
        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) {
279
        passwordEchoDurationInSecondsBackup = document->settings()->passwordEchoDurationInSeconds();
280 281 282 283 284
        passwordEchoDurationInSecondsBackedUp = true;
    }
    document->settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
void Internals::setScrollViewPosition(Document* document, long x, long y, ExceptionCode& ec)
{
    if (!document || !document->view()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    FrameView* frameView = document->view();
    bool constrainsScrollingToContentEdgeOldValue = frameView->constrainsScrollingToContentEdge();
    bool scrollbarsSuppressedOldValue = frameView->scrollbarsSuppressed();

    frameView->setConstrainsScrollingToContentEdge(false);
    frameView->setScrollbarsSuppressed(false);
    frameView->setScrollOffsetFromInternals(IntPoint(x, y));
    frameView->setScrollbarsSuppressed(scrollbarsSuppressedOldValue);
    frameView->setConstrainsScrollingToContentEdge(constrainsScrollingToContentEdgeOldValue);
}

303 304 305 306 307
void Internals::reset(Document* document)
{
    if (!document || !document->settings())
        return;

308
    if (passwordEchoDurationInSecondsBackedUp) {
309
        document->settings()->setPasswordEchoDurationInSeconds(passwordEchoDurationInSecondsBackup);
310 311
        passwordEchoDurationInSecondsBackedUp = false;
    }
312

313 314 315 316
    if (passwordEchoEnabledBackedUp) {
        document->settings()->setPasswordEchoEnabled(passwordEchoEnabledBackup);
        passwordEchoEnabledBackedUp = false;
    }
317 318
}

319 320 321 322 323 324 325
bool Internals::wasLastChangeUserEdit(Element* textField, ExceptionCode& ec)
{
    if (!textField) {
        ec = INVALID_ACCESS_ERR;
        return false;
    }

326 327
    if (HTMLInputElement* inputElement = textField->toInputElement())
        return inputElement->lastChangeWasUserEdit();
328

329 330
    // FIXME: We should be using hasTagName instead but Windows port doesn't link QualifiedNames properly.
    if (textField->tagName() == "TEXTAREA")
331 332 333 334 335 336
        return static_cast<HTMLTextAreaElement*>(textField)->lastChangeWasUserEdit();

    ec = INVALID_NODE_TYPE_ERR;
    return false;
}

337
String Internals::suggestedValue(Element* element, ExceptionCode& ec)
338
{
339
    if (!element) {
340 341 342 343
        ec = INVALID_ACCESS_ERR;
        return String();
    }

344 345
    HTMLInputElement* inputElement = element->toInputElement();
    if (!inputElement) {
346 347 348 349
        ec = INVALID_NODE_TYPE_ERR;
        return String();
    }

350
    return inputElement->suggestedValue();
351 352
}

353
void Internals::setSuggestedValue(Element* element, const String& value, ExceptionCode& ec)
354
{
355
    if (!element) {
356 357 358 359
        ec = INVALID_ACCESS_ERR;
        return;
    }

360 361
    HTMLInputElement* inputElement = element->toInputElement();
    if (!inputElement) {
362 363 364 365
        ec = INVALID_NODE_TYPE_ERR;
        return;
    }

366
    inputElement->setSuggestedValue(value);
367 368
}

369 370 371 372 373 374 375 376 377 378 379
void Internals::paintControlTints(Document* document, ExceptionCode& ec)
{
    if (!document || !document->view()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }

    FrameView* frameView = document->view();
    frameView->paintControlTints();
}

380 381 382 383 384 385 386 387 388 389
void Internals::scrollElementToRect(Element* element, long x, long y, long w, long h, ExceptionCode& ec)
{
    if (!element || !element->document() || !element->document()->view()) {
        ec = INVALID_ACCESS_ERR;
        return;
    }
    FrameView* frameView = element->document()->view();
    frameView->scrollElementToRect(element, IntRect(x, y, w, h));
}

390
}