1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "JSONRPCUtils.h"
12 #include "JSONServiceDescription.h"
13 
14 #include <iostream>
15 #include <map>
16 #include <stdio.h>
17 #include <string>
18 
19 class CVariant;
20 
21 namespace JSONRPC
22 {
23   /*!
24    \ingroup jsonrpc
25    \brief JSON RPC handler
26 
27    Sets up and manages all needed information to process
28    JSON-RPC requests and answering with the appropriate
29    JSON-RPC response (actual response or error message).
30    */
31   class CJSONRPC
32   {
33   public:
34     /*!
35      \brief Initializes the JSON-RPC handler
36      */
37     static void Initialize();
38 
39     static void Cleanup();
40 
41     /*
42      \brief Handles an incoming JSON-RPC request
43      \param inputString received JSON-RPC request
44      \param transport Transport protocol on which the request arrived
45      \param client Client which sent the request
46      \return JSON-RPC response to be sent back to the client
47 
48      Parses the received input string for the called method and provided
49      parameters. If the request does not conform to the JSON-RPC 2.0
50      specification an error is returned. Otherwise the parameters provided
51      in the request are checked for validity and completeness. If the request
52      is valid and the requested method exists it is called and executed.
53      */
54     static std::string MethodCall(const std::string &inputString, ITransportLayer *transport, IClient *client);
55 
56     static JSONRPC_STATUS Introspect(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
57     static JSONRPC_STATUS Version(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
58     static JSONRPC_STATUS Permission(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
59     static JSONRPC_STATUS Ping(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
60     static JSONRPC_STATUS GetConfiguration(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
61     static JSONRPC_STATUS SetConfiguration(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
62     static JSONRPC_STATUS NotifyAll(const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
63 
64   private:
65     static bool HandleMethodCall(const CVariant& request, CVariant& response, ITransportLayer *transport, IClient *client);
66     static inline bool IsProperJSONRPC(const CVariant& inputroot);
67 
68     inline static void BuildResponse(const CVariant& request, JSONRPC_STATUS code, const CVariant& result, CVariant& response);
69 
70     static bool m_initialized;
71   };
72 }
73