| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "content/renderer/pepper/host_globals.h" | 7 #include "content/renderer/pepper/host_globals.h" |
| 8 #include "content/renderer/pepper/host_var_tracker.h" | 8 #include "content/renderer/pepper/host_var_tracker.h" |
| 9 #include "content/renderer/pepper/mock_resource.h" | 9 #include "content/renderer/pepper/mock_resource.h" |
| 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 int g_v8objects_alive = 0; | 27 int g_v8objects_alive = 0; |
| 28 | 28 |
| 29 class MyObject : public gin::Wrappable<MyObject> { | 29 class MyObject : public gin::Wrappable<MyObject> { |
| 30 public: | 30 public: |
| 31 static gin::WrapperInfo kWrapperInfo; | 31 static gin::WrapperInfo kWrapperInfo; |
| 32 | 32 |
| 33 static v8::Handle<v8::Value> Create(v8::Isolate* isolate) { | 33 static v8::Local<v8::Value> Create(v8::Isolate* isolate) { |
| 34 return gin::CreateHandle(isolate, new MyObject()).ToV8(); | 34 return gin::CreateHandle(isolate, new MyObject()).ToV8(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 MyObject() { ++g_v8objects_alive; } | 38 MyObject() { ++g_v8objects_alive; } |
| 39 ~MyObject() override { --g_v8objects_alive; } | 39 ~MyObject() override { --g_v8objects_alive; } |
| 40 | 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(MyObject); | 41 DISALLOW_COPY_AND_ASSIGN(MyObject); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 gin::WrapperInfo MyObject::kWrapperInfo = {gin::kEmbedderNativeGin}; | 44 gin::WrapperInfo MyObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 45 | 45 |
| 46 class PepperTryCatchForTest : public PepperTryCatch { | 46 class PepperTryCatchForTest : public PepperTryCatch { |
| 47 public: | 47 public: |
| 48 PepperTryCatchForTest(PepperPluginInstanceImpl* instance, | 48 PepperTryCatchForTest(PepperPluginInstanceImpl* instance, |
| 49 V8VarConverter* converter) | 49 V8VarConverter* converter) |
| 50 : PepperTryCatch(instance, converter), | 50 : PepperTryCatch(instance, converter), |
| 51 handle_scope_(instance->GetIsolate()), | 51 handle_scope_(instance->GetIsolate()), |
| 52 context_scope_(v8::Context::New(instance->GetIsolate())) {} | 52 context_scope_(v8::Context::New(instance->GetIsolate())) {} |
| 53 | 53 |
| 54 void SetException(const char* message) override { NOTREACHED(); } | 54 void SetException(const char* message) override { NOTREACHED(); } |
| 55 bool HasException() override { return false; } | 55 bool HasException() override { return false; } |
| 56 v8::Handle<v8::Context> GetContext() override { | 56 v8::Local<v8::Context> GetContext() override { |
| 57 return instance_->GetIsolate()->GetCurrentContext(); | 57 return instance_->GetIsolate()->GetCurrentContext(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 v8::HandleScope handle_scope_; | 61 v8::HandleScope handle_scope_; |
| 62 v8::Context::Scope context_scope_; | 62 v8::Context::Scope context_scope_; |
| 63 | 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchForTest); | 64 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchForTest); |
| 65 }; | 65 }; |
| 66 | 66 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 EXPECT_EQ(0, tracker().GetLiveV8ObjectVarsForTest(pp_instance2)); | 105 EXPECT_EQ(0, tracker().GetLiveV8ObjectVarsForTest(pp_instance2)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Make sure that using the same v8 object should give the same PP_Var | 108 // Make sure that using the same v8 object should give the same PP_Var |
| 109 // each time. | 109 // each time. |
| 110 TEST_F(HostVarTrackerTest, ReuseVar) { | 110 TEST_F(HostVarTrackerTest, ReuseVar) { |
| 111 V8VarConverter converter( | 111 V8VarConverter converter( |
| 112 instance()->pp_instance(), V8VarConverter::kAllowObjectVars); | 112 instance()->pp_instance(), V8VarConverter::kAllowObjectVars); |
| 113 PepperTryCatchForTest try_catch(instance(), &converter); | 113 PepperTryCatchForTest try_catch(instance(), &converter); |
| 114 | 114 |
| 115 v8::Handle<v8::Value> v8_object = MyObject::Create(v8::Isolate::GetCurrent()); | 115 v8::Local<v8::Value> v8_object = MyObject::Create(v8::Isolate::GetCurrent()); |
| 116 ppapi::ScopedPPVar pp_object1 = try_catch.FromV8(v8_object); | 116 ppapi::ScopedPPVar pp_object1 = try_catch.FromV8(v8_object); |
| 117 ppapi::ScopedPPVar pp_object2 = try_catch.FromV8(v8_object); | 117 ppapi::ScopedPPVar pp_object2 = try_catch.FromV8(v8_object); |
| 118 | 118 |
| 119 // The two results should be the same. | 119 // The two results should be the same. |
| 120 EXPECT_EQ(pp_object1.get().value.as_id, pp_object2.get().value.as_id); | 120 EXPECT_EQ(pp_object1.get().value.as_id, pp_object2.get().value.as_id); |
| 121 | 121 |
| 122 // The objects should be able to get us back to the associated v8 object. | 122 // The objects should be able to get us back to the associated v8 object. |
| 123 { | 123 { |
| 124 scoped_refptr<V8ObjectVar> check_object( | 124 scoped_refptr<V8ObjectVar> check_object( |
| 125 V8ObjectVar::FromPPVar(pp_object1.get())); | 125 V8ObjectVar::FromPPVar(pp_object1.get())); |
| 126 ASSERT_TRUE(check_object.get()); | 126 ASSERT_TRUE(check_object.get()); |
| 127 EXPECT_EQ(instance(), check_object->instance()); | 127 EXPECT_EQ(instance(), check_object->instance()); |
| 128 EXPECT_EQ(v8_object, check_object->GetHandle()); | 128 EXPECT_EQ(v8_object, check_object->GetHandle()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Remove both of the refs we made above. | 131 // Remove both of the refs we made above. |
| 132 pp_object1 = ppapi::ScopedPPVar(); | 132 pp_object1 = ppapi::ScopedPPVar(); |
| 133 pp_object2 = ppapi::ScopedPPVar(); | 133 pp_object2 = ppapi::ScopedPPVar(); |
| 134 | 134 |
| 135 // Releasing the resource should free the internal ref, and so making a new | 135 // Releasing the resource should free the internal ref, and so making a new |
| 136 // one now should generate a new ID. | 136 // one now should generate a new ID. |
| 137 ppapi::ScopedPPVar pp_object3 = try_catch.FromV8(v8_object); | 137 ppapi::ScopedPPVar pp_object3 = try_catch.FromV8(v8_object); |
| 138 EXPECT_NE(pp_object1.get().value.as_id, pp_object3.get().value.as_id); | 138 EXPECT_NE(pp_object1.get().value.as_id, pp_object3.get().value.as_id); |
| 139 } | 139 } |
| 140 | 140 |
| 141 } // namespace content | 141 } // namespace content |
| OLD | NEW |