1 /*
2  * Copyright (c) 2021 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_JS_WALLPAPER_EXTENSION_CONTEXT_H
17 #define ABILITY_RUNTIME_JS_WALLPAPER_EXTENSION_CONTEXT_H
18 
19 #include <node_api.h>
20 
21 #include <memory>
22 
23 #include "ability_connect_callback.h"
24 #include "event_handler.h"
25 #include "napi/native_api.h"
26 #include "wallpaper_extension_context.h"
27 
28 class NativeReference;
29 
30 namespace OHOS {
31 namespace AbilityRuntime {
32 napi_value CreateJsWallpaperExtensionContext(napi_env env, std::shared_ptr<WallpaperExtensionContext> context);
33 
34 class JSWallpaperExtensionConnection : public AbilityConnectCallback {
35 public:
36     explicit JSWallpaperExtensionConnection(napi_env env);
37     ~JSWallpaperExtensionConnection();
38     void OnAbilityConnectDone(
39         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode) override;
40     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
41     void HandleOnAbilityConnectDone(
42         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode);
43     void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode);
44     void SetJsConnectionObject(napi_value jsConnectionObject);
45     void CallJsFailed(int32_t errorCode);
46 
47 private:
48     napi_env env_;
49     std::unique_ptr<NativeReference> jsConnectionObject_ = nullptr;
50 };
51 
52 struct ConnecttionKey {
53     AAFwk::Want want;
54     int64_t id;
55 };
56 
57 struct key_compare {
operatorkey_compare58     bool operator()(const ConnecttionKey &key1, const ConnecttionKey &key2) const
59     {
60         if (key1.id < key2.id) {
61             return true;
62         }
63         return false;
64     }
65 };
66 
67 } // namespace AbilityRuntime
68 } // namespace OHOS
69 #endif // ABILITY_RUNTIME_JS_WALLPAPER_EXTENSION_CONTEXT_H
70