• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #define MLOG_TAG "Media_Background"
16 
17 #include "media_background_task_factory.h"
18 
19 #include "media_cloud_sync_backgroud_task.h"
20 #include "medialibrary_subscriber.h"
21 #include "media_log.h"
22 
23 namespace OHOS::Media::Background {
MediaBackgroundTaskFactory()24 MediaBackgroundTaskFactory::MediaBackgroundTaskFactory()
25 {
26     this->tasks_ = {
27         std::make_shared<MediaCloudSyncBackgroundTask>(),
28     };
29 }
30 
31 // Check if the task can be executed.
Accept()32 bool MediaBackgroundTaskFactory::Accept()
33 {
34     return MedialibrarySubscriber::IsCurrentStatusOn();
35 }
36 
Execute()37 void MediaBackgroundTaskFactory::Execute()
38 {
39     // Only one task can be executed at the same time.
40     std::unique_lock<std::mutex> taskLock(mutex_, std::defer_lock);
41     CHECK_AND_RETURN_WARN_LOG(taskLock.try_lock(), "task is running");
42     // Execute all tasks.
43     for (auto &task : this->tasks_) {
44         CHECK_AND_CONTINUE_ERR_LOG(task != nullptr, "task is null");
45         CHECK_AND_RETURN_INFO_LOG(this->Accept(), "check accept failed");
46         task->Execute();
47     }
48     return;
49 }
50 }  // namespace OHOS::Media::Background