• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_user_data.h"
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <iostream>
20 #include <fstream>
21 #include <streambuf>
22 #include <json/json.h>
23 
24 #include "print_log.h"
25 #include "print_constant.h"
26 #include "print_json_util.h"
27 
28 namespace OHOS {
29 namespace Print {
30 
RegisterPrinterCallback(const std::string & type,const sptr<IPrintCallback> & listener)31 void PrintUserData::RegisterPrinterCallback(const std::string &type, const sptr<IPrintCallback> &listener)
32 {
33     registeredListeners_[type] = listener;
34 }
35 
UnregisterPrinterCallback(const std::string & type)36 void PrintUserData::UnregisterPrinterCallback(const std::string &type)
37 {
38     auto iter = registeredListeners_.find(type);
39     if (iter != registeredListeners_.end()) {
40         registeredListeners_.erase(iter);
41     }
42 }
43 
SendPrinterEvent(const std::string & type,int event,const PrinterInfo & info)44 void PrintUserData::SendPrinterEvent(const std::string &type, int event, const PrinterInfo &info)
45 {
46     auto iter = registeredListeners_.find(type);
47     if (iter != registeredListeners_.end() && iter->second != nullptr) {
48         iter->second->OnCallback(event, info);
49     }
50 }
51 
AddToPrintJobList(const std::string jobId,const std::shared_ptr<PrintJob> & printjob)52 void PrintUserData::AddToPrintJobList(const std::string jobId, const std::shared_ptr<PrintJob> &printjob)
53 {
54     printJobList_.insert(std::make_pair(jobId, printjob));
55 }
56 
UpdateQueuedJobList(const std::string & jobId,const std::shared_ptr<PrintJob> & printJob,std::string jobOrderId)57 void PrintUserData::UpdateQueuedJobList(
58     const std::string &jobId, const std::shared_ptr<PrintJob> &printJob, std::string jobOrderId)
59 {
60     if (jobOrderId == "0") {
61         jobOrderList_.clear();
62     }
63     auto jobIt = printJobList_.find(jobId);
64     if (jobIt == printJobList_.end()) {
65         PRINT_HILOGE("invalid job id");
66         return;
67     }
68     printJobList_.erase(jobIt);
69 
70     if (queuedJobList_.find(jobId) != queuedJobList_.end()) {
71         queuedJobList_[jobId] = printJob;
72         jobOrderList_[jobOrderId] = jobId;
73     } else {
74         queuedJobList_.insert(std::make_pair(jobId, printJob));
75         jobOrderList_.insert(std::make_pair(jobOrderId, jobId));
76     }
77 }
78 
QueryPrintJobById(std::string & printJobId,PrintJob & printJob)79 int32_t PrintUserData::QueryPrintJobById(std::string &printJobId, PrintJob &printJob)
80 {
81     if (printJobList_.empty()) {
82         PRINT_HILOGE("printJobList is empty!");
83         return E_PRINT_INVALID_PRINTJOB;
84     }
85     auto jobIt = printJobList_.find(printJobId);
86     if (jobIt == printJobList_.end()) {
87         PRINT_HILOGW("no print job exists");
88         return E_PRINT_INVALID_PRINTJOB;
89     } else {
90         if (jobIt->second != nullptr) {
91             printJob = *jobIt->second;
92         }
93     }
94     PRINT_HILOGI("QueryPrintJobById End.");
95     return E_PRINT_NONE;
96 }
97 
QueryAllPrintJob(std::vector<PrintJob> & printJobs)98 int32_t PrintUserData::QueryAllPrintJob(std::vector<PrintJob> &printJobs)
99 {
100     printJobs.clear();
101     for (auto iter : jobOrderList_) {
102         PRINT_HILOGI("QueryAllPrintJob queuedJobList_ jobOrderId: %{public}s, jobId: %{public}s",
103             iter.first.c_str(),
104             iter.second.c_str());
105         auto jobIt = queuedJobList_.find(iter.second);
106         if (jobIt == queuedJobList_.end()) {
107             PRINT_HILOGW("This job dose not exist.");
108             continue;
109         } else {
110             if (jobIt->second != nullptr) {
111                 printJobs.emplace_back(*jobIt->second);
112             }
113         }
114     }
115     PRINT_HILOGI("QueryAllPrintJob End.");
116     return E_PRINT_NONE;
117 }
118 
SetUserId(int32_t userId)119 void PrintUserData::SetUserId(int32_t userId)
120 {
121     userId_ = userId;
122 }
123 
SetLastUsedPrinter(const std::string & printerId)124 int32_t PrintUserData::SetLastUsedPrinter(const std::string &printerId)
125 {
126     PRINT_HILOGI("begin SetLastUsedPrinter, printerId: %{private}s", printerId.c_str());
127     if (printerId.empty()) {
128         PRINT_HILOGE("printerId is empty");
129         return E_PRINT_INVALID_PARAMETER;
130     }
131     std::lock_guard<std::recursive_mutex> lock(userDataMutex_);
132     lastUsedPrinterId_ = printerId;
133 
134     DeletePrinterFromUsedPrinterList(printerId);
135     usedPrinterList_.push_front(printerId);
136     PRINT_HILOGI("put printer at the head of the queue, printerId: %{private}s", usedPrinterList_.front().c_str());
137     if (useLastUsedPrinterForDefault_) {
138         defaultPrinterId_ = printerId;
139         PRINT_HILOGI("set the last used printer as the default printer");
140     }
141     if (!SetUserDataToFile()) {
142         PRINT_HILOGE("SetUserDataToFile failed.");
143         return E_PRINT_SERVER_FAILURE;
144     }
145 
146     return E_PRINT_NONE;
147 }
148 
GetLastUsedPrinter()149 std::string PrintUserData::GetLastUsedPrinter()
150 {
151     std::lock_guard<std::recursive_mutex> lock(userDataMutex_);
152     return lastUsedPrinterId_;
153 }
154 
SetDefaultPrinter(const std::string & printerId,uint32_t type)155 int32_t PrintUserData::SetDefaultPrinter(const std::string &printerId, uint32_t type)
156 {
157     std::lock_guard<std::recursive_mutex> lock(userDataMutex_);
158     PRINT_HILOGI("begin SetDefaultPrinter");
159     PRINT_HILOGD("printerId: %{private}s", printerId.c_str());
160     PRINT_HILOGI("type: %{public}d", type);
161     if (type == DEFAULT_PRINTER_TYPE_SETTED_BY_USER) {
162         defaultPrinterId_ = printerId;
163         useLastUsedPrinterForDefault_ = false;
164     } else if (type == DEFAULT_PRINTER_TYPE_LAST_USED_PRINTER || type == DELETE_DEFAULT_PRINTER) {
165         defaultPrinterId_ = lastUsedPrinterId_;
166         useLastUsedPrinterForDefault_ = true;
167     } else if (type == DELETE_LAST_USED_PRINTER) {
168         defaultPrinterId_ = lastUsedPrinterId_;
169     }
170     PRINT_HILOGD("defaultPrinterId_: %{private}s", defaultPrinterId_.c_str());
171     if (!SetUserDataToFile()) {
172         PRINT_HILOGE("SetUserDataToFile failed.");
173         return E_PRINT_SERVER_FAILURE;
174     }
175 
176     return E_PRINT_NONE;
177 }
178 
GetDefaultPrinter()179 std::string PrintUserData::GetDefaultPrinter()
180 {
181     return defaultPrinterId_;
182 }
183 
CheckIfUseLastUsedPrinterForDefault()184 bool PrintUserData::CheckIfUseLastUsedPrinterForDefault()
185 {
186     PRINT_HILOGI("useLastUsedPrinterForDefault_: %{public}d", useLastUsedPrinterForDefault_);
187     return useLastUsedPrinterForDefault_;
188 }
189 
DeletePrinter(const std::string & printerId)190 void PrintUserData::DeletePrinter(const std::string &printerId)
191 {
192     DeletePrinterFromUsedPrinterList(printerId);
193     if (!strcmp(lastUsedPrinterId_.c_str(), printerId.c_str())) {
194         std::lock_guard<std::recursive_mutex> lock(userDataMutex_);
195         if (usedPrinterList_.size()) {
196             auto it = usedPrinterList_.begin();
197             lastUsedPrinterId_ = *it;
198             PRINT_HILOGI(
199                 "change last used printer for delete printer, printerId: %{private}s", lastUsedPrinterId_.c_str());
200         } else {
201             lastUsedPrinterId_ = "";
202             PRINT_HILOGW("last used printer is null");
203         }
204     }
205     if (!SetUserDataToFile()) {
206         PRINT_HILOGE("SetUserDataToFile failed.");
207         return;
208     }
209 }
210 
DeletePrinterFromUsedPrinterList(const std::string & printerId)211 void PrintUserData::DeletePrinterFromUsedPrinterList(const std::string &printerId)
212 {
213     std::lock_guard<std::recursive_mutex> lock(userDataMutex_);
214     for (auto it = usedPrinterList_.begin(); it != usedPrinterList_.end(); ++it) {
215         std::string id = *it;
216         PRINT_HILOGI("printerId in usedPrinterList_: %{private}s", id.c_str());
217         if (!strcmp(id.c_str(), printerId.c_str())) {
218             PRINT_HILOGI("find printerId in used printer list.");
219             usedPrinterList_.erase(it);
220             break;
221         }
222     }
223 }
224 
ParseUserData()225 bool PrintUserData::ParseUserData()
226 {
227     std::string fileData = "";
228     if (!GetFileData(fileData)) {
229         PRINT_HILOGW("get file data failed");
230         return false;
231     }
232     Json::Value jsonObject;
233     if (CheckFileData(fileData, jsonObject)) {
234         ParseUserDataFromJson(jsonObject);
235     }
236     return true;
237 }
238 
ParseUserDataFromJson(Json::Value & jsonObject)239 void PrintUserData::ParseUserDataFromJson(Json::Value &jsonObject)
240 {
241     if (!PrintJsonUtil::IsMember(jsonObject, "print_user_data")) {
242         PRINT_HILOGW("can not find print_user_data");
243         return;
244     }
245     PRINT_HILOGI("userId_: %{public}d", userId_);
246     Json::Value userDataList = jsonObject["print_user_data"];
247     if (!PrintJsonUtil::IsMember(userDataList, std::to_string(userId_)) ||
248         !userDataList[std::to_string(userId_)].isObject()) {
249         PRINT_HILOGW("can not find current userId");
250         SetUserDataToFile();
251     }
252     Json::Value userData = userDataList[std::to_string(userId_)];
253     if (!PrintJsonUtil::IsMember(userData, "defaultPrinter") || !userData["defaultPrinter"].isString()) {
254         PRINT_HILOGW("can not find defaultPrinter");
255         return;
256     }
257     defaultPrinterId_ = userData["defaultPrinter"].asString();
258     if (!PrintJsonUtil::IsMember(userData, "lastUsedPrinter") || !userData["lastUsedPrinter"].isString()) {
259         PRINT_HILOGW("can not find lastUsedPrinter");
260         return;
261     }
262     lastUsedPrinterId_ = userData["lastUsedPrinter"].asString();
263     if (!PrintJsonUtil::IsMember(userData, "useLastUsedPrinterForDefault") ||
264         !userData["useLastUsedPrinterForDefault"].isBool()) {
265         PRINT_HILOGW("can not find useLastUsedPrinterForDefault");
266         return;
267     }
268     useLastUsedPrinterForDefault_ = userData["useLastUsedPrinterForDefault"].asBool();
269     if (!PrintJsonUtil::IsMember(userData, "usedPrinterList") || !userData["usedPrinterList"].isArray()) {
270         PRINT_HILOGW("can not find usedPrinterList");
271         return;
272     }
273     if (!ConvertJsonToUsedPrinterList(userData)) {
274         PRINT_HILOGW("convert json to usedPrinterList failed");
275         return;
276     }
277     PRINT_HILOGI(
278         "defaultPrinterId_: %{private}s, lastUsedPrinterId_: %{private}s, useLastUsedPrinterForDefault_: %{public}d",
279         defaultPrinterId_.c_str(),
280         lastUsedPrinterId_.c_str(),
281         useLastUsedPrinterForDefault_);
282 }
283 
ConvertJsonToUsedPrinterList(Json::Value & userData)284 bool PrintUserData::ConvertJsonToUsedPrinterList(Json::Value &userData)
285 {
286     Json::Value usedPrinterListJson = userData["usedPrinterList"];
287     uint32_t jsonSize = usedPrinterListJson.size();
288     if (jsonSize > MAX_PRINTER_SIZE) {
289         PRINT_HILOGE("usedPrinterList size is illegal");
290         return false;
291     }
292     for (uint32_t i = 0; i < jsonSize; i++) {
293         if (!usedPrinterListJson[i].isString()) {
294             PRINT_HILOGW("usedPrinterListJson item is not string");
295             return false;
296         }
297         usedPrinterList_.push_back(usedPrinterListJson[i].asString());
298     }
299     uint32_t size = usedPrinterList_.size();
300     PRINT_HILOGI("usedPrinterList_ size: %{public}d", size);
301     for (auto it = usedPrinterList_.begin(); it != usedPrinterList_.end(); ++it) {
302         PRINT_HILOGD("printerId in usedPrinterList_: %{private}s", it->c_str());
303     }
304     return true;
305 }
306 
ConvertUsedPrinterListToJson(Json::Value & usedPrinterListJson)307 void PrintUserData::ConvertUsedPrinterListToJson(Json::Value &usedPrinterListJson)
308 {
309     for (auto iter = usedPrinterList_.begin(); iter != usedPrinterList_.end(); ++iter) {
310         usedPrinterListJson.append(*iter);
311     }
312 }
313 
GetFileData(std::string & fileData)314 bool PrintUserData::GetFileData(std::string &fileData)
315 {
316     PRINT_HILOGI("begin GetFileData");
317     std::string userDataFilePath = PRINTER_SERVICE_FILE_PATH + "/" + PRINT_USER_DATA_FILE;
318     std::ifstream ifs(userDataFilePath.c_str(), std::ios::in | std::ios::binary);
319     if (!ifs.is_open()) {
320         PRINT_HILOGW("open printer list file fail");
321         char realPidFile[PATH_MAX] = {};
322         if (realpath(PRINTER_SERVICE_FILE_PATH.c_str(), realPidFile) == nullptr) {
323             PRINT_HILOGE("The realPidFile is null, errno:%{public}s", std::to_string(errno).c_str());
324             return false;
325         }
326         FILE *file = fopen(userDataFilePath.c_str(), "w+");
327         if (file == nullptr) {
328             PRINT_HILOGW("Failed to open file errno: %{public}s", std::to_string(errno).c_str());
329             return false;
330         }
331         Json::Value userDataJson;
332         Json::Value jsonObject;
333         jsonObject["version"] = PRINT_USER_DATA_VERSION;
334         jsonObject["print_user_data"] = userDataJson;
335         fileData = PrintJsonUtil::WriteString(jsonObject);
336         size_t jsonLength = fileData.length();
337         size_t writeLength = fwrite(fileData.c_str(), 1, strlen(fileData.c_str()), file);
338         int fcloseResult = fclose(file);
339         if (fcloseResult != 0 || writeLength < 0 || writeLength != jsonLength) {
340             PRINT_HILOGE("File Operation Failure.");
341             return false;
342         }
343     } else {
344         fileData.assign((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
345         ifs.close();
346     }
347     return true;
348 }
349 
SetUserDataToFile()350 bool PrintUserData::SetUserDataToFile()
351 {
352     PRINT_HILOGI("begin SetUserDataToFile");
353     std::string fileData = "";
354     if (!GetFileData(fileData)) {
355         PRINT_HILOGW("get file data failed");
356         return false;
357     }
358     Json::Value jsonObject;
359     if (CheckFileData(fileData, jsonObject)) {
360         PRINT_HILOGI("userId_: %{private}d", userId_);
361         Json::Value userData;
362         userData["defaultPrinter"] = defaultPrinterId_;
363         userData["lastUsedPrinter"] = lastUsedPrinterId_;
364         userData["useLastUsedPrinterForDefault"] = useLastUsedPrinterForDefault_;
365         Json::Value usedPrinterListJson;
366         ConvertUsedPrinterListToJson(usedPrinterListJson);
367         userData["usedPrinterList"] = usedPrinterListJson;
368         jsonObject["print_user_data"][std::to_string(userId_)] = userData;
369         std::string temp = PrintJsonUtil::WriteString(jsonObject);
370         PRINT_HILOGD("json temp: %{public}s", temp.c_str());
371         char realPidFile[PATH_MAX] = {};
372         std::string userDataFilePath = PRINTER_SERVICE_FILE_PATH + "/" + PRINT_USER_DATA_FILE;
373         if (realpath(PRINTER_SERVICE_FILE_PATH.c_str(), realPidFile) == nullptr) {
374             PRINT_HILOGE("The realPidFile is null, errno:%{public}s", std::to_string(errno).c_str());
375             return false;
376         }
377         FILE *file = fopen(userDataFilePath.c_str(), "w+");
378         if (file == nullptr) {
379             PRINT_HILOGW("Failed to open file errno: %{public}s", std::to_string(errno).c_str());
380             return false;
381         }
382         std::string jsonString = PrintJsonUtil::WriteString(jsonObject);
383         size_t jsonLength = jsonString.length();
384         size_t writeLength = fwrite(jsonString.c_str(), 1, strlen(jsonString.c_str()), file);
385         int fcloseResult = fclose(file);
386         if (fcloseResult != 0 || writeLength < 0) {
387             PRINT_HILOGE("File Operation Failure.");
388             return false;
389         }
390         PRINT_HILOGI("SetUserDataToFile finished");
391         return writeLength == jsonLength;
392     }
393     return true;
394 }
395 
CheckFileData(std::string & fileData,Json::Value & jsonObject)396 bool PrintUserData::CheckFileData(std::string &fileData, Json::Value &jsonObject)
397 {
398     if (!PrintJsonUtil::Parse(fileData, jsonObject)) {
399         PRINT_HILOGW("json accept fail");
400         return false;
401     }
402     if (!PrintJsonUtil::IsMember(jsonObject, "version") || !jsonObject["version"].isString()) {
403         PRINT_HILOGW("can not find version");
404         return false;
405     }
406     std::string version = jsonObject["version"].asString();
407     PRINT_HILOGI("json version: %{public}s", version.c_str());
408     if (version != PRINT_USER_DATA_VERSION || !PrintJsonUtil::IsMember(jsonObject, "print_user_data")) {
409         PRINT_HILOGW("can not find print_user_data");
410         return false;
411     }
412     return true;
413 }
414 }  // namespace Print
415 }  // namespace OHOS