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 PRINT_TASK_H
17 #define PRINT_TASK_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "ui_content.h"
25 #include "modal_ui_extension_config.h"
26 #include "want.h"
27 #include "napi_base_context.h"
28 #include "ipc_skeleton.h"
29 #include "bundle_mgr_client.h"
30 #include "iremote_object.h"
31 #include "napi/native_api.h"
32 #include "napi/native_common.h"
33 
34 #include "iprint_callback.h"
35 #include "print_async_call.h"
36 #include "print_modal_ui_callback.h"
37 #include "print_context.h"
38 #include "print_utils.h"
39 
40 namespace OHOS::Print {
41 class PrintTask {
42 public:
43     explicit PrintTask(const std::vector<std::string> &fileList, const sptr<IRemoteObject> &callerToken_);
44     PrintTask(const std::string &printJobName_, const sptr<IPrintCallback> &printAdapterCallback_,
45         const std::shared_ptr<PrintAttributes> &printAttributes_, const sptr<IRemoteObject> &callerToken_);
46     ~PrintTask();
47 
48     uint32_t Start(napi_env env, napi_callback_info info);
49     uint32_t StartPrintAdapter(napi_env env, napi_callback_info info);
50     void Stop();
51     const std::string &GetId() const;
52     static napi_value On(napi_env env, napi_callback_info info);
53     static napi_value Off(napi_env env, napi_callback_info info);
54 
55     bool IsSupportType(const std::string &type) const;
56 
57 private:
58     uint32_t CallSpooler(napi_env env, napi_callback_info info, const std::shared_ptr<AdapterParam> &adapterParam,
59         bool isPrintByAdapter);
60     bool ParseAbilityContextReq(napi_env env, const napi_value &obj,
61         std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> &abilityContext,
62         std::shared_ptr<OHOS::AbilityRuntime::UIExtensionContext> &uiExtensionContext);
63     uint32_t StartUIExtensionAbility(
64         std::shared_ptr<BaseContext> asyncContext, const std::shared_ptr<AdapterParam> &adapterParam);
65     uint32_t StartUIExtensionAbility(OHOS::AAFwk::Want &want, std::shared_ptr<BaseContext> asyncContext);
66     OHOS::Ace::UIContent* GetUIContent(const BaseContext *asyncContext);
67     void CreateDefaultAdapterParam(const std::shared_ptr<AdapterParam> &adapterParam);
68     bool CheckPermission(const std::string &name);
69 
70 private:
71     struct TaskEventContext : public PrintAsyncCall::Context {
72         std::string type = "";
73         std::string taskId = "";
74         sptr<IPrintCallback> callback = nullptr;
75         bool result = false;
76         napi_status status = napi_generic_failure;
TaskEventContextTaskEventContext77         TaskEventContext() : Context(nullptr, nullptr) {};
TaskEventContextTaskEventContext78         TaskEventContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
~TaskEventContextTaskEventContext79         virtual ~TaskEventContext() {};
80     };
81 
82     std::string taskId_;
83     std::vector<std::string> fileList_;
84     std::vector<uint32_t> fdList_;
85     std::map<std::string, bool> supportEvents_;
86     uint32_t pathType_ = 0;
87     sptr<IRemoteObject> callerToken_ = nullptr;
88     std::string printJobName_;
89     sptr<IPrintCallback> printAdapterCallback_ = nullptr;
90     std::shared_ptr<PrintAttributes> printAttributes_ = nullptr;
91     enum FilePathType {
92         FD_UNDEFINED,
93         FD_PATH,
94         FILE_PATH,
95         FILE_PATH_ABSOLUTED
96     };
97 };
98 }  // namespace OHOS::Print
99 #endif  // PRINT_TASK_H
100