c01715d4bfc46943297510069dd6b88dbb98bf0b
[platform/framework/web/crosswalk-tizen.git] /
1 /*
2  * Copyright (C) 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef V8StringResource_h
27 #define V8StringResource_h
28
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/CoreExport.h"
31 #include "wtf/Allocator.h"
32 #include "wtf/Threading.h"
33 #include "wtf/text/AtomicString.h"
34 #include <v8.h>
35
36 namespace blink {
37
38 // WebCoreStringResource is a helper class for v8ExternalString. It is used
39 // to manage the life-cycle of the underlying buffer of the external string.
40 class WebCoreStringResourceBase {
41   USING_FAST_MALLOC(WebCoreStringResourceBase);
42   WTF_MAKE_NONCOPYABLE(WebCoreStringResourceBase);
43
44  public:
45   explicit WebCoreStringResourceBase(const String& string)
46       : m_plainString(string) {
47 #if ENABLE(ASSERT)
48     m_threadId = WTF::currentThread();
49 #endif
50     ASSERT(!string.isNull());
51     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
52         string.charactersSizeInBytes());
53   }
54
55   explicit WebCoreStringResourceBase(const AtomicString& string)
56       : m_plainString(string.getString()), m_atomicString(string) {
57 #if ENABLE(ASSERT)
58     m_threadId = WTF::currentThread();
59 #endif
60     ASSERT(!string.isNull());
61     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
62         string.charactersSizeInBytes());
63   }
64
65   virtual ~WebCoreStringResourceBase() {
66 #if ENABLE(ASSERT)
67     ASSERT(m_threadId == WTF::currentThread());
68 #endif
69     int64_t reducedExternalMemory = m_plainString.charactersSizeInBytes();
70     if (m_plainString.impl() != m_atomicString.impl() &&
71         !m_atomicString.isNull())
72       reducedExternalMemory += m_atomicString.charactersSizeInBytes();
73     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
74         -reducedExternalMemory);
75   }
76
77   const String& webcoreString() { return m_plainString; }
78
79   const AtomicString& getAtomicString() {
80 #if ENABLE(ASSERT)
81     ASSERT(m_threadId == WTF::currentThread());
82 #endif
83     if (m_atomicString.isNull()) {
84       m_atomicString = AtomicString(m_plainString);
85       ASSERT(!m_atomicString.isNull());
86       if (m_plainString.impl() != m_atomicString.impl())
87         v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
88             m_atomicString.charactersSizeInBytes());
89     }
90     return m_atomicString;
91   }
92
93  protected:
94   // A shallow copy of the string. Keeps the string buffer alive until the V8
95   // engine garbage collects it.
96   String m_plainString;
97   // If this string is atomic or has been made atomic earlier the
98   // atomic string is held here. In the case where the string starts
99   // off non-atomic and becomes atomic later it is necessary to keep
100   // the original string alive because v8 may keep derived pointers
101   // into that string.
102   AtomicString m_atomicString;
103
104  private:
105 #if ENABLE(ASSERT)
106   WTF::ThreadIdentifier m_threadId;
107 #endif
108 };
109
110 class WebCoreStringResource16 final
111     : public WebCoreStringResourceBase,
112       public v8::String::ExternalStringResource {
113   WTF_MAKE_NONCOPYABLE(WebCoreStringResource16);
114
115  public:
116   explicit WebCoreStringResource16(const String& string)
117       : WebCoreStringResourceBase(string) {
118     ASSERT(!string.is8Bit());
119   }
120
121   explicit WebCoreStringResource16(const AtomicString& string)
122       : WebCoreStringResourceBase(string) {
123     ASSERT(!string.is8Bit());
124   }
125
126   size_t length() const override { return m_plainString.impl()->length(); }
127   const uint16_t* data() const override {
128     return reinterpret_cast<const uint16_t*>(
129         m_plainString.impl()->characters16());
130   }
131 };
132
133 class WebCoreStringResource8 final
134     : public WebCoreStringResourceBase,
135       public v8::String::ExternalOneByteStringResource {
136   WTF_MAKE_NONCOPYABLE(WebCoreStringResource8);
137
138  public:
139   explicit WebCoreStringResource8(const String& string)
140       : WebCoreStringResourceBase(string) {
141     ASSERT(string.is8Bit());
142   }
143
144   explicit WebCoreStringResource8(const AtomicString& string)
145       : WebCoreStringResourceBase(string) {
146     ASSERT(string.is8Bit());
147   }
148
149   size_t length() const override { return m_plainString.impl()->length(); }
150   const char* data() const override {
151     return reinterpret_cast<const char*>(m_plainString.impl()->characters8());
152   }
153 };
154
155 enum ExternalMode { Externalize, DoNotExternalize };
156
157 template <typename StringType>
158 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>,
159                                                ExternalMode);
160 CORE_EXPORT String int32ToWebCoreString(int value);
161
162 // V8StringResource is an adapter class that converts V8 values to Strings
163 // or AtomicStrings as appropriate, using multiple typecast operators.
164 enum V8StringResourceMode {
165   DefaultMode,
166   TreatNullAsEmptyString,
167   TreatNullAsNullString,
168   TreatNullAndUndefinedAsNullString
169 };
170
171 template <V8StringResourceMode Mode = DefaultMode>
172 class V8StringResource {
173   STACK_ALLOCATED();
174
175  public:
176   V8StringResource() : m_mode(Externalize) {}
177
178   V8StringResource(v8::Local<v8::Value> object)
179       : m_v8Object(object), m_mode(Externalize) {}
180
181   V8StringResource(const String& string)
182       : m_mode(Externalize), m_string(string) {}
183
184   void operator=(v8::Local<v8::Value> object) { m_v8Object = object; }
185
186   void operator=(const String& string) { setString(string); }
187
188   void operator=(std::nullptr_t) { setString(String()); }
189
190   bool prepare() {  // DEPRECATED
191     if (prepareFast())
192       return true;
193
194     return m_v8Object->ToString(v8::Isolate::GetCurrent()->GetCurrentContext())
195         .ToLocal(&m_v8Object);
196   }
197
198   bool prepare(v8::Isolate* isolate, ExceptionState& exceptionState) {
199     return prepareFast() || prepareSlow(isolate, exceptionState);
200   }
201
202   bool prepare(ExceptionState& exceptionState) {  // DEPRECATED
203     return prepareFast() ||
204            prepareSlow(v8::Isolate::GetCurrent(), exceptionState);
205   }
206
207   operator String() const { return toString<String>(); }
208   operator AtomicString() const { return toString<AtomicString>(); }
209
210  private:
211   bool prepareFast() {
212     if (m_v8Object.IsEmpty())
213       return true;
214
215     if (!isValid()) {
216       setString(fallbackString());
217       return true;
218     }
219
220     if (LIKELY(m_v8Object->IsString()))
221       return true;
222
223     if (LIKELY(m_v8Object->IsInt32())) {
224       setString(int32ToWebCoreString(m_v8Object.As<v8::Int32>()->Value()));
225       return true;
226     }
227
228     m_mode = DoNotExternalize;
229     return false;
230   }
231
232   bool prepareSlow(v8::Isolate* isolate, ExceptionState& exceptionState) {
233     v8::TryCatch tryCatch(isolate);
234     if (!m_v8Object->ToString(isolate->GetCurrentContext())
235              .ToLocal(&m_v8Object)) {
236       exceptionState.rethrowV8Exception(tryCatch.Exception());
237       return false;
238     }
239     return true;
240   }
241
242   bool isValid() const;
243   String fallbackString() const;
244
245   void setString(const String& string) {
246     m_string = string;
247     m_v8Object.Clear();  // To signal that String is ready.
248   }
249
250   template <class StringType>
251   StringType toString() const {
252     if (LIKELY(!m_v8Object.IsEmpty()))
253       return v8StringToWebCoreString<StringType>(
254           const_cast<v8::Local<v8::Value>*>(&m_v8Object)->As<v8::String>(),
255           m_mode);
256
257     return StringType(m_string);
258   }
259
260   v8::Local<v8::Value> m_v8Object;
261   ExternalMode m_mode;
262   String m_string;
263 };
264
265 template <>
266 inline bool V8StringResource<DefaultMode>::isValid() const {
267   return true;
268 }
269
270 template <>
271 inline String V8StringResource<DefaultMode>::fallbackString() const {
272   ASSERT_NOT_REACHED();
273   return String();
274 }
275
276 template <>
277 inline bool V8StringResource<TreatNullAsEmptyString>::isValid() const {
278   return !m_v8Object->IsNull();
279 }
280
281 template <>
282 inline String V8StringResource<TreatNullAsEmptyString>::fallbackString() const {
283   return emptyString();
284 }
285
286 template <>
287 inline bool V8StringResource<TreatNullAsNullString>::isValid() const {
288   return !m_v8Object->IsNull();
289 }
290
291 template <>
292 inline String V8StringResource<TreatNullAsNullString>::fallbackString() const {
293   return String();
294 }
295
296 template <>
297 inline bool V8StringResource<TreatNullAndUndefinedAsNullString>::isValid()
298     const {
299   return !m_v8Object->IsNull() && !m_v8Object->IsUndefined();
300 }
301
302 template <>
303 inline String
304 V8StringResource<TreatNullAndUndefinedAsNullString>::fallbackString() const {
305   return String();
306 }
307
308 }  // namespace blink
309
310 #endif  // V8StringResource_h