OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "v8_binding.h" | 5 #include "v8_binding.h" |
6 | 6 |
7 #include "AtomicString.h" | 7 #include "AtomicString.h" |
8 #include "CString.h" | 8 #include "CString.h" |
9 #include "MathExtras.h" | 9 #include "MathExtras.h" |
10 #include "PlatformString.h" | 10 #include "PlatformString.h" |
11 #include "StringBuffer.h" | 11 #include "StringBuffer.h" |
12 | 12 |
13 #include <v8.h> | 13 #include <v8.h> |
14 | 14 |
15 namespace WebCore { | 15 namespace WebCore { |
16 | 16 |
17 // WebCoreStringResource is a helper class for v8ExternalString. It is used | 17 // WebCoreStringResource is a helper class for v8ExternalString. It is used |
18 // to manage the life-cycle of the underlying buffer of the external string. | 18 // to manage the life-cycle of the underlying buffer of the external string. |
19 class WebCoreStringResource: public v8::String::ExternalStringResource { | 19 class WebCoreStringResource: public v8::String::ExternalStringResource { |
20 public: | 20 public: |
21 explicit WebCoreStringResource(const String& str) | 21 explicit WebCoreStringResource(const String& str) |
22 : impl_(str.impl()) { | 22 : impl_(str.impl()) { } |
23 // We seem to be occasionally losing the backing string for external | |
24 // strings: http://crbug.com/9746 | |
25 // | |
26 // In order to verify that this is caused by a ref counting bug, we | |
27 // artificially increase the ref count on the backing string until | |
28 // we are done using it for external strings. | |
29 // | |
30 // TODO(ager): This is temporary and should be removed once we have | |
31 // found the underlying cause of the problem. | |
32 for (int i = 0; i < kArtificialRefIncrease; i++) { | |
33 impl_.impl()->ref(); | |
34 } | |
35 } | |
36 | 23 |
37 virtual ~WebCoreStringResource() { | 24 virtual ~WebCoreStringResource() {} |
38 // Remove the artificial ref counts added in the constructor. | |
39 // | |
40 // TODO(ager): This is temporary and should be removed once we have | |
41 // found the underlying cause of the problem. | |
42 for (int i = 0; i < kArtificialRefIncrease; i++) { | |
43 impl_.impl()->deref(); | |
44 } | |
45 } | |
46 | 25 |
47 const uint16_t* data() const { | 26 const uint16_t* data() const { |
48 return reinterpret_cast<const uint16_t*>(impl_.characters()); | 27 return reinterpret_cast<const uint16_t*>(impl_.characters()); |
49 } | 28 } |
50 | 29 |
51 size_t length() const { return impl_.length(); } | 30 size_t length() const { return impl_.length(); } |
52 | 31 |
53 String webcore_string() { return impl_; } | 32 String webcore_string() { return impl_; } |
54 | 33 |
55 private: | 34 private: |
56 // The amount by which we artificially increase the reference count | |
57 // of the backing string. | |
58 // | |
59 // TODO(ager): This is temporary and should be removed once we have | |
60 // found the underlying cause of the problem. | |
61 static const int kArtificialRefIncrease = 5; | |
62 | |
63 // A shallow copy of the string. | 35 // A shallow copy of the string. |
64 // Keeps the string buffer alive until the V8 engine garbage collects it. | 36 // Keeps the string buffer alive until the V8 engine garbage collects it. |
65 String impl_; | 37 String impl_; |
66 }; | 38 }; |
67 | 39 |
68 String v8StringToWebCoreString( | 40 String v8StringToWebCoreString( |
69 v8::Handle<v8::String> v8_str, bool externalize) { | 41 v8::Handle<v8::String> v8_str, bool externalize) { |
70 if (v8_str->IsExternal()) { | 42 if (v8_str->IsExternal()) { |
71 WebCoreStringResource* str_resource = static_cast<WebCoreStringResource*>( | 43 WebCoreStringResource* str_resource = static_cast<WebCoreStringResource*>( |
72 v8_str->GetExternalStringResource()); | 44 v8_str->GetExternalStringResource()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 return v8::String::NewExternal(new WebCoreStringResource(str)); | 115 return v8::String::NewExternal(new WebCoreStringResource(str)); |
144 } | 116 } |
145 | 117 |
146 v8::Local<v8::String> v8ExternalString(const String& str) { | 118 v8::Local<v8::String> v8ExternalString(const String& str) { |
147 if (!str.length()) | 119 if (!str.length()) |
148 return v8::String::Empty(); | 120 return v8::String::Empty(); |
149 return v8::String::NewExternal(new WebCoreStringResource(str)); | 121 return v8::String::NewExternal(new WebCoreStringResource(str)); |
150 } | 122 } |
151 | 123 |
152 } // namespace WebCore | 124 } // namespace WebCore |
OLD | NEW |