• 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 #if !defined(PREVIEW)
16 #include <dlfcn.h>
17 #endif
18 
19 #include "data_ability_helper.h"
20 #include "datashare_helper.h"
21 #include "pixel_map.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 namespace {
29 
30 #if !defined(PREVIEW)
31 using ThumbnailNapiEntry = void* (*)(const char*, void*);
GetThumbnailNapiEntry()32 ThumbnailNapiEntry GetThumbnailNapiEntry()
33 {
34     static ThumbnailNapiEntry thumbnailNapiEntry = nullptr;
35     if (!thumbnailNapiEntry) {
36 #if defined(_ARM64_) || defined(SIMULATOR_64)
37         std::string prefix = "/system/lib64/module/";
38 #else
39         std::string prefix = "/system/lib/module/";
40 #endif
41 #ifdef OHOS_STANDARD_SYSTEM
42         std::string napiPluginName = "multimedia/libmedialibrary.z.so";
43 #endif
44         auto napiPluginPath = prefix.append(napiPluginName);
45         void* handle = dlopen(napiPluginPath.c_str(), RTLD_LAZY);
46         CHECK_NULL_RETURN(handle, nullptr);
47         thumbnailNapiEntry = reinterpret_cast<ThumbnailNapiEntry>(dlsym(handle, "OHOS_MEDIA_NativeGetThumbnail"));
48         if (thumbnailNapiEntry == nullptr) {
49             dlclose(handle);
50             LOGE("Failed to get symbol OHOS_MEDIA_NativeGetThumbnail in %{public}s", napiPluginPath.c_str());
51             return nullptr;
52         }
53     }
54     return thumbnailNapiEntry;
55 }
56 #endif
57 
58 } // namespace
59 
DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context> & context,const std::shared_ptr<OHOS::AbilityRuntime::Context> & runtimeContext,bool useStageModel)60 DataAbilityHelperStandard::DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context>& context,
61     const std::shared_ptr<OHOS::AbilityRuntime::Context>& runtimeContext, bool useStageModel)
62     : useStageModel_(useStageModel)
63 {
64     if (useStageModel) {
65         runtimeContext_ = runtimeContext;
66     } else {
67         context_ = context;
68     }
69 }
70 
QueryThumbnailResFromDataAbility(const std::string & uri)71 void* DataAbilityHelperStandard::QueryThumbnailResFromDataAbility(const std::string& uri)
72 {
73 #ifdef PREVIEW
74     return nullptr;
75 #else
76     ThumbnailNapiEntry thumbnailNapiEntry = GetThumbnailNapiEntry();
77     CHECK_NULL_RETURN(thumbnailNapiEntry, nullptr);
78     auto runtimeContextSptr = runtimeContext_.lock();
79     CHECK_NULL_RETURN(runtimeContextSptr, nullptr);
80     return thumbnailNapiEntry(uri.c_str(), &runtimeContextSptr);
81 #endif
82 }
83 
OpenFile(const std::string & uriStr,const std::string & mode)84 int32_t DataAbilityHelperStandard::OpenFile(const std::string& uriStr, const std::string& mode)
85 {
86     LOGD("DataAbilityHelperStandard::OpenFile start uri: %{private}s, mode: %{private}s", uriStr.c_str(), mode.c_str());
87     // FA model always uses DataAbility
88     if (!useStageModel_ || StringUtils::StartWith(uriStr, "dataability://")) {
89         return OpenFileWithDataAbility(uriStr, mode);
90     }
91     if (StringUtils::StartWith(uriStr, "datashare://")) {
92         return OpenFileWithDataShare(uriStr, mode);
93     }
94     LOGE("DataAbilityHelperStandard::OpenFile uri is not support.");
95     return -1;
96 }
97 
OpenFileWithDataAbility(const std::string & uriStr,const std::string & mode)98 int32_t DataAbilityHelperStandard::OpenFileWithDataAbility(const std::string& uriStr, const std::string& mode)
99 {
100     std::shared_ptr<OHOS::Uri> uri = std::make_shared<Uri>(uriStr);
101     if (!dataAbilityHelper_) {
102         if (useStageModel_) {
103             dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(runtimeContext_.lock(), uri, false);
104         } else {
105             dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(context_.lock(), uri);
106         }
107     }
108 
109     CHECK_NULL_RETURN(dataAbilityHelper_, -1);
110     return dataAbilityHelper_->OpenFile(*uri, mode);
111 }
112 
OpenFileWithDataShare(const std::string & uriStr,const std::string & mode)113 int32_t DataAbilityHelperStandard::OpenFileWithDataShare(const std::string& uriStr, const std::string& mode)
114 {
115     auto context = runtimeContext_.lock();
116     if (useStageModel_ && !dataShareHelper_ && context) {
117         dataShareHelper_ = DataShare::DataShareHelper::Creator(context->GetToken(), uriStr);
118     }
119 
120     CHECK_NULL_RETURN(dataShareHelper_, -1);
121     Uri uri = Uri(uriStr);
122     return dataShareHelper_->OpenFile(uri, mode);
123 }
124 
125 } // namespace OHOS::Ace
126