Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2013 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "bindings/v8/ExceptionState.h"
     33 
     34 #include "bindings/v8/ExceptionMessages.h"
     35 #include "bindings/v8/V8ThrowException.h"
     36 #include "core/dom/ExceptionCode.h"
     37 
     38 namespace WebCore {
     39 
     40 void ExceptionState::clearException()
     41 {
     42     m_code = 0;
     43     m_exception.clear();
     44 }
     45 
     46 void ExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
     47 {
     48     ASSERT(ec);
     49     ASSERT(m_isolate);
     50     ASSERT(!m_creationContext.IsEmpty());
     51 
     52     // SecurityError is thrown via ::throwSecurityError, and _careful_ consideration must be given to the data exposed to JavaScript via the 'sanitizedMessage'.
     53     ASSERT(ec != SecurityError);
     54 
     55     m_code = ec;
     56     String processedMessage = addExceptionContext(message);
     57     m_message = processedMessage;
     58     setException(V8ThrowException::createDOMException(ec, processedMessage, m_creationContext, m_isolate));
     59 }
     60 
     61 void ExceptionState::throwSecurityError(const String& sanitizedMessage, const String& unsanitizedMessage)
     62 {
     63     ASSERT(m_isolate);
     64     ASSERT(!m_creationContext.IsEmpty());
     65     m_code = SecurityError;
     66     String finalSanitized = addExceptionContext(sanitizedMessage);
     67     m_message = finalSanitized;
     68     String finalUnsanitized = addExceptionContext(unsanitizedMessage);
     69 
     70     setException(V8ThrowException::createDOMException(SecurityError, finalSanitized, finalUnsanitized, m_creationContext, m_isolate));
     71 }
     72 
     73 void ExceptionState::setException(v8::Handle<v8::Value> exception)
     74 {
     75     // FIXME: Assert that exception is not empty?
     76     if (exception.IsEmpty()) {
     77         clearException();
     78         return;
     79     }
     80 
     81     m_exception.set(m_isolate, exception);
     82 }
     83 
     84 void ExceptionState::throwException()
     85 {
     86     ASSERT(!m_exception.isEmpty());
     87     V8ThrowException::throwError(m_exception.newLocal(m_isolate), m_isolate);
     88 }
     89 
     90 void ExceptionState::throwTypeError(const String& message)
     91 {
     92     ASSERT(m_isolate);
     93     m_code = TypeError;
     94     m_message = message;
     95     setException(V8ThrowException::createTypeError(addExceptionContext(message), m_isolate));
     96 }
     97 
     98 void NonThrowableExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
     99 {
    100     ASSERT_NOT_REACHED();
    101     m_code = ec;
    102     m_message = message;
    103 }
    104 
    105 void NonThrowableExceptionState::throwTypeError(const String& message)
    106 {
    107     ASSERT_NOT_REACHED();
    108     m_code = TypeError;
    109     m_message = message;
    110 }
    111 
    112 void NonThrowableExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
    113 {
    114     ASSERT_NOT_REACHED();
    115     m_code = SecurityError;
    116     m_message = sanitizedMessage;
    117 }
    118 
    119 void TrackExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
    120 {
    121     m_code = ec;
    122     m_message = message;
    123 }
    124 
    125 void TrackExceptionState::throwTypeError(const String& message)
    126 {
    127     m_code = TypeError;
    128     m_message = message;
    129 }
    130 
    131 void TrackExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
    132 {
    133     m_code = SecurityError;
    134     m_message = sanitizedMessage;
    135 }
    136 
    137 String ExceptionState::addExceptionContext(const String& message) const
    138 {
    139     if (message.isEmpty())
    140         return message;
    141 
    142     String processedMessage = message;
    143     if (propertyName() && interfaceName() && m_context != UnknownContext) {
    144         if (m_context == DeletionContext)
    145             processedMessage = ExceptionMessages::failedToDelete(propertyName(), interfaceName(), message);
    146         else if (m_context == ExecutionContext)
    147             processedMessage = ExceptionMessages::failedToExecute(propertyName(), interfaceName(), message);
    148         else if (m_context == GetterContext)
    149             processedMessage = ExceptionMessages::failedToGet(propertyName(), interfaceName(), message);
    150         else if (m_context == SetterContext)
    151             processedMessage = ExceptionMessages::failedToSet(propertyName(), interfaceName(), message);
    152     } else if (!propertyName() && interfaceName()) {
    153         if (m_context == ConstructionContext)
    154             processedMessage = ExceptionMessages::failedToConstruct(interfaceName(), message);
    155         else if (m_context == EnumerationContext)
    156             processedMessage = ExceptionMessages::failedToEnumerate(interfaceName(), message);
    157         else if (m_context == IndexedDeletionContext)
    158             processedMessage = ExceptionMessages::failedToDeleteIndexed(interfaceName(), message);
    159         else if (m_context == IndexedGetterContext)
    160             processedMessage = ExceptionMessages::failedToGetIndexed(interfaceName(), message);
    161         else if (m_context == IndexedSetterContext)
    162             processedMessage = ExceptionMessages::failedToSetIndexed(interfaceName(), message);
    163     }
    164     return processedMessage;
    165 }
    166 
    167 } // namespace WebCore
    168