Chromium Code Reviews| Index: ppapi/tests/blink_deprecated_test_plugin.cc |
| diff --git a/ppapi/tests/blink_deprecated_test_plugin.cc b/ppapi/tests/blink_deprecated_test_plugin.cc |
| index b11a37df4334e913beba0ef39947a713664de45b..faf4953cc9e539f66c76bcad6caca7a989f950a9 100644 |
| --- a/ppapi/tests/blink_deprecated_test_plugin.cc |
| +++ b/ppapi/tests/blink_deprecated_test_plugin.cc |
| @@ -19,6 +19,7 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/callback.h" |
| +#include "base/strings/stringprintf.h" |
| #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
| #include "ppapi/cpp/module.h" |
| #include "ppapi/cpp/private/instance_private.h" |
| @@ -27,22 +28,21 @@ |
| namespace { |
| -class InstanceSO : public pp::deprecated::ScriptableObject { |
| +class ScriptableBase : public pp::deprecated::ScriptableObject { |
| public: |
| - explicit InstanceSO(pp::InstancePrivate* instance) : instance_(instance) { |
| - methods_.insert(std::make_pair( |
| - "testExecuteScript", |
| - base::Bind(&InstanceSO::TestExecuteScript, base::Unretained(this)))); |
| - methods_.insert(std::make_pair( |
| - "testGetProperty", |
| - base::Bind(&InstanceSO::TestGetProperty, base::Unretained(this)))); |
| - } |
| + explicit ScriptableBase(pp::InstancePrivate* instance) |
| + : instance_(instance) {} |
| + ~ScriptableBase() override {} |
| // pp::deprecated::ScriptableObject overrides: |
| - bool HasMethod(const pp::Var& name, pp::Var* exception) { |
| + bool HasMethod(const pp::Var& name, pp::Var* exception) override { |
| return FindMethod(name) != methods_.end(); |
| } |
| + bool HasProperty(const pp::Var& name, pp::Var* exception) override { |
| + return FindProperty(name) != properties_.end(); |
| + } |
| + |
| pp::Var Call(const pp::Var& method_name, |
| const std::vector<pp::Var>& args, |
| pp::Var* exception) override { |
| @@ -54,10 +54,33 @@ class InstanceSO : public pp::deprecated::ScriptableObject { |
| return ScriptableObject::Call(method_name, args, exception); |
| } |
| - private: |
| + pp::Var GetProperty(const pp::Var& name, pp::Var* exception) override { |
| + auto accessor = FindProperty(name); |
| + if (accessor != properties_.end()) { |
| + pp::Var value; |
| + accessor->second.Run(false, &value); |
| + return value; |
| + } |
| + return ScriptableObject::GetProperty(name, exception); |
| + } |
| + |
| + void SetProperty(const pp::Var& name, |
| + const pp::Var& value, |
| + pp::Var* exception) override { |
| + auto accessor = FindProperty(name); |
| + if (accessor != properties_.end()) |
| + accessor->second.Run(true, const_cast<pp::Var*>(&value)); |
|
dcheng
2016/03/18 07:08:09
Heh
|
| + else |
| + ScriptableObject::SetProperty(name, value, exception); |
| + } |
| + |
| + protected: |
| using MethodMap = |
| std::map<std::string, |
| base::Callback<pp::Var(const std::vector<pp::Var>&, pp::Var*)>>; |
| + using PropertyMap = |
| + std::map<std::string, base::Callback<void(bool, pp::Var*)>>; |
| + |
| MethodMap::iterator FindMethod(const pp::Var& name) { |
| if (!name.is_string()) |
| @@ -65,6 +88,65 @@ class InstanceSO : public pp::deprecated::ScriptableObject { |
| return methods_.find(name.AsString()); |
| } |
| + PropertyMap::iterator FindProperty(const pp::Var& name) { |
| + if (!name.is_string()) |
| + return properties_.end(); |
| + return properties_.find(name.AsString()); |
| + } |
| + |
| + pp::InstancePrivate* const instance_; |
| + MethodMap methods_; |
| + PropertyMap properties_; |
| +}; |
| + |
| +class TestObjectSO : public ScriptableBase { |
| + public: |
| + explicit TestObjectSO(pp::InstancePrivate* instance) |
| + : ScriptableBase(instance) { |
| + properties_.insert(std::make_pair( |
| + "testObject", |
| + base::Bind(&TestObjectSO::TestObjectAccessor, base::Unretained(this)))); |
| + } |
| + ~TestObjectSO() override {} |
| + |
| + private: |
| + void TestObjectAccessor(bool set, pp::Var* var) { |
| + if (set) |
| + return; |
| + if (!test_object_) { |
| + test_object_.reset(new TestObjectSO(instance_)); |
| + test_object_var_ = pp::VarPrivate(instance_, test_object_.get()); |
| + } |
| + *var = test_object_var_; |
| + } |
| + |
| + scoped_ptr<TestObjectSO> test_object_; |
| + pp::VarPrivate test_object_var_; |
| +}; |
| + |
| +class InstanceSO : public ScriptableBase { |
| + public: |
| + explicit InstanceSO(pp::InstancePrivate* instance) |
| + : ScriptableBase(instance), log_destroy_(false) { |
| + methods_.insert(std::make_pair( |
| + "testExecuteScript", |
| + base::Bind(&InstanceSO::TestExecuteScript, base::Unretained(this)))); |
| + methods_.insert(std::make_pair( |
| + "testGetProperty", |
| + base::Bind(&InstanceSO::TestGetProperty, base::Unretained(this)))); |
| + methods_.insert(std::make_pair( |
| + "testPassTestObject", |
| + base::Bind(&InstanceSO::TestPassTestObject, base::Unretained(this)))); |
| + properties_.insert(std::make_pair( |
| + "logDestroy", base::Bind(&InstanceSO::LogDestroyAccessor, |
| + base::Unretained(this)))); |
| + properties_.insert(std::make_pair( |
| + "testObject", base::Bind(&InstanceSO::TestObjectAccessor, |
| + base::Unretained(this)))); |
| + } |
| + ~InstanceSO() override {} |
| + |
| + private: |
| // Requires one argument. The argument is passed through as-is to |
| // pp::InstancePrivate::ExecuteScript(). |
| pp::Var TestExecuteScript(const std::vector<pp::Var>& args, |
| @@ -94,15 +176,47 @@ class InstanceSO : public pp::deprecated::ScriptableObject { |
| return object; |
| } |
| - pp::InstancePrivate* const instance_; |
| - MethodMap methods_; |
| + // Requires 2 or more arguments. The first argument is the name of a function |
| + // to invoke, and the second argument is a value to pass to that function. |
| + pp::Var TestPassTestObject(const std::vector<pp::Var>& args, |
| + pp::Var* exception) { |
| + if (args.size() < 2) { |
| + *exception = pp::Var("testPassTestObject requires at least 2 arguments"); |
| + return pp::Var(); |
| + } |
| + pp::VarPrivate object = instance_->GetWindowObject(); |
| + return object.Call(args[0], args[1], exception); |
| + } |
| + |
| + void LogDestroyAccessor(bool set, pp::Var* var) { |
|
dcheng
2016/03/18 07:08:09
Maybe we don't need this? We're unconditionally lo
piman
2016/03/18 18:41:13
You're right... the NPAPI one did this conditional
|
| + if (set) |
| + log_destroy_ = var->AsBool(); |
| + else |
| + *var = pp::Var(log_destroy_); |
| + } |
| + |
| + void TestObjectAccessor(bool set, pp::Var* var) { |
| + if (set) |
| + return; |
| + if (!test_object_) { |
| + test_object_.reset(new TestObjectSO(instance_)); |
| + test_object_var_ = pp::VarPrivate(instance_, test_object_.get()); |
|
dcheng
2016/03/18 07:08:09
Some comments seemed to imply that pp::VarPrivate
piman
2016/03/18 18:41:13
You're absolutely right. Fixed.
|
| + } |
| + *var = test_object_var_; |
| + } |
| + |
| + scoped_ptr<TestObjectSO> test_object_; |
| + pp::VarPrivate test_object_var_; |
| + bool log_destroy_; |
| }; |
| class BlinkDeprecatedTestInstance : public pp::InstancePrivate { |
| public: |
| explicit BlinkDeprecatedTestInstance(PP_Instance instance) |
| : pp::InstancePrivate(instance) {} |
| - ~BlinkDeprecatedTestInstance() override {} |
| + ~BlinkDeprecatedTestInstance() override { |
| + LogMessage("%s", "Destroying"); |
| + } |
| bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| return true; |
| @@ -117,6 +231,15 @@ class BlinkDeprecatedTestInstance : public pp::InstancePrivate { |
| return instance_var_; |
| } |
| + void LogMessage(const char* format...) { |
| + va_list args; |
| + va_start(args, format); |
| + LogToConsoleWithSource(PP_LOGLEVEL_LOG, |
| + pp::Var("Blink Deprecated Test Plugin"), |
| + pp::Var(base::StringPrintV(format, args))); |
| + va_end(args); |
| + } |
| + |
| private: |
| pp::VarPrivate instance_var_; |
| // Owned by |instance_var_|. |