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 #include "core/pipeline/pipeline_context.h"
16 #if !defined(PREVIEW)
17 #include <dlfcn.h>
18 #endif
19
20 #include "data_ability_helper.h"
21 #include "datashare_helper.h"
22
23 #include "adapter/ohos/entrance/data_ability_helper_standard.h"
24 #include "base/utils/string_utils.h"
25 #include "base/utils/utils.h"
26
27 namespace OHOS::Ace {
28 const std::string MEDIA_SERVER_HEAD = "datashare:///media";
29
DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context> & context,const std::shared_ptr<OHOS::AbilityRuntime::Context> & runtimeContext,bool useStageModel)30 DataAbilityHelperStandard::DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context>& context,
31 const std::shared_ptr<OHOS::AbilityRuntime::Context>& runtimeContext, bool useStageModel)
32 : useStageModel_(useStageModel)
33 {
34 if (useStageModel) {
35 runtimeContext_ = runtimeContext;
36 mgr_.InitMediaLibraryManager(runtimeContext->GetToken());
37 } else {
38 context_ = context;
39 mgr_.InitMediaLibraryManager(context->GetToken());
40 }
41 }
42
QueryThumbnailResFromDataAbility(const std::string & uri)43 void* DataAbilityHelperStandard::QueryThumbnailResFromDataAbility(const std::string& uri)
44 {
45 #ifdef PREVIEW
46 return nullptr;
47 #else
48 Uri fileUri(uri);
49 return mgr_.GetThumbnail(fileUri).release();
50 #endif
51 }
52
OpenFile(const std::string & uriStr,const std::string & mode)53 int32_t DataAbilityHelperStandard::OpenFile(const std::string& uriStr, const std::string& mode)
54 {
55 LOGD("DataAbilityHelperStandard::OpenFile start uri: %{private}s, mode: %{private}s", uriStr.c_str(), mode.c_str());
56 // FA model always uses DataAbility
57 if (!useStageModel_ || StringUtils::StartWith(uriStr, "dataability://")) {
58 return OpenFileWithDataAbility(uriStr, mode);
59 }
60 if (StringUtils::StartWith(uriStr, "datashare://") || StringUtils::StartWith(uriStr, "file://")) {
61 return OpenFileWithDataShare(uriStr, mode);
62 }
63 LOGE("DataAbilityHelperStandard::OpenFile uri is not support.");
64 return -1;
65 }
66
OpenFileWithDataAbility(const std::string & uriStr,const std::string & mode)67 int32_t DataAbilityHelperStandard::OpenFileWithDataAbility(const std::string& uriStr, const std::string& mode)
68 {
69 std::shared_ptr<OHOS::Uri> uri = std::make_shared<Uri>(uriStr);
70 if (!dataAbilityHelper_) {
71 if (useStageModel_) {
72 dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(runtimeContext_.lock(), uri, false);
73 } else {
74 dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(context_.lock(), uri);
75 }
76 }
77
78 CHECK_NULL_RETURN(dataAbilityHelper_, -1);
79 return dataAbilityHelper_->OpenFile(*uri, mode);
80 }
81
OpenFileWithDataShare(const std::string & uriStr,const std::string & mode)82 int32_t DataAbilityHelperStandard::OpenFileWithDataShare(const std::string& uriStr, const std::string& mode)
83 {
84 auto context = runtimeContext_.lock();
85 if (useStageModel_ && !dataShareHelper_ && context) {
86 dataShareHelper_ = DataShare::DataShareHelper::Creator(context->GetToken(), MEDIA_SERVER_HEAD);
87 }
88
89 CHECK_NULL_RETURN(dataShareHelper_, -1);
90 Uri uri = Uri(uriStr);
91 return dataShareHelper_->OpenFile(uri, mode);
92 }
93
94 } // namespace OHOS::Ace
95