• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "print_ani_callback.h"
17 #include "print_job_helper.h"
18 #include "print_attributes_helper.h"
19 #include "print_log.h"
20 #include "print_base_ani_util.h"
21 #include "ani_printer_info_helper.h"
22 #include "print_callback_ani_util.h"
23 
24 namespace OHOS::Print {
PrintAniCallback(ani_env * env,ani_object aniCallback)25 PrintAniCallback::PrintAniCallback(ani_env *env, ani_object aniCallback)
26 {
27     if (env == nullptr || aniCallback == nullptr) {
28         PRINT_HILOGE("nullptr error");
29         return;
30     }
31     ani_vm *vm = nullptr;
32     env->GetVM(&vm);
33     aniVm_ = vm;
34     env->GlobalReference_Create(reinterpret_cast<ani_ref>(aniCallback), &aniCallback_);
35 }
36 
~PrintAniCallback()37 PrintAniCallback::~PrintAniCallback()
38 {
39     aniVm_ = nullptr;
40     aniCallback_ = nullptr;
41 }
42 
43 
OnCallback()44 bool PrintAniCallback::OnCallback()
45 {
46     PRINT_HILOGI("PrintTask Notification in");
47     return true;
48 }
49 
OnCallback(uint32_t state,const PrinterInfo & info)50 bool PrintAniCallback::OnCallback(uint32_t state, const PrinterInfo &info)
51 {
52     PRINT_HILOGI("Printer Notification in");
53     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
54         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
55         return false;
56     }
57     ani_env *env;
58     ani_options aniArgs { 0, nullptr };
59     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
60     if (status != ANI_OK) {
61         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
62         if (status != ANI_OK) {
63             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
64             return false;
65         }
66     }
67     aniVm_->DetachCurrentThread();
68     return true;
69 }
70 
OnCallback(uint32_t state,const PrintJob & info)71 bool PrintAniCallback::OnCallback(uint32_t state, const PrintJob &info)
72 {
73     PRINT_HILOGI("PrintJob Notification in");
74     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
75         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
76         return false;
77     }
78     ani_env *env;
79     ani_options aniArgs { 0, nullptr };
80     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
81     if (status != ANI_OK) {
82         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
83         if (status != ANI_OK) {
84             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
85             return false;
86         }
87     }
88     aniVm_->DetachCurrentThread();
89     return true;
90 }
91 
OnCallback(const std::string & extensionId,const std::string & info)92 bool PrintAniCallback::OnCallback(const std::string &extensionId, const std::string &info)
93 {
94     PRINT_HILOGI("ExtensionInfo Notification in");
95     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
96         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
97         return false;
98     }
99     ani_env *env;
100     ani_options aniArgs { 0, nullptr };
101     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
102     if (status != ANI_OK) {
103         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
104         if (status != ANI_OK) {
105             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
106             return false;
107         }
108     }
109     aniVm_->DetachCurrentThread();
110     return true;
111 }
112 
OnCallbackAdapterLayout(const std::string & jobId,const PrintAttributes & oldAttrs,const PrintAttributes & newAttrs,uint32_t fd)113 bool PrintAniCallback::OnCallbackAdapterLayout(const std::string &jobId,
114     const PrintAttributes &oldAttrs, const PrintAttributes &newAttrs, uint32_t fd)
115 {
116     PRINT_HILOGI("PrintCallback OnCallbackAdapterLayout Notification in, jobId:%{public}s newAttrs copyNum:%{public}d",
117                  jobId.c_str(),
118                  newAttrs.GetCopyNumber());
119     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
120         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
121         return false;
122     }
123     ani_env *env;
124     ani_options aniArgs { 0, nullptr };
125     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
126     if (status != ANI_OK) {
127         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
128         if (status != ANI_OK) {
129             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
130             return false;
131         }
132     }
133     aniVm_->DetachCurrentThread();
134     return true;
135 }
136 
137 
onCallbackAdapterJobStateChanged(const std::string jobId,const uint32_t state,const uint32_t subState)138 bool PrintAniCallback::onCallbackAdapterJobStateChanged(const std::string jobId, const uint32_t state,
139                                                         const uint32_t subState)
140 {
141     PRINT_HILOGI("PrintCallback onCallbackAdapterJobStateChanged Notification in, jobId:%{public}s subState:%{public}d",
142                  jobId.c_str(), subState);
143     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
144         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
145         return false;
146     }
147     ani_env *env;
148     ani_options aniArgs { 0, nullptr };
149     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
150     if (status != ANI_OK) {
151         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
152         if (status != ANI_OK) {
153             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
154             return false;
155         }
156     }
157     aniVm_->DetachCurrentThread();
158     return true;
159 }
160 
OnCallbackAdapterGetFile(uint32_t state)161 bool PrintAniCallback::OnCallbackAdapterGetFile(uint32_t state)
162 {
163     PRINT_HILOGI("OnCallbackAdapterGetFile in");
164     if (aniVm_ != nullptr && aniCallback_ != nullptr) {
165         PRINT_HILOGE("aniVm_ or aniCallback_ is nullptr");
166         return false;
167     }
168     ani_env *env;
169     ani_options aniArgs { 0, nullptr };
170     auto status = aniVm_->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env);
171     if (status != ANI_OK) {
172         status = aniVm_->GetEnv(ANI_VERSION_1, &env);
173         if (status != ANI_OK) {
174             PRINT_HILOGI("vm GetEnv, err: %{private}d", status);
175             return false;
176         }
177     }
178     aniVm_->DetachCurrentThread();
179     return true;
180 }
181 
182 }  // namespace OHOS::Print
183