97eeeeb086d137a9b39153b7dafe3c9ada8c8b60
[platform/core/dotnet/launcher.git] / Managed / Tizen.Init / Tizen.Init.cs
1 /*
2  * Copyright (c) 2019 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.Reflection;
19 using System.Runtime.Loader;
20 using System.IO;
21 using System.Collections.Generic;
22
23 namespace Tizen.Init {
24
25     class TypeLoader
26     {
27         // key : assembly name, value : list of types
28         static IDictionary<string, List<string>> assem_type = new Dictionary<string, List<string>>
29         {
30             {"ElmSharp", new List<string>(){
31                 "ElmSharp.Box",
32                 "ElmSharp.Button",
33                 "ElmSharp.Check",
34                 "ElmSharp.Color",
35                 "ElmSharp.Conformant",
36                 "ElmSharp.DateTimeSelector",
37                 "ElmSharp.DisplayRotation",
38                 "ElmSharp.EdjeObject",
39                 "ElmSharp.EdjeObject+SignalData",
40                 "ElmSharp.EdjeTextPartObject",
41                 "ElmSharp.Elementary",
42                 "ElmSharp.Entry",
43                 "ElmSharp.EvasKeyEventArgs",
44                 "ElmSharp.EvasMap",
45                 "ElmSharp.EvasObject",
46                 "ElmSharp.FocusDirection",
47                 "ElmSharp.GenList",
48                 "ElmSharp.GestureLayer+GestureType",
49                 "ElmSharp.IInvalidatable",
50                 "ElmSharp.Image",
51                 "ElmSharp.ItemObject+SignalData",
52                 "ElmSharp.Label",
53                 "ElmSharp.Layout",
54                 "ElmSharp.Naviframe",
55                 "ElmSharp.NaviItem",
56                 "ElmSharp.Point3D",
57                 "ElmSharp.ProgressBar",
58                 "ElmSharp.Radio",
59                 "ElmSharp.Rect",
60                 "ElmSharp.Rectangle",
61                 "ElmSharp.Scroller",
62                 "ElmSharp.Size",
63                 "ElmSharp.Slider",
64                 "ElmSharp.Spinner",
65                 "ElmSharp.Widget",
66                 "ElmSharp.Window",
67                 "ElmSharp.WrapType",
68                 "Interop"}},
69             {"ElmSharp.Wearable", new List<string>(){
70                 "ElmSharp.Wearable.CircleDateTimeSelector",
71                 "ElmSharp.Wearable.CircleGenList",
72                 "ElmSharp.Wearable.CircleScroller",
73                 "ElmSharp.Wearable.CircleSpinner",
74                 "ElmSharp.Wearable.ICircleWidget",
75                 "ElmSharp.Wearable.IRotaryActionWidget",
76                 "Interop"}},
77             {"System.IO.FileSystem", new List<string>(){
78                 "Interop",
79                 "<PrivateImplementationDetails>",
80                 "System.IO.File"}},
81             {"System.Security.Cryptography.Algorithms", new List<string>(){
82                 "Interop",
83                 "System.Security.Cryptography.MD5"}},
84             {"System.Collections", new List<string>(){
85                 "<PrivateImplementationDetails>",
86                 "System.Collections.Generic.HashSet`1",
87                 "System.Collections.Generic.Queue`1"}},
88             {"System.Private.Uri", new List<string>(){
89                 "<PrivateImplementationDetails>",
90                 "System.Uri"}},
91             {"System.Private.Xml", new List<string>(){
92                 "<PrivateImplementationDetails>",
93                 "System.Xml.IXmlLineInfo",
94                 "System.Xml.IXmlNamespaceResolver"}},
95             {"System.Runtime", new List<string>(){
96                 "System.Collections.Generic.ISet`1",
97                 "System.Reflection.RuntimeReflectionExtensions"}},
98             {"System.ObjectModel", new List<string>(){
99                 "System.Collections.ObjectModel.ObservableCollection`1",
100                 "System.Collections.Specialized.INotifyCollectionChanged",
101                 "System.Collections.Specialized.NotifyCollectionChangedAction",
102                 "System.Collections.Specialized.NotifyCollectionChangedEventArgs",
103                 "System.Collections.Specialized.NotifyCollectionChangedEventHandler",
104                 "System.ComponentModel.INotifyPropertyChanged",
105                 "System.ComponentModel.PropertyChangedEventArgs",
106                 "System.ComponentModel.PropertyChangedEventHandler",
107                 "System.Windows.Input.ICommand"}},
108             {"System.ComponentModel", new List<string>(){
109                 "System.IServiceProvider"}},
110             {"System.Linq", new List<string>(){
111                 "System.Linq.Enumerable",
112                 "System.Linq.EnumerableSorter`1",
113                 "System.Linq.IIListProvider`1",
114                 "System.Linq.IOrderedEnumerable`1",
115                 "System.Linq.IPartition`1",
116                 "System.Linq.OrderedEnumerable`1"}},
117             {"System.Linq.Expressions", new List<string>(){
118                 "System.Linq.Expressions.ExpressionVisitor"}},
119             {"System.Security.Cryptography.Primitives", new List<string>(){
120                 "System.Security.Cryptography.HashAlgorithm"}},
121             {"Tizen.Applications.Common", new List<string>(){
122                 "Interop",
123                 "Tizen.Applications.Application",
124                 "Tizen.Applications.CoreApplication",
125                 "Tizen.Applications.CoreBackend.EventType",
126                 "Tizen.Applications.TizenSynchronizationContext"}},
127             {"Tizen.Applications.UI", new List<string>(){
128                 "Tizen.Applications.CoreUIApplication",
129                 "Interop"}},
130             {"Tizen", new List<string>(){
131                 "Interop"}},
132             {"Tizen.Log", new List<string>(){
133                 "Interop",
134                 "Tizen.Log"}},
135             {"Tizen.System.Information", new List<string>(){
136                 "Tizen.System.Information"}}
137         };
138
139         static void PreloadEnd()
140         {
141             assem_type = null;
142             GC.Collect();
143             GC.WaitForPendingFinalizers();
144         }
145
146         static void PreloadTypes()
147         {
148             foreach (KeyValuePair<string, List<string>> item in assem_type)
149             {
150                 Assembly asm = LoadAssembly(item.Key);
151                 if (asm != null) {
152                     foreach (string t in item.Value)
153                     {
154                         LoadType(asm, t);
155                     }
156                 }
157             }
158
159             PreloadLibICU();
160
161             PreloadEnd();
162         }
163
164         static Assembly LoadAssembly(string name)
165         {
166             var context = AssemblyLoadContext.Default;
167             Assembly ret = null;
168
169             try
170             {
171                 ret = context.LoadFromAssemblyName(new AssemblyName(name));
172             }
173             catch (Exception e)
174             {
175                 Console.WriteLine(e.ToString());
176                 return null;
177             }
178
179             return ret;
180         }
181
182         static void LoadType(Assembly assem, string typeName)
183         {
184             try
185             {
186                 var tp = assem.GetType(typeName);
187             }
188             catch
189             {
190                 Console.WriteLine("Tizen.Init.TypeLoader.PreloadTypes can't load type " + typeName + " from assembly " + assem.ToString());
191             }
192         }
193
194         // CU loading makes launching time much slower.
195         // To prevent that kind of overhead, preload ICU by calling String.ToLower()
196         static void PreloadLibICU()
197         {
198             // to preinitialize icu, do string compare
199             if ("A".ToLower() != "a") {
200                 Console.WriteLine("Failed to String.ToLower()");
201             }
202         }
203     }
204 }