Post-review fixes to pull request #5203 part 1
authorAdam Michalski <a.michalski2@partner.samsung.com>
Tue, 25 Apr 2023 17:35:08 +0000 (19:35 +0200)
committerChanwoo Choi <chanwoo@kernel.org>
Mon, 15 May 2023 02:58:09 +0000 (11:58 +0900)
src/Tizen.System.Session/Session/Session.cs
src/Tizen.System.Session/Session/SessionEnums.cs
src/Tizen.System.Session/Session/SessionErrorFactory.cs
src/Tizen.System.Session/Session/SessionEventArgs.cs

index 6303e78..8edfc4a 100644 (file)
@@ -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
         /// <exception cref="UnauthorizedAccessException">Not permitted</exception>
         /// <exception cref="NotSupportedException">Not supported</exception>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public event EventHandler<SwitchUserCompletionEventArgs> SwitchUserCompletion
+        public event EventHandler<SwitchUserCompletionEventArgs> 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)
index e5866c8..44d0a11 100644 (file)
  * 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,
index 70f407b..8125bcc 100644 (file)
@@ -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;
+                       }
+               }
     }
 }
index 85d7799..b30bd87 100644 (file)
@@ -126,11 +126,11 @@ namespace Tizen.System
     }
 
     /// <summary>
-    /// An event arguemnt type for SwitchUserCompletion event type
+    /// An event arguemnt type for SwitchUserCompleted event type
     /// </summary>
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class SwitchUserCompletionEventArgs : SwitchUserEventArgs
     {
         internal SwitchUserCompletionEventArgs(SubsessionEventInfoNative infoNative) : base(infoNative) { }
     }
-}
\ No newline at end of file
+}