| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/pepper/v8_var_converter.h" | 5 #include "content/renderer/pepper/v8_var_converter.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 using ppapi::ScopedPPVar; | 36 using ppapi::ScopedPPVar; |
| 37 using ppapi::StringVar; | 37 using ppapi::StringVar; |
| 38 using ppapi::TestGlobals; | 38 using ppapi::TestGlobals; |
| 39 using ppapi::TestEqual; | 39 using ppapi::TestEqual; |
| 40 using ppapi::VarTracker; | 40 using ppapi::VarTracker; |
| 41 | 41 |
| 42 namespace content { | 42 namespace content { |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 void FromV8ValueComplete(const ScopedPPVar& scoped_var, |
| 47 bool success) { |
| 48 NOTREACHED(); |
| 49 } |
| 50 |
| 46 class MockResourceConverter : public content::ResourceConverter { | 51 class MockResourceConverter : public content::ResourceConverter { |
| 47 public: | 52 public: |
| 48 virtual ~MockResourceConverter() {} | 53 virtual ~MockResourceConverter() {} |
| 54 virtual void Reset() OVERRIDE {} |
| 55 virtual bool NeedsFlush() OVERRIDE { return false; } |
| 49 virtual void Flush(const base::Callback<void(bool)>& callback) OVERRIDE { | 56 virtual void Flush(const base::Callback<void(bool)>& callback) OVERRIDE { |
| 50 callback.Run(true); | 57 NOTREACHED(); |
| 51 } | 58 } |
| 52 virtual bool FromV8Value(v8::Handle<v8::Object> val, | 59 virtual bool FromV8Value(v8::Handle<v8::Object> val, |
| 53 v8::Handle<v8::Context> context, | 60 v8::Handle<v8::Context> context, |
| 54 PP_Var* result, | 61 PP_Var* result, |
| 55 bool* was_resource) OVERRIDE { | 62 bool* was_resource) OVERRIDE { |
| 56 *was_resource = false; | 63 *was_resource = false; |
| 57 return true; | 64 return true; |
| 58 } | 65 } |
| 59 virtual bool ToV8Value(const PP_Var& var, | 66 virtual bool ToV8Value(const PP_Var& var, |
| 60 v8::Handle<v8::Context> context, | 67 v8::Handle<v8::Context> context, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 156 } |
| 150 | 157 |
| 151 bool Equals(const PP_Var& var, v8::Handle<v8::Value> val) { | 158 bool Equals(const PP_Var& var, v8::Handle<v8::Value> val) { |
| 152 VarHandleMap var_handle_map; | 159 VarHandleMap var_handle_map; |
| 153 return Equals(var, val, &var_handle_map); | 160 return Equals(var, val, &var_handle_map); |
| 154 } | 161 } |
| 155 | 162 |
| 156 class V8VarConverterTest : public testing::Test { | 163 class V8VarConverterTest : public testing::Test { |
| 157 public: | 164 public: |
| 158 V8VarConverterTest() | 165 V8VarConverterTest() |
| 159 : isolate_(v8::Isolate::GetCurrent()), conversion_success_(false) { | 166 : isolate_(v8::Isolate::GetCurrent()) { |
| 160 PP_Instance dummy = 1234; | 167 PP_Instance dummy = 1234; |
| 161 converter_.reset(new V8VarConverter( | 168 converter_.reset(new V8VarConverter( |
| 162 dummy, | 169 dummy, |
| 163 scoped_ptr<ResourceConverter>(new MockResourceConverter).Pass())); | 170 scoped_ptr<ResourceConverter>(new MockResourceConverter).Pass())); |
| 164 } | 171 } |
| 165 virtual ~V8VarConverterTest() {} | 172 virtual ~V8VarConverterTest() {} |
| 166 | 173 |
| 167 // testing::Test implementation. | 174 // testing::Test implementation. |
| 168 virtual void SetUp() { | 175 virtual void SetUp() { |
| 169 ProxyLock::Acquire(); | 176 ProxyLock::Acquire(); |
| 170 v8::HandleScope handle_scope(isolate_); | 177 v8::HandleScope handle_scope(isolate_); |
| 171 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_); | 178 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_); |
| 172 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); | 179 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
| 173 } | 180 } |
| 174 virtual void TearDown() { | 181 virtual void TearDown() { |
| 175 context_.Reset(); | 182 context_.Reset(); |
| 176 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); | 183 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); |
| 177 ProxyLock::Release(); | 184 ProxyLock::Release(); |
| 178 } | 185 } |
| 179 | 186 |
| 180 protected: | 187 protected: |
| 181 bool FromV8ValueSync(v8::Handle<v8::Value> val, | 188 bool FromV8ValueSync(v8::Handle<v8::Value> val, |
| 182 v8::Handle<v8::Context> context, | 189 v8::Handle<v8::Context> context, |
| 183 PP_Var* result) { | 190 PP_Var* result) { |
| 184 base::RunLoop loop; | 191 V8VarConverter::VarConversionResult conversion_result = |
| 185 converter_->FromV8Value(val, | 192 converter_->FromV8Value(val, |
| 186 context, | 193 context, |
| 187 base::Bind(&V8VarConverterTest::FromV8ValueComplete, | 194 base::Bind(&FromV8ValueComplete)); |
| 188 base::Unretained(this), | 195 DCHECK(conversion_result.completed_synchronously); |
| 189 loop.QuitClosure())); | 196 if (conversion_result.success) |
| 190 loop.Run(); | 197 *result = conversion_result.var.Release(); |
| 191 if (conversion_success_) | |
| 192 *result = conversion_result_; | |
| 193 return conversion_success_; | |
| 194 } | |
| 195 | 198 |
| 196 void FromV8ValueComplete(base::Closure quit_closure, | 199 return conversion_result.success; |
| 197 const ScopedPPVar& scoped_var, | |
| 198 bool success) { | |
| 199 conversion_success_ = success; | |
| 200 if (success) { | |
| 201 ScopedPPVar var = scoped_var; | |
| 202 conversion_result_ = var.Release(); | |
| 203 } | |
| 204 quit_closure.Run(); | |
| 205 } | 200 } |
| 206 | 201 |
| 207 bool RoundTrip(const PP_Var& var, PP_Var* result) { | 202 bool RoundTrip(const PP_Var& var, PP_Var* result) { |
| 208 v8::HandleScope handle_scope(isolate_); | 203 v8::HandleScope handle_scope(isolate_); |
| 209 v8::Local<v8::Context> context = | 204 v8::Local<v8::Context> context = |
| 210 v8::Local<v8::Context>::New(isolate_, context_); | 205 v8::Local<v8::Context>::New(isolate_, context_); |
| 211 v8::Context::Scope context_scope(context); | 206 v8::Context::Scope context_scope(context); |
| 212 v8::Handle<v8::Value> v8_result; | 207 v8::Handle<v8::Value> v8_result; |
| 213 if (!converter_->ToV8Value(var, context, &v8_result)) | 208 if (!converter_->ToV8Value(var, context, &v8_result)) |
| 214 return false; | 209 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 232 v8::Isolate* isolate_; | 227 v8::Isolate* isolate_; |
| 233 | 228 |
| 234 // Context for the JavaScript in the test. | 229 // Context for the JavaScript in the test. |
| 235 v8::Persistent<v8::Context> context_; | 230 v8::Persistent<v8::Context> context_; |
| 236 | 231 |
| 237 scoped_ptr<V8VarConverter> converter_; | 232 scoped_ptr<V8VarConverter> converter_; |
| 238 | 233 |
| 239 private: | 234 private: |
| 240 TestGlobals globals_; | 235 TestGlobals globals_; |
| 241 | 236 |
| 242 PP_Var conversion_result_; | |
| 243 bool conversion_success_; | |
| 244 base::MessageLoop message_loop_; | 237 base::MessageLoop message_loop_; |
| 245 }; | 238 }; |
| 246 | 239 |
| 247 } // namespace | 240 } // namespace |
| 248 | 241 |
| 249 TEST_F(V8VarConverterTest, SimpleRoundTripTest) { | 242 TEST_F(V8VarConverterTest, SimpleRoundTripTest) { |
| 250 EXPECT_TRUE(RoundTripAndCompare(PP_MakeUndefined())); | 243 EXPECT_TRUE(RoundTripAndCompare(PP_MakeUndefined())); |
| 251 EXPECT_TRUE(RoundTripAndCompare(PP_MakeNull())); | 244 EXPECT_TRUE(RoundTripAndCompare(PP_MakeNull())); |
| 252 EXPECT_TRUE(RoundTripAndCompare(PP_MakeInt32(100))); | 245 EXPECT_TRUE(RoundTripAndCompare(PP_MakeInt32(100))); |
| 253 EXPECT_TRUE(RoundTripAndCompare(PP_MakeBool(PP_TRUE))); | 246 EXPECT_TRUE(RoundTripAndCompare(PP_MakeBool(PP_TRUE))); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 expected->SetWithStringKey("null", quux.get()); | 435 expected->SetWithStringKey("null", quux.get()); |
| 443 ScopedPPVar oops(ScopedPPVar::PassRef(), StringVar::StringToPPVar("oops")); | 436 ScopedPPVar oops(ScopedPPVar::PassRef(), StringVar::StringToPPVar("oops")); |
| 444 expected->SetWithStringKey("undefined", oops.get()); | 437 expected->SetWithStringKey("undefined", oops.get()); |
| 445 ScopedPPVar release_expected(ScopedPPVar::PassRef(), expected->GetPPVar()); | 438 ScopedPPVar release_expected(ScopedPPVar::PassRef(), expected->GetPPVar()); |
| 446 | 439 |
| 447 ASSERT_TRUE(TestEqual(release_expected.get(), release_actual.get(), true)); | 440 ASSERT_TRUE(TestEqual(release_expected.get(), release_actual.get(), true)); |
| 448 } | 441 } |
| 449 } | 442 } |
| 450 | 443 |
| 451 } // namespace content | 444 } // namespace content |
| OLD | NEW |