1 /*
2 * Copyright (C) 2024 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 "request_manager_impl.h"
17
18 #include <atomic>
19 #include <cstdint>
20 #include <memory>
21
22 #include "data_ability_predicates.h"
23 #include "errors.h"
24 #include "log.h"
25 #include "rdb_errno.h"
26 #include "rdb_helper.h"
27 #include "rdb_open_callback.h"
28 #include "rdb_predicates.h"
29 #include "rdb_store.h"
30 #include "request_manager.h"
31 #include "request_running_task_count.h"
32 #include "request_sync_load_callback.h"
33 #include "response_message_receiver.h"
34 #include "result_set.h"
35 #include "runcount_notify_stub.h"
36 #include "system_ability_definition.h"
37
38 namespace OHOS::Request {
39 constexpr const int32_t RETRY_INTERVAL = 500 * 1000;
40 constexpr const int32_t RETRY_MAX_TIMES = 5;
41
GetInstance()42 const std::unique_ptr<RequestManagerImpl> &RequestManagerImpl::GetInstance()
43 {
44 static std::unique_ptr<RequestManagerImpl> instance(new RequestManagerImpl());
45 return instance;
46 }
47
Create(const Config & config,int32_t seq,std::string & tid)48 int32_t RequestManagerImpl::Create(const Config &config, int32_t seq, std::string &tid)
49 {
50 auto proxy = GetRequestServiceProxy();
51 if (proxy == nullptr) {
52 REQUEST_HILOGE("GetRequestServiceProxy fail.");
53 return E_SERVICE_ERROR;
54 }
55 REQUEST_HILOGD("Request create, seq: %{public}d", seq);
56 this->EnsureChannelOpen();
57 int32_t ret = proxy->Create(config, tid);
58 if (ret == E_UNLOADING_SA) {
59 REQUEST_HILOGE("End Request create, seq: %{public}d, failed: Service ability is quitting", seq);
60 ret = Retry(tid, config, ret);
61 if (ret != E_OK) {
62 REQUEST_HILOGE("End Request create, seq: %{public}d, failed: %{public}d", seq, ret);
63 return ret;
64 }
65 }
66 if (ret == E_CHANNEL_NOT_OPEN) {
67 this->ReopenChannel();
68 ret = proxy->Subscribe(tid);
69 }
70 if (ret == E_OK && config.version != Version::API10) {
71 ret = proxy->Start(tid);
72 }
73 if (ret != E_OK) {
74 REQUEST_HILOGE("Request create, seq: %{public}d, failed: %{public}d", seq, ret);
75 } else {
76 REQUEST_HILOGD("End Request create ok, seq: %{public}d, ret: %{public}d", seq, ret);
77 }
78
79 return ret;
80 }
81
Retry(std::string & taskId,const Config & config,int32_t errorCode)82 int32_t RequestManagerImpl::Retry(std::string &taskId, const Config &config, int32_t errorCode)
83 {
84 REQUEST_HILOGD("Retry in");
85 int32_t interval = 1;
86 while (errorCode == E_UNLOADING_SA && interval <= RETRY_MAX_TIMES) {
87 if (config.action == Action::DOWNLOAD) {
88 for (auto file : config.files) {
89 std::remove(file.uri.c_str());
90 }
91 }
92
93 if (errorCode == E_UNLOADING_SA) {
94 // Waitting for system ability quit
95 usleep(RETRY_INTERVAL);
96 }
97 SetRequestServiceProxy(nullptr);
98 LoadRequestServer();
99 auto proxy = GetRequestServiceProxy();
100 if (proxy == nullptr) {
101 REQUEST_HILOGE("proxy is nullptr!");
102 continue;
103 }
104 errorCode = proxy->Create(config, taskId);
105 ++interval;
106 }
107 if (errorCode != E_OK && config.action == Action::DOWNLOAD) {
108 for (auto file : config.files) {
109 std::remove(file.uri.c_str());
110 }
111 }
112 return errorCode;
113 }
114
SetRequestServiceProxy(sptr<RequestServiceInterface> proxy)115 void RequestManagerImpl::SetRequestServiceProxy(sptr<RequestServiceInterface> proxy)
116 {
117 std::lock_guard<std::mutex> lock(serviceProxyMutex_);
118 requestServiceProxy_ = proxy;
119 }
120
GetTask(const std::string & tid,const std::string & token,Config & config)121 int32_t RequestManagerImpl::GetTask(const std::string &tid, const std::string &token, Config &config)
122 {
123 REQUEST_HILOGD("GetTask in");
124 auto proxy = GetRequestServiceProxy();
125 if (proxy == nullptr) {
126 REQUEST_HILOGE("GetRequestServiceProxy fail, tid: %{public}s", tid.c_str());
127 return E_SERVICE_ERROR;
128 }
129 REQUEST_HILOGD("Request getTask, tid: %{public}s", tid.c_str());
130 this->EnsureChannelOpen();
131 int32_t ret = proxy->GetTask(tid, token, config);
132 if (ret == E_UNLOADING_SA) {
133 REQUEST_HILOGE("End Request getTask, tid: %{public}s, failed: %{public}d", tid.c_str(), ret);
134 return ret;
135 }
136 if (ret == E_CHANNEL_NOT_OPEN) {
137 this->ReopenChannel();
138 ret = proxy->Subscribe(tid);
139 }
140
141 if (ret != E_OK) {
142 REQUEST_HILOGE("Request getTask, tid: %{public}s, failed: %{public}d", tid.c_str(), ret);
143 } else {
144 REQUEST_HILOGD("End Request getTask ok, tid: %{public}s, ret: %{public}d", tid.c_str(), ret);
145 }
146
147 return ret;
148 }
149
Start(const std::string & tid)150 int32_t RequestManagerImpl::Start(const std::string &tid)
151 {
152 REQUEST_HILOGD("Start in");
153 auto proxy = GetRequestServiceProxy();
154 if (proxy == nullptr) {
155 if (!RequestManager::GetInstance()->LoadRequestServer()) {
156 return E_SERVICE_ERROR;
157 }
158 proxy = GetRequestServiceProxy();
159 }
160
161 if (proxy == nullptr) {
162 return E_SERVICE_ERROR;
163 }
164
165 return proxy->Start(tid);
166 }
167
Stop(const std::string & tid)168 int32_t RequestManagerImpl::Stop(const std::string &tid)
169 {
170 REQUEST_HILOGD("Stop in");
171 auto proxy = GetRequestServiceProxy();
172 if (proxy == nullptr) {
173 return E_SERVICE_ERROR;
174 }
175
176 return proxy->Stop(tid);
177 }
178
Query(const std::string & tid,TaskInfo & info)179 int32_t RequestManagerImpl::Query(const std::string &tid, TaskInfo &info)
180 {
181 REQUEST_HILOGD("Query in");
182 auto proxy = GetRequestServiceProxy();
183 if (proxy == nullptr) {
184 return E_SERVICE_ERROR;
185 }
186
187 return proxy->Query(tid, info);
188 }
189
Touch(const std::string & tid,const std::string & token,TaskInfo & info)190 int32_t RequestManagerImpl::Touch(const std::string &tid, const std::string &token, TaskInfo &info)
191 {
192 REQUEST_HILOGD("Touch in");
193 auto proxy = GetRequestServiceProxy();
194 if (proxy == nullptr) {
195 return E_SERVICE_ERROR;
196 }
197
198 return proxy->Touch(tid, token, info);
199 }
200
Search(const Filter & filter,std::vector<std::string> & tids)201 int32_t RequestManagerImpl::Search(const Filter &filter, std::vector<std::string> &tids)
202 {
203 REQUEST_HILOGD("Search in");
204 auto proxy = GetRequestServiceProxy();
205 if (proxy == nullptr) {
206 return E_SERVICE_ERROR;
207 }
208
209 return proxy->Search(filter, tids);
210 }
211
Show(const std::string & tid,TaskInfo & info)212 int32_t RequestManagerImpl::Show(const std::string &tid, TaskInfo &info)
213 {
214 REQUEST_HILOGD("Show in");
215 auto proxy = GetRequestServiceProxy();
216 if (proxy == nullptr) {
217 return E_SERVICE_ERROR;
218 }
219
220 return proxy->Show(tid, info);
221 }
222
Pause(const std::string & tid,Version version)223 int32_t RequestManagerImpl::Pause(const std::string &tid, Version version)
224 {
225 REQUEST_HILOGD("Pause in");
226 auto proxy = GetRequestServiceProxy();
227 if (proxy == nullptr) {
228 return E_SERVICE_ERROR;
229 }
230
231 return proxy->Pause(tid, version);
232 }
233
QueryMimeType(const std::string & tid,std::string & mimeType)234 int32_t RequestManagerImpl::QueryMimeType(const std::string &tid, std::string &mimeType)
235 {
236 REQUEST_HILOGD("QueryMimeType in");
237 auto proxy = GetRequestServiceProxy();
238 if (proxy == nullptr) {
239 return E_SERVICE_ERROR;
240 }
241
242 return proxy->QueryMimeType(tid, mimeType);
243 }
244
Remove(const std::string & tid,Version version)245 int32_t RequestManagerImpl::Remove(const std::string &tid, Version version)
246 {
247 REQUEST_HILOGD("Remove in");
248 auto proxy = GetRequestServiceProxy();
249 if (proxy == nullptr) {
250 return E_SERVICE_ERROR;
251 }
252
253 return proxy->Remove(tid, version);
254 }
255
Resume(const std::string & tid)256 int32_t RequestManagerImpl::Resume(const std::string &tid)
257 {
258 REQUEST_HILOGD("Resume in");
259 auto proxy = GetRequestServiceProxy();
260 if (proxy == nullptr) {
261 return E_SERVICE_ERROR;
262 }
263
264 return proxy->Resume(tid);
265 }
266
AddListener(const std::string & taskId,const SubscribeType & type,const std::shared_ptr<IResponseListener> & listener)267 int32_t RequestManagerImpl::AddListener(
268 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener)
269 {
270 REQUEST_HILOGD("AddListener in, tid:%{public}s, type: %{public}d", taskId.c_str(), type);
271 std::shared_ptr<Request> task = this->GetTask(taskId);
272 if (task.get()) {
273 task->AddListener(type, listener);
274 return E_OK;
275 } else {
276 return E_OTHER;
277 }
278 }
279
RemoveListener(const std::string & taskId,const SubscribeType & type,const std::shared_ptr<IResponseListener> & listener)280 int32_t RequestManagerImpl::RemoveListener(
281 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener)
282 {
283 REQUEST_HILOGD("RemoveListener in, tid:%{public}s, type: %{public}d", taskId.c_str(), type);
284 std::shared_ptr<Request> task = this->GetTask(taskId);
285 if (task.get()) {
286 task->RemoveListener(type, listener);
287 return E_OK;
288 } else {
289 return E_OTHER;
290 }
291 }
292
AddListener(const std::string & taskId,const SubscribeType & type,const std::shared_ptr<INotifyDataListener> & listener)293 int32_t RequestManagerImpl::AddListener(
294 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener)
295 {
296 REQUEST_HILOGD("AddListener in, tid:%{public}s, type: %{public}d", taskId.c_str(), type);
297 std::shared_ptr<Request> task = this->GetTask(taskId);
298 if (task.get()) {
299 task->AddListener(type, listener);
300 return E_OK;
301 } else {
302 REQUEST_HILOGE("GetTask Failed");
303 return E_OTHER;
304 }
305 }
306
RemoveListener(const std::string & taskId,const SubscribeType & type,const std::shared_ptr<INotifyDataListener> & listener)307 int32_t RequestManagerImpl::RemoveListener(
308 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener)
309 {
310 REQUEST_HILOGD("RemoveListener in, tid:%{public}s, type: %{public}d", taskId.c_str(), type);
311 std::shared_ptr<Request> task = this->GetTask(taskId);
312 if (task.get()) {
313 task->RemoveListener(type, listener);
314 return E_OK;
315 } else {
316 return E_OTHER;
317 }
318 }
319
RemoveAllListeners(const std::string & taskId)320 void RequestManagerImpl::RemoveAllListeners(const std::string &taskId)
321 {
322 REQUEST_HILOGD("RemoveAllListeners in, tid:%{public}s", taskId.c_str());
323 std::lock_guard<std::mutex> lock(tasksMutex_);
324 tasks_.erase(taskId);
325 }
326
Subscribe(const std::string & taskId)327 int32_t RequestManagerImpl::Subscribe(const std::string &taskId)
328 {
329 REQUEST_HILOGD("Subscribe in");
330 auto proxy = GetRequestServiceProxy();
331 if (proxy == nullptr) {
332 REQUEST_HILOGE("GetRequestServiceProxy fail.");
333 return E_SERVICE_ERROR;
334 }
335 this->EnsureChannelOpen();
336
337 // channel not open may happen when app state notified terminated but actually does not exit.
338 int32_t ret = proxy->Subscribe(taskId);
339 if (ret == E_CHANNEL_NOT_OPEN) {
340 this->ReopenChannel();
341 ret = proxy->Subscribe(taskId);
342 }
343 return ret;
344 }
345
Unsubscribe(const std::string & taskId)346 int32_t RequestManagerImpl::Unsubscribe(const std::string &taskId)
347 {
348 REQUEST_HILOGD("Unsubscribe in");
349 auto proxy = GetRequestServiceProxy();
350 if (proxy == nullptr) {
351 REQUEST_HILOGE("GetRequestServiceProxy fail.");
352 return E_SERVICE_ERROR;
353 }
354 return proxy->Unsubscribe(taskId);
355 }
356
SubRunCount(const sptr<NotifyInterface> & listener)357 int32_t RequestManagerImpl::SubRunCount(const sptr<NotifyInterface> &listener)
358 {
359 REQUEST_HILOGD("Impl SubRunCount in");
360 auto proxy = GetRequestServiceProxy();
361 if (proxy == nullptr) {
362 REQUEST_HILOGE("Impl SubRunCount in, get request service proxy failed.");
363 FwkRunningTaskCountManager::GetInstance()->SetSaStatus(false);
364 // Proxy does not affect sub runcount at framework.
365 return E_OK;
366 }
367 return proxy->SubRunCount(listener);
368 }
369
UnsubRunCount()370 int32_t RequestManagerImpl::UnsubRunCount()
371 {
372 REQUEST_HILOGD("Impl UnsubRunCount in");
373 auto proxy = GetRequestServiceProxy();
374 if (proxy == nullptr) {
375 REQUEST_HILOGE("GetRequestServiceProxy fail.");
376 return E_SERVICE_ERROR;
377 }
378
379 return proxy->UnsubRunCount();
380 }
381
EnsureChannelOpen()382 int32_t RequestManagerImpl::EnsureChannelOpen()
383 {
384 std::lock_guard<std::recursive_mutex> lock(msgReceiverMutex_);
385 if (msgReceiver_) {
386 return E_OK;
387 }
388
389 auto proxy = GetRequestServiceProxy();
390 if (proxy == nullptr) {
391 REQUEST_HILOGE("EnsureChannelOpen failed: proxy is null");
392 return false;
393 }
394
395 int32_t sockFd = -1;
396 int32_t ret = proxy->OpenChannel(sockFd);
397 if (ret != E_OK) {
398 REQUEST_HILOGE("EnsureChannelOpen failed: %{public}d", ret);
399 return ret;
400 }
401 REQUEST_HILOGD("EnsureChannelOpen ok: %{public}d", sockFd);
402 msgReceiver_ = std::make_shared<ResponseMessageReceiver>(this, sockFd);
403 msgReceiver_->BeginReceive();
404 return E_OK;
405 }
406
GetTask(const std::string & taskId)407 std::shared_ptr<Request> RequestManagerImpl::GetTask(const std::string &taskId)
408 {
409 std::lock_guard<std::mutex> lock(tasksMutex_);
410 auto it = tasks_.find(taskId);
411 if (it != tasks_.end()) {
412 return it->second;
413 }
414
415 auto retPair = this->tasks_.emplace(taskId, std::make_shared<Request>(taskId));
416 if (retPair.second) {
417 return retPair.first->second;
418 } else {
419 this->tasks_.erase(taskId);
420 REQUEST_HILOGE("Response Task create fail");
421 return std::shared_ptr<Request>(nullptr);
422 }
423 }
424
OnChannelBroken()425 void RequestManagerImpl::OnChannelBroken()
426 {
427 std::lock_guard<std::recursive_mutex> lock(msgReceiverMutex_);
428 this->msgReceiver_.reset();
429 }
430
OnResponseReceive(const std::shared_ptr<Response> & response)431 void RequestManagerImpl::OnResponseReceive(const std::shared_ptr<Response> &response)
432 {
433 std::shared_ptr<Request> task = this->GetTask(response->taskId);
434 if (task.get() == nullptr) {
435 REQUEST_HILOGE("OnResponseReceive task not found");
436 return;
437 }
438 task->OnResponseReceive(response);
439 }
440
OnNotifyDataReceive(const std::shared_ptr<NotifyData> & notifyData)441 void RequestManagerImpl::OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData)
442 {
443 std::shared_ptr<Request> task = this->GetTask(std::to_string(notifyData->taskId));
444 if (task.get() == nullptr) {
445 REQUEST_HILOGE("OnNotifyDataReceive task not found");
446 return;
447 }
448 task->OnNotifyDataReceive(notifyData);
449 }
450
GetRequestServiceProxy()451 sptr<RequestServiceInterface> RequestManagerImpl::GetRequestServiceProxy()
452 {
453 std::lock_guard<std::mutex> lock(serviceProxyMutex_);
454 if (requestServiceProxy_ != nullptr) {
455 return requestServiceProxy_;
456 }
457 sptr<ISystemAbilityManager> systemAbilityManager =
458 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
459 if (systemAbilityManager == nullptr) {
460 REQUEST_HILOGE("Getting SystemAbilityManager failed.");
461 return nullptr;
462 }
463 auto systemAbility = systemAbilityManager->GetSystemAbility(DOWNLOAD_SERVICE_ID, "");
464 if (systemAbility == nullptr) {
465 REQUEST_HILOGE("Get SystemAbility failed.");
466 return nullptr;
467 }
468 deathRecipient_ = new RequestSaDeathRecipient();
469 systemAbility->AddDeathRecipient(deathRecipient_);
470 requestServiceProxy_ = iface_cast<RequestServiceInterface>(systemAbility);
471 if (requestServiceProxy_ == nullptr) {
472 REQUEST_HILOGE("Get requestServiceProxy_ fail.");
473 return nullptr;
474 }
475 return requestServiceProxy_;
476 }
477
SubscribeSA()478 bool RequestManagerImpl::SubscribeSA()
479 {
480 std::lock_guard<std::mutex> lock(saChangeListenerMutex_);
481 if (saChangeListener_ != nullptr) {
482 return true;
483 }
484 sptr<ISystemAbilityManager> systemAbilityManager =
485 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
486 if (systemAbilityManager == nullptr) {
487 REQUEST_HILOGE("Getting SystemAbilityManager failed.");
488 return false;
489 }
490 saChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener();
491 if (saChangeListener_ == nullptr) {
492 REQUEST_HILOGE("Get saChangeListener_ failed.");
493 return false;
494 }
495 if (systemAbilityManager->SubscribeSystemAbility(DOWNLOAD_SERVICE_ID, saChangeListener_) != E_OK) {
496 REQUEST_HILOGE("SubscribeSystemAbility failed.");
497 return false;
498 }
499 return true;
500 }
501
UnsubscribeSA()502 bool RequestManagerImpl::UnsubscribeSA()
503 {
504 std::lock_guard<std::mutex> lock(saChangeListenerMutex_);
505 if (saChangeListener_ == nullptr) {
506 return true;
507 }
508 sptr<ISystemAbilityManager> systemAbilityManager =
509 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
510 if (systemAbilityManager == nullptr) {
511 REQUEST_HILOGE("Getting SystemAbilityManager failed.");
512 return false;
513 }
514 if (systemAbilityManager->UnSubscribeSystemAbility(DOWNLOAD_SERVICE_ID, saChangeListener_) != E_OK) {
515 REQUEST_HILOGE("UnsubscribeSystemAbility failed.");
516 return false;
517 }
518 return true;
519 }
520
RestoreListener(void (* callback)())521 void RequestManagerImpl::RestoreListener(void (*callback)())
522 {
523 callback_ = callback;
524 }
525
RestoreSubRunCount()526 void RequestManagerImpl::RestoreSubRunCount()
527 {
528 REQUEST_HILOGD("Restore sub run count in");
529 auto proxy = GetRequestServiceProxy();
530 if (proxy == nullptr) {
531 REQUEST_HILOGE("Restore sub run count, but get request service proxy fail.");
532 return;
533 }
534
535 auto listener = RunCountNotifyStub::GetInstance();
536 int32_t ret = proxy->SubRunCount(listener);
537 if (ret != E_OK) {
538 REQUEST_HILOGE("Restore sub run count failed, ret: %{public}d.", ret);
539 }
540 }
541
SystemAbilityStatusChangeListener()542 RequestManagerImpl::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener()
543 {
544 }
545
546 // Sometimes too slow to return.
OnAddSystemAbility(int32_t saId,const std::string & deviceId)547 void RequestManagerImpl::SystemAbilityStatusChangeListener::OnAddSystemAbility(
548 int32_t saId, const std::string &deviceId)
549 {
550 if (saId != DOWNLOAD_SERVICE_ID) {
551 REQUEST_HILOGE("SA ID is not DOWNLOAD_SERVICE_ID.");
552 }
553 REQUEST_HILOGD("SystemAbility Add.");
554 if (RequestManagerImpl::GetInstance()->callback_ != nullptr) {
555 RequestManagerImpl::GetInstance()->callback_();
556 }
557 if (FwkRunningTaskCountManager::GetInstance()->HasObserver()) {
558 RequestManagerImpl::GetInstance()->RestoreSubRunCount();
559 }
560 }
561
OnRemoveSystemAbility(int32_t saId,const std::string & deviceId)562 void RequestManagerImpl::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
563 int32_t saId, const std::string &deviceId)
564 {
565 if (saId != DOWNLOAD_SERVICE_ID) {
566 REQUEST_HILOGE("SA ID is not DOWNLOAD_SERVICE_ID.");
567 }
568 REQUEST_HILOGD("SystemAbility Remove.");
569 }
570
OnRemoteSaDied(const wptr<IRemoteObject> & remote)571 void RequestManagerImpl::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
572 {
573 REQUEST_HILOGD(" RequestManagerImpl::OnRemoteSaDied");
574 ready_.store(false);
575 SetRequestServiceProxy(nullptr);
576 FwkRunningTaskCountManager::GetInstance()->SetCount(0);
577 FwkRunningTaskCountManager::GetInstance()->SetSaStatus(false);
578 FwkRunningTaskCountManager::GetInstance()->NotifyAllObservers();
579 std::lock_guard<std::recursive_mutex> lock(msgReceiverMutex_);
580 if (!msgReceiver_) {
581 return;
582 }
583 msgReceiver_->Shutdown();
584 }
585
RequestSaDeathRecipient()586 RequestSaDeathRecipient::RequestSaDeathRecipient()
587 {
588 }
589
OnRemoteDied(const wptr<IRemoteObject> & object)590 void RequestSaDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
591 {
592 REQUEST_HILOGI("RequestSaDeathRecipient on remote systemAbility died.");
593 RequestManagerImpl::GetInstance()->OnRemoteSaDied(object);
594 }
595
LoadRequestServer()596 bool RequestManagerImpl::LoadRequestServer()
597 {
598 if (ready_.load()) {
599 REQUEST_HILOGD("GetSystemAbilityManager ready_ true");
600 return true;
601 }
602 REQUEST_HILOGI("Process load request server");
603 std::lock_guard<std::mutex> lock(downloadMutex_);
604 if (ready_.load()) {
605 REQUEST_HILOGD("GetSystemAbilityManager ready_ true");
606 return true;
607 }
608
609 auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
610 if (sm == nullptr) {
611 REQUEST_HILOGE("End load request server, failed: GetSystemAbilityManager return null");
612 return false;
613 }
614 auto systemAbility = sm->CheckSystemAbility(DOWNLOAD_SERVICE_ID);
615 if (systemAbility != nullptr) {
616 REQUEST_HILOGI("End load request server, service already exists");
617 ready_.store(true);
618 return true;
619 }
620 sptr<RequestSyncLoadCallback> loadCallback_ = new (std::nothrow) RequestSyncLoadCallback();
621 if (loadCallback_ == nullptr) {
622 REQUEST_HILOGE("End load request server, failed: new DownloadAbilityCallback fail");
623 return false;
624 }
625
626 int32_t result = sm->LoadSystemAbility(DOWNLOAD_SERVICE_ID, loadCallback_);
627 if (result != E_OK) {
628 REQUEST_HILOGE("End load request server, failed: LoadSystemAbility %{public}d failed, result: "
629 "%{public}d",
630 DOWNLOAD_SERVICE_ID, result);
631 return false;
632 }
633
634 {
635 std::unique_lock<std::mutex> conditionLock(conditionMutex_);
636 auto waitStatus = syncCon_.wait_for(
637 conditionLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() { return ready_.load(); });
638 if (!waitStatus) {
639 REQUEST_HILOGE("End load request server, failed: download server load sa timeout");
640 return false;
641 }
642 }
643 REQUEST_HILOGI("End load request server successfully");
644 return true;
645 }
646
IsSaReady()647 bool RequestManagerImpl::IsSaReady()
648 {
649 return ready_.load();
650 }
651
LoadServerSuccess()652 void RequestManagerImpl::LoadServerSuccess()
653 {
654 std::unique_lock<std::mutex> lock(conditionMutex_);
655 ready_.store(true);
656 syncCon_.notify_one();
657 REQUEST_HILOGI("load download server success");
658 }
659
LoadServerFail()660 void RequestManagerImpl::LoadServerFail()
661 {
662 std::unique_lock<std::mutex> lock(conditionMutex_);
663 ready_.store(false);
664 syncCon_.notify_one();
665 REQUEST_HILOGE("load download server fail");
666 }
667
ReopenChannel()668 void RequestManagerImpl::ReopenChannel()
669 {
670 std::lock_guard<std::recursive_mutex> lock(msgReceiverMutex_);
671 if (!msgReceiver_) {
672 return;
673 }
674 msgReceiver_->Shutdown();
675 this->EnsureChannelOpen();
676 }
677
GetNextSeq()678 int32_t RequestManagerImpl::GetNextSeq()
679 {
680 static std::atomic<int32_t> seq{ 0 };
681 return seq.fetch_add(1);
682 }
683
684 } // namespace OHOS::Request
685