1 /*
2  * Copyright (c) 2023-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 
16 #include "session_info.h"
17 
18 #include "iservice_registry.h"
19 #include "session_manager.h"
20 #include "dp_log.h"
21 #include "ideferred_photo_processing_session.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 class SessionInfo::CallbackDeathRecipient : public IRemoteObject::DeathRecipient {
27 public:
CallbackDeathRecipient(SessionInfo * sessionInfo)28     explicit CallbackDeathRecipient(SessionInfo* sessionInfo)
29         : sessionInfo_(sessionInfo)
30     {
31     }
~CallbackDeathRecipient()32     ~CallbackDeathRecipient()
33     {
34         sessionInfo_ = nullptr;
35     }
OnRemoteDied(const wptr<IRemoteObject> & remote)36     void OnRemoteDied(const wptr<IRemoteObject> &remote) override
37     {
38         DP_ERR_LOG("Remote died, do clean works.");
39         if (sessionInfo_ == nullptr) {
40             return;
41         }
42         sessionInfo_->OnCallbackDied();
43     }
44 private:
45     SessionInfo* sessionInfo_;
46 };
47 
SessionInfo(const int32_t userId,const sptr<IDeferredPhotoProcessingSessionCallback> & callback,SessionManager * sessionManager)48 SessionInfo::SessionInfo(const int32_t userId, const sptr<IDeferredPhotoProcessingSessionCallback>& callback,
49     SessionManager* sessionManager)
50     : userId_(userId),
51       callback_(callback),
52       sessionManager_(sessionManager)
53 {
54     DP_DEBUG_LOG("userId: %{public}d.", userId_);
55     callbackDeathRecipient_ = sptr<CallbackDeathRecipient>::MakeSptr(this);
56     SetCallback(callback);
57 }
58 
~SessionInfo()59 SessionInfo::~SessionInfo()
60 {
61     DP_DEBUG_LOG("entered.");
62     callback_ = nullptr;
63     sessionManager_ = nullptr;
64 }
65 
CreateDeferredPhotoProcessingSession(const int32_t userId,std::shared_ptr<DeferredPhotoProcessor> processor,TaskManager * taskManager,sptr<IDeferredPhotoProcessingSessionCallback> callback)66 sptr<IDeferredPhotoProcessingSession> SessionInfo::CreateDeferredPhotoProcessingSession(const int32_t userId,
67     std::shared_ptr<DeferredPhotoProcessor> processor, TaskManager* taskManager,
68     sptr<IDeferredPhotoProcessingSessionCallback> callback)
69 {
70     session_ = sptr<DeferredPhotoProcessingSession>::MakeSptr(userId, processor, taskManager, callback);
71     DP_INFO_LOG("CreateDeferredProcessingSession successful.");
72     return session_;
73 }
74 
GetDeferredPhotoProcessingSession()75 sptr<IDeferredPhotoProcessingSession> SessionInfo::GetDeferredPhotoProcessingSession()
76 {
77     return session_;
78 }
79 
GetRemoteCallback()80 sptr<IDeferredPhotoProcessingSessionCallback> SessionInfo::GetRemoteCallback()
81 {
82     return callback_;
83 }
84 
OnCallbackDied()85 void SessionInfo::OnCallbackDied()
86 {
87     if (sessionManager_ == nullptr) {
88         DP_ERR_LOG("SessionInfo::sessionManager_ is null.");
89         return;
90     }
91     sessionManager_->OnCallbackDied(userId_);
92 }
93 
SetCallback(const sptr<IDeferredPhotoProcessingSessionCallback> & callback)94 void SessionInfo::SetCallback(const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
95 {
96     DP_INFO_LOG("reset callback");
97     callback_ = callback;
98     sptr<IRemoteObject> object = callback_->AsObject();
99     auto result = object->AddDeathRecipient(callbackDeathRecipient_);
100     if (!result) {
101         DP_ERR_LOG("AddDeathRecipient for Callback failed.");
102     }
103     return;
104 }
105 } // namespace DeferredProcessing
106 } // namespace CameraStandard
107 } // namespace OHOS
108