|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <react/renderer/components/image/conversions.h> |
| 9 | + |
| 10 | +#include <glog/logging.h> |
| 11 | +#include <react/debug/react_native_expect.h> |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <unordered_map> |
| 15 | + |
| 16 | +namespace facebook::react { |
| 17 | + |
| 18 | +void fromRawValue( |
| 19 | + const PropsParserContext& /* context */, |
| 20 | + const RawValue& value, |
| 21 | + ImageSource& result) { |
| 22 | + if (value.hasType<std::string>()) { |
| 23 | + result = { |
| 24 | + .type = ImageSource::Type::Remote, |
| 25 | + .uri = (std::string)value, |
| 26 | + }; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (value.hasType<std::unordered_map<std::string, RawValue>>()) { |
| 31 | + auto items = (std::unordered_map<std::string, RawValue>)value; |
| 32 | + result = {}; |
| 33 | + |
| 34 | + result.type = ImageSource::Type::Remote; |
| 35 | + |
| 36 | + if (items.find("__packager_asset") != items.end()) { |
| 37 | + result.type = ImageSource::Type::Local; |
| 38 | + } |
| 39 | + |
| 40 | + if (items.find("width") != items.end() && |
| 41 | + items.find("height") != items.end() && |
| 42 | + // The following checks have to be removed after codegen is shipped. |
| 43 | + // See T45151459. |
| 44 | + items.at("width").hasType<Float>() && |
| 45 | + items.at("height").hasType<Float>()) { |
| 46 | + result.size = { |
| 47 | + .width = (Float)items.at("width"), |
| 48 | + .height = (Float)items.at("height")}; |
| 49 | + } |
| 50 | + |
| 51 | + if (items.find("scale") != items.end() && |
| 52 | + // The following checks have to be removed after codegen is shipped. |
| 53 | + // See T45151459. |
| 54 | + items.at("scale").hasType<Float>()) { |
| 55 | + result.scale = (Float)items.at("scale"); |
| 56 | + } else { |
| 57 | + result.scale = items.find("deprecated") != items.end() ? 0.0f : 1.0f; |
| 58 | + } |
| 59 | + |
| 60 | + if (items.find("url") != items.end() && |
| 61 | + // The following should be removed after codegen is shipped. |
| 62 | + // See T45151459. |
| 63 | + items.at("url").hasType<std::string>()) { |
| 64 | + result.uri = (std::string)items.at("url"); |
| 65 | + } |
| 66 | + |
| 67 | + if (items.find("uri") != items.end() && |
| 68 | + // The following should be removed after codegen is shipped. |
| 69 | + // See T45151459. |
| 70 | + items.at("uri").hasType<std::string>()) { |
| 71 | + result.uri = (std::string)items.at("uri"); |
| 72 | + } |
| 73 | + |
| 74 | + if (items.find("bundle") != items.end() && |
| 75 | + // The following should be removed after codegen is shipped. |
| 76 | + // See T45151459. |
| 77 | + items.at("bundle").hasType<std::string>()) { |
| 78 | + result.bundle = (std::string)items.at("bundle"); |
| 79 | + result.type = ImageSource::Type::Local; |
| 80 | + } |
| 81 | + |
| 82 | + if (items.find("headers") != items.end() && |
| 83 | + items.at("headers") |
| 84 | + .hasType<std::unordered_map<std::string, std::string>>()) { |
| 85 | + auto headers = |
| 86 | + (std::unordered_map<std::string, std::string>)items.at("headers"); |
| 87 | + for (const auto& header : headers) { |
| 88 | + result.headers.emplace_back(header); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (items.find("body") != items.end() && |
| 93 | + items.at("body").hasType<std::string>()) { |
| 94 | + result.body = (std::string)items.at("body"); |
| 95 | + } |
| 96 | + |
| 97 | + if (items.find("method") != items.end() && |
| 98 | + items.at("method").hasType<std::string>()) { |
| 99 | + result.method = (std::string)items.at("method"); |
| 100 | + } |
| 101 | + |
| 102 | + if (items.find("cache") != items.end() && |
| 103 | + items.at("cache").hasType<std::string>()) { |
| 104 | + auto cache = (std::string)items.at("cache"); |
| 105 | + if (cache == "reload") { |
| 106 | + result.cache = ImageSource::CacheStategy::Reload; |
| 107 | + } else if (cache == "force-cache") { |
| 108 | + result.cache = ImageSource::CacheStategy::ForceCache; |
| 109 | + } else if (cache == "only-if-cached") { |
| 110 | + result.cache = ImageSource::CacheStategy::OnlyIfCached; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // The following should be removed after codegen is shipped. |
| 118 | + // See T45151459. |
| 119 | + result = {}; |
| 120 | + result.type = ImageSource::Type::Invalid; |
| 121 | +} |
| 122 | + |
| 123 | +std::string toString(const ImageSource& value) { |
| 124 | + return "{uri: " + value.uri + "}"; |
| 125 | +} |
| 126 | + |
| 127 | +void fromRawValue( |
| 128 | + const PropsParserContext& /* context */, |
| 129 | + const RawValue& value, |
| 130 | + ImageResizeMode& result) { |
| 131 | + react_native_expect(value.hasType<std::string>()); |
| 132 | + if (!value.hasType<std::string>()) { |
| 133 | + LOG(ERROR) << "Unsupported ImageResizeMode type"; |
| 134 | + // "cover" is default in non-Fabric web and iOS |
| 135 | + result = ImageResizeMode::Cover; |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + auto stringValue = (std::string)value; |
| 140 | + if (stringValue == "cover") { |
| 141 | + result = ImageResizeMode::Cover; |
| 142 | + } else if (stringValue == "contain") { |
| 143 | + result = ImageResizeMode::Contain; |
| 144 | + } else if (stringValue == "stretch") { |
| 145 | + result = ImageResizeMode::Stretch; |
| 146 | + } else if (stringValue == "center") { |
| 147 | + result = ImageResizeMode::Center; |
| 148 | + } else if (stringValue == "repeat") { |
| 149 | + result = ImageResizeMode::Repeat; |
| 150 | + } else if (stringValue == "none") { |
| 151 | + result = ImageResizeMode::None; |
| 152 | + } else { |
| 153 | + LOG(ERROR) << "Unsupported ImageResizeMode value: " << stringValue; |
| 154 | + react_native_expect(false); |
| 155 | + // "cover" is default in non-Fabric web and iOS |
| 156 | + result = ImageResizeMode::Cover; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +std::string toString(const ImageResizeMode& value) { |
| 161 | + switch (value) { |
| 162 | + case ImageResizeMode::Cover: |
| 163 | + return "cover"; |
| 164 | + case ImageResizeMode::Contain: |
| 165 | + return "contain"; |
| 166 | + case ImageResizeMode::Stretch: |
| 167 | + return "stretch"; |
| 168 | + case ImageResizeMode::Center: |
| 169 | + return "center"; |
| 170 | + case ImageResizeMode::Repeat: |
| 171 | + return "repeat"; |
| 172 | + case ImageResizeMode::None: |
| 173 | + return "none"; |
| 174 | + default: |
| 175 | + LOG(ERROR) << "Unsupported ImageResizeMode value: " << (int)value; |
| 176 | + react_native_expect(false); |
| 177 | + // "cover" is default in non-Fabric web and iOS |
| 178 | + return "cover"; |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +} // namespace facebook::react |
0 commit comments