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 "statistic_reporter.h" 37 #include "surface_buffer.h" 38 #include "system_ability.h" 39 #include "wallpaper_common.h" 40 #include "wallpaper_common_event_subscriber.h" 41 #include "wallpaper_data.h" 42 #include "wallpaper_event_listener.h" 43 #include "wallpaper_extension_ability_connection.h" 44 #include "wallpaper_manager_common_info.h" 45 #include "wallpaper_service_stub.h" 46 #include "want.h" 47 #include "window_option.h" 48 #include "wm_common.h" 49 50 namespace OHOS { 51 namespace ColorManager { 52 class Color; 53 } 54 namespace WallpaperMgrService { 55 class WallpaperService : public SystemAbility, public WallpaperServiceStub { 56 DECLARE_SYSTEM_ABILITY(WallpaperService); 57 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 58 enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE }; 59 using WallpaperListenerMap = std::map<int32_t, sptr<IWallpaperEventListener>>; 60 61 public: 62 DISALLOW_COPY_AND_MOVE(WallpaperService); 63 64 WallpaperService(int32_t systemAbilityId, bool runOnCreate); 65 WallpaperService(); 66 ~WallpaperService(); 67 68 ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override; 69 ErrorCode GetPixelMap(int32_t wallpaperType, FdInfo &fdInfo) override; 70 ErrorCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override; 71 ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override; 72 int32_t GetWallpaperId(int32_t wallpaperType) override; 73 bool IsChangePermitted() override; 74 bool IsOperationAllowed() override; 75 ErrorCode ResetWallpaper(int32_t wallpaperType) override; 76 ErrorCode On(const std::string &type, sptr<IWallpaperEventListener> listener) override; 77 ErrorCode Off(const std::string &type, sptr<IWallpaperEventListener> listener) override; 78 bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override; 79 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 80 81 ErrorCode SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length) 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(const std::string &uri, int32_t wallpaperType) override; 87 ErrorCode SendEvent(const std::string &eventType) override; 88 89 public: 90 static void OnBootPhase(); 91 void OnInitUser(int32_t newUserId); 92 void OnRemovedUser(int32_t userId); 93 void OnSwitchedUser(int32_t userId); 94 void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode); 95 void ReporterUsageTimeStatistic(); 96 void RegisterSubscriber(int32_t times); 97 void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject); 98 void StartExtensionAbility(int32_t times); 99 100 protected: 101 void OnStart() override; 102 void OnStop() override; 103 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 104 105 private: 106 void InitData(); 107 void InitResources(int32_t userId, WallpaperType wallpaperType); 108 void InitQueryUserId(int32_t times); 109 bool InitUsersOnBoot(); 110 bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color); 111 bool SaveColor(int32_t userId, WallpaperType wallpaperType); 112 void LoadSettingsLocked(int32_t userId, bool keepDimensionHints); 113 std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType); 114 bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName); 115 bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName); 116 bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData); 117 void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType); 118 ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType); 119 int32_t MakeWallpaperIdLocked(); 120 bool CheckCallingPermission(const std::string &permissionName); 121 bool GetBundleNameByUid(std::int32_t uid, std::string &bname); 122 ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType, 123 const std::string &uriOrPixelMap, WallpaperType wallpaperType); 124 bool ConnectExtensionAbility(); 125 bool IsSystemApp(); 126 ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd); 127 ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size); 128 bool RestoreUserResources(const WallpaperData &wallpaperData, WallpaperType wallpaperType); 129 bool InitUserDir(int32_t userId); 130 bool BlockRetry(int64_t interval, uint32_t maxRetryTimes, std::function<bool()> function); 131 int32_t QueryActiveUserId(); 132 bool CheckUserPermissionById(int32_t userId); 133 134 bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType); 135 ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType); 136 void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color); 137 ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType); 138 bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri); 139 void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType); 140 bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType); 141 void LoadWallpaperState(); 142 WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType); 143 void GrantUriPermissionByUserId(int32_t userId); 144 int32_t GrantUriPermission(const std::string &path, const std::string &bundleName); 145 void InitBundleNameParameter(); 146 void RemoveExtensionDeathRecipient(); 147 148 private: 149 int32_t Init(); 150 ServiceRunningState state_; 151 void InitServiceHandler(); 152 static std::mutex instanceLock_; 153 static sptr<WallpaperService> instance_; 154 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 155 std::string wallpaperLockScreenFilePath_; 156 std::string wallpaperSystemFilePath_; 157 std::string wallpaperLockScreenFileFullPath_; 158 std::string wallpaperSystemFileFullPath_; 159 std::string wallpaperLockScreenCropFileFullPath_; 160 std::string wallpaperSystemCropFileFullPath_; 161 std::string wallpaperTmpFullPath_; 162 std::string wallpaperCropPath_; 163 typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP; 164 typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP; 165 typedef std::list<WallpaperEventListener *> LISTENERLIST; 166 LISTENERLIST colorListeners_; 167 COLORSLISTENERMAP colorsChangedListeners_; 168 ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_; 169 ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_; 170 atomic<int32_t> wallpaperId_; 171 sptr<IWallpaperCallback> callbackProxy_ = nullptr; 172 std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_; 173 sptr<WallpaperExtensionAbilityConnection> connection_; 174 sptr<IRemoteObject::DeathRecipient> recipient_; 175 sptr<IRemoteObject> extensionRemoteObject_; 176 std::mutex remoteObjectMutex_; 177 178 std::string name_; 179 std::mutex mtx_; 180 uint64_t lockWallpaperColor_; 181 uint64_t systemWallpaperColor_; 182 std::map<std::string, WallpaperListenerMap> wallpaperEventMap_; 183 std::mutex listenerMapMutex_; 184 int32_t pictureWidth_ = 0; 185 int32_t pictureHeight_ = 0; 186 std::string appBundleName_; 187 }; 188 } // namespace WallpaperMgrService 189 } // namespace OHOS 190 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H