Skip to content

Commit bbf5143

Browse files
coadometa-codesync[bot]
authored andcommitted
Clean up Image public header dependencies (#58760)
Summary: Pull Request resolved: #58760 Keep the Image component's exported headers self-contained and free of implementation-only dependencies. - Move the `ImageSource` and `ImageResizeMode` `fromRawValue`/`toString` implementations from `conversions.h` into a new `conversions.cpp`. The header now only declares them, so it no longer exports `glog/logging.h` and `react_native_expect.h`. Every consumer of `conversions.h` (Image props, text input props, and generated component props) already links the Image library in Buck, CocoaPods, and CMake. - Drop the unused Android-only `MapBuffer.h` and `MapBufferBuilder.h` includes from `ImageState.h`; nothing in the Image module uses MapBuffer. - Declare standard-library dependencies directly in the Image headers (`<memory>`, `<utility>`, `<optional>`, `<string>`, `<cstdint>`). Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D122534476 fbshipit-source-id: 9dd569630df1b3117a92201bc3012b2ff8cb5803
1 parent e69f61b commit bbf5143

7 files changed

Lines changed: 200 additions & 155 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/components/image/ImageComponentDescriptor.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <memory>
13+
1214
#include <react/renderer/components/image/ImageShadowNode.h>
1315
#include <react/renderer/core/ConcreteComponentDescriptor.h>
1416

‎packages/react-native/ReactCommon/react/renderer/components/image/ImageEventEmitter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <cstdint>
13+
1214
#include <react/renderer/components/view/ViewEventEmitter.h>
1315
#include <react/renderer/imagemanager/primitives.h>
1416

‎packages/react-native/ReactCommon/react/renderer/components/image/ImageProps.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <optional>
13+
#include <string>
14+
1215
#include <react/renderer/components/view/ViewProps.h>
1316
#include <react/renderer/core/PropsParserContext.h>
1417
#include <react/renderer/graphics/Color.h>

‎packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <memory>
13+
1214
#include <react/renderer/components/image/ImageEventEmitter.h>
1315
#include <react/renderer/components/image/ImageProps.h>
1416
#include <react/renderer/components/image/ImageState.h>

‎packages/react-native/ReactCommon/react/renderer/components/image/ImageState.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <memory>
13+
#include <utility>
14+
1215
#include <react/renderer/imagemanager/ImageRequest.h>
1316
#include <react/renderer/imagemanager/ImageRequestParams.h>
1417
#include <react/renderer/imagemanager/primitives.h>
1518

1619
#ifdef ANDROID
1720
#include <folly/dynamic.h>
18-
#include <react/renderer/mapbuffer/MapBuffer.h>
19-
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
2021
#endif
2122

2223
namespace facebook::react {
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)