[Account Manager] added feature not supported exception (#757)
authorabhishekvijay <40332812+abhishekvijay@users.noreply.github.com>
Fri, 15 Mar 2019 09:58:31 +0000 (15:28 +0530)
committerWonYoung Choi <wy80.choi@samsung.com>
Fri, 15 Mar 2019 09:58:31 +0000 (18:58 +0900)
* added feature not supported exception

Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
* improved debug logs

Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
* improved handling of unmanaged resources

Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
src/Tizen.Account.AccountManager/Interop/Interop.Account.cs
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager.csproj
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager.sln
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager/Account.cs
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager/AccountErrorFactory.cs
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager/AccountService.cs
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager/SafeAccountHandle.cs

index 90489bc..256c69d 100644 (file)
@@ -32,6 +32,8 @@ internal static partial class Interop
         [DllImport(Libraries.AccountSvc, EntryPoint = "account_create", CallingConvention = CallingConvention.Cdecl)]
         internal static extern int Create(out SafeAccountHandle handle);
 
+        [DllImport(Libraries.AccountSvc, EntryPoint = "account_create", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern int CreateUnmanagedHandle(out IntPtr handle);
 
         [DllImport(Libraries.AccountSvc, EntryPoint = "account_destroy", CallingConvention = CallingConvention.Cdecl)]
         internal static extern int Destroy(IntPtr handle);
index 7f4584f..5bcad70 100644 (file)
@@ -5,6 +5,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <ProjectReference Include="..\Tizen.System.Information\Tizen.System.Information.csproj" />
     <ProjectReference Include="..\Tizen\Tizen.csproj" />
     <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj" />
   </ItemGroup>
index 4ea5304..6d416b2 100755 (executable)
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen", "..\Tizen\Tizen.csp
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen.Log", "..\Tizen.Log\Tizen.Log.csproj", "{7B75CA54-AA68-425B-86E2-B550D8F9BA4C}"
 EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen.System.Information", "..\Tizen.System.Information\Tizen.System.Information.csproj", "{EBEAD3D4-4335-4BC4-9101-24FAF565E43E}"
+EndProject
 Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
                Debug|Any CPU = Debug|Any CPU
@@ -27,8 +29,15 @@ Global
                {7B75CA54-AA68-425B-86E2-B550D8F9BA4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
                {7B75CA54-AA68-425B-86E2-B550D8F9BA4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
                {7B75CA54-AA68-425B-86E2-B550D8F9BA4C}.Release|Any CPU.Build.0 = Release|Any CPU
+               {EBEAD3D4-4335-4BC4-9101-24FAF565E43E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+               {EBEAD3D4-4335-4BC4-9101-24FAF565E43E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+               {EBEAD3D4-4335-4BC4-9101-24FAF565E43E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+               {EBEAD3D4-4335-4BC4-9101-24FAF565E43E}.Release|Any CPU.Build.0 = Release|Any CPU
        EndGlobalSection
        GlobalSection(SolutionProperties) = preSolution
                HideSolutionNode = FALSE
        EndGlobalSection
+       GlobalSection(ExtensibilityGlobals) = postSolution
+               SolutionGuid = {45E17114-C180-41F6-B625-800EFB07838F}
+       EndGlobalSection
 EndGlobal
index 04af81c..765d698 100644 (file)
@@ -34,6 +34,7 @@ namespace Tizen.Account.AccountManager
        /// <param name="handle"> The account handle.</param>
         public Account(SafeAccountHandle handle)
         {
+            AccountErrorFactory.CheckAccountFeature();
             _handle = handle;
         }
 
@@ -53,6 +54,8 @@ namespace Tizen.Account.AccountManager
         /// <returns>Account Instance.</returns>
         public static Account CreateAccount()
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             SafeAccountHandle handle;
             AccountError err = (AccountError)Interop.Account.Create(out handle);
             if (err != AccountError.None)
@@ -623,7 +626,7 @@ namespace Tizen.Account.AccountManager
         /// <param name="disposing">The boolean value for destoying account handle.</param>
         protected virtual void Dispose(bool disposing)
         {
-            if (!disposing)
+            if (disposing)
             {
                 _handle.Dispose();
             }
index 837c4f4..1df75f7 100644 (file)
@@ -15,7 +15,8 @@
  */
 
 using System;
-using Tizen;
+using System.IO;
+using Tizen.System;
 
 namespace Tizen.Account.AccountManager
 {
@@ -135,59 +136,59 @@ namespace Tizen.Account.AccountManager
             {
                 case AccountError.InvalidParameter:
                     {
-                        exp = new ArgumentException(msg + " Invalid Parameters Provided");
+                        exp = new ArgumentException($"{msg} Invalid Parameters Provided");
                         break;
                     }
 
                 case AccountError.OutOfMemory:
                     {
-                        exp = new OutOfMemoryException(msg + " Out Of Memory");
+                        exp = new OutOfMemoryException($"{msg} Out Of Memory");
                         break;
                     }
 
                 case AccountError.InvalidOperation:
                     {
-                        exp = new InvalidOperationException(msg + " Inavlid operation");
+                        exp = new InvalidOperationException($"{msg} Inavlid operation");
                         break;
                     }
 
                 case AccountError.NoData:
                     {
-                        exp = new InvalidOperationException(msg + " Empty Data");
+                        exp = new InvalidOperationException($"{msg} Empty Data");
                         break;
                     }
 
                 case AccountError.PermissionDenied:
                     {
-                        exp = new UnauthorizedAccessException(msg + " Permission Denied");
+                        exp = new UnauthorizedAccessException($"{msg} Permission Denied");
                         break;
                     }
 
                 case AccountError.DBFailed:
                     {
-                        exp = new InvalidOperationException(msg + " DataBase Failed");
+                        exp = new InvalidOperationException($"{msg} DataBase Failed");
                         break;
                     }
 
                 case AccountError.DBBusy:
                     {
-                        exp = new InvalidOperationException(msg + " DataBase Busy");
+                        exp = new InvalidOperationException($"{msg} DataBase Busy");
                         break;
                     }
 
                 case AccountError.QuerySyntaxError:
                     {
-                        exp = new InvalidOperationException(msg + " Network Error");
+                        exp = new InvalidOperationException($"{msg} Network Error");
                         break;
                     }
                 case AccountError.XMLFileNotFound:
                     {
-                        exp = new System.IO.FileNotFoundException(msg + " XML File not found");
+                        exp = new FileNotFoundException($"{msg} XML File not found");
                         break;
                     }
                 case AccountError.XMLParseFailed:
                     {
-                        exp = new System.IO.InvalidDataException(msg + " XML parse error");
+                        exp = new InvalidDataException($"{msg} XML parse error");
                         break;
                     }
 
@@ -200,5 +201,19 @@ namespace Tizen.Account.AccountManager
 
             return exp;
         }
+
+        internal static bool IsAccountFeatureSupported()
+        {
+            return ((Information.TryGetValue("http://tizen.org/feature/account", out bool IsAccountSupported)) && IsAccountSupported);
+        }
+
+        internal static void CheckAccountFeature()
+        {
+            if (IsAccountFeatureSupported() == false)
+            {
+                Log.Warn(AccountErrorFactory.LogTag, "platform account feature is disabled");
+                throw new NotSupportedException("platform account feature is disabled");
+            }
+        }
     }
 }
index 88b0428..fac0f29 100644 (file)
@@ -102,11 +102,13 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<Account> GetAccountsAsync()
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<Account> accounts = new List<Account>();
             List<int> values = new List<int>();
             Interop.Account.AccountCallback accountCallback = (IntPtr data, IntPtr userdata) =>
             {
-                Account account = new Account(new SafeAccountHandle(data, true));
+                Account account = new Account(new SafeAccountHandle(data, false));
                 values.Add(account.AccountId);
                 account.Dispose();
                 return true;
@@ -141,16 +143,25 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static Account GetAccountById(int accountId)
         {
-            Account account = Account.CreateAccount();
-            SafeAccountHandle handle = account.SafeAccountHandle;
+            AccountErrorFactory.CheckAccountFeature();
 
-            AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref handle);
+            AccountError err = (AccountError)Interop.Account.CreateUnmanagedHandle(out IntPtr handle);
+            if (err != AccountError.None)
+            {
+                Log.Warn(AccountErrorFactory.LogTag, "Failed to create handle");
+                throw AccountErrorFactory.CreateException(err, "Failed to create unmanaged handle");
+            }
+
+            SafeAccountHandle accountHandle = new SafeAccountHandle(handle, false);
+
+            AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref accountHandle);
             if (res != AccountError.None)
             {
                 throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
             }
-            Account ref_account = new Account(handle);
-                       
+
+            Account ref_account = new Account(accountHandle);
+
             return ref_account;
         }
 
@@ -166,6 +177,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<AccountProvider> GetAccountProviders()
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<string> values = new List<string>();
             List<AccountProvider> providers = new List<AccountProvider>();
             Interop.AccountProvider.AccountProviderCallback accountCallback = (IntPtr handle, IntPtr data) =>
@@ -206,6 +219,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static AccountProvider GetAccountProviderByAppId(string appId)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             IntPtr handle;
             Interop.AccountProvider.Create(out handle);
             AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByAppId(appId, out handle);
@@ -232,6 +247,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<AccountProvider> GetAccountProvidersByFeature(string feature)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<string> values = new List<string>();
             List<AccountProvider> providers = new List<AccountProvider>();
             Interop.AccountProvider.AccountProviderCallback providerCallback = (IntPtr handle, IntPtr data) =>
@@ -273,6 +290,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static int AddAccount(Account account)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             if (account == null)
             {
                 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
@@ -303,6 +322,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static void UpdateAccount(Account account)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             if (account == null)
             {
                 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
@@ -329,6 +350,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static void DeleteAccount(Account account)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             if (account == null)
             {
                 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
@@ -356,6 +379,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static void DeleteAccount(string userName, string packageName)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
             if (err != AccountError.None)
             {
@@ -377,6 +402,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static void DeleteAccount(string packageName)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
             if (err != AccountError.None)
             {
@@ -399,11 +426,13 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<Account> GetAccountsByUserName(string userName)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<Account> accounts = new List<Account>();
             List<int> values = new List<int>();
             Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
             {
-                Account account = new Account(new SafeAccountHandle(handle, true));
+                Account account = new Account(new SafeAccountHandle(handle, false));
                 values.Add(account.AccountId);
                 account.Dispose();
                 return true;
@@ -438,11 +467,13 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<Account> GetAccountsByPackageName(string packageName)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<Account> accounts = new List<Account>();
             List<int> values = new List<int>();
             Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
             {
-                Account account = new Account(new SafeAccountHandle(handle, true));
+                Account account = new Account(new SafeAccountHandle(handle, false));
                 values.Add(account.AccountId);
                 account.Dispose();
                 return true;
@@ -477,11 +508,13 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static IEnumerable<Account> GetAccountsByCapabilityType(string type)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             List<Account> accounts = new List<Account>();
             List<int> values = new List<int>();
             Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
             {
-                Account account = new Account(new SafeAccountHandle(handle, true));
+                Account account = new Account(new SafeAccountHandle(handle, false));
                 values.Add(account.AccountId);
                 account.Dispose();
                 return true;
@@ -516,6 +549,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static Dictionary<string, CapabilityState> GetCapabilitiesById(int accountId)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             Dictionary<string, CapabilityState> capabilities = new Dictionary<string, CapabilityState>();
             Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
             {
@@ -544,6 +579,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static int GetAccountsCount()
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             int count = 0;
             AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
             if (err != AccountError.None)
@@ -569,6 +606,8 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static void UpdateSyncStatusById(Account account, AccountSyncState status)
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
             if (err != AccountError.None)
             {
@@ -578,6 +617,8 @@ namespace Tizen.Account.AccountManager
 
         private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
         {
+            AccountErrorFactory.CheckAccountFeature();
+
             AccountSubscriberEventArgs eventArgs = new AccountSubscriberEventArgs(eventType, accountId);
             s_accountUpdated?.Invoke(null, eventArgs);
             return true;
@@ -603,6 +644,8 @@ namespace Tizen.Account.AccountManager
         {
             add
             {
+                AccountErrorFactory.CheckAccountFeature();
+
                 if (s_accountUpdated == null)
                 {
                     if (s_subscriberHandle == null)
@@ -623,6 +666,8 @@ namespace Tizen.Account.AccountManager
 
             remove
             {
+                AccountErrorFactory.CheckAccountFeature();
+
                 s_accountUpdated -= value;
                 if (s_accountUpdated == null)
                 {
index abdf95e..159d04f 100644 (file)
@@ -60,6 +60,7 @@ namespace Tizen.Account.AccountManager
         /// <returns>true if the handle is released successfully</returns>
         protected override bool ReleaseHandle()
         {
+            Interop.Account.Destroy(this.handle);
             this.SetHandle(IntPtr.Zero);
             return true;
         }