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 };
48 
49 class VendorManager : public IPrinterVendorManager {
50 public:
51     static std::string GetGlobalVendorName(const std::string &vendorName);
52     static std::string GetGlobalPrinterId(const std::string &globalVendorName, const std::string &printerId);
53     static std::string ExtractVendorName(const std::string &globalVendorName);
54     static std::string ExtractGlobalVendorName(const std::string &globalPrinterId);
55     static std::string ExtractPrinterId(const std::string &globalPrinterId);
56     VendorManager();
57     ~VendorManager();
58     bool Init(IPrintServiceAbility *sa, bool loadDefault = true);
59     void UnInit();
60     void StartDiscovery();
61     void StopDiscovery();
62     bool LoadVendorDriver(std::shared_ptr<VendorDriverBase> vendorDriver);
63     bool UnloadVendorDriver(const std::string &vendorName);
64     bool ConnectPrinter(const std::string &globalPrinterId);
65     bool ConnectPrinterByIp(const std::string &globalPrinterIp, const std::string &protocol);
66     bool QueryPrinterInfo(const std::string &globalPrinterId, int timeout);
67     int32_t AddPrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) override;
68     int32_t UpdatePrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) override;
69     int32_t RemovePrinterFromDiscovery(const std::string &vendorName, const std::string &printerId) override;
70     int32_t AddPrinterToCupsWithPpd(const std::string &vendorName, const std::string &printerId,
71                                     const std::string &ppdData) override;
72     int32_t RemovePrinterFromCups(const std::string &vendorName, const std::string &printerId) override;
73     bool OnPrinterStatusChanged(const std::string &vendorName, const std::string &printerId,
74                                 const PrinterVendorStatus &status) override;
75     bool OnPrinterPpdQueried(const std::string &vendorName, const std::string &printerId,
76                              const std::string &ppdData) override;
77     bool MonitorPrinterStatus(const std::string &globalPrinterId, bool on);
78     void StartStatusMonitor();
79     void StopStatusMonitor();
80     bool IsConnectingPrinter(const std::string &globalPrinterIdOrIP, const std::string &uri) override;
81     void SetConnectingPrinter(ConnectMethod method, const std::string &globalPrinterIdOrIP) override;
82     void ClearConnectingPrinter() override;
83     bool QueryPrinterCapabilityByUri(const std::string &uri, PrinterCapability &printerCap) override;
84     bool QueryPrinterStatusByUri(const std::string &uri, PrinterStatus &status) override;
85     std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &vendorName,
86         const std::string &printerId) override;
87     int32_t QueryPrinterInfoByPrinterId(const std::string &vendorName, const std::string &printerId,
88         PrinterInfo &info) override;
89     bool QueryPPDInformation(const char *makeModel, std::vector<std::string> &ppds) override;
90     std::shared_ptr<VendorDriverBase> FindDriverByPrinterId(const std::string &globalPrinterId);
91     std::shared_ptr<VendorDriverBase> FindDriverByVendorName(const std::string &vendorName);
92 
93 private:
94     void StatusMonitorProcess();
95     void UpdateAllPrinterStatus();
96     bool WaitNext();
97     bool IsPrivatePpdDriver(const std::string &vendorName);
98     bool IsWlanGroupDriver(const std::string &bothPrinterId);
99 
100 private:
101     std::atomic<bool> defaultLoaded{false};
102     IPrintServiceAbility *printServiceAbility = nullptr;
103     std::map<std::string, std::shared_ptr<VendorDriverBase>> vendorMap;
104     std::mutex vendorMapMutex;
105     std::shared_ptr<VendorDriverGroup> wlanGroupDriver = nullptr;
106     std::thread statusMonitorThread;
107     bool statusMonitorOn = false;
108     std::mutex statusMonitorMutex;
109     std::condition_variable statusMonitorCondition;
110     std::string connectingPrinterId;
111     bool isConnecting = false;
112     ConnectMethod connectingMethod = ID_AUTO;
113     std::string connectingPrinter;
114     std::mutex simpleObjectMutex;
115 };
116 }  // namespace Print
117 }  // namespace OHOS
118 #endif  // VENDOR_MANAGER_H
119