1 /* 2 * Copyright (c) 2022 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 #ifndef PRINT_SERVICE_ABILITY_H 17 #define PRINT_SERVICE_ABILITY_H 18 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 #include <unordered_map> 23 #include <nlohmann/json.hpp> 24 25 #include "ability_manager_client.h" 26 #include "event_handler.h" 27 #include "extension_ability_info.h" 28 #include "iprint_callback.h" 29 #include "iremote_object.h" 30 #include "print_constant.h" 31 #include "print_service_stub.h" 32 #include "system_ability.h" 33 #include "print_security_guard_manager.h" 34 #include "print_service_helper.h" 35 #include "print_user_data.h" 36 #include "print_system_data.h" 37 #include "print_attribute_preference.h" 38 39 namespace OHOS::Print { 40 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 41 class IKeyguardStateCallback; 42 43 class PrintServiceAbility : public SystemAbility, public PrintServiceStub { 44 DECLARE_SYSTEM_ABILITY(PrintServiceAbility); 45 46 public: 47 DISALLOW_COPY_AND_MOVE(PrintServiceAbility); 48 PrintServiceAbility(int32_t systemAbilityId, bool runOnCreate); 49 PrintServiceAbility(); 50 ~PrintServiceAbility(); 51 static sptr<PrintServiceAbility> GetInstance(); 52 int32_t StartService() override; 53 int32_t StartPrint(const std::vector<std::string> &fileList, 54 const std::vector<uint32_t> &fdList, std::string &taskId) override; 55 int32_t StopPrint(const std::string &taskId) override; 56 int32_t ConnectPrinter(const std::string &printerId) override; 57 int32_t DisconnectPrinter(const std::string &printerId) override; 58 int32_t StartDiscoverPrinter(const std::vector<std::string> &extensionList) override; 59 int32_t StopDiscoverPrinter() override; 60 int32_t QueryAllExtension(std::vector<PrintExtensionInfo> &extensionInfos) override; 61 int32_t StartPrintJob(PrintJob &jobinfo) override; 62 int32_t CancelPrintJob(const std::string &jobId) override; 63 int32_t AddPrinters(const std::vector<PrinterInfo> &printerInfos) override; 64 int32_t RemovePrinters(const std::vector<std::string> &printerIds) override; 65 int32_t UpdatePrinters(const std::vector<PrinterInfo> &printerInfos) override; 66 int32_t UpdatePrinterState(const std::string &printerId, uint32_t state) override; 67 int32_t UpdatePrintJobStateOnlyForSystemApp(const std::string &jobId, uint32_t state, uint32_t subState) override; 68 int32_t UpdateExtensionInfo(const std::string &extInfo) override; 69 int32_t RequestPreview(const PrintJob &jobinfo, std::string &previewResult) override; 70 int32_t QueryPrinterCapability(const std::string &printerId) override; 71 int32_t On(const std::string taskId, const std::string &type, const sptr<IPrintCallback> &listener) override; 72 int32_t Off(const std::string taskId, const std::string &type) override; 73 int32_t RegisterPrinterCallback(const std::string &type, const sptr<IPrintCallback> &listener) override; 74 int32_t UnregisterPrinterCallback(const std::string &type) override; 75 int32_t RegisterExtCallback(const std::string &extensionCID, 76 const sptr<IPrintExtensionCallback> &listener) override; 77 int32_t UnregisterAllExtCallback(const std::string &extensionId) override; 78 int32_t LoadExtSuccess(const std::string &extensionId) override; 79 int32_t QueryAllPrintJob(std::vector<PrintJob> &printJobs) override; 80 int32_t QueryPrintJobById(std::string &printJobId, PrintJob &printjob) override; 81 int32_t AddPrinterToCups(const std::string &printerUri, const std::string &printerName, 82 const std::string &printerMake) override; 83 int32_t QueryPrinterCapabilityByUri(const std::string &printerUri, const std::string &printerId, 84 PrinterCapability &printerCaps) override; 85 void SetHelper(const std::shared_ptr<PrintServiceHelper> &helper); 86 int32_t PrintByAdapter(const std::string jobName, const PrintAttributes &printAttributes, 87 std::string &taskId) override; 88 int32_t StartGetPrintFile(const std::string &jobId, const PrintAttributes &printAttributes, 89 const uint32_t fd) override; 90 int32_t NotifyPrintService(const std::string &jobId, const std::string &type) override; 91 92 int32_t QueryPrinterInfoByPrinterId(const std::string &printerId, PrinterInfo &info) override; 93 94 int32_t QueryAddedPrinter(std::vector<std::string> &printerNameList) override; 95 96 int32_t QueryPrinterProperties(const std::string &printerId, const std::vector<std::string> &keyList, 97 std::vector<std::string> &valueList) override; 98 int32_t StartNativePrintJob(PrintJob &printJob) override; 99 int32_t UpdatePrintJobState(const std::string &jobId, uint32_t state, uint32_t subState); 100 void CancelUserPrintJobs(const int32_t userId); 101 void NotifyCurrentUserChanged(const int32_t userId); 102 int32_t NotifyPrintServiceEvent(std::string &jobId, uint32_t event) override; 103 int32_t GetPrinterPreference(const std::string &printerId, std::string &printerPreference) override; 104 int32_t SetPrinterPreference(const std::string &printerId, const std::string &printerPreference) override; 105 int32_t SetDefaultPrinter(const std::string &printerId, uint32_t type) override; 106 int32_t DeletePrinterFromCups(const std::string &printerUri, const std::string &printerName, 107 const std::string &printerMake) override; 108 109 protected: 110 void OnStart() override; 111 void OnStop() override; 112 113 private: 114 int32_t Init(); 115 void InitServiceHandler(); 116 void ManualStart(); 117 std::string GetPrintJobOrderId(); 118 bool StartAbility(const AAFwk::Want &want); 119 PrintExtensionInfo ConvertToPrintExtensionInfo(const AppExecFwk::ExtensionAbilityInfo &extInfo); 120 bool DelayStartDiscovery(const std::string &extensionId); 121 void SendPrinterDiscoverEvent(int event, const PrinterInfo &info); 122 void SendPrinterChangeEvent(int event, const PrinterInfo &info); 123 void SendPrinterEvent(const PrinterInfo &info); 124 void SendPrinterEventChangeEvent(PrinterEvent printerEvent, const PrinterInfo &info); 125 void SendPrintJobEvent(const PrintJob &jobInfo); 126 void SendExtensionEvent(const std::string &extensionId, const std::string &extInfo); 127 bool CheckPermission(const std::string &permissionName); 128 void SendQueuePrintJob(const std::string &printerId); 129 void NotifyAppJobQueueChanged(const std::string &applyResult); 130 std::shared_ptr<PrinterInfo> getPrinterInfo(const std::string printerId); 131 bool isEprint(const std::string &printerId); 132 void ReportHisysEvent(const std::shared_ptr<PrintJob> &jobInfo, const std::string &printerId, uint32_t subState); 133 void ReportCompletedPrint(const std::string &printerId); 134 void CheckJobQueueBlocked(const PrintJob &jobInfo); 135 int32_t CallSpooler(const std::vector<std::string> &fileList, const std::vector<uint32_t> &fdList, 136 std::string &taskId); 137 void notifyAdapterJobChanged(const std::string jobId, const uint32_t state, const uint32_t subState); 138 bool checkJobState(uint32_t state, uint32_t subState); 139 int32_t CheckAndSendQueuePrintJob(const std::string &jobId, uint32_t state, uint32_t subState); 140 void UpdateQueuedJobList(const std::string &jobId, const std::shared_ptr<PrintJob> &printJob); 141 void StartPrintJobCB(const std::string &jobId, const std::shared_ptr<PrintJob> &printJob); 142 void RegisterAdapterListener(const std::string &jobId); 143 int32_t AdapterGetFileCallBack(const std::string &jobId, uint32_t state, uint32_t subState); 144 bool UpdatePrintJobOptionByPrinterId(PrintJob &printJob); 145 std::shared_ptr<PrintJob> AddNativePrintJob(const std::string &jobId, PrintJob &printJob); 146 int32_t CallStatusBar(); 147 bool StartPluginPrintIconExtAbility(const AAFwk::Want &want); 148 bool IsPrinterJobMapEmpty(); 149 int32_t GetCurrentUserId(); 150 void UpdatePrintUserMap(); 151 void AddToPrintJobList(std::string jobId, const std::shared_ptr<PrintJob> &printjob); 152 std::shared_ptr<PrintUserData> GetCurrentUserData(); 153 int32_t GetUserIdByJobId(const std::string jobId); 154 std::shared_ptr<PrintUserData> GetUserDataByJobId(const std::string jobId); 155 bool IsQueuedJobListEmpty(const std::string &jobId); 156 void SetPrintJobCanceled(PrintJob &jobinfo); 157 void UnloadSystemAbility(); 158 void ReduceAppCount(); 159 void InitPreferenceMap(); 160 bool WritePreferenceToFile(); 161 bool WritePrinterPreference(const std::string &printerId, PrinterCapability &printerCaps); 162 bool WriteEprinterPreference(const std::string &printerId, PrinterCapability &printerCaps); 163 bool ReadPreferenceFromFile(const std::string &printerId, std::string& printPreference); 164 int32_t BuildPrinterPreference(PrinterCapability &cap, PrinterPreference &printPreference); 165 void BuildPrinterPreferenceByDefault(nlohmann::json& capOpt, PreferenceSetting &printerDefaultAttr); 166 void BuildPrinterPreferenceByOption(std::string& key, std::string& supportedOpts, 167 std::vector<std::string>& optAttrs); 168 void BuildPrinterAttrComponentByJson(std::string& key, nlohmann::json& jsonArrObject, 169 std::vector<std::string> &printerAttrs); 170 std::string StandardizePrinterId(const std::string &printerId); 171 bool CheckIsDefaultPrinter(const std::string &printerId); 172 bool CheckIsLastUsedPrinter(const std::string &printerId); 173 void SetLastUsedPrinter(const std::string &printerId); 174 int32_t DestroyExtension(); 175 void DeletePrinterFromUserData(const std::string &printerId); 176 std::shared_ptr<PrintUserData> GetUserDataByUserId(int32_t userId); 177 PrintJobState DetermineUserJobStatus(const std::map<std::string, std::shared_ptr<PrintJob>> &jobList); 178 void NotifyAppDeletePrinterWithDefaultPrinter(const std::string &printerId); 179 void ChangeDefaultPrinterForDelete(std::shared_ptr<PrintUserData> &userData, const std::string &printerId); 180 bool UpdatePrinterCapability(const std::string &printerId, PrinterInfo &info); 181 bool UpdatePrinterSystemData(const std::string &printerId, PrinterInfo &info); 182 uint32_t GetListeningState(const uint32_t subState); 183 std::string QueryPrinterIdByStandardizeName(const std::string &printerName); 184 bool CheckPrintJob(PrintJob &jobInfo); 185 bool CheckPrinterUriDifferent(const std::shared_ptr<PrinterInfo> &info); 186 187 private: 188 PrintSecurityGuardManager securityGuardManager_; 189 ServiceRunningState state_; 190 static std::mutex instanceLock_; 191 static sptr<PrintServiceAbility> instance_; 192 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 193 static std::chrono::time_point<std::chrono::high_resolution_clock> startPrintTime_; 194 static std::string ingressPackage; 195 196 std::recursive_mutex apiMutex_; 197 std::map<std::string, sptr<IPrintCallback>> registeredListeners_; 198 std::map<std::string, sptr<IPrintCallback>> adapterListenersByJobId_; 199 std::map<std::string, sptr<IPrintExtensionCallback>> extCallbackMap_; 200 201 std::map<std::string, AppExecFwk::ExtensionAbilityInfo> extensionList_; 202 std::map<std::string, PrintExtensionState> extensionStateList_; 203 std::map<std::string, std::shared_ptr<PrintJob>> printJobList_; 204 std::map<std::string, std::shared_ptr<PrintJob>> queuedJobList_; 205 std::map<std::string, std::string, JobIdCmp> jobOrderList_; 206 std::map<std::string, PrintAttributes> printAttributesList_; 207 208 std::map<std::string, std::shared_ptr<PrinterInfo>> printerInfoList_; 209 std::map<std::string, std::unordered_map<std::string, bool>> printerJobMap_; 210 211 std::string spoolerBundleName_; 212 std::string spoolerAbilityName_; 213 214 std::mutex lock_; 215 uint64_t currentJobOrderId_; 216 std::shared_ptr<PrintServiceHelper> helper_; 217 218 bool isJobQueueBlocked_; 219 std::map<int64_t, std::shared_ptr<PrintUserData>> printUserDataMap_; 220 PrintSystemData printSystemData_; 221 std::map<int32_t, std::shared_ptr<PrintUserData>> printUserMap_; 222 std::map<std::string, int32_t> userJobMap_; 223 int32_t currentUserId_; 224 225 uint32_t printAppCount_; 226 uint32_t unloadCount_; 227 std::map<std::string, std::string> printerIdAndPreferenceMap_; 228 }; 229 } // namespace OHOS::Print 230 #endif // PRINT_SYSTEM_ABILITY_H 231