Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Registry.cs
1 /*
2  * Copyright(c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Collections.Concurrent;
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// This is used to store a mapping between C++ base handle objects and it's C# instances.
28     ///
29     /// </summary>
30     internal sealed class Registry
31     {
32         private static readonly Registry registry = new Registry();
33
34         /// <summary>
35         /// static initialization singleton
36         /// </summary>
37         internal static Registry Instance
38         {
39             get { return registry; }
40         }
41
42         /// <summary>
43         /// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
44         /// By keeping the weak reference only, it will allow the object to be garbage collected.
45         /// </summary>
46         private ConcurrentDictionary<IntPtr, WeakReference> _controlMap;
47
48         private Registry()
49         {
50             _controlMap = new ConcurrentDictionary<IntPtr, WeakReference>();
51         }
52
53
54         /// <summary>
55         /// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
56         /// </summary>
57         /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
58         internal static void Register(BaseHandle baseHandle)
59         {
60             // We store a pointer to the RefObject for the control
61             IntPtr refCptr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
62
63             RegistryCurrentThreadCheck();
64
65             if (Instance._controlMap.TryAdd(refCptr, new WeakReference(baseHandle, false)) != true)
66             {
67                 NUILog.Debug("refCptr is already exist! OR something wrong!");
68             }
69
70             return;
71         }
72
73         /// <summary>
74         /// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
75         /// </summary>
76         /// <param name="baseHandle"> The instance of BaseHandle (C# base class)</param>
77         internal static void Unregister(BaseHandle baseHandle)
78         {
79             IntPtr refCptr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
80
81             RegistryCurrentThreadCheck();
82             WeakReference refe;
83             if (Instance._controlMap.TryRemove(refCptr, out refe) != true)
84             {
85                 NUILog.Debug("something wrong when removing refCptr!");
86             }
87
88             return;
89         }
90
91         internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
92         {
93             IntPtr refObjectPtr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
94
95             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
96             return GetManagedBaseHandleFromRefObject(refObjectPtr);
97         }
98
99         internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
100         {
101             IntPtr refObjectPtr = Interop.RefObject.GetRefObjectPtr(cPtr);
102
103             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
104             return GetManagedBaseHandleFromRefObject(refObjectPtr);
105         }
106
107         internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
108         {
109             if (refObjectPtr == global::System.IntPtr.Zero)
110             {
111                 NUILog.Debug("Registry refObjectPtr is NULL! This means bind native object is NULL!");
112                 //return null;
113             }
114             else
115             {
116                 NUILog.Debug($"refObjectPtr=0x{refObjectPtr.ToInt64():X}");
117             }
118
119             RegistryCurrentThreadCheck();
120
121             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
122             WeakReference weakReference;
123
124             if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
125             {
126                 if (weakReference == null)
127                 {
128                     throw new System.InvalidOperationException("Error! NUI Registry weakReference should not be NULL!");
129                 }
130
131                 BaseHandle ret = weakReference.Target as BaseHandle;
132                 return ret;
133             }
134             else
135             {
136                 return null;
137             }
138         }
139
140         private static Thread savedApplicationThread;
141         internal Thread SavedApplicationThread
142         {
143             get
144             {
145                 return savedApplicationThread;
146             }
147             set
148             {
149                 savedApplicationThread = value;
150             }
151         }
152
153         private static void RegistryCurrentThreadCheck()
154         {
155
156             if (savedApplicationThread == null)
157             {
158                 Tizen.Log.Fatal("NUI", $"Error! maybe main thread is created by other process\n");
159                 return;
160             }
161             int currentId = Thread.CurrentThread.ManagedThreadId;
162             int mainThreadId = savedApplicationThread.ManagedThreadId;
163
164             if (currentId != mainThreadId)
165             {
166                 Tizen.Log.Fatal("NUI", $"Error! current thread({currentId}) which is NOT main thread({mainThreadId}) utilizes NUI object!");
167             }
168         }
169     }
170 }