1 /* 2 * Copyright (c) 2021-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 #include "ui_controller.h" 16 17 namespace OHOS::uitest { 18 using namespace std; 19 mutex UiController::controllerAccessMutex_; 20 list<unique_ptr<UiController>> UiController::controllers_; 21 UiControllerProvider UiController::controllerProvider_; 22 set<string> UiController::controllerInstalledDevices_; 23 UiController(string_view name,string_view device)24 UiController::UiController(string_view name, string_view device) : name_(name), targetDevice_(device) {} 25 RegisterControllerProvider(UiControllerProvider func)26 void UiController::RegisterControllerProvider(UiControllerProvider func) 27 { 28 controllerProvider_ = move(func); 29 } 30 RegisterController(unique_ptr<UiController> controller,Priority priority)31 void UiController::RegisterController(unique_ptr<UiController> controller, Priority priority) 32 { 33 if (controller == nullptr) { return; } 34 LOG_I("Add controller '%{public}s', priority=%{public}d", controller->GetName().c_str(), priority); 35 controller->SetPriority(priority); 36 lock_guard<mutex> guard(controllerAccessMutex_); 37 controllers_.push_back(move(controller)); 38 // sort controllers by priority descending 39 controllers_.sort(UiController::Comparator); 40 } 41 InstallForDevice(string_view device)42 void UiController::InstallForDevice(string_view device) 43 { 44 string name(device); 45 if (!device.empty() && controllerInstalledDevices_.find(name) == controllerInstalledDevices_.end()) { 46 // install controller on-demand 47 if (controllerProvider_ != nullptr) { 48 LOG_I("Begin to install UiControllers for device '%{public}s'", name.c_str()); 49 auto receiver = list<unique_ptr<UiController>>(); 50 controllerProvider_(device, receiver); 51 for (auto &uPtr:receiver) { 52 const auto priority = uPtr->priority_; 53 RegisterController(move(uPtr), priority); 54 } 55 controllerInstalledDevices_.insert(move(name)); 56 } 57 } 58 } 59 GetController(string_view targetDevice)60 const UiController *UiController::GetController(string_view targetDevice) 61 { 62 lock_guard<mutex> guard(controllerAccessMutex_); 63 for (auto &ctrl:controllers_) { 64 if (ctrl->targetDevice_ == targetDevice && ctrl->IsWorkable()) { 65 return ctrl.get(); 66 } 67 } 68 return nullptr; 69 } 70 Comparator(const unique_ptr<UiController> & c1,const unique_ptr<UiController> & c2)71 bool UiController::Comparator(const unique_ptr<UiController> &c1, const unique_ptr<UiController> &c2) 72 { 73 return c1->priority_ > c2->priority_; 74 } 75 RemoveController(string_view name)76 void UiController::RemoveController(string_view name) 77 { 78 lock_guard<mutex> guard(controllerAccessMutex_); 79 auto iter = controllers_.begin(); 80 while (iter != controllers_.end()) { 81 if ((*iter)->name_ == name) { 82 iter = controllers_.erase(iter); 83 } else { 84 iter++; 85 } 86 } 87 } 88 RemoveAllControllers()89 void UiController::RemoveAllControllers() 90 { 91 lock_guard<mutex> guard(controllerAccessMutex_); 92 controllers_.clear(); 93 } 94 } 95