John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/console.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 11 | #include "fpdfsdk/javascript/JS_Define.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/javascript/JS_Object.h" |
| 13 | #include "fpdfsdk/javascript/JS_Value.h" |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 14 | #include "fpdfsdk/javascript/cjs_event_context.h" |
dan sinclair | 8b6acdd | 2017-10-25 20:35:19 -0400 | [diff] [blame] | 15 | #include "fpdfsdk/javascript/cjs_eventhandler.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 17 | JSConstSpec CJS_Console::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 19 | JSPropertySpec CJS_Console::PropertySpecs[] = {{0, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 21 | JSMethodSpec CJS_Console::MethodSpecs[] = {{"clear", clear_static}, |
| 22 | {"hide", hide_static}, |
| 23 | {"println", println_static}, |
| 24 | {"show", show_static}, |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 25 | {0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame^] | 27 | const char* CJS_Console::g_pClassName = "console"; |
| 28 | int CJS_Console::g_nObjDefnID = -1; |
| 29 | |
| 30 | void CJS_Console::DefineConsts(CFXJS_Engine* pEngine) { |
| 31 | for (size_t i = 0; i < FX_ArraySize(ConstSpecs) - 1; ++i) { |
| 32 | pEngine->DefineObjConst( |
| 33 | g_nObjDefnID, ConstSpecs[i].pName, |
| 34 | ConstSpecs[i].eType == JSConstSpec::Number |
| 35 | ? pEngine->NewNumber(ConstSpecs[i].number).As<v8::Value>() |
| 36 | : pEngine->NewString(ConstSpecs[i].pStr).As<v8::Value>()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void CJS_Console::JSConstructor(CFXJS_Engine* pEngine, |
| 41 | v8::Local<v8::Object> obj) { |
| 42 | CJS_Object* pObj = new CJS_Console(obj); |
| 43 | pObj->SetEmbedObject(new console(pObj)); |
| 44 | pEngine->SetObjectPrivate(obj, pObj); |
| 45 | pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine)); |
| 46 | } |
| 47 | |
| 48 | void CJS_Console::JSDestructor(CFXJS_Engine* pEngine, |
| 49 | v8::Local<v8::Object> obj) { |
| 50 | delete static_cast<CJS_Console*>(pEngine->GetObjectPrivate(obj)); |
| 51 | } |
| 52 | |
| 53 | void CJS_Console::DefineProps(CFXJS_Engine* pEngine) { |
| 54 | for (size_t i = 0; i < FX_ArraySize(PropertySpecs) - 1; ++i) { |
| 55 | pEngine->DefineObjProperty(g_nObjDefnID, PropertySpecs[i].pName, |
| 56 | PropertySpecs[i].pPropGet, |
| 57 | PropertySpecs[i].pPropPut); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void CJS_Console::DefineMethods(CFXJS_Engine* pEngine) { |
| 62 | for (size_t i = 0; i < FX_ArraySize(MethodSpecs) - 1; ++i) { |
| 63 | pEngine->DefineObjMethod(g_nObjDefnID, MethodSpecs[i].pName, |
| 64 | MethodSpecs[i].pMethodCall); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void CJS_Console::DefineJSObjects(CFXJS_Engine* pEngine, FXJSOBJTYPE eObjType) { |
| 69 | g_nObjDefnID = pEngine->DefineObj(CJS_Console::g_pClassName, eObjType, |
| 70 | JSConstructor, JSDestructor); |
| 71 | DefineConsts(pEngine); |
| 72 | DefineProps(pEngine); |
| 73 | DefineMethods(pEngine); |
| 74 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 75 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | console::console(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
| 77 | |
| 78 | console::~console() {} |
| 79 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 80 | CJS_Return console::clear(CJS_Runtime* pRuntime, |
| 81 | const std::vector<v8::Local<v8::Value>>& params) { |
| 82 | return CJS_Return(true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 85 | CJS_Return console::hide(CJS_Runtime* pRuntime, |
| 86 | const std::vector<v8::Local<v8::Value>>& params) { |
| 87 | return CJS_Return(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 90 | CJS_Return console::println(CJS_Runtime* pRuntime, |
| 91 | const std::vector<v8::Local<v8::Value>>& params) { |
| 92 | return CJS_Return(params.size() > 0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 95 | CJS_Return console::show(CJS_Runtime* pRuntime, |
| 96 | const std::vector<v8::Local<v8::Value>>& params) { |
| 97 | return CJS_Return(true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 98 | } |