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