Add SafeBundleHandle for creating Bundle object from native
authorWonYoung Choi <wy80.choi@samsung.com>
Thu, 18 Aug 2016 06:00:29 +0000 (15:00 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Thu, 18 Aug 2016 06:00:29 +0000 (15:00 +0900)
Change-Id: I3da8c2f6d3fcf5c9d9cbdaa46af3d7c44519fe70

Tizen.Applications/Interop/Interop.Bundle.cs [changed mode: 0755->0644]
Tizen.Applications/Interop/Interop.MessagePort.cs [changed mode: 0755->0644]
Tizen.Applications/Tizen.Applications.Messages/MessagePort.cs [changed mode: 0755->0644]
Tizen.Applications/Tizen.Applications.csproj
Tizen.Applications/Tizen.Applications/AppControl.cs
Tizen.Applications/Tizen.Applications/Bundle.cs
Tizen.Applications/Tizen.Applications/SafeAppControlHandle.cs
Tizen.Applications/Tizen.Applications/SafeBundleHandle.cs [new file with mode: 0644]

old mode 100755 (executable)
new mode 100644 (file)
index 9668b00..64b0d64
@@ -9,6 +9,8 @@
 using System;
 using System.Runtime.InteropServices;
 
+using Tizen.Applications;
+
 internal static partial class Interop
 {
     internal static partial class Bundle
@@ -17,44 +19,44 @@ internal static partial class Interop
         internal delegate void Iterator(string key, int type, IntPtr keyval, IntPtr userData);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_create")]
-        internal static extern IntPtr Create();
+        internal static extern SafeBundleHandle Create();
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_free")]
-        internal static extern int Free(IntPtr handle);
+        internal static extern int DangerousFree(IntPtr handle);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_del")]
-        internal static extern int RemoveItem(IntPtr handle, string key);
+        internal static extern int RemoveItem(SafeBundleHandle handle, string key);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_add_str")]
-        internal static extern int AddString(IntPtr handle, string key, string value);
+        internal static extern int AddString(SafeBundleHandle handle, string key, string value);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_get_type")]
-        internal static extern int GetType(IntPtr handle, string key);
+        internal static extern int GetType(SafeBundleHandle handle, string key);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_get_str")]
-        internal static extern int GetString(IntPtr handle, string key, out IntPtr value);
+        internal static extern int GetString(SafeBundleHandle handle, string key, out IntPtr value);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_add_byte")]
-        internal static extern unsafe int AddByte(IntPtr handle, string key, byte* value, int size);
+        internal static extern unsafe int AddByte(SafeBundleHandle handle, string key, byte* value, int size);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_get_byte")]
-        internal static extern int GetByte(IntPtr handle, string key, out IntPtr value, out int size);
+        internal static extern int GetByte(SafeBundleHandle handle, string key, out IntPtr value, out int size);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_add_str_array")]
-        internal static extern int AddStringArray(IntPtr handle, string key, string[] value, int size);
+        internal static extern int AddStringArray(SafeBundleHandle handle, string key, string[] value, int size);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_get_str_array")]
-        internal static extern IntPtr GetStringArray(IntPtr handle, string key, out int size);
+        internal static extern IntPtr GetStringArray(SafeBundleHandle handle, string key, out int size);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_foreach")]
-        internal static extern void Foreach(IntPtr handle, Iterator iterator, IntPtr userData);
+        internal static extern void Foreach(SafeBundleHandle handle, Iterator iterator, IntPtr userData);
 
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_dup")]
-        internal static extern IntPtr Clone(IntPtr handle);
+        internal static extern SafeBundleHandle DangerousClone(IntPtr handle);
 
         internal static class UnsafeCode
         {
-            internal static unsafe int AddItem(IntPtr handle, string key, byte[] value, int offset, int count)
+            internal static unsafe int AddItem(SafeBundleHandle handle, string key, byte[] value, int offset, int count)
             {
                 fixed (byte* pointer = value)
                 {
old mode 100755 (executable)
new mode 100644 (file)
index 0d35250..f84a70a
@@ -9,6 +9,8 @@
 using System;
 using System.Runtime.InteropServices;
 
+using Tizen.Applications;
+
 internal static partial class Interop
 {
     internal static partial class MessagePort
@@ -26,10 +28,10 @@ internal static partial class Interop
         internal static extern int UnregisterTrustedPort(int trusted_local_port_id);
 
         [DllImport(Libraries.MessagePort, EntryPoint = "message_port_send_message_with_local_port", CallingConvention = CallingConvention.Cdecl)]
-        internal static extern int SendMessageWithLocalPort(string remote_app_id, string remote_port, IntPtr message, int local_port_id);
+        internal static extern int SendMessageWithLocalPort(string remote_app_id, string remote_port, SafeBundleHandle message, int local_port_id);
 
         [DllImport(Libraries.MessagePort, EntryPoint = "message_port_send_trusted_message_with_local_port", CallingConvention = CallingConvention.Cdecl)]
-        internal static extern int SendTrustedMessageWithLocalPort(string remote_app_id, string remote_port, IntPtr message, int local_port_id);
+        internal static extern int SendTrustedMessageWithLocalPort(string remote_app_id, string remote_port, SafeBundleHandle message, int local_port_id);
 
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         internal delegate void message_port_message_cb(int local_port_id, string remote_app_id, string remote_port, bool trusted_remote_port, IntPtr message, IntPtr userData);
old mode 100755 (executable)
new mode 100644 (file)
index 2624dc7..bc082fa
@@ -134,7 +134,7 @@ namespace Tizen.Applications.Messages
                 {
                     MessageReceivedEventArgs args = new MessageReceivedEventArgs()
                     {
-                        Message = Bundle.MakeRetainedBundle(message)
+                        Message = new Bundle(new SafeBundleHandle(message, false))
                     };
 
                     if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
@@ -253,8 +253,8 @@ namespace Tizen.Applications.Messages
                 throw new ArgumentNullException("message");
             }
             int ret = trusted ?
-                        Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
-                        Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
+                        Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId) :
+                        Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId);
 
             if (ret != (int)MessagePortError.None)
             {
index 513d52aabf508bc8b9d8fffaaa982ccc58d3482b..a2dcec9765d52ebac164315376ce6eaf18f0ab56 100644 (file)
     <Compile Include="Tizen.Applications\ReceivedAppControl.cs" />
     <Compile Include="Tizen.Applications\RegionFormatChangedEventArgs.cs" />
     <Compile Include="Tizen.Applications\SafeAppControlHandle.cs" />
+    <Compile Include="Tizen.Applications\SafeBundleHandle.cs" />
     <Compile Include="Tizen.Applications\SafePackageManagerHandle.cs" />
     <Compile Include="Tizen.Applications\ServiceApplication.cs" />
     <Compile Include="Tizen.Applications\StorageType.cs" />
index 124e28a1396d6006aa153e15ebc6ae651d8343df..c434fc406d38ad033f5b478e4f27b69d2116cfb3 100644 (file)
@@ -67,6 +67,11 @@ namespace Tizen.Applications
         /// <param name="handle"></param>
         public AppControl(SafeAppControlHandle handle)
         {
+            if (handle == null)
+            {
+                throw new ArgumentNullException("handle");
+            }
+
             Interop.AppControl.ErrorCode err = Interop.AppControl.DangerousClone(out _handle, handle.DangerousGetHandle());
             if (err != Interop.AppControl.ErrorCode.None)
             {
index 5f54a2d7ebb9621f1e533117ff482b706ac2df59..0a5bc68a27e6e605c5b987e5133dbd1bb3d97d31 100644 (file)
@@ -24,7 +24,7 @@ namespace Tizen.Applications
     /// </summary>
     public class Bundle : IDisposable
     {
-        private IntPtr _handle;
+        private SafeBundleHandle _handle;
         private bool _disposed = false;
         private readonly HashSet<string> _keys;
 
@@ -42,28 +42,29 @@ namespace Tizen.Applications
             _keys = new HashSet<string>();
         }
 
-        internal Bundle(IntPtr handle, bool ownership = false)
+        /// <summary>
+        /// The Bundle constructor.
+        /// </summary>
+        /// <param name="handle">The SafeBundleHandle instance.</param>
+        /// <exception cref="System.ArgumentNullException">Thrown when the handle is null or invalid.</exception>
+        public Bundle(SafeBundleHandle handle)
         {
-            if (handle != IntPtr.Zero)
+            if (handle == null || handle.IsInvalid)
             {
-                _handle = handle;
-                if (!ownership)
-                    _disposed = true;
-                _keys = new HashSet<string>();
-                Interop.Bundle.Iterator iterator = (string key, int type, IntPtr keyval, IntPtr userData) =>
-                {
-                    _keys.Add(key);
-                };
-
-                Interop.Bundle.Foreach(_handle, iterator, IntPtr.Zero);
-                if ((BundleErrorFactory.BundleError)ErrorFacts.GetLastResult() == BundleErrorFactory.BundleError.InvalidParameter)
-                {
-                    throw new ArgumentException("Invalid parameter - cannot create bundle instance");
-                }
+                throw new ArgumentNullException("handle");
             }
-            else
+
+            _handle = Interop.Bundle.DangerousClone(handle.DangerousGetHandle());
+            _keys = new HashSet<string>();
+            Interop.Bundle.Iterator iterator = (string key, int type, IntPtr keyval, IntPtr userData) =>
             {
-                throw new ArgumentNullException("handle");
+                _keys.Add(key);
+            };
+
+            Interop.Bundle.Foreach(_handle, iterator, IntPtr.Zero);
+            if ((BundleErrorFactory.BundleError)ErrorFacts.GetLastResult() == BundleErrorFactory.BundleError.InvalidParameter)
+            {
+                throw new ArgumentException("Invalid parameter - cannot create bundle instance");
             }
         }
 
@@ -122,12 +123,12 @@ namespace Tizen.Applications
             }
         }
 
-        internal IntPtr Handle
+        /// <summary>
+        /// Gets the SafeBundleHandle instance.
+        /// </summary>
+        public SafeBundleHandle SafeBundleHandle
         {
-            get
-            {
-                return _handle;
-            }
+            get { return _handle; }
         }
 
         /// <summary>
@@ -576,12 +577,6 @@ namespace Tizen.Applications
             }
         }
 
-        static internal Bundle MakeRetainedBundle(IntPtr handle)
-        {
-            IntPtr clonedHandle = Interop.Bundle.Clone(handle);
-            return new Bundle(clonedHandle, true);
-        }
-
         /// <summary>
         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
         /// </summary>
@@ -592,12 +587,8 @@ namespace Tizen.Applications
             {
                 if (disposing)
                 {
-                    // to be used if there are any other disposable objects
-                }
-                if (_handle != IntPtr.Zero)
-                {
-                    Interop.Bundle.Free(_handle);
-                    _handle = IntPtr.Zero;
+                    if (_handle != null && !_handle.IsInvalid)
+                        _handle.Dispose();
                 }
 
                 _disposed = true;
@@ -637,7 +628,7 @@ namespace Tizen.Applications
             KeyExists = -0x01180000 | 0x01
         }
 
-        static internal void CheckAndThrowException(int error, IntPtr handle)
+        static internal void CheckAndThrowException(int error, SafeBundleHandle handle)
         {
             if ((BundleError)error == BundleError.None)
             {
@@ -649,7 +640,7 @@ namespace Tizen.Applications
             }
             else if ((BundleError)error == BundleError.InvalidParameter)
             {
-                if (handle == IntPtr.Zero)
+                if (handle.IsInvalid)
                 {
                     throw new InvalidOperationException("Invalid bundle instance (object may have been disposed or released)");
                 }
index 1740190769933d2d2970658417eb9618d04893a3..58264fa5ebe12cb74d5b425ed7fd60aaaf5e3a38 100644 (file)
@@ -1,3 +1,11 @@
+// Copyright 2016 by Samsung Electronics, Inc.,
+//
+// This software is the confidential and proprietary information
+// of Samsung Electronics, Inc. ("Confidential Information"). You
+// shall not disclose such Confidential Information and shall use
+// it only in accordance with the terms of the license agreement
+// you entered into with Samsung.
+
 using System;
 using System.Runtime.InteropServices;
 
diff --git a/Tizen.Applications/Tizen.Applications/SafeBundleHandle.cs b/Tizen.Applications/Tizen.Applications/SafeBundleHandle.cs
new file mode 100644 (file)
index 0000000..28be52c
--- /dev/null
@@ -0,0 +1,55 @@
+// Copyright 2016 by Samsung Electronics, Inc.,
+//
+// This software is the confidential and proprietary information
+// of Samsung Electronics, Inc. ("Confidential Information"). You
+// shall not disclose such Confidential Information and shall use
+// it only in accordance with the terms of the license agreement
+// you entered into with Samsung.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Tizen.Applications
+{
+    /// <summary>
+    /// Represents a wrapper class for a unmanaged Bundle handle.
+    /// </summary>
+    public sealed class SafeBundleHandle : SafeHandle
+    {
+        /// <summary>
+        /// Initializes a new instance of the SafeBundleHandle class.
+        /// </summary>
+        public SafeBundleHandle() : base(IntPtr.Zero, true)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the SafeBundleHandle class.
+        /// </summary>
+        /// <param name="existingHandle">An IntPtr object that represents the pre-existing handle to use.</param>
+        /// <param name="ownsHandle">true to reliably release the handle during the finalization phase; false to prevent reliable release.</param>
+        public SafeBundleHandle(IntPtr existingHandle, bool ownsHandle) : base(IntPtr.Zero, ownsHandle)
+        {
+            SetHandle(existingHandle);
+        }
+
+        /// <summary>
+        /// Gets a value that indicates whether the handle is invalid.
+        /// </summary>
+        public override bool IsInvalid
+        {
+            get { return this.handle == IntPtr.Zero; }
+        }
+
+        /// <summary>
+        /// When overridden in a derived class, executes the code required to free the handle.
+        /// </summary>
+        /// <returns>true if the handle is released successfully</returns>
+        protected override bool ReleaseHandle()
+        {
+            Interop.Bundle.DangerousFree(this.handle);
+            this.SetHandle(IntPtr.Zero);
+            return true;
+        }
+    }
+}