• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H
17 #define NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H
18 
19 #include <set>
20 #include <mutex>
21 
22 #include "napi/native_api.h"
23 
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 namespace {
28     constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64;
29 }
30 
31 struct EventListener {
32     char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 };
33     bool isOnce = false;
34     napi_ref handlerRef = nullptr;
35     EventListener* back = nullptr;
36     EventListener* next = nullptr;
37 };
38 
39 class Event {
40 public:
41     virtual napi_value ToJsObject(napi_env env) = 0;
42 };
43 
44 class EventAgent {
45 public:
46     EventAgent(napi_env env, napi_value thisVar);
47     virtual ~EventAgent();
48 
49     void On(const char* type, napi_value handler);
50     void Off(const char* type, napi_value handler);
51     void Off(const char* type);
52     void Emit(const char* type, Event* event);
53     void InsertDevice(const std::string&);
54     void RemoveDevice(const std::string&);
55     bool FindDevice(const std::string&);
56     void ClearDevice();
57 
58 protected:
59     std::mutex deviceListMut_;
60     std::set<std::string> deviceList_;
61     napi_env env_;
62     napi_ref thisVarRef_;
63     EventListener* first_;
64     EventListener* last_;
65 };
66 } // namespace DistributedFile
67 } // namespace Storage
68 } // namespace OHOS
69 #endif /* NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H */