1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
11 using System.Text.RegularExpressions;
13 namespace Tizen.Applications
18 [AttributeUsage(AttributeTargets.Class)]
19 public class AppControlFilter : Attribute
21 private readonly string _operation;
22 private readonly string _mime;
23 private readonly string _uri;
28 /// <param name="operation"></param>
29 /// <param name="mime"></param>
30 /// <param name="uri"></param>
31 public AppControlFilter(string operation, string mime = null, string uri = null)
33 _operation = operation;
41 public string Operation { get { return _operation; } }
46 public string Mime { get { return _mime; } }
51 public string Uri { get { return _uri; } }
56 /// <param name="obj"></param>
57 /// <returns></returns>
58 public override bool Equals(object obj)
60 AppControlFilter f = obj as AppControlFilter;
61 if (f == null) return false;
63 return (_operation == f._operation) & (_mime == f._mime) & (_uri == f._uri);
69 /// <returns></returns>
70 public override int GetHashCode()
73 if (_operation != null)
75 hash ^= _operation.GetHashCode();
79 hash ^= _mime.GetHashCode();
83 hash ^= _uri.GetHashCode();
88 internal bool IsMatch(AppControl e)
91 if (String.IsNullOrEmpty(mime) && !String.IsNullOrEmpty(e.Uri))
93 mime = Interop.Aul.GetMimeFromUri(e.Uri);
95 return _operation == e.Operation && IsMimeMatched(mime) && IsUriMatched(e.Uri);
98 private bool IsMimeMatched(string mime)
100 if (_mime == "*" || _mime == "*/*")
104 if (String.IsNullOrEmpty(_mime))
106 return String.IsNullOrEmpty(mime);
108 Regex pat = new Regex(_mime.Replace("*", ".*"));
109 return pat.IsMatch(mime);
112 private bool IsUriMatched(string uri)
118 if (String.IsNullOrEmpty(_uri))
120 return String.IsNullOrEmpty(uri);
122 string schema = _uri.Split(':')[0];
123 if (schema == _uri || _uri.EndsWith("://") || _uri.EndsWith("://*"))
125 return schema == uri.Split(':')[0];
127 if (_uri.EndsWith("*"))
129 return uri.StartsWith(_uri.Substring(0, _uri.Length - 1));