/// <summary> /// Process a display request. For example the calling JS would look like: Authority.request(handlername, somedata). /// </summary> /// <param name="pDisplay">The display which called this api function.</param> /// <param name="pSurface">The surface which this display is hosted on.</param> /// <param name="sRequestHandler">The name of the request handler.</param> /// <param name="dArguments">The table of arguments which were given in the data parameter.</param> /// <returns>True if the request was sucessfully handled. False if not.</returns> public static bool ProcessRequest(Display pDisplay, Surface pSurface, String sRequestHandler, Awesomium.Core.JSObject dArguments) { // Check the display and surface are valid. if (pDisplay == null) { throw new ArgumentNullException("Cannot process a display API request without a display."); } if (pSurface == null) { throw new ArgumentNullException("Cannot process a display API request without a surface."); } // Check we have a valid request handler. if (sRequestHandler == null || sRequestHandler.Length == 0) { Log.Write("Cannot process a display API request without a handler name.", pDisplay.ToString(), Log.Type.DisplayWarning); return(false); } // Make the request handler lower case. sRequestHandler = sRequestHandler.ToLower(); // Search the bound request handlers. DisplayAPI.IRequest pHandler = null; if (dRequestHandlers.TryGetValue(sRequestHandler, out pHandler)) { // If one is found, process the request and return the success condition. return(pHandler.ProcessRequest(pDisplay, pSurface, dArguments)); } // No handler for request. Log.Write("Authority could not find handler for request '" + sRequestHandler + "'.", pDisplay.ToString(), Log.Type.DisplayWarning); return(false); }
/// <summary> /// Register a handler name with a new handler instance. /// </summary> /// <param name="sHandlerName">The name of the request handler we want the API to use as a look up.</param> /// <param name="kHandlerInstance">The instance responsible for handling all these requests.</param> /// <returns>The normalised name of the handler. This is practically just a lower case version of sHandlerName.</returns> /// <remarks>This is threadsafe.</remarks> public static String RegisterRequestHandler(String sHandlerName, DisplayAPI.IRequest kHandlerInstance) { // Check we have valid data. if (sHandlerName == null || sHandlerName == "") { throw new ArgumentNullException("Invalid handler name."); } // Transform the handler into lower case for the lookups. sHandlerName = sHandlerName.ToLower(); // Add us to the table of registered items. mHandlerLock.WaitOne(); dRequestHandlers[sHandlerName] = kHandlerInstance; mHandlerLock.ReleaseMutex(); // Return the name. return(sHandlerName); }