• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 std::mutex FileAccessExtConnection::mutex_;
30 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)31 void FileAccessExtConnection::OnAbilityConnectDone(
32     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
33 {
34     if (remoteObject == nullptr) {
35         HILOG_ERROR("remote is nullptr");
36         return;
37     }
38     fileExtProxy_ = iface_cast<FileAccessExtProxy>(remoteObject);
39     if (fileExtProxy_ == nullptr) {
40         HILOG_ERROR("fileExtProxy_ is nullptr");
41         return;
42     }
43     HILOG_INFO("OnAbilityConnectDone set connected info");
44     isConnected_.store(true);
45     std::lock_guard<std::mutex> lock(connectLockInfo_.mutex);
46     connectLockInfo_.isReady = true;
47     connectLockInfo_.condition.notify_all();
48 }
49 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)50 void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
51 {
52     fileExtProxy_ = nullptr;
53     isConnected_.store(false);
54 }
55 
ConnectFileExtAbility(const AAFwk::Want & want,const sptr<IRemoteObject> & token)56 void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr<IRemoteObject> &token)
57 {
58     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, token);
59     HILOG_INFO("ConnectFileExtAbility ret: %{public}d ", ret);
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     const int WAIT_TIME = 3;  // second
66     if (!connectLockInfo_.condition.wait_for(lock, std::chrono::seconds(WAIT_TIME),
67         [this] { return fileExtProxy_ != nullptr && connectLockInfo_.isReady; })) {
68         HILOG_INFO("Wait connect timeout.");
69     }
70 }
71 
DisconnectFileExtAbility()72 void FileAccessExtConnection::DisconnectFileExtAbility()
73 {
74     fileExtProxy_ = nullptr;
75     isConnected_.store(false);
76     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this);
77     HILOG_INFO("DisconnectFileExtAbility called end, ret=%{public}d", ret);
78 }
79 
IsExtAbilityConnected()80 bool FileAccessExtConnection::IsExtAbilityConnected()
81 {
82     return isConnected_.load();
83 }
84 
GetFileExtProxy()85 sptr<IFileAccessExtBase> FileAccessExtConnection::GetFileExtProxy()
86 {
87     return fileExtProxy_;
88 }
89 } // namespace FileAccessFwk
90 } // namespace OHOS
91