• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_DISTRIBUTED_EXTENSION_CONTEXT_JS_H
17 #define OHOS_DISTRIBUTED_EXTENSION_CONTEXT_JS_H
18 
19 #include "ability_connect_callback.h"
20 #include "distributed_extension_context.h"
21 #include "event_handler.h"
22 #include "js_runtime_utils.h"
23 #include "native_engine/native_engine.h"
24 #include "native_engine/native_value.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 napi_value CreateDistributedExtensionContextJS(napi_env env, std::shared_ptr<DistributedExtensionContext> ct);
29 
30 class DistributedExtensionContextJSConnection : public AbilityConnectCallback {
31 public:
32     explicit DistributedExtensionContextJSConnection(napi_env env);
33     ~DistributedExtensionContextJSConnection();
34     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
35         int32_t resultCode) override;
36     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
37     // this function need to execute in main thread.
38     void CallJsFailed(int32_t errorCode);
39     void SetJsConnectionObject(napi_value jsConnectionObject);
40 
41 private:
42     void HandleOnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
43         int32_t resultCode);
44     void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode);
45     void ReleaseConnection();
46 
47 private:
48     napi_env env_;
49     napi_ref jsConnectionObject_ = nullptr;
50     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
51 };
52 
53 struct ConnectionKey {
54     AAFwk::Want want;
55     int64_t id;
56 };
57 
58 struct key_compare {
operatorkey_compare59     bool operator()(const ConnectionKey &key1, const ConnectionKey &key2) const
60     {
61         return key1.id < key2.id;
62     }
63 };
64 
65 static std::map<ConnectionKey, sptr<DistributedExtensionContextJSConnection>, key_compare> connects_;
66 static int64_t serialNumber_ = 0;
67 static std::mutex g_connectMapMtx;
68 }
69 }
70 
71 #endif // OHOS_DISTRIBUTED_EXTENSION_CONTEXT_JS_H
72