OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // A simple C++ Pepper plugin for exercising deprecated PPAPI interfaces in | 5 // A simple C++ Pepper plugin for exercising deprecated PPAPI interfaces in |
6 // Blink layout tests. | 6 // Blink layout tests. |
7 // | 7 // |
8 // Most layout tests should prefer to use the normal Blink test plugin, with the | 8 // Most layout tests should prefer to use the normal Blink test plugin, with the |
9 // MIME type application/x-blink-test-plugin. For layout tests that absolutely | 9 // MIME type application/x-blink-test-plugin. For layout tests that absolutely |
10 // need to test deprecated synchronous scripting interfaces, this plugin can be | 10 // need to test deprecated synchronous scripting interfaces, this plugin can be |
11 // instantiated using the application/x-blink-deprecated-test-plugin MIME type. | 11 // instantiated using the application/x-blink-deprecated-test-plugin MIME type. |
| 12 // |
| 13 // The plugin exposes the following interface: |
| 14 // |
| 15 // Attributes: |
| 16 // testwindowopen: if set, the plugin will synchronously attempt to open a |
| 17 // window from DidCreateInstance, and log a message if successful. |
| 18 // |
| 19 // keydownscript: if set, the plugin will execute the value of the attribute as |
| 20 // a script on a key down. |
| 21 // |
| 22 // mousedownscript: if set, the plugin will execute the value of the attribute |
| 23 // as a script on a mouse button down. |
| 24 // |
| 25 // |
| 26 // Functions: |
| 27 // * plugin.normalize(): synchronously calls window.pluginCallback. |
| 28 // |
| 29 // * plugin.remember(value): keeps a reference on |value| in the plugin. |
| 30 // |
| 31 // * plugin.testCloneObject(): creates and returns another instance of the |
| 32 // plugin object. |
| 33 // |
| 34 // * plugin.testExecuteScript(script): synchronously evaluates |script| and |
| 35 // returns the result. |
| 36 // |
| 37 // * plugin.testGetProperty(property): returns the property named |property| |
| 38 // from the window object. |
| 39 // |
| 40 // * plugin.testPassTestObject(function, object): synchronously calls the |
| 41 // function named |function| on the window object, passing it |object| as a |
| 42 // parameter, and returns its result. |
| 43 // |
| 44 // * plugin.testScriptObjectInvoke(function, value): synchronously calls the |
| 45 // function named |function| on the window object, passing it |value| as a |
| 46 // parameter, and returns its result. |
| 47 // |
| 48 // |
| 49 // Properties: |
| 50 // * plugin.testObject (read-only): a TestObject instance (see below). |
| 51 // |
| 52 // |
| 53 // TestObject exposes the following interface: |
| 54 // Properties: |
| 55 // * object.testObject (read-only: another TestObject instance. |
12 | 56 |
13 #include <stdint.h> | 57 #include <stdint.h> |
14 | 58 |
15 #include <map> | 59 #include <map> |
16 #include <sstream> | 60 #include <sstream> |
| 61 #include <unordered_map> |
17 #include <utility> | 62 #include <utility> |
18 | 63 |
19 #include "base/bind.h" | 64 #include "base/bind.h" |
20 #include "base/bind_helpers.h" | 65 #include "base/bind_helpers.h" |
21 #include "base/callback.h" | 66 #include "base/callback.h" |
22 #include "base/strings/stringprintf.h" | 67 #include "base/strings/stringprintf.h" |
23 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | 68 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
| 69 #include "ppapi/cpp/input_event.h" |
24 #include "ppapi/cpp/module.h" | 70 #include "ppapi/cpp/module.h" |
25 #include "ppapi/cpp/private/instance_private.h" | 71 #include "ppapi/cpp/private/instance_private.h" |
26 #include "ppapi/cpp/private/var_private.h" | 72 #include "ppapi/cpp/private/var_private.h" |
27 #include "ppapi/cpp/var.h" | 73 #include "ppapi/cpp/var.h" |
28 | 74 |
29 namespace { | 75 namespace { |
30 | 76 |
31 class ScriptableBase : public pp::deprecated::ScriptableObject { | 77 class ScriptableBase : public pp::deprecated::ScriptableObject { |
32 public: | 78 public: |
33 explicit ScriptableBase(pp::InstancePrivate* instance) | 79 explicit ScriptableBase(pp::InstancePrivate* instance) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 165 } |
120 | 166 |
121 pp::VarPrivate test_object_; | 167 pp::VarPrivate test_object_; |
122 }; | 168 }; |
123 | 169 |
124 class InstanceSO : public ScriptableBase { | 170 class InstanceSO : public ScriptableBase { |
125 public: | 171 public: |
126 explicit InstanceSO(pp::InstancePrivate* instance) | 172 explicit InstanceSO(pp::InstancePrivate* instance) |
127 : ScriptableBase(instance) { | 173 : ScriptableBase(instance) { |
128 methods_.insert(std::make_pair( | 174 methods_.insert(std::make_pair( |
| 175 "normalize", |
| 176 base::Bind(&InstanceSO::Normalize, base::Unretained(this)))); |
| 177 methods_.insert(std::make_pair( |
| 178 "remember", |
| 179 base::Bind(&InstanceSO::Remember, base::Unretained(this)))); |
| 180 methods_.insert(std::make_pair( |
| 181 "testCloneObject", |
| 182 base::Bind(&InstanceSO::TestCloneObject, base::Unretained(this)))); |
| 183 methods_.insert(std::make_pair( |
129 "testExecuteScript", | 184 "testExecuteScript", |
130 base::Bind(&InstanceSO::TestExecuteScript, base::Unretained(this)))); | 185 base::Bind(&InstanceSO::TestExecuteScript, base::Unretained(this)))); |
131 methods_.insert(std::make_pair( | 186 methods_.insert(std::make_pair( |
132 "testGetProperty", | 187 "testGetProperty", |
133 base::Bind(&InstanceSO::TestGetProperty, base::Unretained(this)))); | 188 base::Bind(&InstanceSO::TestGetProperty, base::Unretained(this)))); |
134 methods_.insert(std::make_pair( | 189 methods_.insert(std::make_pair( |
135 "testPassTestObject", | 190 "testPassTestObject", |
136 base::Bind(&InstanceSO::TestPassTestObject, base::Unretained(this)))); | 191 base::Bind(&InstanceSO::TestPassTestObject, base::Unretained(this)))); |
| 192 // Note: the semantics of testScriptObjectInvoke are identical to the |
| 193 // semantics of testPassTestObject: call args[0] with args[1] as a |
| 194 // parameter. |
| 195 methods_.insert( |
| 196 std::make_pair("testScriptObjectInvoke", |
| 197 base::Bind(&InstanceSO::TestPassTestObject, |
| 198 base::Unretained(this)))); |
137 properties_.insert(std::make_pair( | 199 properties_.insert(std::make_pair( |
138 "testObject", base::Bind(&InstanceSO::TestObjectAccessor, | 200 "testObject", base::Bind(&InstanceSO::TestObjectAccessor, |
139 base::Unretained(this)))); | 201 base::Unretained(this)))); |
140 } | 202 } |
141 ~InstanceSO() override {} | 203 ~InstanceSO() override {} |
142 | 204 |
143 private: | 205 private: |
| 206 // Requires no argument. |
| 207 pp::Var Normalize(const std::vector<pp::Var>& args, pp::Var* exception) { |
| 208 pp::VarPrivate object = instance_->GetWindowObject(); |
| 209 return object.Call(pp::Var("pluginCallback"), exception); |
| 210 } |
| 211 |
| 212 // Requires 1 argument. The argument is retained into remembered_ |
| 213 pp::Var Remember(const std::vector<pp::Var>& args, pp::Var* exception) { |
| 214 if (args.size() != 1) { |
| 215 *exception = pp::Var("remember requires one argument"); |
| 216 return pp::Var(); |
| 217 } |
| 218 remembered_ = args[0]; |
| 219 return pp::Var(); |
| 220 } |
| 221 |
| 222 // Requires no argument. |
| 223 pp::Var TestCloneObject(const std::vector<pp::Var>& args, |
| 224 pp::Var* exception) { |
| 225 return pp::VarPrivate(instance_, new InstanceSO(instance_)); |
| 226 } |
| 227 |
144 // Requires one argument. The argument is passed through as-is to | 228 // Requires one argument. The argument is passed through as-is to |
145 // pp::InstancePrivate::ExecuteScript(). | 229 // pp::InstancePrivate::ExecuteScript(). |
146 pp::Var TestExecuteScript(const std::vector<pp::Var>& args, | 230 pp::Var TestExecuteScript(const std::vector<pp::Var>& args, |
147 pp::Var* exception) { | 231 pp::Var* exception) { |
148 if (args.size() != 1) { | 232 if (args.size() != 1) { |
149 *exception = pp::Var("testExecuteScript requires one argument"); | 233 *exception = pp::Var("testExecuteScript requires one argument"); |
150 return pp::Var(); | 234 return pp::Var(); |
151 } | 235 } |
152 return instance_->ExecuteScript(args[0], exception); | 236 return instance_->ExecuteScript(args[0], exception); |
153 } | 237 } |
(...skipping 30 matching lines...) Expand all Loading... |
184 | 268 |
185 void TestObjectAccessor(bool set, pp::Var* var) { | 269 void TestObjectAccessor(bool set, pp::Var* var) { |
186 if (set) | 270 if (set) |
187 return; | 271 return; |
188 if (test_object_.is_undefined()) | 272 if (test_object_.is_undefined()) |
189 test_object_ = pp::VarPrivate(instance_, new TestObjectSO(instance_)); | 273 test_object_ = pp::VarPrivate(instance_, new TestObjectSO(instance_)); |
190 *var = test_object_; | 274 *var = test_object_; |
191 } | 275 } |
192 | 276 |
193 pp::VarPrivate test_object_; | 277 pp::VarPrivate test_object_; |
| 278 pp::Var remembered_; |
194 }; | 279 }; |
195 | 280 |
196 class BlinkDeprecatedTestInstance : public pp::InstancePrivate { | 281 class BlinkDeprecatedTestInstance : public pp::InstancePrivate { |
197 public: | 282 public: |
198 explicit BlinkDeprecatedTestInstance(PP_Instance instance) | 283 explicit BlinkDeprecatedTestInstance(PP_Instance instance) |
199 : pp::InstancePrivate(instance) {} | 284 : pp::InstancePrivate(instance) {} |
200 ~BlinkDeprecatedTestInstance() override { | 285 ~BlinkDeprecatedTestInstance() override { |
201 LogMessage("%s", "Destroying"); | 286 LogMessage("%s", "Destroying"); |
202 } | 287 } |
203 | 288 |
204 bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 289 // pp::Instance overrides |
| 290 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override { |
| 291 for (uint32_t i = 0; i < argc; ++i) |
| 292 attributes_[argn[i]] = argv[i]; |
| 293 |
| 294 if (HasAttribute("testwindowopen")) |
| 295 return TestWindowOpen(); |
| 296 |
| 297 uint32_t event_classes = 0; |
| 298 if (HasAttribute("keydownscript")) |
| 299 event_classes |= PP_INPUTEVENT_CLASS_KEYBOARD; |
| 300 if (HasAttribute("mousedownscript")) |
| 301 event_classes |= PP_INPUTEVENT_CLASS_MOUSE; |
| 302 RequestFilteringInputEvents(event_classes); |
| 303 |
205 return true; | 304 return true; |
206 } | 305 } |
207 | 306 |
| 307 virtual bool HandleInputEvent(const pp::InputEvent& event) override { |
| 308 switch (event.GetType()) { |
| 309 case PP_INPUTEVENT_TYPE_MOUSEDOWN: |
| 310 if (HasAttribute("mousedownscript")) |
| 311 ExecuteScript(attributes_["mousedownscript"]); |
| 312 return true; |
| 313 case PP_INPUTEVENT_TYPE_KEYDOWN: |
| 314 if (HasAttribute("keydownscript")) |
| 315 ExecuteScript(attributes_["keydownscript"]); |
| 316 return true; |
| 317 default: |
| 318 return false; |
| 319 } |
| 320 } |
| 321 |
208 // pp::InstancePrivate overrides: | 322 // pp::InstancePrivate overrides: |
209 pp::Var GetInstanceObject() override { | 323 pp::Var GetInstanceObject() override { |
210 if (instance_var_.is_undefined()) { | 324 if (instance_var_.is_undefined()) { |
211 instance_so_ = new InstanceSO(this); | 325 instance_so_ = new InstanceSO(this); |
212 instance_var_ = pp::VarPrivate(this, instance_so_); | 326 instance_var_ = pp::VarPrivate(this, instance_so_); |
213 } | 327 } |
214 return instance_var_; | 328 return instance_var_; |
215 } | 329 } |
216 | 330 |
| 331 void NotifyTestCompletion() { |
| 332 ExecuteScript("window.testRunner.notifyDone()"); |
| 333 } |
| 334 |
| 335 bool TestWindowOpen() { |
| 336 pp::Var result = GetWindowObject().Call( |
| 337 pp::Var("open"), pp::Var("about:blank"), pp::Var("_blank")); |
| 338 if (result.is_object()) |
| 339 LogMessage("PLUGIN: WINDOW OPEN SUCCESS"); |
| 340 NotifyTestCompletion(); |
| 341 return true; |
| 342 } |
| 343 |
217 void LogMessage(const char* format...) { | 344 void LogMessage(const char* format...) { |
218 va_list args; | 345 va_list args; |
219 va_start(args, format); | 346 va_start(args, format); |
220 LogToConsoleWithSource(PP_LOGLEVEL_LOG, | 347 LogToConsoleWithSource(PP_LOGLEVEL_LOG, |
221 pp::Var("Blink Deprecated Test Plugin"), | 348 pp::Var("Blink Deprecated Test Plugin"), |
222 pp::Var(base::StringPrintV(format, args))); | 349 pp::Var(base::StringPrintV(format, args))); |
223 va_end(args); | 350 va_end(args); |
224 } | 351 } |
225 | 352 |
226 private: | 353 private: |
| 354 bool HasAttribute(const std::string& name) { |
| 355 return attributes_.find(name) != attributes_.end(); |
| 356 } |
| 357 |
| 358 std::unordered_map<std::string, std::string> attributes_; |
227 pp::VarPrivate instance_var_; | 359 pp::VarPrivate instance_var_; |
228 // Owned by |instance_var_|. | 360 // Owned by |instance_var_|. |
229 InstanceSO* instance_so_; | 361 InstanceSO* instance_so_; |
230 }; | 362 }; |
231 | 363 |
232 class BlinkDeprecatedTestModule : public pp::Module { | 364 class BlinkDeprecatedTestModule : public pp::Module { |
233 public: | 365 public: |
234 BlinkDeprecatedTestModule() {} | 366 BlinkDeprecatedTestModule() {} |
235 ~BlinkDeprecatedTestModule() override {} | 367 ~BlinkDeprecatedTestModule() override {} |
236 | 368 |
237 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 369 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
238 return new BlinkDeprecatedTestInstance(instance); | 370 return new BlinkDeprecatedTestInstance(instance); |
239 } | 371 } |
240 }; | 372 }; |
241 | 373 |
242 } // namespace | 374 } // namespace |
243 | 375 |
244 namespace pp { | 376 namespace pp { |
245 | 377 |
246 Module* CreateModule() { | 378 Module* CreateModule() { |
247 return new BlinkDeprecatedTestModule(); | 379 return new BlinkDeprecatedTestModule(); |
248 } | 380 } |
249 | 381 |
250 } // namespace pp | 382 } // namespace pp |
OLD | NEW |