WebCore/dom/ViewportArguments.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
       
     5  *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
       
     6  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
       
     7  * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
       
     8  *
       
     9  * This library is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU Library General Public
       
    11  * License as published by the Free Software Foundation; either
       
    12  * version 2 of the License, or (at your option) any later version.
       
    13  *
       
    14  * This library is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * Library General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU Library General Public License
       
    20  * along with this library; see the file COPYING.LIB.  If not, write to
       
    21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    22  * Boston, MA 02110-1301, USA.
       
    23  *
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "ViewportArguments.h"
       
    28 
       
    29 #include "Chrome.h"
       
    30 #include "Console.h"
       
    31 #include "DOMWindow.h"
       
    32 #include "Document.h"
       
    33 #include "Frame.h"
       
    34 #include "Page.h"
       
    35 #include "PlatformString.h"
       
    36 #include "ScriptableDocumentParser.h"
       
    37 
       
    38 namespace WebCore {
       
    39 
       
    40 void setViewportFeature(const String& keyString, const String& valueString, Document* document, void* data)
       
    41 {
       
    42     ViewportArguments* arguments = static_cast<ViewportArguments*>(data);
       
    43     float value = ViewportArguments::ValueUndefined;
       
    44     bool didUseConstants = false;
       
    45 
       
    46     if (equalIgnoringCase(valueString, "yes"))
       
    47         value = 1;
       
    48     else if (equalIgnoringCase(valueString, "device-width")) {
       
    49         didUseConstants = true;
       
    50         if (document->page())
       
    51             value = document->page()->chrome()->windowRect().width();
       
    52     } else if (equalIgnoringCase(valueString, "device-height")) {
       
    53         didUseConstants = true;
       
    54         if (document->page())
       
    55             value = document->page()->chrome()->windowRect().height();
       
    56     } else if (equalIgnoringCase(valueString, "default")) // This allows us to distinguish the omission of a key from asking for the default value.
       
    57         value = -2;
       
    58     else if (valueString.length()) // listing a key with no value is shorthand for key=default
       
    59         value = valueString.toFloat();
       
    60 
       
    61     if (keyString == "initial-scale")
       
    62         arguments->initialScale = value;
       
    63     else if (keyString == "minimum-scale")
       
    64         arguments->minimumScale = value;
       
    65     else if (keyString == "maximum-scale") {
       
    66         arguments->maximumScale = value;
       
    67         if (value > 10.0)
       
    68             reportViewportWarning(document, MaximumScaleTooLargeError, keyString);
       
    69     } else if (keyString == "user-scalable")
       
    70         arguments->userScalable = value;
       
    71     else if (keyString == "width") {
       
    72         if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants)
       
    73             reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString);
       
    74         else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants)
       
    75             reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString);
       
    76 
       
    77         arguments->width = value;
       
    78     } else if (keyString == "height") {
       
    79         if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants)
       
    80             reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString);
       
    81         else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants)
       
    82             reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString);
       
    83         
       
    84         arguments->height = value;
       
    85     } else
       
    86         reportViewportWarning(document, UnrecognizedViewportArgumentError, keyString);
       
    87 }
       
    88 
       
    89 static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode)
       
    90 {
       
    91     static const char* const errors[] = { 
       
    92         "Viewport width or height set to physical device width, try using \"device-width\" constant instead for future compatibility.",
       
    93         "Viewport height or height set to physical device height, try using \"device-height\" constant instead for future compatibility.",
       
    94         "Viewport argument \"%replacement\" not recognized. Content ignored.",
       
    95         "Viewport maximum-scale cannot be larger than 10.0.  The maximum-scale will be set to 10.0."
       
    96     };
       
    97 
       
    98     return errors[errorCode];
       
    99 }
       
   100 
       
   101 static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode)
       
   102 {
       
   103     return errorCode == UnrecognizedViewportArgumentError || errorCode == MaximumScaleTooLargeError ? ErrorMessageLevel : TipMessageLevel;
       
   104 }
       
   105 
       
   106 // FIXME: Why is this different from SVGDocumentExtensions parserLineNumber?
       
   107 // FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
       
   108 static int parserLineNumber(Document* document)
       
   109 {
       
   110     if (!document)
       
   111         return 0;
       
   112     ScriptableDocumentParser* parser = document->scriptableDocumentParser();
       
   113     if (!parser)
       
   114         return 0;
       
   115     return parser->lineNumber() + 1;
       
   116 }
       
   117 
       
   118 void reportViewportWarning(Document* document, ViewportErrorCode errorCode, const String& replacement)
       
   119 {
       
   120     Frame* frame = document->frame();
       
   121     if (!frame)
       
   122         return;
       
   123 
       
   124     String message = viewportErrorMessageTemplate(errorCode);
       
   125     message.replace("%replacement", replacement);
       
   126 
       
   127     frame->domWindow()->console()->addMessage(HTMLMessageSource, LogMessageType, viewportErrorMessageLevel(errorCode), message, parserLineNumber(document), document->url().string());
       
   128 }
       
   129 
       
   130 } // namespace WebCore