1 /*
2  * Copyright (c) 2023 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 <cstdint>
17 #include <unistd.h>
18 
19 #include "background_mode.h"
20 #include "background_task_adapter.h"
21 #include "background_task_mgr_helper.h"
22 #include "nweb_log.h"
23 
24 namespace OHOS::NWeb {
25 using namespace OHOS::BackgroundTaskMgr;
26 
ConvertBackgroundMode(BackgroundModeAdapter mode)27 BackgroundMode::Type ConvertBackgroundMode(BackgroundModeAdapter mode)
28 {
29     switch (mode) {
30         case BackgroundModeAdapter::DATA_TRANSFER:
31             return BackgroundMode::Type::DATA_TRANSFER;
32         case BackgroundModeAdapter::AUDIO_PLAYBACK:
33             return BackgroundMode::Type::AUDIO_PLAYBACK;
34         case BackgroundModeAdapter::AUDIO_RECORDING:
35             return BackgroundMode::Type::AUDIO_RECORDING;
36         case BackgroundModeAdapter::LOCATION:
37             return BackgroundMode::Type::LOCATION;
38         case BackgroundModeAdapter::BLUETOOTH_INTERACTION:
39             return BackgroundMode::Type::BLUETOOTH_INTERACTION;
40         case BackgroundModeAdapter::MULTI_DEVICE_CONNECTION:
41             return BackgroundMode::Type::MULTI_DEVICE_CONNECTION;
42         case BackgroundModeAdapter::WIFI_INTERACTION:
43             return BackgroundMode::Type::WIFI_INTERACTION;
44         case BackgroundModeAdapter::VOIP:
45             return BackgroundMode::Type::VOIP;
46         case BackgroundModeAdapter::TASK_KEEPING:
47             return BackgroundMode::Type::TASK_KEEPING;
48         default:
49             return BackgroundMode::Type::AUDIO_PLAYBACK;
50     }
51 }
52 
RequestBackgroundRunning(bool running,BackgroundModeAdapter bgMode)53 bool BackgroundTaskAdapter::RequestBackgroundRunning(bool running, BackgroundModeAdapter bgMode)
54 {
55     auto uid = getuid();
56     WVLOG_I("RequestBackgroundRunning uid: %{public}d, running: %{public}d", uid, running);
57     ContinuousTaskParamForInner taskParam { uid, ConvertBackgroundMode(bgMode), running };
58     ErrCode errCode = BackgroundTaskMgrHelper::RequestBackgroundRunningForInner(taskParam);
59     if (errCode != ERR_OK) {
60         WVLOG_I("RequestBackgroundRunning failed, error code: %{public}d", errCode);
61         return false;
62     }
63     return true;
64 }
65 } // namespace OHOS::NWeb
66