Commit bf38ee8e authored by marja@chromium.org's avatar marja@chromium.org

Remove unsafe access hacks from ScopedPersistent.

BUG=236290

Review URL: https://chromiumcodereview.appspot.com/23636015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222102 0039d316-1c4b-4281-b951-d872f2087c98
parent a95d3a14
...@@ -19,7 +19,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -19,7 +19,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
: isolate_(v8::Isolate::GetCurrent()) : isolate_(v8::Isolate::GetCurrent())
, handle_scope_(isolate_) , handle_scope_(isolate_)
, context_(v8::Context::New(isolate_)) , context_(v8::Context::New(isolate_))
, context_scope_(context_.get()) { , context_scope_(context()) {
} }
protected: protected:
...@@ -32,7 +32,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -32,7 +32,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) { testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) {
scoped_ptr<base::Value> value( scoped_ptr<base::Value> value(
converter_->FromV8Value(v8_value, context_.get())); converter_->FromV8Value(v8_value, context()));
if (value->IsType(base::Value::TYPE_NULL)) if (value->IsType(base::Value::TYPE_NULL))
return testing::AssertionSuccess(); return testing::AssertionSuccess();
return testing::AssertionFailure(); return testing::AssertionFailure();
...@@ -42,7 +42,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -42,7 +42,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
bool expected) { bool expected) {
bool out; bool out;
scoped_ptr<base::Value> value( scoped_ptr<base::Value> value(
converter_->FromV8Value(v8_value, context_.get())); converter_->FromV8Value(v8_value, context()));
if (value->IsType(base::Value::TYPE_BOOLEAN) if (value->IsType(base::Value::TYPE_BOOLEAN)
&& value->GetAsBoolean(&out) && value->GetAsBoolean(&out)
&& out == expected) && out == expected)
...@@ -54,7 +54,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -54,7 +54,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
int expected) { int expected) {
int out; int out;
scoped_ptr<base::Value> value( scoped_ptr<base::Value> value(
converter_->FromV8Value(v8_value, context_.get())); converter_->FromV8Value(v8_value, context()));
if (value->IsType(base::Value::TYPE_INTEGER) if (value->IsType(base::Value::TYPE_INTEGER)
&& value->GetAsInteger(&out) && value->GetAsInteger(&out)
&& out == expected) && out == expected)
...@@ -66,7 +66,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -66,7 +66,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
double expected) { double expected) {
double out; double out;
scoped_ptr<base::Value> value( scoped_ptr<base::Value> value(
converter_->FromV8Value(v8_value, context_.get())); converter_->FromV8Value(v8_value, context()));
if (value->IsType(base::Value::TYPE_DOUBLE) if (value->IsType(base::Value::TYPE_DOUBLE)
&& value->GetAsDouble(&out) && value->GetAsDouble(&out)
&& out == expected) && out == expected)
...@@ -78,7 +78,7 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -78,7 +78,7 @@ class ActivityLogConverterStrategyTest : public testing::Test {
const std::string& expected) { const std::string& expected) {
std::string out; std::string out;
scoped_ptr<base::Value> value( scoped_ptr<base::Value> value(
converter_->FromV8Value(v8_value, context_.get())); converter_->FromV8Value(v8_value, context()));
if (value->IsType(base::Value::TYPE_STRING) if (value->IsType(base::Value::TYPE_STRING)
&& value->GetAsString(&out) && value->GetAsString(&out)
&& out == expected) && out == expected)
...@@ -86,6 +86,10 @@ class ActivityLogConverterStrategyTest : public testing::Test { ...@@ -86,6 +86,10 @@ class ActivityLogConverterStrategyTest : public testing::Test {
return testing::AssertionFailure(); return testing::AssertionFailure();
} }
v8::Handle<v8::Context> context() const {
return context_.NewHandle(isolate_);
}
v8::Isolate* isolate_; v8::Isolate* isolate_;
v8::HandleScope handle_scope_; v8::HandleScope handle_scope_;
ScopedPersistent<v8::Context> context_; ScopedPersistent<v8::Context> context_;
...@@ -155,4 +159,3 @@ TEST_F(ActivityLogConverterStrategyTest, ConversionTest) { ...@@ -155,4 +159,3 @@ TEST_F(ActivityLogConverterStrategyTest, ConversionTest) {
} }
} // namespace extensions } // namespace extensions
...@@ -143,7 +143,7 @@ void ChromeV8Context::OnResponseReceived(const std::string& name, ...@@ -143,7 +143,7 @@ void ChromeV8Context::OnResponseReceived(const std::string& name,
v8::Integer::New(request_id), v8::Integer::New(request_id),
v8::String::New(name.c_str()), v8::String::New(name.c_str()),
v8::Boolean::New(success), v8::Boolean::New(success),
converter->ToV8Value(&response, v8_context_.get()), converter->ToV8Value(&response, v8_context_.NewHandle(isolate())),
v8::String::New(error.c_str()) v8::String::New(error.c_str())
}; };
......
...@@ -43,11 +43,11 @@ class ChromeV8Context : public RequestSender::Source { ...@@ -43,11 +43,11 @@ class ChromeV8Context : public RequestSender::Source {
// Returns true if this context is still valid, false if it isn't. // Returns true if this context is still valid, false if it isn't.
// A context becomes invalid via Invalidate(). // A context becomes invalid via Invalidate().
bool is_valid() const { bool is_valid() const {
return !v8_context_.get().IsEmpty(); return !v8_context_.IsEmpty();
} }
v8::Handle<v8::Context> v8_context() const { v8::Handle<v8::Context> v8_context() const {
return v8_context_.get(); return v8_context_.NewHandle(v8::Isolate::GetCurrent());
} }
const Extension* extension() const { const Extension* extension() const {
......
...@@ -192,12 +192,13 @@ class ExtensionImpl : public extensions::ChromeV8Extension { ...@@ -192,12 +192,13 @@ class ExtensionImpl : public extensions::ChromeV8Extension {
void RunCallback() { void RunCallback() {
v8::HandleScope handle_scope(isolate_); v8::HandleScope handle_scope(isolate_);
v8::Handle<v8::Context> context = callback_->CreationContext(); v8::Handle<v8::Function> callback = callback_.NewHandle(isolate_);
v8::Handle<v8::Context> context = callback->CreationContext();
if (context.IsEmpty()) if (context.IsEmpty())
return; return;
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
WebKit::WebScopedMicrotaskSuppression suppression; WebKit::WebScopedMicrotaskSuppression suppression;
callback_->Call(context->Global(), 0, NULL); callback->Call(context->Global(), 0, NULL);
} }
extensions::ScopedPersistent<v8::Object> object_; extensions::ScopedPersistent<v8::Object> object_;
......
...@@ -32,7 +32,7 @@ ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { ...@@ -32,7 +32,7 @@ ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
} }
v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() {
return object_template_->NewInstance(); return object_template_.NewHandle(v8::Isolate::GetCurrent())->NewInstance();
} }
// static // static
...@@ -68,7 +68,7 @@ void ObjectBackedNativeHandler::RouteFunction( ...@@ -68,7 +68,7 @@ void ObjectBackedNativeHandler::RouteFunction(
v8::External::New(new HandlerFunction(handler_function))); v8::External::New(new HandlerFunction(handler_function)));
v8::Handle<v8::FunctionTemplate> function_template = v8::Handle<v8::FunctionTemplate> function_template =
v8::FunctionTemplate::New(Router, local_data); v8::FunctionTemplate::New(Router, local_data);
object_template_->Set(name.c_str(), function_template); object_template_.NewHandle(isolate)->Set(name.c_str(), function_template);
router_data_.push_back(UnsafePersistent<v8::Object>(&data)); router_data_.push_back(UnsafePersistent<v8::Object>(&data));
} }
......
...@@ -40,7 +40,7 @@ class LoadWatcher : public content::RenderViewObserver { ...@@ -40,7 +40,7 @@ class LoadWatcher : public content::RenderViewObserver {
void CallbackAndDie(bool succeeded) { void CallbackAndDie(bool succeeded) {
v8::HandleScope handle_scope(context_->isolate()); v8::HandleScope handle_scope(context_->isolate());
v8::Handle<v8::Value> args[] = { v8::Boolean::New(succeeded) }; v8::Handle<v8::Value> args[] = { v8::Boolean::New(succeeded) };
context_->CallFunction(callback_.get(), 1, args); context_->CallFunction(callback_.NewHandle(context_->isolate()), 1, args);
delete this; delete this;
} }
......
...@@ -40,15 +40,8 @@ class ScopedPersistent { ...@@ -40,15 +40,8 @@ class ScopedPersistent {
handle_.Clear(); handle_.Clear();
} }
v8::Handle<T> operator->() const { bool IsEmpty() const {
return get(); return handle_.IsEmpty();
}
// TODO(dcarney): Remove this function
// This is an unsafe access to the underlying handle
v8::Handle<T> get() const {
return *reinterpret_cast<v8::Handle<T>*>(
const_cast<v8::Persistent<T>* >(&handle_));
} }
v8::Handle<T> NewHandle() const { v8::Handle<T> NewHandle() const {
...@@ -57,6 +50,12 @@ class ScopedPersistent { ...@@ -57,6 +50,12 @@ class ScopedPersistent {
return v8::Local<T>::New(GetIsolate(handle_), handle_); return v8::Local<T>::New(GetIsolate(handle_), handle_);
} }
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
if (handle_.IsEmpty())
return v8::Local<T>();
return v8::Local<T>::New(isolate, handle_);
}
template<typename P> template<typename P>
void MakeWeak(P* parameters, void MakeWeak(P* parameters,
typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) { typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) {
......
...@@ -108,12 +108,12 @@ v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext( ...@@ -108,12 +108,12 @@ v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext(
v8::Isolate* isolate) { v8::Isolate* isolate) {
// It's ok to create local handles in this function, since this is only called // It's ok to create local handles in this function, since this is only called
// when we have a HandleScope. // when we have a HandleScope.
if (context_.get().IsEmpty()) { if (context_.IsEmpty()) {
v8::Handle<v8::Context> context = v8::Context::New(isolate); v8::Handle<v8::Context> context = v8::Context::New(isolate);
context_.reset(context); context_.reset(context);
return context; return context;
} }
return v8::Local<v8::Context>::New(isolate, context_.get()); return context_.NewHandle(isolate);
} }
} // namespace extensions } // namespace extensions
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment