summaryrefslogtreecommitdiffstats
path: root/WebCore/dom/ViewportArguments.cpp
blob: c5070325216077cb73fc3a15d5b9e4f8e3bdd712 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
 *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "ViewportArguments.h"

#include "Chrome.h"
#include "Console.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Frame.h"
#include "Page.h"
#include "PlatformString.h"
#include "ScriptableDocumentParser.h"

namespace WebCore {

void setViewportFeature(const String& keyString, const String& valueString, Document* document, void* data)
{
    ViewportArguments* arguments = static_cast<ViewportArguments*>(data);
    float value = ViewportArguments::ValueUndefined;
    bool didUseConstants = false;

    if (equalIgnoringCase(valueString, "yes"))
        value = 1;
    else if (equalIgnoringCase(valueString, "device-width")) {
        didUseConstants = true;
        if (document->page())
            value = document->page()->chrome()->windowRect().width();
    } else if (equalIgnoringCase(valueString, "device-height")) {
        didUseConstants = true;
        if (document->page())
            value = document->page()->chrome()->windowRect().height();
    } else if (equalIgnoringCase(valueString, "default")) // This allows us to distinguish the omission of a key from asking for the default value.
        value = -2;
    else if (valueString.length()) // listing a key with no value is shorthand for key=default
        value = valueString.toFloat();

    if (keyString == "initial-scale")
        arguments->initialScale = value;
    else if (keyString == "minimum-scale")
        arguments->minimumScale = value;
    else if (keyString == "maximum-scale") {
        arguments->maximumScale = value;
        if (value > 10.0)
            reportViewportWarning(document, MaximumScaleTooLargeError, keyString);
    } else if (keyString == "user-scalable")
        arguments->userScalable = value;
    else if (keyString == "width") {
        if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants)
            reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString);
        else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants)
            reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString);

        arguments->width = value;
    } else if (keyString == "height") {
        if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants)
            reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString);
        else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants)
            reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString);
        
        arguments->height = value;
    } else
        reportViewportWarning(document, UnrecognizedViewportArgumentError, keyString);
}

static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode)
{
    static const char* const errors[] = { 
        "Viewport width or height set to physical device width, try using \"device-width\" constant instead for future compatibility.",
        "Viewport height or height set to physical device height, try using \"device-height\" constant instead for future compatibility.",
        "Viewport argument \"%replacement\" not recognized. Content ignored.",
        "Viewport maximum-scale cannot be larger than 10.0.  The maximum-scale will be set to 10.0."
    };

    return errors[errorCode];
}

static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode)
{
    return errorCode == UnrecognizedViewportArgumentError || errorCode == MaximumScaleTooLargeError ? ErrorMessageLevel : TipMessageLevel;
}

// FIXME: Why is this different from SVGDocumentExtensions parserLineNumber?
// FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
static int parserLineNumber(Document* document)
{
    if (!document)
        return 0;
    ScriptableDocumentParser* parser = document->scriptableDocumentParser();
    if (!parser)
        return 0;
    return parser->lineNumber() + 1;
}

void reportViewportWarning(Document* document, ViewportErrorCode errorCode, const String& replacement)
{
    Frame* frame = document->frame();
    if (!frame)
        return;

    String message = viewportErrorMessageTemplate(errorCode);
    message.replace("%replacement", replacement);

    frame->domWindow()->console()->addMessage(HTMLMessageSource, LogMessageType, viewportErrorMessageLevel(errorCode), message, parserLineNumber(document), document->url().string());
}

} // namespace WebCore