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 #ifndef JS_FILE_ACCESS_EXT_ABILITY_H 17 #define JS_FILE_ACCESS_EXT_ABILITY_H 18 19 #include "file_access_ext_ability.h" 20 #include "file_access_extension_info.h" 21 #include "file_access_framework_errno.h" 22 #include "js_runtime.h" 23 #include "napi_common_fileaccess.h" 24 #include "native_engine/native_reference.h" 25 #include "native_engine/native_value.h" 26 27 namespace OHOS { 28 namespace FileAccessFwk { 29 using namespace AbilityRuntime; 30 31 using InputArgsParser = std::function<bool(napi_env&, napi_value*, size_t&)>; 32 using ResultValueParser = std::function<bool(napi_env&, napi_value)>; 33 34 struct CallJsParam { 35 std::mutex fileOperateMutex; 36 std::condition_variable fileOperateCondition; 37 bool isReady = false; 38 std::string funcName; 39 JsRuntime *jsRuntime; 40 NativeReference *jsObj; 41 InputArgsParser argParser; 42 ResultValueParser retParser; 43 CallJsParamCallJsParam44 CallJsParam(const std::string &funcNameIn, JsRuntime *jsRuntimeIn, NativeReference *jsObjIn, 45 InputArgsParser &argParserIn, ResultValueParser &retParserIn) 46 : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argParser(argParserIn), retParser(retParserIn) 47 {} 48 }; 49 50 struct FilterParam { 51 FileInfo fileInfo; 52 int64_t offset; 53 int64_t maxCount; 54 }; 55 56 struct FileInfoNumParam { 57 const std::string &sourceFileUri; 58 const FileFilter &filter; 59 bool recursion; 60 }; 61 62 struct FileFilterValue { 63 napi_value suffixArray = nullptr; 64 napi_value displayNameArray = nullptr; 65 napi_value mimeTypeArray = nullptr; 66 napi_value nativeFileSizeOver = nullptr; 67 napi_value nativeLastModifiedAfter = nullptr; 68 napi_value nativeExcludeMedia = nullptr; 69 }; 70 71 class JsFileAccessExtAbility : public FileAccessExtAbility { 72 public: 73 JsFileAccessExtAbility(JsRuntime &jsRuntime); 74 virtual ~JsFileAccessExtAbility() override; 75 76 static JsFileAccessExtAbility* Create(const std::unique_ptr<Runtime> &runtime); 77 78 void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 79 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 80 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, 81 const sptr<IRemoteObject> &token) override; 82 void OnStart(const AAFwk::Want &want) override; 83 sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 84 int OpenFile(const Uri &uri, const int flags, int &fd) override; 85 int CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) override; 86 int Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) override; 87 int Delete(const Uri &sourceFile) override; 88 int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; 89 int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> ©Result, bool force = false) override; 90 int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName, 91 Uri &newFileUri) override; 92 int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; 93 int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter, 94 std::vector<FileInfo> &fileInfoVec) override; 95 int ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter, 96 std::vector<FileInfo> &fileInfoVec) override; 97 int GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo) override; 98 int GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo) override; 99 int GetRoots(std::vector<RootInfo> &rootInfoVec) override; 100 int Access(const Uri &uri, bool &isExist) override; 101 int Query(const Uri &uri, std::vector<std::string> &columns, std::vector<std::string> &results) override; 102 int StartWatcher(const Uri &uri) override; 103 int StopWatcher(const Uri &uri) override; 104 int MoveItem(const Uri &sourceFile, const Uri &targetParent, std::vector<Result> &moveResult, 105 bool force = false) override; 106 int MoveFile(const Uri &sourceFile, const Uri &targetParent, std::string &fileName, Uri &newFile) override; 107 int GetFileInfoNum(const std::string &sourceFileUri, const FileFilter &filter, bool recursion, 108 uint32_t &counts) override; 109 110 private: 111 template <typename T> 112 struct Value { 113 T data; 114 int code {ERR_OK}; 115 }; 116 napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0); 117 int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, 118 InputArgsParser argParser, ResultValueParser retParser); 119 void GetSrcPath(std::string &srcPath); 120 static int Notify(Uri &uri, NotifyType notifyType); 121 static napi_value FuncCallback(napi_env env, napi_callback_info info); 122 static bool ParserListFileJsResult(napi_env &env, napi_value nativeValue, Value<std::vector<FileInfo>> &result); 123 static bool ParserGetRootsJsResult(napi_env &env, napi_value nativeValue, Value<std::vector<RootInfo>> &result); 124 static bool ParserQueryFileJsResult(napi_env &env, napi_value nativeValue, 125 Value<std::vector<std::string>> &results); 126 static bool ParserFileInfoNumJsResult(napi_env &env, napi_value &nativeValue, bool &success, uint32_t &counts); 127 static int MakeStringNativeArray(napi_env &env, std::vector<std::string> &inputArray, napi_value resultArray); 128 static int CreateNativeValue(napi_env &env, const FileFilter &filter, struct FileFilterValue &fileFilter); 129 static int MakeJsNativeFileFilter(napi_env &env, const FileFilter &filter, napi_value nativeFilter); 130 static bool BuildFilterParam(napi_env &env, const FileFilter &filter, const FilterParam ¶m, napi_value *argv, 131 size_t &argc); 132 static bool BuildFileInfoNumParam(napi_env &env, FileInfoNumParam ¶m, napi_value *argv, size_t &argc); 133 static napi_status GetFileInfoFromJs(napi_env &env, napi_value obj, FileInfo &fileInfo); 134 static napi_status GetUriAndCodeFromJs(napi_env &env, napi_value result, 135 const std::shared_ptr<Value<std::string>> &value); 136 static napi_status GetFdAndCodeFromJs(napi_env &env, napi_value result, const std::shared_ptr<Value<int>> &value); 137 static napi_status ConstructQueryArg(napi_env &env, napi_value *argv, size_t &argc, const Uri &uri, 138 std::vector<std::string> &columns); 139 static napi_status GetRootInfo(napi_env env, napi_value nativeRootInfo, RootInfo &rootInfo); 140 JsRuntime &jsRuntime_; 141 std::shared_ptr<NativeReference> jsObj_; 142 }; 143 } // namespace FileAccessFwk 144 } // namespace OHOS 145 #endif // JS_FILE_ACCESS_EXT_ABILITY_H 146