1 /* 2 * Copyright (c) 2024 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 VENDOR_MANAGER_H 17 #define VENDOR_MANAGER_H 18 19 #include <map> 20 #include <string> 21 #include <mutex> 22 #include <thread> 23 #include "vendor_driver_base.h" 24 #include "vendor_driver_group.h" 25 26 namespace OHOS { 27 namespace Print { 28 29 class IPrintServiceAbility { 30 public: 31 virtual bool AddVendorPrinterToDiscovery(const std::string &globalVendorName, const PrinterInfo &info) = 0; 32 virtual bool UpdateVendorPrinterToDiscovery(const std::string &globalVendorName, const PrinterInfo &info) = 0; 33 virtual bool RemoveVendorPrinterFromDiscovery(const std::string &globalVendorName, 34 const std::string &printerId) = 0; 35 virtual bool AddVendorPrinterToCupsWithPpd(const std::string &globalVendorName, const std::string &printerId, 36 const std::string &ppdData) = 0; 37 virtual bool AddVendorPrinterToCupsWithSpecificPpd(const std::string &globalVendorName, 38 const std::string &printerId, const std::string &ppdData) = 0; 39 virtual bool RemoveVendorPrinterFromCups(const std::string &vendorName, const std::string &printerId) = 0; 40 virtual bool OnVendorStatusUpdate(const std::string &globalVendorName, const std::string &printerId, 41 const PrinterVendorStatus &status) = 0; 42 virtual bool QueryPrinterCapabilityByUri(const std::string &uri, PrinterCapability &printerCap) = 0; 43 virtual bool QueryPrinterStatusByUri(const std::string &uri, PrinterStatus &status) = 0; 44 virtual std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &printerId) = 0; 45 virtual int32_t QueryPrinterInfoByPrinterId(const std::string &printerId, PrinterInfo &info) = 0; 46 virtual bool QueryPPDInformation(const char *makeModel, std::vector<std::string> &ppds) = 0; 47 virtual bool AddIpPrinterToSystemData(const std::string &globalVendorName, const PrinterInfo &info) = 0; 48 virtual bool AddIpPrinterToCupsWithPpd(const std::string &globalVendorName, const std::string &printerId, 49 const std::string &ppdData) = 0; 50 }; 51 52 class VendorManager : public IPrinterVendorManager { 53 public: 54 static std::string GetGlobalVendorName(const std::string &vendorName); 55 static std::string GetGlobalPrinterId(const std::string &globalVendorName, const std::string &printerId); 56 static std::string ExtractVendorName(const std::string &globalVendorName); 57 static std::string ExtractGlobalVendorName(const std::string &globalPrinterId); 58 static std::string ExtractPrinterId(const std::string &globalPrinterId); 59 VendorManager(); 60 ~VendorManager(); 61 bool Init(IPrintServiceAbility *sa, bool loadDefault = true); 62 void UnInit(); 63 void StartDiscovery(); 64 void StopDiscovery(); 65 bool LoadVendorDriver(std::shared_ptr<VendorDriverBase> vendorDriver); 66 bool UnloadVendorDriver(const std::string &vendorName); 67 bool ConnectPrinter(const std::string &globalPrinterId); 68 bool ConnectPrinterByIp(const std::string &globalPrinterIp, const std::string &protocol); 69 bool QueryPrinterInfo(const std::string &globalPrinterId, int timeout); 70 int32_t AddPrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) override; 71 int32_t UpdatePrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) override; 72 int32_t RemovePrinterFromDiscovery(const std::string &vendorName, const std::string &printerId) override; 73 int32_t AddPrinterToCupsWithPpd(const std::string &vendorName, const std::string &printerId, 74 const std::string &ppdData) override; 75 int32_t RemovePrinterFromCups(const std::string &vendorName, const std::string &printerId) override; 76 bool OnPrinterStatusChanged(const std::string &vendorName, const std::string &printerId, 77 const PrinterVendorStatus &status) override; 78 bool OnPrinterPpdQueried(const std::string &vendorName, const std::string &printerId, 79 const std::string &ppdData) override; 80 bool MonitorPrinterStatus(const std::string &globalPrinterId, bool on); 81 void StartStatusMonitor(); 82 void StopStatusMonitor(); 83 bool IsConnectingPrinter(const std::string &globalPrinterIdOrIP, const std::string &uri) override; 84 ConnectMethod GetConnectingMethod(const std::string &globalPrinterIdOrIp) override; 85 void SetConnectingPrinter(ConnectMethod method, const std::string &globalPrinterIdOrIP) override; 86 void ClearConnectingPrinter() override; 87 bool QueryPrinterCapabilityByUri(const std::string &uri, PrinterCapability &printerCap) override; 88 bool QueryPrinterStatusByUri(const std::string &uri, PrinterStatus &status) override; 89 std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &vendorName, 90 const std::string &printerId) override; 91 int32_t QueryPrinterInfoByPrinterId(const std::string &vendorName, const std::string &printerId, 92 PrinterInfo &info) override; 93 bool QueryPPDInformation(const char *makeModel, std::vector<std::string> &ppds) override; 94 std::shared_ptr<VendorDriverBase> FindDriverByPrinterId(const std::string &globalPrinterId); 95 std::shared_ptr<VendorDriverBase> FindDriverByVendorName(const std::string &vendorName); 96 97 private: 98 void StatusMonitorProcess(); 99 void UpdateAllPrinterStatus(); 100 bool WaitNext(); 101 bool IsPrivatePpdDriver(const std::string &vendorName); 102 bool IsWlanGroupDriver(const std::string &bothPrinterId); 103 104 private: 105 std::atomic<bool> defaultLoaded{false}; 106 IPrintServiceAbility *printServiceAbility = nullptr; 107 std::map<std::string, std::shared_ptr<VendorDriverBase>> vendorMap; 108 std::mutex vendorMapMutex; 109 std::shared_ptr<VendorDriverGroup> wlanGroupDriver = nullptr; 110 std::thread statusMonitorThread; 111 bool statusMonitorOn = false; 112 std::mutex statusMonitorMutex; 113 std::condition_variable statusMonitorCondition; 114 std::string connectingPrinterId; 115 bool isConnecting = false; 116 ConnectMethod connectingMethod = ID_AUTO; 117 std::string connectingPrinter; 118 std::mutex simpleObjectMutex; 119 }; 120 } // namespace Print 121 } // namespace OHOS 122 #endif // VENDOR_MANAGER_H 123