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 JS_PRINT_EXTENSION_CONNECTION_H
17 #define JS_PRINT_EXTENSION_CONNECTION_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 
22 #include "ability_connect_callback.h"
23 #include "event_handler.h"
24 #include "print_extension_context.h"
25 #include "napi/native_api.h"
26 
27 class NativeReference;
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
31 class JSPrintExtensionConnection : public AbilityConnectCallback {
32 public:
33     explicit JSPrintExtensionConnection(napi_env engine);
34     ~JSPrintExtensionConnection();
35     void OnAbilityConnectDone(
36         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
37     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
38     void HandleOnAbilityConnectDone(
39         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode);
40     void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode);
41     void SetJsConnectionObject(napi_value jsConnectionObject);
42     void CallJsFailed(int32_t errorCode);
43 private:
44     napi_env engine_;
45     std::unique_ptr<NativeReference> jsConnectionObject_ = nullptr;
46     std::shared_timed_mutex managersMutex_;
47 };
48 
49 struct ConnecttionKey {
50     AAFwk::Want want;
51     int64_t id;
52 };
53 
54 struct key_compare {
operatorkey_compare55     bool operator()(const ConnecttionKey &key1, const ConnecttionKey &key2) const
56     {
57         if (key1.id < key2.id) {
58             return true;
59         }
60         return false;
61     }
62 };
63 
64 static std::map<ConnecttionKey, sptr<JSPrintExtensionConnection>, key_compare> connects_;
65 static int64_t serialNumber_ = 0;
66 static std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
67 }  // namespace AbilityRuntime
68 }  // namespace OHOS
69 #endif  // JS_PRINT_EXTENSION_CONNECTION_H