• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ABILITY_RUNTIME_MAIN_THREAD_H
17 #define OHOS_ABILITY_RUNTIME_MAIN_THREAD_H
18 
19 #include <string>
20 #include <mutex>
21 #include "event_handler.h"
22 #include "extension_config_mgr.h"
23 #include "idle_time.h"
24 #include "inner_event.h"
25 #include "app_scheduler_host.h"
26 #include "app_mgr_interface.h"
27 #include "ability_record_mgr.h"
28 #include "application_impl.h"
29 #include "resource_manager.h"
30 #include "foundation/ability/ability_runtime/interfaces/inner_api/runtime/include/runtime.h"
31 #include "foundation/ability/ability_runtime/interfaces/inner_api/runtime/include/source_map.h"
32 #include "ipc_singleton.h"
33 #include "native_engine/native_engine.h"
34 #include "watchdog.h"
35 #define ABILITY_LIBRARY_LOADER
36 
37 class Runtime;
38 class ModSourceMap;
39 namespace OHOS {
40 namespace AppExecFwk {
41 using namespace OHOS::Global;
42 using OHOS::AbilityRuntime::Runtime;
43 using OHOS::AbilityRuntime::ModSourceMap;
44 enum class MainThreadState { INIT, ATTACH, READY, RUNNING };
45 struct BundleInfo;
46 class ContextDeal;
47 // class Global::Resource::ResourceManager;
48 class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
49 public:
50     /**
51      *
52      * @brief Notify the AppMgrDeathRecipient that the remote is dead.
53      *
54      * @param remote The remote which is dead.
55      */
56     virtual void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
57     AppMgrDeathRecipient() = default;
58     ~AppMgrDeathRecipient() override = default;
59 };
60 
61 class MainThread : public AppSchedulerHost {
62     DECLARE_DELAYED_IPCSINGLETON(MainThread);
63 
64 public:
65     /**
66      *
67      * @brief Get the current MainThreadState.
68      *
69      * @return Returns the current MainThreadState.
70      */
71     MainThreadState GetMainThreadState() const;
72 
73     /**
74      *
75      * @brief Get the runner state of mainthread.
76      *
77      * @return Returns the runner state of mainthread.
78      */
79     bool GetRunnerStarted() const;
80 
81     /**
82      *
83      * @brief Get the newThreadId.
84      *
85      * @return Returns the newThreadId.
86      */
87     int GetNewThreadId();
88 
89     /**
90      *
91      * @brief Get the application.
92      *
93      * @return Returns the application.
94      */
95     std::shared_ptr<OHOSApplication> GetApplication() const;
96 
97     /**
98      *
99      * @brief Get the applicationInfo.
100      *
101      * @return Returns the applicationInfo.
102      */
103     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const;
104 
105     /**
106      *
107      * @brief Get the applicationImpl.
108      *
109      * @return Returns the applicationImpl.
110      */
111     std::shared_ptr<ApplicationImpl> GetApplicationImpl();
112 
113     /**
114      *
115      * @brief Get the eventHandler of mainthread.
116      *
117      * @return Returns the eventHandler of mainthread.
118      */
119     std::shared_ptr<EventHandler> GetMainHandler() const;
120 
121     /**
122      *
123      * @brief Schedule the foreground lifecycle of application.
124      *
125      */
126     void ScheduleForegroundApplication() override;
127 
128     /**
129      *
130      * @brief Schedule the background lifecycle of application.
131      *
132      */
133     void ScheduleBackgroundApplication() override;
134 
135     /**
136      *
137      * @brief Schedule the terminate lifecycle of application.
138      *
139      */
140     void ScheduleTerminateApplication() override;
141 
142     /**
143      *
144      * @brief Shrink the memory which used by application.
145      *
146      * @param level Indicates the memory trim level, which shows the current memory usage status.
147      */
148     void ScheduleShrinkMemory(const int level) override;
149 
150     /**
151      *
152      * @brief Notify the current memory.
153      *
154      * @param level Indicates the memory trim level, which shows the current memory usage status.
155      */
156     void ScheduleMemoryLevel(const int level) override;
157 
158     /**
159      *
160      * @brief Low the memory which used by application.
161      *
162      */
163     void ScheduleLowMemory() override;
164 
165     /**
166      *
167      * @brief Launch the application.
168      *
169      * @param data The launchdata of the application witch launced.
170      *
171      */
172     void ScheduleLaunchApplication(const AppLaunchData &data, const Configuration &config) override;
173 
174     /**
175      *
176      * @brief update the application info after new module installed.
177      *
178      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
179      *
180      */
181     void ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo) override;
182 
183     /**
184      * Notify application to launch ability stage.
185      *
186      * @param The resident process data value.
187      */
188     void ScheduleAbilityStage(const HapModuleInfo &abilityStage) override;
189 
190     void ScheduleLaunchAbility(const AbilityInfo &info, const sptr<IRemoteObject> &token,
191         const std::shared_ptr<AAFwk::Want> &want) override;
192 
193     /**
194      *
195      * @brief clean the ability by token.
196      *
197      * @param token The token belong to the ability which want to be cleaned.
198      *
199      */
200     void ScheduleCleanAbility(const sptr<IRemoteObject> &token) override;
201 
202     /**
203      *
204      * @brief send the new profile.
205      *
206      * @param profile The updated profile.
207      *
208      */
209     void ScheduleProfileChanged(const Profile &profile) override;
210 
211     /**
212      *
213      * @brief send the new config to the application.
214      *
215      * @param config The updated config.
216      *
217      */
218     void ScheduleConfigurationUpdated(const Configuration &config) override;
219 
220     /**
221      *
222      * @brief Starts the mainthread.
223      *
224      */
225     static void Start();
226 
227     /**
228      *
229      * @brief Schedule the application process exit safely.
230      *
231      */
232     void ScheduleProcessSecurityExit() override;
233 
234     void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName) override;
235 
236     /**
237      *
238      * @brief Check the App main thread state.
239      *
240      */
241     void CheckMainThreadIsAlive();
242 
243     int32_t ScheduleNotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
244         const int32_t recordId) override;
245 
246     int32_t ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId) override;
247 
248     int32_t ScheduleNotifyUnLoadRepairPatch(const std::string &bundleName,
249         const sptr<IQuickFixCallback> &callback, const int32_t recordId) override;
250 
251 private:
252     /**
253      *
254      * @brief Terminate the application but don't notify ams.
255      *
256      */
257     void HandleTerminateApplicationLocal();
258 
259     void HandleScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName);
260 
261     /**
262      *
263      * @brief Schedule the application process exit safely.
264      *
265      */
266     void HandleProcessSecurityExit();
267 
268     /**
269      *
270      * @brief Clean the ability but don't notify ams.
271      *
272      * @param token The token which belongs to the ability launched.
273      *
274      */
275     void HandleCleanAbilityLocal(const sptr<IRemoteObject> &token);
276 
277     /**
278      *
279      * @brief Launch the application.
280      *
281      * @param appLaunchData The launchdata of the application witch launced.
282      *
283      */
284     void HandleLaunchApplication(const AppLaunchData &appLaunchData, const Configuration &config);
285 
286     /**
287      *
288      * @brief update the application info after new module installed.
289      *
290      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
291      *
292      */
293     void HandleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo);
294 
295     /**
296      *
297      * @brief Launch the application.
298      *
299      * @param appLaunchData The launchdata of the application witch launced.
300      *
301      */
302     void HandleAbilityStage(const HapModuleInfo &abilityStage);
303 
304     /**
305      *
306      * @brief Launch the ability.
307      *
308      * @param abilityRecord The abilityRecord which belongs to the ability launched.
309      *
310      */
311     void HandleLaunchAbility(const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
312 
313     /**
314      *
315      * @brief Clean the ability.
316      *
317      * @param token The token which belongs to the ability launched.
318      *
319      */
320     void HandleCleanAbility(const sptr<IRemoteObject> &token);
321 
322     /**
323      *
324      * @brief Foreground the application.
325      *
326      */
327     void HandleForegroundApplication();
328 
329     /**
330      *
331      * @brief Background the application.
332      *
333      */
334     void HandleBackgroundApplication();
335 
336     /**
337      *
338      * @brief Terminate the application.
339      *
340      */
341     void HandleTerminateApplication();
342 
343     /**
344      *
345      * @brief Shrink the memory which used by application.
346      *
347      * @param level Indicates the memory trim level, which shows the current memory usage status.
348      *
349      */
350     void HandleShrinkMemory(const int level);
351 
352     /**
353      *
354      * @brief Notify the memory.
355      *
356      * @param level Indicates the memory trim level, which shows the current memory usage status.
357      *
358      */
359     void HandleMemoryLevel(int level);
360 
361     /**
362      *
363      * @brief send the new config to the application.
364      *
365      * @param config The updated config.
366      *
367      */
368     void HandleConfigurationUpdated(const Configuration &config);
369 
370     /**
371      *
372      * @brief remove the deathRecipient from appMgr.
373      *
374      */
375     void RemoveAppMgrDeathRecipient();
376 
377     /**
378      *
379      * @brief Attach the mainthread to the AppMgr.
380      *
381      */
382     void Attach();
383 
384     /**
385      *
386      * @brief Set the runner state of mainthread.
387      *
388      * @param runnerStart whether the runner is started.
389      */
390     void SetRunnerStarted(bool runnerStart);
391 
392     /**
393      *
394      * @brief Connect the mainthread to the AppMgr.
395      *
396      */
397     bool ConnectToAppMgr();
398 
399     /**
400      *
401      * @brief Check whether the appLaunchData is legal.
402      *
403      * @param appLaunchData The appLaunchData should be checked.
404      *
405      * @return if the appLaunchData is legal, return true. else return false.
406      */
407     bool CheckLaunchApplicationParam(const AppLaunchData &appLaunchData) const;
408 
409     /**
410      *
411      * @brief Check whether the record is legal.
412      *
413      * @param record The record should be checked.
414      *
415      * @return if the record is legal, return true. else return false.
416      */
417     bool CheckAbilityItem(const std::shared_ptr<AbilityLocalRecord> &record) const;
418 
419     /**
420      *
421      * @brief Init the mainthread.
422      *
423      * @param runner the runner belong to the mainthread.
424      *
425      */
426     void Init(const std::shared_ptr<EventRunner> &runner);
427 
428     /**
429      *
430      * @brief Task in event handler timeout detected.
431      *
432      * @param runner the runner belong to the mainthread.
433      *
434      */
435     void TaskTimeoutDetected(const std::shared_ptr<EventRunner>& runner);
436 
437     /**
438      *
439      * @brief Check whether the OHOSApplication is ready.
440      *
441      * @return if the OHOSApplication is ready, return true. else return false.
442      *
443      */
444     bool IsApplicationReady() const;
445 
446     /**
447      * @brief Load all extension so
448      *
449      * @param nativeEngine nativeEngine
450      * @param filePath extension so file path
451      */
452     void LoadAllExtensions(NativeEngine &nativeEngine, const std::string &filePath);
453 
454     /**
455      *
456      * @brief Ability Delegator Prepare.
457      *
458      * @param record User Test info.
459      *
460      */
461     bool PrepareAbilityDelegator(const std::shared_ptr<UserTestRecord> &record, bool isStageBased,
462         BundleInfo& bundleInfo);
463 
464     /**
465      * @brief Update current process extension type
466      *
467      * @param abilityRecord current running ability record
468      */
469     void UpdateProcessExtensionType(const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
470 
471     /**
472      * @brief Add Extension block item
473      *
474      * @param extensionName extension name
475      * @param type extension type
476      */
477     void AddExtensionBlockItem(const std::string &extensionName, int32_t type);
478 
479     /**
480      * @brief Update extension block list to nativeEngine
481      *
482      * @param nativeEngine nativeEngine instance
483      */
484     void UpdateEngineExtensionBlockList(NativeEngine &nativeEngine);
485 
486     static void HandleDumpHeap(bool isPrivate);
487     static void HandleSignal(int signal);
488 
489     bool Timer();
490     bool WaitForDuration(uint32_t duration);
491     void reportEvent();
492     bool IsStopWatchdog();
493 
494     class MainHandler : public EventHandler {
495     public:
496         MainHandler(const std::shared_ptr<EventRunner> &runner, const sptr<MainThread> &thread);
497         virtual ~MainHandler() = default;
498 
499         /**
500          *
501          * @brief Process the event.
502          *
503          * @param event the event want to be processed.
504          *
505          */
506         void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) override;
507 
508     private:
509         sptr<MainThread> mainThreadObj_ = nullptr;
510     };
511 
512     bool isRunnerStarted_ = false;
513     int newThreadId_ = -1;
514     std::shared_ptr<Profile> appProfile_ = nullptr;
515     std::shared_ptr<ApplicationInfo> applicationInfo_ = nullptr;
516     std::shared_ptr<ProcessInfo> processInfo_ = nullptr;
517     std::shared_ptr<OHOSApplication> application_ = nullptr;
518     std::shared_ptr<ApplicationImpl> applicationImpl_ = nullptr;
519     static std::shared_ptr<MainHandler> mainHandler_;
520     std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr;
521     std::shared_ptr<Watchdog> watchdog_ = nullptr;
522     std::unique_ptr<AbilityRuntime::ExtensionConfigMgr> extensionConfigMgr_ = nullptr;
523     MainThreadState mainThreadState_ = MainThreadState::INIT;
524     sptr<IAppMgr> appMgr_ = nullptr;  // appMgrService Handler
525     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
526     std::string aceApplicationName_ = "AceApplication";
527     std::string pathSeparator_ = "/";
528     std::string abilityLibraryType_ = ".so";
529     static std::shared_ptr<EventHandler> signalHandler_;
530     static std::shared_ptr<OHOSApplication> applicationForDump_;
531 
532 #ifdef ABILITY_LIBRARY_LOADER
533     /**
534      *
535      * @brief Load the ability library.
536      *
537      * @param libraryPaths the library paths.
538      *
539      */
540     void LoadAbilityLibrary(const std::vector<std::string> &libraryPaths);
541 
542     void LoadNativeLiabrary(std::string &nativeLibraryPath);
543 
544     void LoadAppDetailAbilityLibrary(std::string &nativeLibraryPath);
545 
546     void LoadAppLibrary();
547 
548     void ChangeToLocalPath(const std::string &bundleName,
549         const std::vector<std::string> &sourceDirs, std::vector<std::string> &localPath);
550 
551     bool ScanDir(const std::string &dirPath, std::vector<std::string> &files);
552 
553     /**
554      *
555      * @brief Check the fileType.
556      *
557      * @param fileName The fileName of the lib.
558      * @param extensionName The extensionName of the lib.
559      *
560      * @return if the FileType is legal, return true. else return false.
561      *
562      */
563     bool CheckFileType(const std::string &fileName, const std::string &extensionName);
564 
565     bool InitCreate(std::shared_ptr<ContextDeal> &contextDeal, ApplicationInfo &appInfo, ProcessInfo &processInfo,
566         Profile &appProfile);
567     bool CheckForHandleLaunchApplication(const AppLaunchData &appLaunchData);
568     bool InitResourceManager(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
569         BundleInfo& bundleInfo, const Configuration &config);
570 
571     bool GetHqfFileAndHapPath(const std::string &bundleName,
572         std::vector<std::pair<std::string, std::string>> &fileMap);
573 
574     std::vector<std::string> fileEntries_;
575     std::vector<std::string> nativeFileEntries_;
576     std::vector<void *> handleAbilityLib_;  // the handler of ACE Library.
577     std::shared_ptr<IdleTime> idleTime_ = nullptr;
578 #endif                                      // ABILITY_LIBRARY_LOADER
579 #ifdef APPLICATION_LIBRARY_LOADER
580     void *handleAppLib_ = nullptr;  // the handler of ACE Library.
581     constexpr static std::string applicationLibraryPath = "/hos/lib/libapplication_native.z.so";
582 #endif  // APPLICATION_LIBRARY_LOADER
583     DISALLOW_COPY_AND_MOVE(MainThread);
584 };
585 }  // namespace AppExecFwk
586 }  // namespace OHOS
587 #endif  // OHOS_ABILITY_RUNTIME_MAIN_THREAD_H
588