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 51 // SecurityError is thrown via ::throwSecurityError, and _careful_ consideration must be given to the data exposed to JavaScript via the 'sanitizedMessage'. 52 ASSERT(ec != SecurityError); 53 54 m_code = ec; 55 String processedMessage = addExceptionContext(message); 56 setException(V8ThrowException::createDOMException(ec, processedMessage, m_creationContext, m_isolate)); 57 } 58 59 void ExceptionState::throwSecurityError(const String& sanitizedMessage, const String& unsanitizedMessage) 60 { 61 ASSERT(m_isolate); 62 m_code = SecurityError; 63 String finalSanitized = addExceptionContext(sanitizedMessage); 64 String finalUnsanitized = addExceptionContext(unsanitizedMessage); 65 setException(V8ThrowException::createDOMException(SecurityError, finalSanitized, finalUnsanitized, m_creationContext, m_isolate)); 66 } 67 68 void ExceptionState::setException(v8::Handle<v8::Value> exception) 69 { 70 // FIXME: Assert that exception is not empty? 71 if (exception.IsEmpty()) { 72 clearException(); 73 return; 74 } 75 76 m_exception.set(m_isolate, exception); 77 } 78 79 void ExceptionState::throwTypeError(const String& message) 80 { 81 ASSERT(m_isolate); 82 m_code = TypeError; 83 setException(V8ThrowException::createTypeError(addExceptionContext(message), m_isolate)); 84 } 85 86 void TrackExceptionState::throwDOMException(const ExceptionCode& ec, const String& message) 87 { 88 m_code = ec; 89 } 90 91 void TrackExceptionState::throwTypeError(const String&) 92 { 93 m_code = TypeError; 94 } 95 96 void TrackExceptionState::throwSecurityError(const String&, const String&) 97 { 98 m_code = SecurityError; 99 } 100 101 String ExceptionState::addExceptionContext(const String& message) const 102 { 103 if (message.isEmpty()) 104 return message; 105 106 String processedMessage = message; 107 if (propertyName() && interfaceName() && m_context != UnknownContext) { 108 if (m_context == DeletionContext) 109 processedMessage = ExceptionMessages::failedToDelete(propertyName(), interfaceName(), message); 110 else if (m_context == ExecutionContext) 111 processedMessage = ExceptionMessages::failedToExecute(propertyName(), interfaceName(), message); 112 else if (m_context == GetterContext) 113 processedMessage = ExceptionMessages::failedToGet(propertyName(), interfaceName(), message); 114 else if (m_context == SetterContext) 115 processedMessage = ExceptionMessages::failedToSet(propertyName(), interfaceName(), message); 116 } else if (!propertyName() && interfaceName() && m_context == ConstructionContext) { 117 processedMessage = ExceptionMessages::failedToConstruct(interfaceName(), message); 118 } 119 return processedMessage; 120 } 121 122 } // namespace WebCore 123