• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ability_connect_callback_interface.h"
24 #include "ability_manager_errors.h"
25 #include "accesstoken_kit.h"
26 #include "component_name.h"
27 #include "concurrent_map.h"
28 #include "event_handler.h"
29 #include "fault_reporter.h"
30 #include "file_deal.h"
31 #include "i_wallpaper_manager_callback.h"
32 #include "image_source.h"
33 #include "ipc_skeleton.h"
34 #include "os_account_manager.h"
35 #include "pixel_map.h"
36 #include "surface_buffer.h"
37 #include "system_ability.h"
38 #include "wallpaper_common.h"
39 #include "wallpaper_common_event_subscriber.h"
40 #include "wallpaper_data.h"
41 #include "wallpaper_event_listener.h"
42 #include "wallpaper_extension_ability_connection.h"
43 #include "wallpaper_manager_common_info.h"
44 #include "wallpaper_service_stub.h"
45 #include "want.h"
46 #include "window_option.h"
47 #include "wm_common.h"
48 
49 namespace OHOS {
50 namespace ColorManager {
51 class Color;
52 }
53 namespace WallpaperMgrService {
54 class WallpaperService : public SystemAbility, public WallpaperServiceStub {
55     DECLARE_SYSTEM_ABILITY(WallpaperService);
56     enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
57     enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE };
58     using WallpaperListenerMap = std::map<int32_t, sptr<IWallpaperEventListener>>;
59 
60 public:
61     DISALLOW_COPY_AND_MOVE(WallpaperService);
62 
63     WallpaperService(int32_t systemAbilityId, bool runOnCreate);
64     WallpaperService();
65     ~WallpaperService();
66 
67     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) 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 GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override;
82     ErrorCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
83     ErrorCode ResetWallpaperV9(int32_t wallpaperType) override;
84     ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override;
85     ErrorCode SetCustomWallpaper(const std::string &uri, int32_t wallpaperType) override;
86     ErrorCode SendEvent(const std::string &eventType) override;
87 
88 public:
89     static void OnBootPhase();
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     void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
96     void StartExtensionAbility(int32_t times);
97 
98 protected:
99     void OnStart() override;
100     void OnStop() override;
101     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102 
103 private:
104     void InitData();
105     void InitResources(int32_t userId, WallpaperType wallpaperType);
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 LoadSettingsLocked(int32_t userId, bool keepDimensionHints);
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     bool GetBundleNameByUid(std::int32_t uid, std::string &bname);
120     ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
121         const std::string &uriOrPixelMap, WallpaperType wallpaperType);
122     bool ConnectExtensionAbility();
123     bool IsSystemApp();
124     ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd);
125     ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size);
126     bool RestoreUserResources(const WallpaperData &wallpaperData, WallpaperType wallpaperType);
127     bool InitUserDir(int32_t userId);
128     bool BlockRetry(int64_t interval, uint32_t maxRetryTimes, std::function<bool()> function);
129     int32_t QueryActiveUserId();
130     bool CheckUserPermissionById(int32_t userId);
131 
132     bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType);
133     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
134     void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color);
135     ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
136     bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri);
137     void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType);
138     bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType);
139     void LoadWallpaperState();
140     WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType);
141     void GrantUriPermissionByUserId(int32_t userId);
142     int32_t GrantUriPermission(const std::string &path, const std::string &bundleName);
143     void InitBundleNameParameter();
144     void RemoveExtensionDeathRecipient();
145 
146 private:
147     int32_t Init();
148     ServiceRunningState state_;
149     void InitServiceHandler();
150     static std::mutex instanceLock_;
151     static sptr<WallpaperService> instance_;
152     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
153     std::string wallpaperLockScreenFilePath_;
154     std::string wallpaperSystemFilePath_;
155     std::string wallpaperLockScreenFileFullPath_;
156     std::string wallpaperSystemFileFullPath_;
157     std::string wallpaperLockScreenCropFileFullPath_;
158     std::string wallpaperSystemCropFileFullPath_;
159     std::string wallpaperTmpFullPath_;
160     std::string wallpaperCropPath_;
161     typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP;
162     typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP;
163     typedef std::list<WallpaperEventListener *> LISTENERLIST;
164     LISTENERLIST colorListeners_;
165     COLORSLISTENERMAP colorsChangedListeners_;
166     ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_;
167     ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_;
168     atomic<int32_t> wallpaperId_;
169     sptr<IWallpaperCallback> callbackProxy_ = nullptr;
170     std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_;
171     sptr<WallpaperExtensionAbilityConnection> connection_;
172     sptr<IRemoteObject::DeathRecipient> recipient_;
173     sptr<IRemoteObject> extensionRemoteObject_;
174     std::mutex remoteObjectMutex_;
175 
176     std::string name_;
177     std::mutex mtx_;
178     uint64_t lockWallpaperColor_;
179     uint64_t systemWallpaperColor_;
180     std::map<std::string, WallpaperListenerMap> wallpaperEventMap_;
181     std::mutex listenerMapMutex_;
182     int32_t pictureWidth_ = 0;
183     int32_t pictureHeight_ = 0;
184     std::string appBundleName_;
185 };
186 } // namespace WallpaperMgrService
187 } // namespace OHOS
188 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H