Implement more request interceptor APIs.
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / web-engine-request-interceptor.h
1 #ifndef DALI_WEB_ENGINE_REQUEST_INTERCEPTOR_H
2 #define DALI_WEB_ENGINE_REQUEST_INTERCEPTOR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <dali/public-api/object/ref-object.h>
25
26 #include <string>
27
28 namespace Dali
29 {
30 /**
31  * @brief A class WebEngineRequestInterceptor for intercepting http request.
32  */
33 class WebEngineRequestInterceptor : public RefObject
34 {
35 public:
36   /**
37    * @brief Constructor.
38    */
39   WebEngineRequestInterceptor() = default;
40
41   /**
42    * @brief Destructor.
43    */
44   virtual ~WebEngineRequestInterceptor() = default;
45
46   /**
47    * @brief Returns request url.
48    *
49    * @return url if succeeded or empty otherwise
50    */
51   virtual std::string GetUrl() const = 0;
52
53   /**
54    * @brief Returns http headers.
55    *
56    * @return headers if succeeded or empty otherwise
57    */
58   virtual Dali::Property::Map GetHeaders() const = 0;
59
60   /**
61    * @brief Returns http method.
62    *
63    * @return method if succeeded or empty otherwise
64    */
65   virtual std::string GetMethod() const = 0;
66
67   /**
68    * @brief Ignores request.
69    * @note After this call, any further calls result in undefined behavior.
70    *       This function can be called only INSIDE Dali::WebEngineContext::WebEngineRequestInterceptedCallback.
71    *
72    * @return true if succeeded or false otherwise
73    */
74   virtual bool Ignore() = 0;
75
76   /**
77    * @brief Sets status code and status text of response for intercepted request.
78    *
79    * @param[in] statusCode Status code of response
80    * @param[in] customStatusText Status code of response
81    *
82    * @return true if succeeded or false otherwise
83    */
84   virtual bool SetResponseStatus(int statusCode, const std::string& customStatusText) = 0;
85
86   /**
87    * @brief Adds HTTP header to response for intercepted request.
88    *
89    * @param[in] fieldName Key of response header
90    * @param[in] fieldValue Value of response header
91    *
92    * @return true if succeeded or false otherwise
93    */
94   virtual bool AddResponseHeader(const std::string& fieldName, const std::string& fieldValue) = 0;
95
96   /**
97    * @brief Adds HTTP headers to response.
98    *
99    * @param[in] headers Headers of response
100    *
101    * @return true if succeeded or false otherwise
102    */
103   virtual bool AddResponseHeaders(const Dali::Property::Map& headers) = 0;
104
105   /**
106    * @brief Writes whole response body at once.
107    *
108    * @param[in] body Contents of response
109    * @param[in] length Length of contents of response
110    *
111    * @return true if succeeded or false otherwise
112    */
113   virtual bool AddResponseBody(const std::string& body, uint32_t length) = 0;
114
115   /**
116    * @brief Writes whole response at once.
117    *
118    * @param[in] headers Headers of response
119    * @param[in] body Contents of response
120    * @param[in] length Length of contents of response
121    *
122    * @return true if succeeded or false otherwise
123    */
124   virtual bool AddResponse(const std::string& headers, const std::string& body, uint32_t length) = 0;
125
126   /**
127    * @brief Writes a part of response body.
128    * @note If this function returns false, handling the request is done.
129    *       Any further calls result in undefined behavior.
130    *       User should always check return value, because response to this request might not be needed any more,
131    *       and function can return false even though user still has data to write.
132    *       This function can be called only OUTSIDE Dali::WebEngineContext::WebEngineRequestInterceptedCallback.
133    *
134    * @param[in] chunk Chunks of response
135    * @param[in] length Length of chunks of response
136    *
137    * @return true if succeeded or false otherwise
138    */
139   virtual bool WriteResponseChunk(const std::string& chunk, uint32_t length) = 0;
140 };
141
142 using WebEngineRequestInterceptorPtr = Dali::IntrusivePtr<WebEngineRequestInterceptor>;
143
144 } // namespace Dali
145
146 #endif // DALI_WEB_ENGINE_REQUEST_INTERCEPTOR_H