1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NWEB_URL_RESOURCE_RESPONSE_H 17 #define NWEB_URL_RESOURCE_RESPONSE_H 18 19 #include <map> 20 #include <string> 21 22 namespace OHOS::NWeb { 23 24 class NWebResourceReadyCallback { 25 public: ~NWebResourceReadyCallback()26 virtual ~NWebResourceReadyCallback() {} 27 virtual void Continue() = 0; 28 virtual void Cancel() = 0; 29 }; 30 31 enum class NWebResponseDataType : int32_t { 32 NWEB_STRING_TYPE, 33 NWEB_FILE_TYPE, 34 NWEB_RESOURCE_URL_TYPE, 35 NWEB_BUFFER_TYPE, 36 }; 37 38 class NWebUrlResourceResponse { 39 public: 40 virtual ~NWebUrlResourceResponse() = default; 41 42 /** 43 * @brief get input stream 44 * 45 * @retval inputstream string 46 */ 47 virtual std::string ResponseData() = 0; 48 49 /** 50 * @brief set input stream 51 * 52 * @param input_stream set inputstream for example: fread(buf, 1, sizeof(buf), 53 * file) 54 */ 55 virtual void PutResponseData(const std::string& input_stream) = 0; 56 57 /** 58 * @brief Construct a resource response with the given parameters. 59 * 60 * @param encoding encoding { "utf-8" } 61 */ 62 virtual void PutResponseEncoding(const std::string& encoding) = 0; 63 64 /** 65 * @brief get encoding 66 * 67 * @retval encoding the resource response's encoding 68 */ 69 virtual std::string ResponseEncoding() = 0; 70 71 /** 72 * @brief Construct a resource response with the given parameters. 73 * 74 * @param mime_type mime_type{ "text/html" } 75 */ 76 virtual void PutResponseMimeType(const std::string& mime_type) = 0; 77 78 /** 79 * @brief Get mimetype 80 * 81 * @retval mimetype The resource response's MIME type 82 */ 83 virtual std::string ResponseMimeType() = 0; 84 85 /** 86 * @brief Set ResponseHeaders 87 * 88 * @param response_headers response header 89 */ 90 virtual void PutResponseHeaders(const std::map<std::string, std::string>& response_headers) = 0; 91 92 /** 93 * @brief Get ResponseHeaders 94 * 95 * @retval response headers 96 */ 97 virtual std::map<std::string, std::string> ResponseHeaders() = 0; 98 99 /** 100 * @brief Set StatusCode And ReasonPhrase 101 * 102 * @param status_code status code 103 * @param reasonphrase reason phrase 104 */ 105 virtual void PutResponseStateAndStatuscode(int status_code, const std::string& reason_phrase) = 0; 106 107 /** 108 * @brief Get status code 109 * 110 * @retval status code 111 */ 112 virtual int ResponseStatusCode() = 0; 113 114 /** 115 * @brief Get ReasonPhrase 116 * 117 * @retval errorcode reason 118 */ 119 virtual std::string ResponseStatus() = 0; 120 121 virtual void PutResponseDataStatus(bool isDataReady) = 0; 122 123 virtual bool ResponseDataStatus() = 0; 124 125 virtual bool ResponseIsFileHandle() = 0; 126 127 virtual void PutResponseFileHandle(int fd) = 0; 128 129 virtual int ResponseFileHandle() = 0; 130 131 virtual void PutResponseResourceUrl(const std::string& url) = 0; 132 133 virtual std::string ResponseResourceUrl() = 0; 134 135 virtual NWebResponseDataType ResponseDataType() = 0; 136 137 virtual void PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback) = 0; 138 139 virtual void PutResponseDataBuffer(char* buffer, size_t bufferSize) = 0; 140 141 virtual char* GetResponseDataBuffer() = 0; 142 143 virtual size_t GetResponseDataBufferSize() = 0; 144 }; 145 146 } // namespace OHOS::NWeb 147 148 #endif // NWEB_URL_RESOURCE_RESPONSE_H 149