1 // Copyright 2016 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fxjs/cjs_annot.h"
8
9 #include "fxjs/JS_Define.h"
10 #include "fxjs/cjs_event_context.h"
11 #include "fxjs/cjs_object.h"
12 #include "fxjs/js_resources.h"
13
14 namespace {
15
ToBAAnnot(CPDFSDK_Annot * annot)16 CPDFSDK_BAAnnot* ToBAAnnot(CPDFSDK_Annot* annot) {
17 return static_cast<CPDFSDK_BAAnnot*>(annot);
18 }
19
20 } // namespace
21
22 const JSPropertySpec CJS_Annot::PropertySpecs[] = {
23 {"hidden", get_hidden_static, set_hidden_static},
24 {"name", get_name_static, set_name_static},
25 {"type", get_type_static, set_type_static}};
26
27 int CJS_Annot::ObjDefnID = -1;
28
29 // static
GetObjDefnID()30 int CJS_Annot::GetObjDefnID() {
31 return ObjDefnID;
32 }
33
34 // static
DefineJSObjects(CFXJS_Engine * pEngine)35 void CJS_Annot::DefineJSObjects(CFXJS_Engine* pEngine) {
36 ObjDefnID = pEngine->DefineObj("Annot", FXJSOBJTYPE_DYNAMIC,
37 JSConstructor<CJS_Annot, Annot>,
38 JSDestructor<CJS_Annot>);
39 DefineProps(pEngine, ObjDefnID, PropertySpecs, FX_ArraySize(PropertySpecs));
40 }
41
Annot(CJS_Object * pJSObject)42 Annot::Annot(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
43
~Annot()44 Annot::~Annot() {}
45
get_hidden(CJS_Runtime * pRuntime)46 CJS_Return Annot::get_hidden(CJS_Runtime* pRuntime) {
47 if (!m_pAnnot)
48 return CJS_Return(JSGetStringFromID(JSMessage::kBadObjectError));
49
50 CPDF_Annot* pPDFAnnot = ToBAAnnot(m_pAnnot.Get())->GetPDFAnnot();
51 return CJS_Return(pRuntime->NewBoolean(
52 CPDF_Annot::IsAnnotationHidden(pPDFAnnot->GetAnnotDict())));
53 }
54
set_hidden(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)55 CJS_Return Annot::set_hidden(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
56 // May invalidate m_pAnnot.
57 bool bHidden = pRuntime->ToBoolean(vp);
58 if (!m_pAnnot)
59 return CJS_Return(JSGetStringFromID(JSMessage::kBadObjectError));
60
61 uint32_t flags = ToBAAnnot(m_pAnnot.Get())->GetFlags();
62 if (bHidden) {
63 flags |= ANNOTFLAG_HIDDEN;
64 flags |= ANNOTFLAG_INVISIBLE;
65 flags |= ANNOTFLAG_NOVIEW;
66 flags &= ~ANNOTFLAG_PRINT;
67 } else {
68 flags &= ~ANNOTFLAG_HIDDEN;
69 flags &= ~ANNOTFLAG_INVISIBLE;
70 flags &= ~ANNOTFLAG_NOVIEW;
71 flags |= ANNOTFLAG_PRINT;
72 }
73 ToBAAnnot(m_pAnnot.Get())->SetFlags(flags);
74
75 return CJS_Return(true);
76 }
77
get_name(CJS_Runtime * pRuntime)78 CJS_Return Annot::get_name(CJS_Runtime* pRuntime) {
79 if (!m_pAnnot)
80 return CJS_Return(JSGetStringFromID(JSMessage::kBadObjectError));
81 return CJS_Return(
82 pRuntime->NewString(ToBAAnnot(m_pAnnot.Get())->GetAnnotName().c_str()));
83 }
84
set_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)85 CJS_Return Annot::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
86 // May invalidate m_pAnnot.
87 WideString annotName = pRuntime->ToWideString(vp);
88 if (!m_pAnnot)
89 return CJS_Return(JSGetStringFromID(JSMessage::kBadObjectError));
90
91 ToBAAnnot(m_pAnnot.Get())->SetAnnotName(annotName);
92 return CJS_Return(true);
93 }
94
get_type(CJS_Runtime * pRuntime)95 CJS_Return Annot::get_type(CJS_Runtime* pRuntime) {
96 if (!m_pAnnot)
97 return CJS_Return(JSGetStringFromID(JSMessage::kBadObjectError));
98 return CJS_Return(pRuntime->NewString(
99 WideString::FromLocal(CPDF_Annot::AnnotSubtypeToString(
100 ToBAAnnot(m_pAnnot.Get())->GetAnnotSubtype())
101 .c_str())
102 .c_str()));
103 }
104
set_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)105 CJS_Return Annot::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
106 return CJS_Return(JSGetStringFromID(JSMessage::kReadOnlyError));
107 }
108
SetSDKAnnot(CPDFSDK_BAAnnot * annot)109 void Annot::SetSDKAnnot(CPDFSDK_BAAnnot* annot) {
110 m_pAnnot.Reset(annot);
111 }
112