1 /* 2 * Copyright (c) 2021-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 ABILITY_RUNTIME_CONTEXT_H 17 #define ABILITY_RUNTIME_CONTEXT_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "application_info.h" 23 #include "bindable.h" 24 #include "hap_module_info.h" 25 #include "foundation/aafwk/standard/interfaces/innerkits/app_manager/include/appmgr/configuration.h" 26 #include "foundation/communication/ipc/interfaces/innerkits/ipc_core/include/iremote_object.h" 27 #include "resource_manager.h" 28 29 using IRemoteObject = OHOS::IRemoteObject; 30 31 namespace OHOS { 32 namespace AbilityRuntime { 33 class Context : public Bindable { 34 public: 35 Context() = default; 36 ~Context() override = default; 37 38 /** 39 * @brief Obtains the Context object of the application. 40 * 41 * @return Returns the Context object of the application. 42 */ 43 static std::shared_ptr<Context> GetApplicationContext(); 44 45 /** 46 * @brief Obtains the bundle name of the current ability. 47 * 48 * @return Returns the bundle name of the current ability. 49 */ 50 virtual std::string GetBundleName() const = 0; 51 52 /** 53 * @brief Creates a Context object for an application with the given bundle name. 54 * 55 * @param bundleName Indicates the bundle name of the application. 56 * 57 * @return Returns a Context object created for the specified application. 58 */ 59 virtual std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) = 0; 60 61 /** 62 * @brief Obtains information about the current application. The returned application information includes basic 63 * information such as the application name and application permissions. 64 * 65 * @return Returns the ApplicationInfo for the current application. 66 */ 67 virtual std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const = 0; 68 69 /** 70 * @brief Obtains a resource manager. 71 * 72 * @return Returns a ResourceManager object. 73 */ 74 virtual std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const = 0; 75 76 /** 77 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 78 * source code, and configuration files of a module. 79 * 80 * @return Returns the path of the package file. 81 */ 82 virtual std::string GetBundleCodePath() const = 0; 83 84 /** 85 * @brief Obtains the HapModuleInfo object of the application. 86 * 87 * @return Returns the HapModuleInfo object of the application. 88 */ 89 virtual std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const = 0; 90 91 /** 92 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 93 * source code, and configuration files of a module. 94 * 95 * @return Returns the path of the package file. 96 */ 97 virtual std::string GetBundleCodeDir() = 0; 98 99 /** 100 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 101 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 102 * Older files are always deleted first. 103 * 104 * @return Returns the application-specific cache directory. 105 */ 106 virtual std::string GetCacheDir() = 0; 107 108 /** 109 * @brief Obtains the temporary directory. 110 * 111 * @return Returns the application temporary directory. 112 */ 113 virtual std::string GetTempDir() = 0; 114 115 /** 116 * @brief Obtains the directory for storing files for the application on the device's internal storage. 117 * 118 * @return Returns the application file directory. 119 */ 120 virtual std::string GetFilesDir() = 0; 121 122 /** 123 * @brief Checks whether the configuration of this ability is changing. 124 * 125 * @return Returns true if the configuration of this ability is changing and false otherwise. 126 */ 127 virtual bool IsUpdatingConfigurations() = 0; 128 129 /** 130 * @brief Informs the system of the time required for drawing this Page ability. 131 * 132 * @return Returns the notification is successful or fail 133 */ 134 virtual bool PrintDrawnCompleted() = 0; 135 136 /** 137 * @brief Obtains the local database path. 138 * If the local database path does not exist, the system creates one and returns the created path. 139 * 140 * @return Returns the local database file. 141 */ 142 virtual std::string GetDatabaseDir() = 0; 143 144 /** 145 * @brief Obtains the path storing the storage file of the application. 146 * 147 * @return Returns the local storage file. 148 */ 149 virtual std::string GetStorageDir() = 0; 150 151 /** 152 * @brief Obtains the path distributed file of the application 153 * 154 * @return Returns the distributed file. 155 */ 156 virtual std::string GetDistributedFilesDir() = 0; 157 158 /** 159 * @brief Obtains token. 160 * 161 * @return Returns the token. 162 */ 163 virtual sptr<IRemoteObject> GetToken() = 0; 164 165 /** 166 * @brief Attachs ability's token. 167 * 168 * @param token The token represents ability. 169 */ 170 virtual void SetToken(const sptr<IRemoteObject> &token) = 0; 171 172 /** 173 * @brief Switch file area 174 * 175 * @param mode file area. 176 */ 177 virtual void SwitchArea(int mode) = 0; 178 179 /** 180 * @brief Obtains the configuration of application. 181 * 182 * @return configuration of application. 183 */ 184 virtual std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const = 0; 185 186 /** 187 * @brief Obtains the application base directory on the device's internal storage. 188 * 189 * @return Returns the application base directory. 190 */ 191 virtual std::string GetBaseDir() const = 0; 192 193 /** 194 * @brief Getting derived class 195 * 196 * @tparam T template 197 * @param context the context object 198 * @return std::shared_ptr<T> derived class 199 */ 200 template<class T> ConvertTo(const std::shared_ptr<Context> & context)201 static std::shared_ptr<T> ConvertTo(const std::shared_ptr<Context>& context) 202 { 203 if constexpr (!std::is_same_v<T, typename T::SelfType>) { 204 return nullptr; 205 } 206 207 if (context && context->IsContext(T::CONTEXT_TYPE_ID)) { 208 return std::static_pointer_cast<T>(context); 209 } 210 211 return nullptr; 212 } 213 214 using SelfType = Context; 215 static const size_t CONTEXT_TYPE_ID; 216 217 protected: IsContext(size_t contextTypeId)218 virtual bool IsContext(size_t contextTypeId) 219 { 220 return contextTypeId == CONTEXT_TYPE_ID; 221 } 222 223 static std::shared_ptr<Context> appContext_; 224 static std::mutex contextMutex_; 225 }; 226 } // namespace AbilityRuntime 227 } // namespace OHOS 228 #endif // ABILITY_RUNTIME_CONTEXT_H 229