2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Threading.Tasks;
21 using Tizen.Applications;
22 using Tizen.Applications.Exceptions;
27 /// Represents the Frame Broker.
29 internal abstract class FrameBrokerBase : IDisposable
31 private string LogTag = "NUI";
32 private readonly SafeFrameBrokerHandle _handle;
33 private Dictionary<int, Interop.FrameBroker.AppControlResultCallback> _resultCallbackMaps = new Dictionary<int, Interop.FrameBroker.AppControlResultCallback>();
34 private int _resultId = 0;
35 private Interop.FrameBroker.FrameContextLifecycleCallbacks _callbacks;
36 private IntPtr _context = IntPtr.Zero;
37 private bool _disposed = false;
40 /// Initializes the FrameBroker class.
42 /// <param name="window">The window instance of Ecore_Wl2_Window pointer.</param>
43 /// <exception cref="ArgumentException">Thrown when failed because of an invalid parameter.</exception>
44 /// <exception cref="OutOfMemoryException">Thrown when the memory is insufficient.</exception>
45 /// <exception cref="InvalidOperationException">Thrown when failed to create the frame broker handle.</exception>
46 /// <remarks>This class is only avaliable for platform level signed applications.</remarks>
47 internal FrameBrokerBase(Window window)
49 Interop.FrameBroker.ErrorCode err;
53 throw FrameBrokerBaseErrorFactory.GetException(Interop.FrameBroker.ErrorCode.InvalidParameter, "Invalid parameter");
56 _callbacks.OnCreate = new Interop.FrameBroker.FrameContextCreateCallback(OnCreateNative);
57 _callbacks.OnResume = new Interop.FrameBroker.FrameContextResumeCallback(OnResumeNavie);
58 _callbacks.OnPause = new Interop.FrameBroker.FrameContextPauseCallback(OnPauseNative);
59 _callbacks.OnDestroy = new Interop.FrameBroker.FrameContextDestroyCallback(OnDestroyNative);
60 _callbacks.OnError = new Interop.FrameBroker.FrameContextErrorCallback(OnErrorNative);
61 _callbacks.OnUpdate = new Interop.FrameBroker.FrameContextUpdateCallback(OnUpdateNative);
63 err = Interop.FrameBroker.Create(window.GetNativeWindowHandler(), ref _callbacks, IntPtr.Zero, out _handle);
64 if (err != Interop.FrameBroker.ErrorCode.None)
66 throw FrameBrokerBaseErrorFactory.GetException(err, "Failed to create frame broker handle");
71 /// Sends the launch request asynchronously.
74 /// The operation is mandatory information for the launch request.
75 /// If the operation is not specified, AppControlOperations.Default is used by default.
76 /// If the operation is AppControlOperations.Default, the application ID is mandatory to explicitly launch the application.<br/>
77 /// Since Tizen 2.4, the launch request of the service application over out of packages is restricted by the platform.
78 /// Also, implicit launch requests are NOT delivered to service applications since 2.4.
79 /// To launch a service application, an explicit launch request with the application ID given by property ApplicationId MUST be sent.
81 /// <param name="appControl">The AppControl.</param>
82 /// <param name="toProvider"> The flag, if it's true, the launch request is sent to the frame provider application.</param>
83 /// <returns>A task with the result of the launch request.</returns>
84 /// <exception cref="ArgumentException">Thrown when failed because of the argument is invalid.</exception>
85 /// <exception cref="AppNotFoundException">Thrown when the application to run is not found.</exception>
86 /// <exception cref="LaunchRejectedException">Thrown when the launch request is rejected.</exception>
87 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
88 internal Task<FrameBrokerBaseResult> SendLaunchRequest(AppControl appControl, bool toProvider)
90 if (appControl == null)
92 throw FrameBrokerBaseErrorFactory.GetException(Interop.FrameBroker.ErrorCode.InvalidParameter, "Invalid parameter");
95 var task = new TaskCompletionSource<FrameBrokerBaseResult>();
98 lock (_resultCallbackMaps)
100 requestId = _resultId++;
101 _resultCallbackMaps[requestId] = (handle, result, userData) =>
103 task.SetResult((FrameBrokerBaseResult)result);
104 lock (_resultCallbackMaps)
106 _resultCallbackMaps.Remove((int)userData);
111 Interop.FrameBroker.ErrorCode err;
113 err = Interop.FrameBroker.SendLaunchRequestToProvider(_handle, appControl.SafeAppControlHandle, _resultCallbackMaps[requestId], null, (IntPtr)requestId);
115 err = Interop.FrameBroker.SendLaunchRequest(_handle, appControl.SafeAppControlHandle, _resultCallbackMaps[requestId], null, (IntPtr)requestId);
117 if (err != Interop.FrameBroker.ErrorCode.None)
119 throw FrameBrokerBaseErrorFactory.GetException(err, "Failed to send launch request");
126 /// Notifies that the animation is started.
128 /// <exception cref="ArgumentException">Thrown when failed because of the argument is invalid.</exception>
129 /// <exception cref="InvalidOperationException">Thrown when failed because of system error.</exception>
130 internal void StartAnimation()
132 Interop.FrameBroker.ErrorCode err = Interop.FrameBroker.StartAnimation(_context);
133 if (err != Interop.FrameBroker.ErrorCode.None)
135 throw FrameBrokerBaseErrorFactory.GetException(err, "Failed to notify that the animation is started");
140 /// Notifies that the animation is finished.
142 /// <exception cref="ArgumentException">Thrown when failed because of the argument is invalid.</exception>
143 /// <exception cref="InvalidOperationException">Thrown when failed because of system error.</exception>
144 internal void FinishAnimation()
146 Interop.FrameBroker.ErrorCode err = Interop.FrameBroker.FinishAnimation(_context);
147 if (err != Interop.FrameBroker.ErrorCode.None)
149 throw FrameBrokerBaseErrorFactory.GetException(err, "Failed to notify that the animation is finished");
154 /// Occurs whenever the frame is created.
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 protected virtual void OnFrameCreated()
159 Log.Warn(LogTag, "The OnFrameCreated() is not implemented");
163 /// Occurs Whenever the frame is resumed.
165 /// <param name="frame">The frame data.</param>
167 /// When the frame has been prepared, this function is called.
168 /// The caller can start animations, To notify that the animation is started, the caller should call StartAnimation().
169 /// After the animation is finished, the caller should call FinishAnimation() to notify.
171 [EditorBrowsable(EditorBrowsableState.Never)]
172 protected virtual void OnFrameResumed(FrameData frame)
174 Log.Warn(LogTag, "The OnFrameResumed() is not implemented");
178 /// Occurs Whenever the frame is updated.
180 /// <param name="frame">The frame data.</param>
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected virtual void OnFrameUpdated(FrameData frame)
184 Log.Warn(LogTag, "The OnFrameUpdated() is not implemented");
188 /// Occurs Whenever the frame is paused.
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 protected virtual void OnFramePaused()
193 Log.Warn(LogTag, "The OnFramePaused() is not implemented");
197 /// Occurs Whenever the frame is destroyed.
199 [EditorBrowsable(EditorBrowsableState.Never)]
200 protected virtual void OnFrameDestroyed()
202 Log.Warn(LogTag, "The OnFrameDestroyed() is not implemented");
206 /// Occurs Whenever the system error is occurred.
208 /// <param name="error">The frame error.</param>
209 [EditorBrowsable(EditorBrowsableState.Never)]
210 protected virtual void OnFrameErred(FrameError error)
212 Log.Warn(LogTag, "The OnFrameErred() is not implemented");
215 private void OnCreateNative(IntPtr context, IntPtr userData)
221 private void OnResumeNavie(IntPtr context, IntPtr frame, IntPtr userData)
223 OnFrameResumed(new FrameData(frame));
226 private void OnPauseNative(IntPtr context, IntPtr userData)
231 private void OnDestroyNative(IntPtr context, IntPtr userData)
233 _context = IntPtr.Zero;
237 private void OnErrorNative(IntPtr context, int error, IntPtr userData)
239 _context = IntPtr.Zero;
240 OnFrameErred((FrameError)error);
243 private void OnUpdateNative(IntPtr context, IntPtr frame, IntPtr userData)
245 OnFrameUpdated(new FrameData(frame));
248 [EditorBrowsable(EditorBrowsableState.Never)]
249 protected virtual void Dispose(bool disposing)
258 [EditorBrowsable(EditorBrowsableState.Never)]
259 public void Dispose()