From d774bb45c7da95324888af4a9d2e386486597699 Mon Sep 17 00:00:00 2001 From: Adam Michalski Date: Tue, 25 Apr 2023 19:35:08 +0200 Subject: [PATCH] Post-review fixes to pull request #5203 part 1 --- src/Tizen.System.Session/Session/Session.cs | 31 +++++++++++++++------- src/Tizen.System.Session/Session/SessionEnums.cs | 4 ++- .../Session/SessionErrorFactory.cs | 24 ++++++++++------- .../Session/SessionEventArgs.cs | 4 +-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/Tizen.System.Session/Session/Session.cs b/src/Tizen.System.Session/Session/Session.cs index 6303e78..8edfc4a 100644 --- a/src/Tizen.System.Session/Session/Session.cs +++ b/src/Tizen.System.Session/Session/Session.cs @@ -80,7 +80,8 @@ namespace Tizen.System [EditorBrowsable(EditorBrowsableState.Never)] public static Session GetInstance(int sessionUID) { - s_sessionInstances.TryAdd(sessionUID, new Session(sessionUID)); + if (!s_sessionInstances.ContainsKey(sessionUID)) + s_sessionInstances.TryAdd(sessionUID, new Session(sessionUID)); return s_sessionInstances[sessionUID]; } @@ -213,19 +214,21 @@ namespace Tizen.System _replyMap[taskID] = (int result, IntPtr data) => { - _replyMap.Remove((int)data); try { CheckError((SessionError)result, "Interop failed to remove a subsession user"); + task.SetResult(true); } catch (Exception exception) { task.SetException(exception); return; } - - task.SetResult(true); + finally + { + _replyMap.Remove((int)data); + } }; SessionError ret = Interop.Session.SubsessionRemoveUser(SessionUID, userName, _replyMap[taskID], (IntPtr)taskID); @@ -258,19 +261,20 @@ namespace Tizen.System _replyMap[taskID] = (int result, IntPtr data) => { - _replyMap.Remove((int)data); - try { CheckError((SessionError)result, "Interop failed to switch to a different subsession user"); + task.SetResult(true); } catch (Exception exception) { task.SetException(exception); return; } - - task.SetResult(true); + finally + { + _replyMap.Remove((int)data); + } }; SessionError ret = Interop.Session.SubsessionSwitchUser(SessionUID, userName, _replyMap[taskID], (IntPtr)taskID); @@ -443,7 +447,7 @@ namespace Tizen.System /// Not permitted /// Not supported [EditorBrowsable(EditorBrowsableState.Never)] - public event EventHandler SwitchUserCompletion + public event EventHandler SwitchUserCompleted { add { @@ -471,7 +475,14 @@ namespace Tizen.System return; Log.Error(SessionErrorFactory.LogTag, msg); - SessionErrorFactory.ThrowException(ret); + Exception ex = SessionErrorFactory.CreateException(ret); + if (ex == null) + { + Log.Error(SessionErrorFactory.LogTag, + "Unexpected exception type for SessionError: " + Enum.GetName(typeof(SessionError), ret)); + throw new InvalidOperationException("Unrecognized error"); + } + throw ex; } private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray) diff --git a/src/Tizen.System.Session/Session/SessionEnums.cs b/src/Tizen.System.Session/Session/SessionEnums.cs index e5866c8..44d0a11 100644 --- a/src/Tizen.System.Session/Session/SessionEnums.cs +++ b/src/Tizen.System.Session/Session/SessionEnums.cs @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + +using System; using System.ComponentModel; namespace Tizen.System { + [Flags] internal enum SessionEventType : int { AddUserWait = 1 << 0, diff --git a/src/Tizen.System.Session/Session/SessionErrorFactory.cs b/src/Tizen.System.Session/Session/SessionErrorFactory.cs index 70f407b..8125bcc 100644 --- a/src/Tizen.System.Session/Session/SessionErrorFactory.cs +++ b/src/Tizen.System.Session/Session/SessionErrorFactory.cs @@ -37,41 +37,45 @@ namespace Tizen.System { internal const string LogTag = "Tizen.System.Session"; - internal static void ThrowException(SessionError err) + internal static Exception CreateException(SessionError err) { SessionError error = (SessionError)err; if (error == SessionError.InvalidParameter) { - throw new ArgumentException("Invalid parameter"); + return new ArgumentException("Invalid parameter"); } else if (error == SessionError.Io) { - throw new IOException("I/O error"); + return new IOException("I/O error"); } else if (error == SessionError.OutOfMemory) { - throw new OutOfMemoryException("Out of memory"); + return new InvalidOperationException("Out of memory"); } else if (error == SessionError.AlreadyExists) { - throw new InvalidOperationException("Already exists"); + return new InvalidOperationException("Already exists"); } else if (error == SessionError.NotAvailible) { - throw new InvalidOperationException("Not availible"); + return new InvalidOperationException("Not availible"); } else if (error == SessionError.ResourceBusy) { - throw new InvalidOperationException("Resource busy"); + return new InvalidOperationException("Resource busy"); } else if (error == SessionError.PermissionDenied) { - throw new UnauthorizedAccessException("Permission denied"); + return new UnauthorizedAccessException("Permission denied"); } else if (error == SessionError.NotSupported) { - throw new NotSupportedException("Not supported"); + return new NotSupportedException("Not supported"); } - } + else + { + return null; + } + } } } diff --git a/src/Tizen.System.Session/Session/SessionEventArgs.cs b/src/Tizen.System.Session/Session/SessionEventArgs.cs index 85d7799..b30bd87 100644 --- a/src/Tizen.System.Session/Session/SessionEventArgs.cs +++ b/src/Tizen.System.Session/Session/SessionEventArgs.cs @@ -126,11 +126,11 @@ namespace Tizen.System } /// - /// An event arguemnt type for SwitchUserCompletion event type + /// An event arguemnt type for SwitchUserCompleted event type /// [EditorBrowsable(EditorBrowsableState.Never)] public class SwitchUserCompletionEventArgs : SwitchUserEventArgs { internal SwitchUserCompletionEventArgs(SubsessionEventInfoNative infoNative) : base(infoNative) { } } -} \ No newline at end of file +} -- 2.7.4