using System;
using System.Runtime.InteropServices;
+using Tizen.Applications;
+
internal static partial class Interop
{
internal static partial class Bundle
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)
{
using System;
using System.Runtime.InteropServices;
+using Tizen.Applications;
+
internal static partial class Interop
{
internal static partial class MessagePort
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);
{
MessageReceivedEventArgs args = new MessageReceivedEventArgs()
{
- Message = Bundle.MakeRetainedBundle(message)
+ Message = new Bundle(new SafeBundleHandle(message, false))
};
if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
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)
{
<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" />
/// <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)
{
/// </summary>
public class Bundle : IDisposable
{
- private IntPtr _handle;
+ private SafeBundleHandle _handle;
private bool _disposed = false;
private readonly HashSet<string> _keys;
_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");
}
}
}
}
- internal IntPtr Handle
+ /// <summary>
+ /// Gets the SafeBundleHandle instance.
+ /// </summary>
+ public SafeBundleHandle SafeBundleHandle
{
- get
- {
- return _handle;
- }
+ get { return _handle; }
}
/// <summary>
}
}
- 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>
{
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;
KeyExists = -0x01180000 | 0x01
}
- static internal void CheckAndThrowException(int error, IntPtr handle)
+ static internal void CheckAndThrowException(int error, SafeBundleHandle handle)
{
if ((BundleError)error == BundleError.None)
{
}
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)");
}
+// 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;
--- /dev/null
+// 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;
+ }
+ }
+}