b5dd7e0640aaca4ad4c1ee6f3bbaf7fa98507749
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Gadget / Tizen.NUI / NUIGadgetAssembly.cs
1 /*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.IO;
19 using System.Reflection;
20 using System.Runtime.Loader;
21
22 using SystemIO = System.IO;
23
24 namespace Tizen.NUI
25 {
26     internal class NUIGadgetAssemblyLoadContext : AssemblyLoadContext
27     {
28         public NUIGadgetAssemblyLoadContext() : base(isCollectible: true)
29         {
30         }
31
32         protected override Assembly Load(AssemblyName name)
33         {
34             return null;
35         }
36     }
37
38     internal class NUIGadgetAssembly
39     {
40         private static readonly object _assemblyLock = new object();
41         private readonly string _assemblyPath;
42         private WeakReference _assemblyRef;
43         private Assembly _assembly = null;
44
45         public NUIGadgetAssembly(string assemblyPath) { _assemblyPath = assemblyPath; }
46
47         public void Load()
48         {
49             lock (_assemblyLock)
50             {
51                 if (_assembly != null)
52                 {
53                     return;
54                 }
55
56                 Log.Warn("Load(): " + _assemblyPath + " ++");
57                 NUIGadgetAssemblyLoadContext context = new NUIGadgetAssemblyLoadContext();
58                 _assemblyRef = new WeakReference(context);
59                 string directoryPath = SystemIO.Path.GetDirectoryName(_assemblyPath);
60                 string fileName = SystemIO.Path.GetFileNameWithoutExtension(_assemblyPath);
61                 string nativeImagePath = directoryPath + "/" + fileName + ".ni.dll";
62                 Log.Debug("NativeImagePath=" + nativeImagePath + ", AssemblyPath=" + _assemblyPath);
63                 _assembly = context.LoadFromNativeImagePath(nativeImagePath, _assemblyPath);
64                 Log.Warn("Load(): " + _assemblyPath + " --");
65             }
66         }
67
68         public bool IsLoaded { get { return _assembly != null; } }
69
70         public NUIGadget CreateInstance(string className)
71         {
72             lock (_assemblyLock)
73             {
74                 return (NUIGadget)_assembly?.CreateInstance(className);
75             }
76         }
77
78         public void Unload()
79         {
80             lock (_assemblyLock)
81             {
82                 if (_assembly == null)
83                 {
84                     return;
85                 }
86
87                 Log.Warn("Unload(): " + _assemblyPath + " ++");
88                 if (_assemblyRef.IsAlive)
89                 {
90                     (_assemblyRef.Target as NUIGadgetAssemblyLoadContext).Unload();
91                 }
92
93                 _assembly = null;
94                 Log.Warn("Unload(): " + _assemblyPath + " --");
95             }
96         }
97     }
98 }