[MediaPlayer] Added ErrorHandler registration methods for internal use. (#33)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / Player.ErrorHandler.cs
1 /*
2  * Copyright (c) 2016 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.Collections.Generic;
19 using System.ComponentModel;
20
21 namespace Tizen.Multimedia
22 {
23     public partial class Player
24     {
25         private static List<Action<Player, int, string>> _errorHandlers;
26
27         private static object _errorHandlerLock = new object();
28
29         /// <summary>
30         /// This method supports the product infrastructure and is not intended to be used directly from application code.
31         /// </summary>
32         /// <since_tizen> 4 </since_tizen>
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         protected static void AddErrorHandler(Action<Player, int, string> errorHandler)
35         {
36             if (errorHandler == null)
37             {
38                 throw new ArgumentNullException(nameof(errorHandler));
39             }
40
41             lock (_errorHandlerLock)
42             {
43                 if (_errorHandlers == null)
44                 {
45                     _errorHandlers = new List<Action<Player, int, string>>();
46                 }
47
48                 _errorHandlers.Add(errorHandler);
49             }
50         }
51
52         /// <summary>
53         /// This method supports the product infrastructure and is not intended to be used directly from application code.
54         /// </summary>
55         /// <since_tizen> 4 </since_tizen>
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         protected static void RemoveErrorHandler(Action<Player, int, string> errorHandler)
58         {
59             lock (_errorHandlerLock)
60             {
61                 _errorHandlers?.Remove(errorHandler);
62             }
63         }
64
65         internal static void NotifyError(Player player, int errorCode, string message)
66         {
67             if (_errorHandlers == null)
68             {
69                 return;
70             }
71
72             lock (_errorHandlerLock)
73             {
74                 foreach (var handler in _errorHandlers)
75                 {
76                     handler(player, errorCode, message);
77                 }
78             }
79         }
80     }
81 }