• 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 #ifndef POWERMGR_BRIGHTNESS_H
17 #define POWERMGR_BRIGHTNESS_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <functional>
22 #include <iosfwd>
23 #include <map>
24 #include <memory>
25 #include <string>
26 
27 #include "display_mgr_errors.h"
28 #include "errors.h"
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31 #include "running_lock.h"
32 
33 namespace OHOS {
34 namespace DisplayPowerMgr {
35 class Brightness {
36 public:
37     explicit Brightness(napi_env env, std::shared_ptr<PowerMgr::RunningLock> runningLock = nullptr);
38     ~Brightness();
39     void GetValue();
40     void SetValue(napi_callback_info& info);
41     void SystemSetValue();
42     void GetMode();
43     void SetMode();
44     void SetKeepScreenOn();
45     napi_value GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType);
46     bool CreateCallbackRef(napi_value& options);
47     void CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType);
48 
49     napi_async_work asyncWork = nullptr;
50 
51     static constexpr const char* BRIGHTNESS_VALUE = "value";
52     static constexpr const char* BRIGHTNESS_MODE = "mode";
53     static constexpr const char* KEEP_SCREENON = "keepScreenOn";
54 
55 private:
56     class Result {
57     public:
58         void Error(int32_t code, const std::string& msg = "");
59         void GetError(napi_env env, napi_value* error, size_t& size) const;
60         napi_value GetError(napi_env& env);
61         napi_value ThrowError(napi_env& env, DisplayErrors code = DisplayErrors::ERR_OK);
62         napi_value GetResult(napi_env env);
SetResult(const std::string & key,int32_t value)63         inline void SetResult(const std::string& key, int32_t value)
64         {
65             mapResult_.emplace(key, value);
66         }
IsError()67         inline bool IsError() const
68         {
69             return code_ != ERR_OK;
70         }
71 
72     private:
73         int32_t code_ {ERR_OK};
74         std::string msg_;
75         std::map<std::string, int32_t> mapResult_;
76         static std::map<DisplayErrors, std::string> errorTable_;
77     };
78 
79     class BrightnessInfo {
80     public:
81         uint32_t GetBrightness() const;
82         bool SetBrightness(int32_t value);
83         int32_t GetAutoMode() const;
84         bool SetAutoMode(bool mode);
85         void ScreenOn(bool keep, std::shared_ptr<PowerMgr::RunningLock>& runningLock);
86         DisplayErrors GetServiceError() const;
87     };
88 
89     void ExecuteCallback();
90     bool CheckValueType(napi_value& value, napi_valuetype checkType);
91     napi_value GetOptions(napi_value& options, const std::string& name, napi_valuetype checkType);
92     void CallFunction(napi_ref& callbackRef, size_t argc, napi_value* response);
93     void ReleaseReference(napi_ref& ref);
94 
95     napi_env env_;
96     Result result_;
97     BrightnessInfo brightnessInfo_;
98 
99     napi_ref successRef_ = nullptr;
100     napi_ref failRef_ = nullptr;
101     napi_ref completeRef_ = nullptr;
102     napi_ref napiValRef_ = nullptr;
103     std::shared_ptr<PowerMgr::RunningLock> runningLock_ = nullptr;
104 };
105 } // namespace DisplayPowerMgr
106 } // namespace OHOS
107 
108 #endif // POWERMGR_BRIGHTNESS_H
109