9cfb19969bbbe85993e1c4a9fa5590f297d9f328
[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 namespace Tizen.NUI
23 {
24     internal class NUIGadgetAssemblyLoadContext : AssemblyLoadContext
25     {
26         public NUIGadgetAssemblyLoadContext() : base(isCollectible: true)
27         {
28         }
29
30         protected override Assembly Load(AssemblyName name)
31         {
32             return null;
33         }
34     }
35
36     internal class NUIGadgetAssembly
37     {
38         private static readonly object _assemblyLock = new object();
39         private readonly string _assemblyPath;
40         private WeakReference _assemblyRef;
41         private Assembly _assembly = null;
42
43         public NUIGadgetAssembly(string assemblyPath) { _assemblyPath = assemblyPath; }
44
45         public void Load()
46         {
47             lock (_assemblyLock)
48             {
49                 if (_assembly != null)
50                 {
51                     return;
52                 }
53
54                 Log.Warn("Load(): " + _assemblyPath + " ++");
55                 NUIGadgetAssemblyLoadContext context = new NUIGadgetAssemblyLoadContext();
56                 _assemblyRef = new WeakReference(context);
57                 using (MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(_assemblyPath)))
58                 {
59                     _assembly = context.LoadFromStream(memoryStream);
60                 }
61                 Log.Warn("Load(): " + _assemblyPath + " --");
62             }
63         }
64
65         public bool IsLoaded { get { return _assembly != null; } }
66
67         public NUIGadget CreateInstance(string className)
68         {
69             lock (_assemblyLock)
70             {
71                 return (NUIGadget)_assembly?.CreateInstance(className);
72             }
73         }
74
75         public void Unload()
76         {
77             lock (_assemblyLock)
78             {
79                 if (_assembly == null)
80                 {
81                     return;
82                 }
83
84                 Log.Warn("Unload(): " + _assemblyPath + " ++");
85                 if (_assemblyRef.IsAlive)
86                 {
87                     (_assemblyRef.Target as NUIGadgetAssemblyLoadContext).Unload();
88                 }
89
90                 _assembly = null;
91                 Log.Warn("Unload(): " + _assemblyPath + " --");
92             }
93         }
94     }
95 }