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(NativeEngine&, NativeValue* [], size_t&)>; 32 using ResultValueParser = std::function<bool(NativeEngine&, NativeValue*)>; 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 class JsFileAccessExtAbility : public FileAccessExtAbility { 51 public: 52 JsFileAccessExtAbility(JsRuntime &jsRuntime); 53 virtual ~JsFileAccessExtAbility() override; 54 55 static JsFileAccessExtAbility* Create(const std::unique_ptr<Runtime> &runtime); 56 57 void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 58 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 59 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, 60 const sptr<IRemoteObject> &token) override; 61 void OnStart(const AAFwk::Want &want) override; 62 sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 63 int OpenFile(const Uri &uri, const int flags, int &fd) override; 64 int CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) override; 65 int Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) override; 66 int Delete(const Uri &sourceFile) override; 67 int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; 68 int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; 69 int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter, 70 std::vector<FileInfo> &fileInfoVec) override; 71 int UriToFileInfo(const Uri &selectFile, FileInfo &fileInfo) override; 72 int GetRoots(std::vector<RootInfo> &rootInfoVec) override; 73 int Access(const Uri &uri, bool &isExist) override; 74 75 private: 76 NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); 77 int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, 78 InputArgsParser argParser, ResultValueParser retParser); 79 static NativeValue* FuncCallback(NativeEngine *engine, NativeCallbackInfo *info); 80 void GetSrcPath(std::string &srcPath); 81 82 JsRuntime &jsRuntime_; 83 std::shared_ptr<NativeReference> jsObj_; 84 }; 85 } // namespace FileAccessFwk 86 } // namespace OHOS 87 #endif // JS_FILE_ACCESS_EXT_ABILITY_H