• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "auto_fill_extension_callback.h"
16 
17 #include "auto_fill_error.h"
18 #include "auto_fill_manager.h"
19 #include "hilog_wrapper.h"
20 #include "view_data.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 namespace {
25 constexpr static char WANT_PARAMS_VIEW_DATA_KEY[] = "ohos.ability.params.viewData";
26 constexpr static char WANT_PARAMS_AUTO_FILL_EVENT_KEY[] = "ability.want.params.AutoFillEvent";
27 } // namespace
OnResult(int32_t errCode,const AAFwk::Want & want)28 void AutoFillExtensionCallback::OnResult(int32_t errCode, const AAFwk::Want &want)
29 {
30     HILOG_DEBUG("Called, result code is %{public}d.", errCode);
31     AutoFillManager::GetInstance().RemoveEvent(eventId_);
32     CloseModalUIExtension();
33 
34     if (errCode == AutoFill::AUTO_FILL_SUCCESS) {
35         SendAutoFillSucess(want);
36     } else {
37         auto resultCode = (errCode == AutoFill::AUTO_FILL_CANCEL) ?
38             AutoFill::AUTO_FILL_CANCEL : AutoFill::AUTO_FILL_FAILED;
39         SendAutoFillFailed(resultCode);
40     }
41 }
42 
OnRelease(int32_t errCode)43 void AutoFillExtensionCallback::OnRelease(int32_t errCode)
44 {
45     HILOG_DEBUG("Called, result code is %{public}d.", errCode);
46     AutoFillManager::GetInstance().RemoveEvent(eventId_);
47     CloseModalUIExtension();
48 
49     if (errCode != 0) {
50         SendAutoFillFailed(AutoFill::AUTO_FILL_RELEASE_FAILED);
51     }
52 }
53 
OnError(int32_t errCode,const std::string & name,const std::string & message)54 void AutoFillExtensionCallback::OnError(int32_t errCode, const std::string &name, const std::string &message)
55 {
56     HILOG_DEBUG("Called, errcode is %{public}d, name is %{public}s, message is %{public}s",
57         errCode, name.c_str(), message.c_str());
58     AutoFillManager::GetInstance().RemoveEvent(eventId_);
59     CloseModalUIExtension();
60 
61     if (errCode != 0) {
62         SendAutoFillFailed(AutoFill::AUTO_FILL_ON_ERROR);
63     }
64 }
65 
OnReceive(const AAFwk::WantParams & wantParams)66 void AutoFillExtensionCallback::OnReceive(const AAFwk::WantParams &wantParams)
67 {
68     HILOG_DEBUG("Called.");
69     if (wantParams.GetIntParam(WANT_PARAMS_AUTO_FILL_EVENT_KEY, 0) != AutoFill::AUTO_FILL_CANCEL_TIME_OUT) {
70         HILOG_ERROR("Event is invalid.");
71         return;
72     }
73     AutoFillManager::GetInstance().RemoveEvent(eventId_);
74 }
75 
SetFillRequestCallback(const std::shared_ptr<IFillRequestCallback> & callback)76 void AutoFillExtensionCallback::SetFillRequestCallback(const std::shared_ptr<IFillRequestCallback> &callback)
77 {
78     fillCallback_ = callback;
79 }
80 
SetSaveRequestCallback(const std::shared_ptr<ISaveRequestCallback> & callback)81 void AutoFillExtensionCallback::SetSaveRequestCallback(const std::shared_ptr<ISaveRequestCallback> &callback)
82 {
83     saveCallback_ = callback;
84 }
85 
SetSessionId(int32_t sessionId)86 void AutoFillExtensionCallback::SetSessionId(int32_t sessionId)
87 {
88     sessionId_= sessionId;
89 }
90 
SetUIContent(Ace::UIContent * uiContent)91 void AutoFillExtensionCallback::SetUIContent(Ace::UIContent *uiContent)
92 {
93     uiContent_ = uiContent;
94 }
95 
SetEventId(uint32_t eventId)96 void AutoFillExtensionCallback::SetEventId(uint32_t eventId)
97 {
98     eventId_ = eventId;
99 }
100 
HandleTimeOut()101 void AutoFillExtensionCallback::HandleTimeOut()
102 {
103     CloseModalUIExtension();
104     SendAutoFillFailed(AutoFill::AUTO_FILL_REQUEST_TIME_OUT);
105 }
106 
SendAutoFillSucess(const AAFwk::Want & want)107 void AutoFillExtensionCallback::SendAutoFillSucess(const AAFwk::Want &want)
108 {
109     if (fillCallback_ != nullptr) {
110         std::string dataStr = want.GetStringParam(WANT_PARAMS_VIEW_DATA_KEY);
111         AbilityBase::ViewData viewData;
112         viewData.FromJsonString(dataStr.c_str());
113         fillCallback_->OnFillRequestSuccess(viewData);
114     }
115 
116     if (saveCallback_ != nullptr) {
117         saveCallback_->OnSaveRequestSuccess();
118     }
119 }
120 
SendAutoFillFailed(int32_t errCode)121 void AutoFillExtensionCallback::SendAutoFillFailed(int32_t errCode)
122 {
123     if (fillCallback_ != nullptr) {
124         fillCallback_->OnFillRequestFailed(errCode);
125     }
126 
127     if (saveCallback_ != nullptr) {
128         saveCallback_->OnSaveRequestFailed();
129     }
130 }
131 
CloseModalUIExtension()132 void AutoFillExtensionCallback::CloseModalUIExtension()
133 {
134     if (uiContent_ == nullptr) {
135         HILOG_DEBUG("uiContent_ is nullptr.");
136         return;
137     }
138     uiContent_->CloseModalUIExtension(sessionId_);
139     uiContent_ = nullptr;
140 }
141 } // namespace AbilityRuntime
142 } // namespace OHOS