1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A simple C++ Pepper plugin for exercising deprecated PPAPI interfaces in
// Blink layout tests.
//
// Most layout tests should prefer to use the normal Blink test plugin, with the
// MIME type application/x-blink-test-plugin. For layout tests that absolutely
// need to test deprecated synchronous scripting interfaces, this plugin can be
// instantiated using the application/x-blink-deprecated-test-plugin MIME type.
//
// The plugin exposes the following interface:
//
// Attributes:
// testwindowopen: if set, the plugin will synchronously attempt to open a
// window from DidCreateInstance, and log a message if successful.
//
// keydownscript: if set, the plugin will execute the value of the attribute as
// a script on a key down.
//
// mousedownscript: if set, the plugin will execute the value of the attribute
// as a script on a mouse button down.
//
//
// Functions:
// * plugin.normalize(): synchronously calls window.pluginCallback.
//
// * plugin.remember(value): keeps a reference on |value| in the plugin.
//
// * plugin.testCloneObject(): creates and returns another instance of the
// plugin object.
//
// * plugin.testCreateTestObject(): creates and returns a new TestObject
// instance (see below).
//
// * plugin.testExecuteScript(script): synchronously evaluates |script| and
// returns the result.
//
// * plugin.testGetProperty(property): returns the property named |property|
// from the window object.
//
// * plugin.testPassTestObject(function, object): synchronously calls the
// function named |function| on the window object, passing it |object| as a
// parameter, and returns its result.
//
// * plugin.testScriptObjectInvoke(function, value): synchronously calls the
// function named |function| on the window object, passing it |value| as a
// parameter, and returns its result.
//
//
// Properties:
// * plugin.testObject (read-only): a TestObject instance (see below).
//
// * plugin.testObjectCount (read-only): the number of TestObject instance
// created.
//
// * plugin.testGetUndefined (read-only): returns undefined.
//
//
// TestObject exposes the following interface:
// Properties:
// * object.testObject (read-only: another TestObject instance.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <stdint.h>
#include <map>
#include <sstream>
#include <unordered_map>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "ppapi/cpp/dev/scriptable_object_deprecated.h"
#include "ppapi/cpp/input_event.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/instance_private.h"
#include "ppapi/cpp/private/var_private.h"
#include "ppapi/cpp/var.h"
namespace {
class ScriptableBase : public pp::deprecated::ScriptableObject {
public:
explicit ScriptableBase(pp::InstancePrivate* instance)
: instance_(instance) {}
~ScriptableBase() override {}
// pp::deprecated::ScriptableObject overrides:
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 {
auto method = FindMethod(method_name);
if (method != methods_.end()) {
return method->second.Run(args, exception);
}
return ScriptableObject::Call(method_name, args, exception);
}
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));
else
ScriptableObject::SetProperty(name, value, exception);
}
protected:
using MethodMap = std::map<
std::string,
base::RepeatingCallback<pp::Var(const std::vector<pp::Var>&, pp::Var*)>>;
using PropertyMap =
std::map<std::string, base::RepeatingCallback<void(bool, pp::Var*)>>;
MethodMap::iterator FindMethod(const pp::Var& name) {
if (!name.is_string())
return methods_.end();
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) {
++count_;
properties_.insert(std::make_pair(
"testObject", base::BindRepeating(&TestObjectSO::TestObjectAccessor,
base::Unretained(this))));
}
~TestObjectSO() override {
--count_;
}
static int32_t count() { return count_; }
private:
void TestObjectAccessor(bool set, pp::Var* var) {
if (set)
return;
if (test_object_.is_undefined())
test_object_ = pp::VarPrivate(instance_, new TestObjectSO(instance_));
*var = test_object_;
}
static int32_t count_;
pp::VarPrivate test_object_;
};
int32_t TestObjectSO::count_ = 0;
class InstanceSO : public ScriptableBase {
public:
explicit InstanceSO(pp::InstancePrivate* instance)
: ScriptableBase(instance) {
methods_.insert(std::make_pair(
"normalize",
base::BindRepeating(&InstanceSO::Normalize, base::Unretained(this))));
methods_.insert(std::make_pair(
"remember",
base::BindRepeating(&InstanceSO::Remember, base::Unretained(this))));
methods_.insert(std::make_pair(
"testCloneObject", base::BindRepeating(&InstanceSO::TestCloneObject,
base::Unretained(this))));
methods_.insert(
std::make_pair("testCreateTestObject",
base::BindRepeating(&InstanceSO::TestCreateTestObject,
base::Unretained(this))));
methods_.insert(std::make_pair(
"testExecuteScript", base::BindRepeating(&InstanceSO::TestExecuteScript,
base::Unretained(this))));
methods_.insert(std::make_pair(
"testGetProperty", base::BindRepeating(&InstanceSO::TestGetProperty,
base::Unretained(this))));
methods_.insert(
std::make_pair("testPassTestObject",
base::BindRepeating(&InstanceSO::TestPassTestObject,
base::Unretained(this))));
// Note: the semantics of testScriptObjectInvoke are identical to the
// semantics of testPassTestObject: call args[0] with args[1] as a
// parameter.
methods_.insert(
std::make_pair("testScriptObjectInvoke",
base::BindRepeating(&InstanceSO::TestPassTestObject,
base::Unretained(this))));
properties_.insert(std::make_pair(
"testObject", base::BindRepeating(&InstanceSO::TestObjectAccessor,
base::Unretained(this))));
properties_.insert(
std::make_pair("testObjectCount",
base::BindRepeating(&InstanceSO::TestObjectCountAccessor,
base::Unretained(this))));
properties_.insert(std::make_pair(
"testGetUndefined",
base::BindRepeating(&InstanceSO::TestGetUndefinedAccessor,
base::Unretained(this))));
}
~InstanceSO() override = default;
private:
// Requires no argument.
pp::Var Normalize(const std::vector<pp::Var>& args, pp::Var* exception) {
pp::VarPrivate object = instance_->GetWindowObject();
return object.Call(pp::Var("pluginCallback"), exception);
}
// Requires 1 argument. The argument is retained into remembered_
pp::Var Remember(const std::vector<pp::Var>& args, pp::Var* exception) {
if (args.size() != 1) {
*exception = pp::Var("remember requires one argument");
return pp::Var();
}
remembered_ = args[0];
return pp::Var();
}
// Requires no argument.
pp::Var TestCloneObject(const std::vector<pp::Var>& args,
pp::Var* exception) {
return pp::VarPrivate(instance_, new InstanceSO(instance_));
}
// Requires no argument.
pp::Var TestCreateTestObject(const std::vector<pp::Var>& args,
pp::Var* exception) {
return pp::VarPrivate(instance_, new TestObjectSO(instance_));
}
// Requires one argument. The argument is passed through as-is to
// pp::InstancePrivate::ExecuteScript().
pp::Var TestExecuteScript(const std::vector<pp::Var>& args,
pp::Var* exception) {
if (args.size() != 1) {
*exception = pp::Var("testExecuteScript requires one argument");
return pp::Var();
}
return instance_->ExecuteScript(args[0], exception);
}
// Requires one or more arguments. Roughly analogous to NPN_GetProperty.
// The arguments are the chain of properties to traverse, starting with the
// global context.
pp::Var TestGetProperty(const std::vector<pp::Var>& args,
pp::Var* exception) {
if (args.size() < 1) {
*exception = pp::Var("testGetProperty requires at least one argument");
return pp::Var();
}
pp::VarPrivate object = instance_->GetWindowObject();
for (const auto& arg : args) {
if (!object.HasProperty(arg, exception))
return pp::Var();
object = object.GetProperty(arg, exception);
}
return object;
}
// 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 TestObjectAccessor(bool set, pp::Var* var) {
if (set)
return;
if (test_object_.is_undefined())
test_object_ = pp::VarPrivate(instance_, new TestObjectSO(instance_));
*var = test_object_;
}
void TestObjectCountAccessor(bool set, pp::Var* var) {
if (set)
return;
*var = pp::Var(TestObjectSO::count());
}
void TestGetUndefinedAccessor(bool set, pp::Var* var) {
if (set)
return;
*var = pp::Var();
}
pp::VarPrivate test_object_;
pp::Var remembered_;
};
class BlinkDeprecatedTestInstance : public pp::InstancePrivate {
public:
explicit BlinkDeprecatedTestInstance(PP_Instance instance)
: pp::InstancePrivate(instance) {}
~BlinkDeprecatedTestInstance() override {
LogMessage("%s", "Destroying");
}
// pp::Instance overrides
bool Init(uint32_t argc, const char* argn[], const char* argv[]) override {
for (uint32_t i = 0; i < argc; ++i)
attributes_[argn[i]] = argv[i];
if (HasAttribute("testwindowopen"))
return TestWindowOpen();
if (HasAttribute("initscript"))
ExecuteScript(attributes_["initscript"]);
uint32_t event_classes = 0;
if (HasAttribute("keydownscript"))
event_classes |= PP_INPUTEVENT_CLASS_KEYBOARD;
if (HasAttribute("mousedownscript"))
event_classes |= PP_INPUTEVENT_CLASS_MOUSE;
RequestFilteringInputEvents(event_classes);
return true;
}
virtual bool HandleInputEvent(const pp::InputEvent& event) override {
switch (event.GetType()) {
case PP_INPUTEVENT_TYPE_MOUSEDOWN:
if (HasAttribute("mousedownscript"))
ExecuteScript(attributes_["mousedownscript"]);
return true;
case PP_INPUTEVENT_TYPE_KEYDOWN:
if (HasAttribute("keydownscript"))
ExecuteScript(attributes_["keydownscript"]);
return true;
default:
return false;
}
}
// pp::InstancePrivate overrides:
pp::Var GetInstanceObject() override {
if (instance_var_.is_undefined()) {
instance_so_ = new InstanceSO(this);
instance_var_ = pp::VarPrivate(this, instance_so_);
}
return instance_var_;
}
void NotifyTestCompletion() {
ExecuteScript("window.testRunner.notifyDone()");
}
bool TestWindowOpen() {
pp::Var result = GetWindowObject().Call(
pp::Var("open"), pp::Var("about:blank"), pp::Var("_blank"));
if (result.is_object())
LogMessage("PLUGIN: WINDOW OPEN SUCCESS");
NotifyTestCompletion();
return true;
}
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:
bool HasAttribute(const std::string& name) {
return attributes_.find(name) != attributes_.end();
}
std::unordered_map<std::string, std::string> attributes_;
pp::VarPrivate instance_var_;
// Owned by |instance_var_|.
InstanceSO* instance_so_;
};
class BlinkDeprecatedTestModule : public pp::Module {
public:
BlinkDeprecatedTestModule() {}
~BlinkDeprecatedTestModule() override {}
pp::Instance* CreateInstance(PP_Instance instance) override {
return new BlinkDeprecatedTestInstance(instance);
}
};
} // namespace
namespace pp {
Module* CreateModule() {
return new BlinkDeprecatedTestModule();
}
} // namespace pp
|