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 "constants/annotation_flags.h"
10 #include "fpdfsdk/cpdfsdk_baannot.h"
11 #include "fxjs/cjs_event_context.h"
12 #include "fxjs/cjs_object.h"
13 #include "fxjs/js_define.h"
14 #include "fxjs/js_resources.h"
15
16 const JSPropertySpec CJS_Annot::PropertySpecs[] = {
17 {"hidden", get_hidden_static, set_hidden_static},
18 {"name", get_name_static, set_name_static},
19 {"type", get_type_static, set_type_static}};
20
21 int CJS_Annot::ObjDefnID = -1;
22
23 const char CJS_Annot::kName[] = "Annot";
24
25 // static
GetObjDefnID()26 int CJS_Annot::GetObjDefnID() {
27 return ObjDefnID;
28 }
29
30 // static
DefineJSObjects(CFXJS_Engine * pEngine)31 void CJS_Annot::DefineJSObjects(CFXJS_Engine* pEngine) {
32 ObjDefnID = pEngine->DefineObj(CJS_Annot::kName, FXJSOBJTYPE_DYNAMIC,
33 JSConstructor<CJS_Annot>, JSDestructor);
34 DefineProps(pEngine, ObjDefnID, PropertySpecs);
35 }
36
CJS_Annot(v8::Local<v8::Object> pObject,CJS_Runtime * pRuntime)37 CJS_Annot::CJS_Annot(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
38 : CJS_Object(pObject, pRuntime) {}
39
40 CJS_Annot::~CJS_Annot() = default;
41
SetSDKAnnot(CPDFSDK_BAAnnot * annot)42 void CJS_Annot::SetSDKAnnot(CPDFSDK_BAAnnot* annot) {
43 m_pAnnot.Reset(annot);
44 }
45
get_hidden(CJS_Runtime * pRuntime)46 CJS_Result CJS_Annot::get_hidden(CJS_Runtime* pRuntime) {
47 if (!m_pAnnot)
48 return CJS_Result::Failure(JSMessage::kBadObjectError);
49
50 CPDF_Annot* pPDFAnnot = m_pAnnot->AsBAAnnot()->GetPDFAnnot();
51 return CJS_Result::Success(pRuntime->NewBoolean(pPDFAnnot->IsHidden()));
52 }
53
set_hidden(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)54 CJS_Result CJS_Annot::set_hidden(CJS_Runtime* pRuntime,
55 v8::Local<v8::Value> vp) {
56 // May invalidate m_pAnnot.
57 bool bHidden = pRuntime->ToBoolean(vp);
58
59 CPDFSDK_BAAnnot* pBAAnnot = ToBAAnnot(m_pAnnot.Get());
60 if (!pBAAnnot)
61 return CJS_Result::Failure(JSMessage::kBadObjectError);
62
63 uint32_t flags = pBAAnnot->GetFlags();
64 if (bHidden) {
65 flags |= pdfium::annotation_flags::kHidden;
66 flags |= pdfium::annotation_flags::kInvisible;
67 flags |= pdfium::annotation_flags::kNoView;
68 flags &= ~pdfium::annotation_flags::kPrint;
69 } else {
70 flags &= ~pdfium::annotation_flags::kHidden;
71 flags &= ~pdfium::annotation_flags::kInvisible;
72 flags &= ~pdfium::annotation_flags::kNoView;
73 flags |= pdfium::annotation_flags::kPrint;
74 }
75 pBAAnnot->SetFlags(flags);
76 return CJS_Result::Success();
77 }
78
get_name(CJS_Runtime * pRuntime)79 CJS_Result CJS_Annot::get_name(CJS_Runtime* pRuntime) {
80 CPDFSDK_BAAnnot* pBAAnnot = ToBAAnnot(m_pAnnot.Get());
81 if (!pBAAnnot)
82 return CJS_Result::Failure(JSMessage::kBadObjectError);
83
84 return CJS_Result::Success(
85 pRuntime->NewString(pBAAnnot->GetAnnotName().AsStringView()));
86 }
87
set_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)88 CJS_Result CJS_Annot::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
89 // May invalidate m_pAnnot.
90 WideString annotName = pRuntime->ToWideString(vp);
91
92 CPDFSDK_BAAnnot* pBAAnnot = ToBAAnnot(m_pAnnot.Get());
93 if (!pBAAnnot)
94 return CJS_Result::Failure(JSMessage::kBadObjectError);
95
96 pBAAnnot->SetAnnotName(annotName);
97 return CJS_Result::Success();
98 }
99
get_type(CJS_Runtime * pRuntime)100 CJS_Result CJS_Annot::get_type(CJS_Runtime* pRuntime) {
101 CPDFSDK_BAAnnot* pBAAnnot = ToBAAnnot(m_pAnnot.Get());
102 if (!pBAAnnot)
103 return CJS_Result::Failure(JSMessage::kBadObjectError);
104
105 return CJS_Result::Success(pRuntime->NewString(
106 WideString::FromDefANSI(
107 CPDF_Annot::AnnotSubtypeToString(pBAAnnot->GetAnnotSubtype())
108 .AsStringView())
109 .AsStringView()));
110 }
111
set_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)112 CJS_Result CJS_Annot::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
113 return CJS_Result::Failure(JSMessage::kReadOnlyError);
114 }
115