• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <vector>
17 #include <new>
18 
19 #include "js_runtime_utils.h"
20 #include "native_engine/native_reference.h"
21 #include "display_manager.h"
22 #include "window_manager_hilog.h"
23 #include "singleton_container.h"
24 #include "js_display_listener.h"
25 #include "js_display.h"
26 #include "js_display_manager.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 using namespace AbilityRuntime;
31 constexpr size_t ARGC_ONE = 1;
32 constexpr size_t ARGC_TWO = 2;
33 constexpr int32_t INDEX_ONE = 1;
34 namespace {
35     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "JsDisplayManager"};
36 }
37 
38 class JsDisplayManager {
39 public:
JsDisplayManager(NativeEngine * engine)40 explicit JsDisplayManager(NativeEngine* engine) {
41 }
42 
43 ~JsDisplayManager() = default;
44 
Finalizer(NativeEngine * engine,void * data,void * hint)45 static void Finalizer(NativeEngine* engine, void* data, void* hint)
46 {
47     WLOGFI("JsDisplayManager::Finalizer is called");
48     std::unique_ptr<JsDisplayManager>(static_cast<JsDisplayManager*>(data));
49 }
50 
GetDefaultDisplay(NativeEngine * engine,NativeCallbackInfo * info)51 static NativeValue* GetDefaultDisplay(NativeEngine* engine, NativeCallbackInfo* info)
52 {
53     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
54     return (me != nullptr) ? me->OnGetDefaultDisplay(*engine, *info) : nullptr;
55 }
56 
GetDefaultDisplaySync(NativeEngine * engine,NativeCallbackInfo * info)57 static NativeValue* GetDefaultDisplaySync(NativeEngine* engine, NativeCallbackInfo* info)
58 {
59     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
60     return (me != nullptr) ? me->OnGetDefaultDisplaySync(*engine, *info) : nullptr;
61 }
62 
GetAllDisplay(NativeEngine * engine,NativeCallbackInfo * info)63 static NativeValue* GetAllDisplay(NativeEngine* engine, NativeCallbackInfo* info)
64 {
65     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
66     return (me != nullptr) ? me->OnGetAllDisplay(*engine, *info) : nullptr;
67 }
68 
GetAllDisplays(NativeEngine * engine,NativeCallbackInfo * info)69 static NativeValue* GetAllDisplays(NativeEngine* engine, NativeCallbackInfo* info)
70 {
71     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
72     return (me != nullptr) ? me->OnGetAllDisplays(*engine, *info) : nullptr;
73 }
74 
RegisterDisplayManagerCallback(NativeEngine * engine,NativeCallbackInfo * info)75 static NativeValue* RegisterDisplayManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
76 {
77     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
78     return (me != nullptr) ? me->OnRegisterDisplayManagerCallback(*engine, *info) : nullptr;
79 }
80 
UnregisterDisplayManagerCallback(NativeEngine * engine,NativeCallbackInfo * info)81 static NativeValue* UnregisterDisplayManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
82 {
83     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
84     return (me != nullptr) ? me->OnUnregisterDisplayManagerCallback(*engine, *info) : nullptr;
85 }
86 
HasPrivateWindow(NativeEngine * engine,NativeCallbackInfo * info)87 static NativeValue* HasPrivateWindow(NativeEngine* engine, NativeCallbackInfo* info)
88 {
89     JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
90     return (me != nullptr) ? me->OnHasPrivateWindow(*engine, *info) : nullptr;
91 }
92 
93 private:
94 std::map<std::string, std::map<std::unique_ptr<NativeReference>, sptr<JsDisplayListener>>> jsCbMap_;
95 std::mutex mtx_;
96 
OnGetDefaultDisplay(NativeEngine & engine,NativeCallbackInfo & info)97 NativeValue* OnGetDefaultDisplay(NativeEngine& engine, NativeCallbackInfo& info)
98 {
99     WLOGFI("JsDisplayManager::OnGetDefaultDisplay is called");
100     DMError errCode = DMError::DM_OK;
101     if (info.argc != 0 && info.argc != ARGC_ONE) {
102         WLOGFE("JsDisplayManager::OnGetDefaultDisplay params not match");
103         errCode = DMError::DM_ERROR_INVALID_PARAM;
104     }
105 
106     AsyncTask::CompleteCallback complete =
107         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
108             if (errCode != DMError::DM_OK) {
109                 task.Reject(engine, CreateJsError(engine,
110                     static_cast<int32_t>(errCode), "JsDisplayManager::OnGetDefaultDisplay failed."));
111             }
112             sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplay();
113             if (display != nullptr) {
114                 task.Resolve(engine, CreateJsDisplayObject(engine, display));
115                 WLOGFI("JsDisplayManager::OnGetDefaultDisplay success");
116             } else {
117                 task.Reject(engine, CreateJsError(engine,
118                     static_cast<int32_t>(DMError::DM_ERROR_NULLPTR), "JsDisplayManager::OnGetDefaultDisplay failed."));
119             }
120         };
121     NativeValue* lastParam = nullptr;
122     if (info.argc == ARGC_ONE && info.argv[0]->TypeOf() == NATIVE_FUNCTION) {
123         lastParam = info.argv[0];
124     }
125     NativeValue* result = nullptr;
126     AsyncTask::Schedule("JsDisplayManager::OnGetDefaultDisplay",
127         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
128     return result;
129 }
130 
OnGetDefaultDisplaySync(NativeEngine & engine,NativeCallbackInfo & info)131 NativeValue* OnGetDefaultDisplaySync(NativeEngine& engine, NativeCallbackInfo& info)
132 {
133     WLOGFI("JsDisplayManager::OnGetDefaultDisplaySync is called");
134     sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplaySync();
135     if (display == nullptr) {
136         WLOGFE("JsDisplayManager::OnGetDefaultDisplaySync, display is nullptr.");
137         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN)));
138         return engine.CreateUndefined();
139     }
140     return CreateJsDisplayObject(engine, display);
141 }
142 
OnGetAllDisplay(NativeEngine & engine,NativeCallbackInfo & info)143 NativeValue* OnGetAllDisplay(NativeEngine& engine, NativeCallbackInfo& info)
144 {
145     WLOGFI("JsDisplayManager::OnGetAllDisplay is called");
146     DMError errCode = DMError::DM_OK;
147     if (info.argc != 0 && info.argc != ARGC_ONE) {
148         WLOGFE("JsDisplayManager::OnGetAllDisplay params not match");
149         errCode = DMError::DM_ERROR_INVALID_PARAM;
150     }
151 
152     AsyncTask::CompleteCallback complete =
153         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
154             if (errCode != DMError::DM_OK) {
155                 task.Reject(engine, CreateJsError(engine,
156                     static_cast<int32_t>(errCode), "JsDisplayManager::OnGetAllDisplay failed."));
157             }
158             std::vector<sptr<Display>> displays = SingletonContainer::Get<DisplayManager>().GetAllDisplays();
159             if (!displays.empty()) {
160                 task.Resolve(engine, CreateJsDisplayArrayObject(engine, displays));
161                 WLOGFI("JsDisplayManager::GetAllDisplays success");
162             } else {
163                 task.Reject(engine, CreateJsError(engine,
164                     static_cast<int32_t>(DMError::DM_ERROR_NULLPTR), "JsDisplayManager::OnGetAllDisplay failed."));
165             }
166         };
167 
168     NativeValue* lastParam = nullptr;
169     if (info.argc == ARGC_ONE && info.argv[0]->TypeOf() == NATIVE_FUNCTION) {
170         lastParam = info.argv[0];
171     }
172     NativeValue* result = nullptr;
173     AsyncTask::Schedule("JsDisplayManager::OnGetAllDisplay",
174         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
175     return result;
176 }
177 
OnGetAllDisplays(NativeEngine & engine,NativeCallbackInfo & info)178 NativeValue* OnGetAllDisplays(NativeEngine& engine, NativeCallbackInfo& info)
179 {
180     WLOGFI("JsDisplayManager::OnGetAllDisplays is called");
181 
182     AsyncTask::CompleteCallback complete =
183         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
184             std::vector<sptr<Display>> displays = SingletonContainer::Get<DisplayManager>().GetAllDisplays();
185             if (!displays.empty()) {
186                 task.Resolve(engine, CreateJsDisplayArrayObject(engine, displays));
187                 WLOGFI("JsDisplayManager::GetAllDisplays success");
188             } else {
189                 task.Reject(engine, CreateJsError(engine,
190                     static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN),
191                     "JsDisplayManager::OnGetAllDisplays failed."));
192             }
193         };
194 
195     NativeValue* lastParam = nullptr;
196     if (info.argc >= ARGC_ONE && info.argv[ARGC_ONE - 1] != nullptr &&
197         info.argv[ARGC_ONE - 1]->TypeOf() == NATIVE_FUNCTION) {
198         lastParam = info.argv[0];
199     }
200     NativeValue* result = nullptr;
201     AsyncTask::Schedule("JsDisplayManager::OnGetAllDisplays",
202         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
203     return result;
204 }
205 
RegisterDisplayListenerWithType(NativeEngine & engine,const std::string & type,NativeValue * value)206 void RegisterDisplayListenerWithType(NativeEngine& engine, const std::string& type, NativeValue* value)
207 {
208     if (IfCallbackRegistered(type, value)) {
209         WLOGFE("JsDisplayManager::RegisterDisplayListenerWithType callback already registered!");
210         return;
211     }
212     std::unique_ptr<NativeReference> callbackRef;
213     callbackRef.reset(engine.CreateReference(value, 1));
214     sptr<JsDisplayListener> displayListener = new(std::nothrow) JsDisplayListener(&engine);
215     if (displayListener == nullptr) {
216         WLOGFE("displayListener is nullptr");
217         return;
218     }
219     if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
220         SingletonContainer::Get<DisplayManager>().RegisterDisplayListener(displayListener);
221         WLOGFI("JsDisplayManager::RegisterDisplayListenerWithType success");
222     } else {
223         WLOGFE("JsDisplayManager::RegisterDisplayListenerWithType failed method: %{public}s not support!",
224                type.c_str());
225         return;
226     }
227     displayListener->AddCallback(type, value);
228     jsCbMap_[type][std::move(callbackRef)] = displayListener;
229 }
230 
IfCallbackRegistered(const std::string & type,NativeValue * jsListenerObject)231 bool IfCallbackRegistered(const std::string& type, NativeValue* jsListenerObject)
232 {
233     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
234         WLOGFI("JsDisplayManager::IfCallbackRegistered methodName %{public}s not registered!", type.c_str());
235         return false;
236     }
237 
238     for (auto& iter : jsCbMap_[type]) {
239         if (jsListenerObject->StrictEquals(iter.first->Get())) {
240             WLOGFE("JsDisplayManager::IfCallbackRegistered callback already registered!");
241             return true;
242         }
243     }
244     return false;
245 }
246 
UnregisterAllDisplayListenerWithType(const std::string & type)247 void UnregisterAllDisplayListenerWithType(const std::string& type)
248 {
249     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
250         WLOGFI("JsDisplayManager::UnregisterAllDisplayListenerWithType methodName %{public}s not registered!",
251                type.c_str());
252         return;
253     }
254     for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
255         it->second->RemoveAllCallback();
256         if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
257             sptr<DisplayManager::IDisplayListener> thisListener(it->second);
258             SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
259             WLOGFI("JsDisplayManager::UnregisterAllDisplayListenerWithType success");
260         }
261         jsCbMap_[type].erase(it++);
262     }
263     jsCbMap_.erase(type);
264 }
265 
UnRegisterDisplayListenerWithType(const std::string & type,NativeValue * value)266 void UnRegisterDisplayListenerWithType(const std::string& type, NativeValue* value)
267 {
268     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
269         WLOGFI("JsDisplayManager::UnRegisterDisplayListenerWithType methodName %{public}s not registered!",
270                type.c_str());
271         return;
272     }
273     for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
274         if (value->StrictEquals(it->first->Get())) {
275             it->second->RemoveCallback(type, value);
276             if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
277                 sptr<DisplayManager::IDisplayListener> thisListener(it->second);
278                 SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
279                 WLOGFI("JsDisplayManager::UnRegisterDisplayListenerWithType success");
280             }
281             jsCbMap_[type].erase(it++);
282             break;
283         } else {
284             it++;
285         }
286     }
287     if (jsCbMap_[type].empty()) {
288         jsCbMap_.erase(type);
289     }
290 }
291 
OnRegisterDisplayManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)292 NativeValue* OnRegisterDisplayManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
293 {
294     WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback is called");
295     if (info.argc < ARGC_TWO) {
296         WLOGFE("JsDisplayManager Params not match: %{public}zu", info.argc);
297         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
298         return engine.CreateUndefined();
299     }
300     std::string cbType;
301     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
302         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
303         WLOGFE("Failed to convert parameter to callbackType");
304         return engine.CreateUndefined();
305     }
306     NativeValue* value = info.argv[INDEX_ONE];
307     if (value == nullptr) {
308         WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback info->argv[1] is nullptr");
309         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
310         return engine.CreateUndefined();
311     }
312     if (!value->IsCallable()) {
313         WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback info->argv[1] is not callable");
314         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
315         return engine.CreateUndefined();
316     }
317     std::lock_guard<std::mutex> lock(mtx_);
318     RegisterDisplayListenerWithType(engine, cbType, value);
319     return engine.CreateUndefined();
320 }
321 
OnUnregisterDisplayManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)322 NativeValue* OnUnregisterDisplayManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
323 {
324     WLOGFI("JsDisplayManager::OnUnregisterDisplayCallback is called");
325     if (info.argc < ARGC_ONE) {
326         WLOGFE("JsDisplayManager Params not match %{public}zu", info.argc);
327         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
328         return engine.CreateUndefined();
329     }
330     std::string cbType;
331     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
332         WLOGFE("Failed to convert parameter to callbackType");
333         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
334         return engine.CreateUndefined();
335     }
336     std::lock_guard<std::mutex> lock(mtx_);
337     if (info.argc == ARGC_ONE) {
338         UnregisterAllDisplayListenerWithType(cbType);
339     } else {
340         NativeValue* value = info.argv[INDEX_ONE];
341         if ((value == nullptr) || (!value->IsCallable())) {
342             UnregisterAllDisplayListenerWithType(cbType);
343         } else {
344             UnRegisterDisplayListenerWithType(cbType, value);
345         }
346     }
347     return engine.CreateUndefined();
348 }
349 
OnHasPrivateWindow(NativeEngine & engine,NativeCallbackInfo & info)350 NativeValue* OnHasPrivateWindow(NativeEngine& engine, NativeCallbackInfo& info)
351 {
352     bool hasPrivateWindow = false;
353     if (info.argc < ARGC_ONE) {
354         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
355         return engine.CreateUndefined();
356     }
357     int64_t displayId = static_cast<int64_t>(DISPLAY_ID_INVALID);
358     if (!ConvertFromJsValue(engine, info.argv[0], displayId)) {
359         WLOGFE("[NAPI]Failed to convert parameter to displayId");
360         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
361         return engine.CreateUndefined();
362     }
363     if (displayId < 0) {
364         engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
365         return engine.CreateUndefined();
366     }
367     DmErrorCode errCode = DM_JS_TO_ERROR_CODE_MAP.at(
368         SingletonContainer::Get<DisplayManager>().HasPrivateWindow(displayId, hasPrivateWindow));
369     WLOGFI("[NAPI]Display id = %{public}" PRIu64", hasPrivateWindow = %{public}u err = %{public}d",
370         static_cast<uint64_t>(displayId), hasPrivateWindow, errCode);
371     if (errCode != DmErrorCode::DM_OK) {
372         engine.Throw(CreateJsError(engine, static_cast<int32_t>(errCode)));
373         return engine.CreateUndefined();
374     }
375     return engine.CreateBoolean(hasPrivateWindow);
376 }
377 
CreateJsDisplayArrayObject(NativeEngine & engine,std::vector<sptr<Display>> & displays)378 NativeValue* CreateJsDisplayArrayObject(NativeEngine& engine, std::vector<sptr<Display>>& displays)
379 {
380     WLOGFI("JsDisplayManager::CreateJsDisplayArrayObject is called");
381     NativeValue* arrayValue = engine.CreateArray(displays.size());
382     NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
383     if (array == nullptr) {
384         WLOGFE("Failed to create display array");
385         return engine.CreateUndefined();
386     }
387     int32_t i = 0;
388     for (auto& display : displays) {
389         if (display == nullptr) {
390             continue;
391         }
392         array->SetElement(i++, CreateJsDisplayObject(engine, display));
393     }
394     return arrayValue;
395 }
396 };
397 
InitDisplayState(NativeEngine * engine)398 NativeValue* InitDisplayState(NativeEngine* engine)
399 {
400     WLOGFI("JsDisplayManager::InitDisplayState called");
401 
402     if (engine == nullptr) {
403         WLOGFE("engine is nullptr");
404         return nullptr;
405     }
406 
407     NativeValue *objValue = engine->CreateObject();
408     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
409     if (object == nullptr) {
410         WLOGFE("Failed to get object");
411         return nullptr;
412     }
413 
414     object->SetProperty("STATE_UNKNOWN", CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_UNKNOWN)));
415     object->SetProperty("STATE_OFF", CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_OFF)));
416     object->SetProperty("STATE_ON", CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_ON)));
417     object->SetProperty("STATE_DOZE",
418         CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_DOZE)));
419     object->SetProperty("STATE_DOZE_SUSPEND",
420         CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_DOZE_SUSPEND)));
421     object->SetProperty("STATE_VR",
422         CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_VR)));
423     object->SetProperty("STATE_ON_SUSPEND",
424         CreateJsValue(*engine, static_cast<int32_t>(DisplayStateMode::STATE_ON_SUSPEND)));
425     return objValue;
426 }
427 
InitDisplayErrorCode(NativeEngine * engine)428 NativeValue* InitDisplayErrorCode(NativeEngine* engine)
429 {
430     WLOGFI("JsDisplayManager::InitDisplayErrorCode called");
431 
432     if (engine == nullptr) {
433         WLOGFE("engine is nullptr");
434         return nullptr;
435     }
436 
437     NativeValue *objValue = engine->CreateObject();
438     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
439     if (object == nullptr) {
440         WLOGFE("Failed to get object");
441         return nullptr;
442     }
443 
444     object->SetProperty("DM_ERROR_NO_PERMISSION",
445         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_NO_PERMISSION)));
446     object->SetProperty("DM_ERROR_INVALID_PARAM",
447         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
448     object->SetProperty("DM_ERROR_DEVICE_NOT_SUPPORT",
449         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT)));
450     object->SetProperty("DM_ERROR_INVALID_SCREEN",
451         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN)));
452     object->SetProperty("DM_ERROR_INVALID_CALLING",
453         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_CALLING)));
454     object->SetProperty("DM_ERROR_SYSTEM_INNORMAL",
455         CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL)));
456 
457     return objValue;
458 }
459 
InitDisplayError(NativeEngine * engine)460 NativeValue* InitDisplayError(NativeEngine* engine)
461 {
462     WLOGFI("JsDisplayManager::InitDisplayError called");
463 
464     if (engine == nullptr) {
465         WLOGFE("engine is nullptr");
466         return nullptr;
467     }
468 
469     NativeValue *objValue = engine->CreateObject();
470     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
471     if (object == nullptr) {
472         WLOGFE("Failed to get object");
473         return nullptr;
474     }
475 
476     object->SetProperty("DM_ERROR_INIT_DMS_PROXY_LOCKED",
477         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED)));
478     object->SetProperty("DM_ERROR_IPC_FAILED",
479         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_IPC_FAILED)));
480     object->SetProperty("DM_ERROR_REMOTE_CREATE_FAILED",
481         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_REMOTE_CREATE_FAILED)));
482     object->SetProperty("DM_ERROR_NULLPTR",
483         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_NULLPTR)));
484     object->SetProperty("DM_ERROR_INVALID_PARAM",
485         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_PARAM)));
486     object->SetProperty("DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED",
487         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED)));
488     object->SetProperty("DM_ERROR_DEATH_RECIPIENT",
489         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_DEATH_RECIPIENT)));
490     object->SetProperty("DM_ERROR_INVALID_MODE_ID",
491         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_MODE_ID)));
492     object->SetProperty("DM_ERROR_WRITE_DATA_FAILED",
493         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_WRITE_DATA_FAILED)));
494     object->SetProperty("DM_ERROR_RENDER_SERVICE_FAILED",
495         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_RENDER_SERVICE_FAILED)));
496     object->SetProperty("DM_ERROR_UNREGISTER_AGENT_FAILED",
497         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_UNREGISTER_AGENT_FAILED)));
498     object->SetProperty("DM_ERROR_INVALID_CALLING",
499         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_CALLING)));
500     object->SetProperty("DM_ERROR_UNKNOWN",
501         CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_UNKNOWN)));
502 
503     return objValue;
504 }
505 
JsDisplayManagerInit(NativeEngine * engine,NativeValue * exportObj)506 NativeValue* JsDisplayManagerInit(NativeEngine* engine, NativeValue* exportObj)
507 {
508     WLOGFI("JsDisplayManagerInit is called");
509 
510     if (engine == nullptr || exportObj == nullptr) {
511         WLOGFE("JsDisplayManagerInit engine or exportObj is nullptr");
512         return nullptr;
513     }
514 
515     NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
516     if (object == nullptr) {
517         WLOGFE("JsDisplayManagerInit object is nullptr");
518         return nullptr;
519     }
520 
521     std::unique_ptr<JsDisplayManager> jsDisplayManager = std::make_unique<JsDisplayManager>(engine);
522     object->SetNativePointer(jsDisplayManager.release(), JsDisplayManager::Finalizer, nullptr);
523 
524     object->SetProperty("DisplayState", InitDisplayState(engine));
525     object->SetProperty("DmErrorCode", InitDisplayErrorCode(engine));
526     object->SetProperty("DMError", InitDisplayError(engine));
527 
528     const char *moduleName = "JsDisplayManager";
529     BindNativeFunction(*engine, *object, "getDefaultDisplay", moduleName, JsDisplayManager::GetDefaultDisplay);
530     BindNativeFunction(*engine, *object, "getDefaultDisplaySync", moduleName, JsDisplayManager::GetDefaultDisplaySync);
531     BindNativeFunction(*engine, *object, "getAllDisplay", moduleName, JsDisplayManager::GetAllDisplay);
532     BindNativeFunction(*engine, *object, "getAllDisplays", moduleName, JsDisplayManager::GetAllDisplays);
533     BindNativeFunction(*engine, *object, "hasPrivateWindow", moduleName, JsDisplayManager::HasPrivateWindow);
534     BindNativeFunction(*engine, *object, "on", moduleName, JsDisplayManager::RegisterDisplayManagerCallback);
535     BindNativeFunction(*engine, *object, "off", moduleName, JsDisplayManager::UnregisterDisplayManagerCallback);
536     return engine->CreateUndefined();
537 }
538 }  // namespace Rosen
539 }  // namespace OHOS