• 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 FOUNDATION_ABILITYRUNTIME_OHOS_JS_SERVICE_EXTENSION_H
17 #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_SERVICE_EXTENSION_H
18 
19 #include "configuration.h"
20 #include "service_extension.h"
21 
22 class NativeReference;
23 class NativeValue;
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class ServiceExtension;
28 class JsRuntime;
29 /**
30  * @brief Basic service components.
31  */
32 class JsServiceExtension : public ServiceExtension,
33                            public std::enable_shared_from_this<JsServiceExtension> {
34 public:
35     JsServiceExtension(JsRuntime& jsRuntime);
36     virtual ~JsServiceExtension() override;
37 
38     /**
39      * @brief Create JsServiceExtension.
40      *
41      * @param runtime The runtime.
42      * @return The JsServiceExtension instance.
43      */
44     static JsServiceExtension* Create(const std::unique_ptr<Runtime>& runtime);
45 
46     /**
47      * @brief Init the extension.
48      *
49      * @param record the extension record.
50      * @param application the application info.
51      * @param handler the extension handler.
52      * @param token the remote token.
53      */
54     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
55         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
56         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
57         const sptr<IRemoteObject> &token) override;
58 
59     /**
60      * @brief Called when this extension is started. You must override this function if you want to perform some
61      *        initialization operations during extension startup.
62      *
63      * This function can be called only once in the entire lifecycle of an extension.
64      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
65      */
66     virtual void OnStart(const AAFwk::Want &want) override;
67 
68     /**
69      * @brief Called when this Service extension is connected for the first time.
70      *
71      * You can override this function to implement your own processing logic.
72      *
73      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
74      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
75      */
76     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
77 
78     /**
79      * @brief Called when all abilities connected to this Service extension are disconnected.
80      *
81      * You can override this function to implement your own processing logic.
82      *
83      */
84     virtual void OnDisconnect(const AAFwk::Want &want) override;
85 
86     /**
87      * @brief Called back when Service is started.
88      * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start
89      * Service. Then the system calls back the current method to use the transferred want parameter to execute its own
90      * logic.
91      *
92      * @param want Indicates the want of Service to start.
93      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
94      * destroyed, and the value false indicates a normal startup.
95      * @param startId Indicates the number of times the Service extension has been started. The startId is incremented
96      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
97      * value of startId is 6.
98      */
99     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
100 
101     /**
102      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
103      *
104      * The extension in the <b>STATE_STOP</b> is being destroyed.
105      * You can override this function to implement your own processing logic.
106      */
107     virtual void OnStop() override;
108 
109     /**
110      * @brief Called when the system configuration is updated.
111      *
112      * @param configuration Indicates the updated configuration information.
113      */
114     void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
115 
116 private:
117     NativeValue* CallObjectMethod(const char* name, NativeValue* const* argv = nullptr, size_t argc = 0);
118 
119     void GetSrcPath(std::string &srcPath);
120 
121     JsRuntime& jsRuntime_;
122     std::unique_ptr<NativeReference> jsObj_;
123     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
124 };
125 }  // namespace AbilityRuntime
126 }  // namespace OHOS
127 #endif  // FOUNDATION_ABILITYRUNTIME_OHOS_JS_SERVICE_EXTENSION_H