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 SERVICES_INCLUDE_WALLPAPER_SERVICES_H
17 #define SERVICES_INCLUDE_WALLPAPER_SERVICES_H
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 
23 #include "accesstoken_kit.h"
24 #include "component_name.h"
25 #include "concurrent_map.h"
26 #include "event_handler.h"
27 #include "fault_reporter.h"
28 #include "file_deal.h"
29 #include "i_wallpaper_manager_callback.h"
30 #include "image_source.h"
31 #include "ipc_skeleton.h"
32 #include "os_account_manager.h"
33 #include "pixel_map.h"
34 #include "system_ability.h"
35 #include "wallpaper_common.h"
36 #include "wallpaper_common_event_subscriber.h"
37 #include "wallpaper_data.h"
38 #include "wallpaper_event_listener.h"
39 #include "wallpaper_manager_common_info.h"
40 #include "wallpaper_service_stub.h"
41 #include "want.h"
42 
43 #ifndef THEME_SERVICE
44 #include "ability_connect_callback_interface.h"
45 #include "ability_manager_errors.h"
46 #include "wallpaper_extension_ability_connection.h"
47 #endif
48 namespace OHOS {
49 namespace ColorManager {
50 class Color;
51 }
52 namespace WallpaperMgrService {
53 class WallpaperService : public SystemAbility, public WallpaperServiceStub {
54     DECLARE_SYSTEM_ABILITY(WallpaperService);
55     enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
56     enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE };
57     using WallpaperListenerMap = std::map<int32_t, sptr<IWallpaperEventListener>>;
58 
59 public:
60     DISALLOW_COPY_AND_MOVE(WallpaperService);
61 
62     WallpaperService(int32_t systemAbilityId, bool runOnCreate);
63     WallpaperService();
64     ~WallpaperService();
65 
66     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override;
67     ErrorCode SetWallpaperByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override;
68     ErrorCode GetPixelMap(int32_t wallpaperType, FdInfo &fdInfo) override;
69     ErrorCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
70     ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override;
71     int32_t GetWallpaperId(int32_t wallpaperType) override;
72     bool IsChangePermitted() override;
73     bool IsOperationAllowed() override;
74     ErrorCode ResetWallpaper(int32_t wallpaperType) override;
75     ErrorCode On(const std::string &type, sptr<IWallpaperEventListener> listener) override;
76     ErrorCode Off(const std::string &type, sptr<IWallpaperEventListener> listener) override;
77     bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override;
78     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
79 
80     ErrorCode SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length) override;
81     ErrorCode SetWallpaperV9ByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override;
82     ErrorCode GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override;
83     ErrorCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
84     ErrorCode ResetWallpaperV9(int32_t wallpaperType) override;
85     ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override;
86     ErrorCode SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override;
87     ErrorCode SendEvent(const std::string &eventType) override;
88 
89 public:
90     void OnInitUser(int32_t newUserId);
91     void OnRemovedUser(int32_t userId);
92     void OnSwitchedUser(int32_t userId);
93     void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode);
94     void RegisterSubscriber(int32_t times);
95 #ifndef THEME_SERVICE
96     void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
97     void StartExtensionAbility(int32_t times);
98 #endif
99 protected:
100     void OnStart() override;
101     void OnStop() override;
102     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
103 
104 private:
105     void InitData();
106     void InitQueryUserId(int32_t times);
107     bool InitUsersOnBoot();
108     bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color);
109     bool SaveColor(int32_t userId, WallpaperType wallpaperType);
110     void UpdataWallpaperMap(int32_t userId, WallpaperType wallpaperType);
111     std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType);
112     bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
113     bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
114     bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData);
115     void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType);
116     ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType);
117     int32_t MakeWallpaperIdLocked();
118     bool CheckCallingPermission(const std::string &permissionName);
119     ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
120         const std::string &uriOrPixelMap, WallpaperType wallpaperType);
121     ErrorCode WritePixelMapToFile(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::string wallpaperTmpFullPath,
122         int32_t wallpaperType, WallpaperResourceType resourceType);
123     int64_t WritePixelMapToStream(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::ostream &outputStream);
124 #ifndef THEME_SERVICE
125     bool ConnectExtensionAbility();
126 #endif
127     bool IsSystemApp();
128     ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd);
129     ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size);
130     bool RestoreUserResources(int32_t userId, WallpaperData &wallpaperData, WallpaperType wallpaperType);
131     bool InitUserDir(int32_t userId);
132     int32_t QueryActiveUserId();
133     bool CheckUserPermissionById(int32_t userId);
134 
135     bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType);
136     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
137     ErrorCode SetWallpaperByPixelMap(
138         std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, WallpaperResourceType resourceType);
139     void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color);
140     ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
141     bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri);
142     void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType);
143     bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType, WallpaperResourceType resourceType);
144     void LoadWallpaperState();
145     WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType);
146     void RemoveExtensionDeathRecipient();
147     static void GetWallpaperFile(
148         WallpaperResourceType resourceType, const WallpaperData &wallpaperData, std::string &wallpaperFile);
149     std::string GetDefaultResDir();
150     std::string GetWallpaperDefaultPath(WallpaperType wallpaperType);
151     std::string GetWallpaperPathInJson(const std::string filePath);
152     void ClearRedundantFile(int32_t userId, WallpaperType wallpaperType, std::string fileName);
153 
154 private:
155     int32_t Init();
156     ServiceRunningState state_;
157     void InitServiceHandler();
158     static std::mutex instanceLock_;
159     static sptr<WallpaperService> instance_;
160     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
161     std::string wallpaperLockScreenFilePath_;
162     std::string wallpaperSystemFilePath_;
163     std::string wallpaperLockScreenFileFullPath_;
164     std::string wallpaperSystemFileFullPath_;
165     std::string wallpaperLockScreenCropFileFullPath_;
166     std::string wallpaperSystemCropFileFullPath_;
167     std::string wallpaperTmpFullPath_;
168     std::string wallpaperCropPath_;
169     typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP;
170     typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP;
171     typedef std::list<WallpaperEventListener *> LISTENERLIST;
172     LISTENERLIST colorListeners_;
173     COLORSLISTENERMAP colorsChangedListeners_;
174     ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_;
175     ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_;
176     atomic<int32_t> wallpaperId_;
177     sptr<IWallpaperCallback> callbackProxy_ = nullptr;
178     std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_;
179 #ifndef THEME_SERVICE
180     sptr<WallpaperExtensionAbilityConnection> connection_;
181 #endif
182     sptr<IRemoteObject::DeathRecipient> recipient_;
183     sptr<IRemoteObject> extensionRemoteObject_;
184     std::mutex remoteObjectMutex_;
185 
186     std::mutex mtx_;
187     uint64_t lockWallpaperColor_;
188     uint64_t systemWallpaperColor_;
189     std::map<std::string, WallpaperListenerMap> wallpaperEventMap_;
190     std::mutex listenerMapMutex_;
191     std::int32_t currentUserId_;
192     std::string appBundleName_;
193     std::mutex wallpaperColorMtx_;
194 };
195 } // namespace WallpaperMgrService
196 } // namespace OHOS
197 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H