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