1 /*
2 * Copyright (c) 2023-2025 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 "photo_session_info.h"
17
18 #include "dp_log.h"
19 #include "dps.h"
20 #include "ideferred_photo_processing_session.h"
21 #include "session_command.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 class PhotoSessionInfo::CallbackDeathRecipient : public IRemoteObject::DeathRecipient {
27 public:
CallbackDeathRecipient(const wptr<PhotoSessionInfo> & sessionInfo)28 explicit CallbackDeathRecipient(const wptr<PhotoSessionInfo>& sessionInfo)
29 : sessionInfo_(sessionInfo)
30 {
31 DP_DEBUG_LOG("entered.");
32 }
33 ~CallbackDeathRecipient() = default;
34
Initialize(const sptr<IDeferredPhotoProcessingSessionCallback> & callback)35 int32_t Initialize(const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
36 {
37 DP_DEBUG_LOG("entered.");
38 sptr<IRemoteObject> object = callback->AsObject();
39 auto result = object->AddDeathRecipient(this);
40 DP_CHECK_ERROR_RETURN_RET_LOG(!result, DP_INIT_FAIL,
41 "Add photo DeathRecipient for Callback failed.");
42 return DP_OK;
43 }
44
Destroy(const sptr<IDeferredPhotoProcessingSessionCallback> & callback)45 int32_t Destroy(const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
46 {
47 DP_DEBUG_LOG("entered.");
48 sptr<IRemoteObject> object = callback->AsObject();
49 auto result = object->RemoveDeathRecipient(this);
50 DP_CHECK_ERROR_RETURN_RET_LOG(!result, DP_INIT_FAIL,
51 "Remove photo DeathRecipient for Callback failed.");
52 return DP_OK;
53 }
54
OnRemoteDied(const wptr<IRemoteObject> & remote)55 void OnRemoteDied(const wptr<IRemoteObject> &remote) override
56 {
57 DP_ERR_LOG("Photo remote died.");
58 auto info = sessionInfo_.promote();
59 DP_CHECK_RETURN_LOG(info == nullptr, "PhotoSessionInfo is nullptr.");
60 info->OnCallbackDied();
61 }
62 private:
63 wptr<PhotoSessionInfo> sessionInfo_;
64 };
65
PhotoSessionInfo(const int32_t userId,const sptr<IDeferredPhotoProcessingSessionCallback> & callback)66 PhotoSessionInfo::PhotoSessionInfo(const int32_t userId, const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
67 : userId_(userId), callback_(callback)
68 {
69 DP_DEBUG_LOG("entered. userId: %{public}d.", userId_);
70 Initialize();
71 }
72
~PhotoSessionInfo()73 PhotoSessionInfo::~PhotoSessionInfo()
74 {
75 DP_INFO_LOG("entered. userId: %{public}d.", userId_);
76 DP_CHECK_EXECUTE(callback_ != nullptr && deathRecipient_ != nullptr, deathRecipient_->Destroy(callback_));
77 }
78
Initialize()79 int32_t PhotoSessionInfo::Initialize()
80 {
81 DP_CHECK_ERROR_RETURN_RET_LOG(callback_ == nullptr, DP_NULL_POINTER,
82 "PhotoSessionInfo init failed, callback is nullptr.");
83
84 session_ = sptr<DeferredPhotoProcessingSession>::MakeSptr(userId_);
85 deathRecipient_ = sptr<CallbackDeathRecipient>::MakeSptr(this);
86
87 auto ret = deathRecipient_->Initialize(callback_);
88 DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "Set photo DeathRecipient failed.");
89 return DP_OK;
90 }
91
GetDeferredPhotoProcessingSession()92 sptr<IDeferredPhotoProcessingSession> PhotoSessionInfo::GetDeferredPhotoProcessingSession()
93 {
94 return session_;
95 }
96
GetRemoteCallback()97 sptr<IDeferredPhotoProcessingSessionCallback> PhotoSessionInfo::GetRemoteCallback()
98 {
99 std::lock_guard lock(callbackMutex_);
100 return callback_;
101 }
102
OnCallbackDied()103 void PhotoSessionInfo::OnCallbackDied()
104 {
105 {
106 std::lock_guard lock(callbackMutex_);
107 callback_ = nullptr;
108 }
109 auto ret = DPS_SendUrgentCommand<DeletePhotoSessionCommand>(this);
110 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "DeletePhotoSession failed.");
111 }
112
SetCallback(const sptr<IDeferredPhotoProcessingSessionCallback> & callback)113 void PhotoSessionInfo::SetCallback(const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
114 {
115 DP_INFO_LOG("Reset photo callback.");
116 std::lock_guard lock(callbackMutex_);
117 isCreate_ = false;
118 callback_ = callback;
119 auto ret = deathRecipient_->Initialize(callback_);
120 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "Set photo DeathRecipient failed.");
121 }
122 } // namespace DeferredProcessing
123 } // namespace CameraStandard
124 } // namespace OHOS
125