1 /* 2 * Copyright (C) 2008, 2009 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 #ifndef ScriptStringImpl_h 32 #define ScriptStringImpl_h 33 34 #include "OwnHandle.h" 35 #include "PlatformString.h" 36 37 #include <v8.h> 38 39 namespace WebCore { 40 41 // This class is used for strings that tend to be shared with JavaScript frequently. The JSC implementation uses wtf::UString - see bindings/js/ScriptString.h 42 // Currently XMLHttpRequest uses a ScriptString to build up the responseText attribute. As data arrives from the network, it is appended to the ScriptString 43 // via operator+= and a JavaScript readystatechange event is fired. JavaScript can access the responseText attribute of the XMLHttpRequest object. JavaScript 44 // may also query the responseXML attribute of the XMLHttpRequest object which results in the responseText attribute being coerced into a WebCore::String and 45 // then parsed as an XML document. 46 // This implementation optimizes for the common case where the responseText is built up with many calls to operator+= before the actual text is queried. 47 class ScriptStringImpl : public RefCounted<ScriptStringImpl> { 48 public: 49 static PassRefPtr<ScriptStringImpl> create(const String& s) 50 { 51 return adoptRef(new ScriptStringImpl(s)); 52 } 53 54 static PassRefPtr<ScriptStringImpl> create(const char* s) 55 { 56 return adoptRef(new ScriptStringImpl(s)); 57 } 58 59 String toString() const; 60 61 bool isNull() const; 62 size_t size() const; 63 64 void append(const String& s); 65 66 v8::Handle<v8::String> v8StringHandle() { return m_handle.get(); } 67 68 private: 69 ScriptStringImpl(const String& s); 70 ScriptStringImpl(const char* s); 71 72 OwnHandle<v8::String> m_handle; 73 }; 74 75 } // namespace WebCore 76 77 #endif // ScriptStringImpl_h 78