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 } |
23 | 36 |
24 virtual ~WebCoreStringResource() {} | 37 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 } |
25 | 46 |
26 const uint16_t* data() const { | 47 const uint16_t* data() const { |
27 return reinterpret_cast<const uint16_t*>(impl_.characters()); | 48 return reinterpret_cast<const uint16_t*>(impl_.characters()); |
28 } | 49 } |
29 | 50 |
30 size_t length() const { return impl_.length(); } | 51 size_t length() const { return impl_.length(); } |
31 | 52 |
32 String webcore_string() { return impl_; } | 53 String webcore_string() { return impl_; } |
33 | 54 |
34 private: | 55 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 |
35 // A shallow copy of the string. | 63 // A shallow copy of the string. |
36 // Keeps the string buffer alive until the V8 engine garbage collects it. | 64 // Keeps the string buffer alive until the V8 engine garbage collects it. |
37 String impl_; | 65 String impl_; |
38 }; | 66 }; |
39 | 67 |
40 String v8StringToWebCoreString( | 68 String v8StringToWebCoreString( |
41 v8::Handle<v8::String> v8_str, bool externalize) { | 69 v8::Handle<v8::String> v8_str, bool externalize) { |
42 if (v8_str->IsExternal()) { | 70 if (v8_str->IsExternal()) { |
43 WebCoreStringResource* str_resource = static_cast<WebCoreStringResource*>( | 71 WebCoreStringResource* str_resource = static_cast<WebCoreStringResource*>( |
44 v8_str->GetExternalStringResource()); | 72 v8_str->GetExternalStringResource()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 return v8::String::NewExternal(new WebCoreStringResource(str)); | 143 return v8::String::NewExternal(new WebCoreStringResource(str)); |
116 } | 144 } |
117 | 145 |
118 v8::Local<v8::String> v8ExternalString(const String& str) { | 146 v8::Local<v8::String> v8ExternalString(const String& str) { |
119 if (!str.length()) | 147 if (!str.length()) |
120 return v8::String::Empty(); | 148 return v8::String::Empty(); |
121 return v8::String::NewExternal(new WebCoreStringResource(str)); | 149 return v8::String::NewExternal(new WebCoreStringResource(str)); |
122 } | 150 } |
123 | 151 |
124 } // namespace WebCore | 152 } // namespace WebCore |
OLD | NEW |