• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_JS_MEDIA_CONTROL_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_JS_MEDIA_CONTROL_EXTENSION_H
18 
19 #include <unordered_set>
20 
21 #include "configuration.h"
22 #include "media_control_extension.h"
23 
24 class NativeReference;
25 class NativeValue;
26 class NativeObject;
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 class MediaControlExtension;
31 class JsRuntime;
32 /**
33  * @brief Basic ui extension components.
34  */
35 class JsMediaControlExtension : public MediaControlExtension,
36   public std::enable_shared_from_this<JsMediaControlExtension> {
37 public:
38     explicit JsMediaControlExtension(JsRuntime& jsRuntime);
39     virtual ~JsMediaControlExtension() override;
40 
41     /**
42      * @brief Create JsMediaControlExtension.
43      *
44      * @param runtime The runtime.
45      * @return The JsMediaControlExtension instance.
46      */
47     static JsMediaControlExtension* Create(const std::unique_ptr<Runtime>& runtime);
48 
49     /**
50      * @brief Init the ui extension.
51      *
52      * @param record the ui extension record.
53      * @param application the application info.
54      * @param handler the ui extension handler.
55      * @param token the remote token.
56      */
57     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
58         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
59         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
60         const sptr<IRemoteObject> &token) override;
61 
62     /**
63      * @brief Called when this ui extension is started. You must override this function if you want to perform some
64      *        initialization operations during ui extension startup.
65      *
66      * This function can be called only once in the entire lifecycle of an ui extension.
67      *
68      * @param Want Indicates the {@link Want} structure containing startup information about the ui extension.
69      */
70     virtual void OnStart(const AAFwk::Want &want) override;
71 
72     /**
73      * @brief Called when this ui extension is connected for the first time.
74      *
75      * You can override this function to implement your own processing logic.
76      *
77      * @param want Indicates the {@link Want} structure containing connection information about the ui extension.
78      * @return Returns a pointer to the <b>sid</b> of the connected ui extension.
79      */
80     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
81 
82     /**
83      * @brief Called when all abilities connected to this ui extension are disconnected.
84      *
85      * You can override this function to implement your own processing logic.
86      *
87      */
88     virtual void OnDisconnect(const AAFwk::Want &want) override;
89 
90     /**
91      * @brief Called back when ui extension is started.
92      *
93      * This method can be called only by ui extension. You can use the StartAbility(Want) method to start
94      * ui extension. Then the system calls back the current method to use the transferred want parameter to
95      * execute its own logic.
96      *
97      * @param want Indicates the want of ui extension to start.
98      * @param restart Indicates the startup mode. The value true indicates that ui extension is restarted after being
99      * destroyed, and the value false indicates a normal startup.
100      * @param startId Indicates the number of times the ui extension has been started. The startId is incremented
101      * by 1 every time the ui extension is started. For example, if the ui extension has been started for six times, the
102      * value of startId is 6.
103      */
104     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
105 
106     virtual void OnCommandWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
107         AAFwk::WindowCommand winCmd) override;
108 
109     /**
110      * @brief Called when this ui extension enters the <b>STATE_STOP</b> state.
111      *
112      * The ui extension in the <b>STATE_STOP</b> is being destroyed.
113      * You can override this function to implement your own processing logic.
114      */
115     virtual void OnStop() override;
116 
117     /**
118      * @brief Called when the system configuration is updated.
119      *
120      * @param configuration Indicates the updated configuration information.
121      */
122     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
123 
124     /**
125      * @brief Called when this extension enters the <b>STATE_FOREGROUND</b> state.
126      *
127      *
128      * The extension in the <b>STATE_FOREGROUND</b> state is visible.
129      * You can override this function to implement your own processing logic.
130      */
131     virtual void OnForeground(const Want &want) override;
132 
133     /**
134      * @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.
135      *
136      *
137      * The extension in the <b>STATE_BACKGROUND</b> state is invisible.
138      * You can override this function to implement your own processing logic.
139      */
140     virtual void OnBackground() override;
141 
142     /**
143      * @brief Called when ui extension need dump info.
144      *
145      * @param params The params from ui extension.
146      * @param info The dump info to show.
147      */
148     virtual void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
149 
150 private:
151     virtual void BindContext(NativeEngine& engine, NativeObject* obj);
152 
153     NativeValue* CallObjectMethod(const char* name, NativeValue * const *argv = nullptr,
154       size_t argc = 0);
155 
156     NativeValue* CallOnConnect(const AAFwk::Want &want);
157 
158     NativeValue* CallOnDisconnect(const AAFwk::Want &want, bool withResult = false);
159 
160     void ForegroundWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo);
161     void BackgroundWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
162     void DestroyWindow(const sptr<AAFwk::SessionInfo> &sessionInfo);
163 
164     JsRuntime& jsRuntime_;
165     std::unique_ptr<NativeReference> jsObj_;
166     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
167     std::unordered_map<int32_t, sptr<Rosen::Window>> uiWindowMap_;
168     std::unordered_set<uint64_t> foregroundWindows_;
169 };
170 }  // namespace AbilityRuntime
171 }  // namespace OHOS
172 #endif  // OHOS_ABILITY_RUNTIME_JS_MEDIA_CONTROL_EXTENSION_H
173