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 #include "file_access_ext_connection.h"
17
18 #include <errors.h>
19
20 #include "ability_connect_callback_interface.h"
21 #include "ability_manager_client.h"
22 #include "file_access_ext_proxy.h"
23 #include "hilog_wrapper.h"
24 #include "iremote_broker.h"
25
26 namespace OHOS {
27 namespace FileAccessFwk {
28 namespace {
29 constexpr int WAIT_TIME = 3; // second
30 }
31 std::mutex FileAccessExtConnection::mutex_;
32
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)33 void FileAccessExtConnection::OnAbilityConnectDone(
34 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
35 {
36 if (remoteObject == nullptr) {
37 HILOG_ERROR("remote is nullptr");
38 return;
39 }
40 fileExtProxy_ = iface_cast<FileAccessExtProxy>(remoteObject);
41 if (fileExtProxy_ == nullptr) {
42 HILOG_ERROR("fileExtProxy_ is nullptr");
43 return;
44 }
45 isConnected_.store(true);
46 std::lock_guard<std::mutex> lock(connectLockInfo_.mutex);
47 connectLockInfo_.isReady = true;
48 connectLockInfo_.condition.notify_all();
49 }
50
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)51 void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
52 {
53 fileExtProxy_ = nullptr;
54 isConnected_.store(false);
55 }
56
ConnectFileExtAbility(const AAFwk::Want & want,const sptr<IRemoteObject> & token)57 void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr<IRemoteObject> &token)
58 {
59 ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, token);
60 if (ret != ERR_OK) {
61 HILOG_INFO("ConnectAbility ret=%{public}d", ret);
62 return;
63 }
64 std::unique_lock<std::mutex> lock(connectLockInfo_.mutex);
65 if (!connectLockInfo_.condition.wait_for(lock, std::chrono::seconds(WAIT_TIME),
66 [this] { return fileExtProxy_ != nullptr && connectLockInfo_.isReady; })) {
67 HILOG_INFO("Wait connect timeout.");
68 }
69 }
70
DisconnectFileExtAbility()71 void FileAccessExtConnection::DisconnectFileExtAbility()
72 {
73 fileExtProxy_ = nullptr;
74 isConnected_.store(false);
75 ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this);
76 HILOG_INFO("DisconnectFileExtAbility called end, ret=%{public}d", ret);
77 }
78
IsExtAbilityConnected()79 bool FileAccessExtConnection::IsExtAbilityConnected()
80 {
81 return isConnected_.load();
82 }
83
GetFileExtProxy()84 sptr<IFileAccessExtBase> FileAccessExtConnection::GetFileExtProxy()
85 {
86 return fileExtProxy_;
87 }
88 } // namespace FileAccessFwk
89 } // namespace OHOS