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