Skip to content

Commit 2faef5e

Browse files
coadofacebook-github-bot
authored andcommitted
Clean up Image public header dependencies
Summary: 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] Differential Revision: D122534476
1 parent b3e239b commit 2faef5e

7 files changed

Lines changed: 198 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: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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 = {(Float)items.at("width"), (Float)items.at("height")};
47+
}
48+
49+
if (items.find("scale") != items.end() &&
50+
// The following checks have to be removed after codegen is shipped.
51+
// See T45151459.
52+
items.at("scale").hasType<Float>()) {
53+
result.scale = (Float)items.at("scale");
54+
} else {
55+
result.scale = items.find("deprecated") != items.end() ? 0.0f : 1.0f;
56+
}
57+
58+
if (items.find("url") != items.end() &&
59+
// The following should be removed after codegen is shipped.
60+
// See T45151459.
61+
items.at("url").hasType<std::string>()) {
62+
result.uri = (std::string)items.at("url");
63+
}
64+
65+
if (items.find("uri") != items.end() &&
66+
// The following should be removed after codegen is shipped.
67+
// See T45151459.
68+
items.at("uri").hasType<std::string>()) {
69+
result.uri = (std::string)items.at("uri");
70+
}
71+
72+
if (items.find("bundle") != items.end() &&
73+
// The following should be removed after codegen is shipped.
74+
// See T45151459.
75+
items.at("bundle").hasType<std::string>()) {
76+
result.bundle = (std::string)items.at("bundle");
77+
result.type = ImageSource::Type::Local;
78+
}
79+
80+
if (items.find("headers") != items.end() &&
81+
items.at("headers")
82+
.hasType<std::unordered_map<std::string, std::string>>()) {
83+
auto headers =
84+
(std::unordered_map<std::string, std::string>)items.at("headers");
85+
for (const auto& header : headers) {
86+
result.headers.push_back(header);
87+
}
88+
}
89+
90+
if (items.find("body") != items.end() &&
91+
items.at("body").hasType<std::string>()) {
92+
result.body = (std::string)items.at("body");
93+
}
94+
95+
if (items.find("method") != items.end() &&
96+
items.at("method").hasType<std::string>()) {
97+
result.method = (std::string)items.at("method");
98+
}
99+
100+
if (items.find("cache") != items.end() &&
101+
items.at("cache").hasType<std::string>()) {
102+
auto cache = (std::string)items.at("cache");
103+
if (cache == "reload") {
104+
result.cache = ImageSource::CacheStategy::Reload;
105+
} else if (cache == "force-cache") {
106+
result.cache = ImageSource::CacheStategy::ForceCache;
107+
} else if (cache == "only-if-cached") {
108+
result.cache = ImageSource::CacheStategy::OnlyIfCached;
109+
}
110+
}
111+
112+
return;
113+
}
114+
115+
// The following should be removed after codegen is shipped.
116+
// See T45151459.
117+
result = {};
118+
result.type = ImageSource::Type::Invalid;
119+
}
120+
121+
std::string toString(const ImageSource& value) {
122+
return "{uri: " + value.uri + "}";
123+
}
124+
125+
void fromRawValue(
126+
const PropsParserContext& /* context */,
127+
const RawValue& value,
128+
ImageResizeMode& result) {
129+
react_native_expect(value.hasType<std::string>());
130+
if (!value.hasType<std::string>()) {
131+
LOG(ERROR) << "Unsupported ImageResizeMode type";
132+
// "cover" is default in non-Fabric web and iOS
133+
result = ImageResizeMode::Cover;
134+
return;
135+
}
136+
137+
auto stringValue = (std::string)value;
138+
if (stringValue == "cover") {
139+
result = ImageResizeMode::Cover;
140+
} else if (stringValue == "contain") {
141+
result = ImageResizeMode::Contain;
142+
} else if (stringValue == "stretch") {
143+
result = ImageResizeMode::Stretch;
144+
} else if (stringValue == "center") {
145+
result = ImageResizeMode::Center;
146+
} else if (stringValue == "repeat") {
147+
result = ImageResizeMode::Repeat;
148+
} else if (stringValue == "none") {
149+
result = ImageResizeMode::None;
150+
} else {
151+
LOG(ERROR) << "Unsupported ImageResizeMode value: " << stringValue;
152+
react_native_expect(false);
153+
// "cover" is default in non-Fabric web and iOS
154+
result = ImageResizeMode::Cover;
155+
}
156+
}
157+
158+
std::string toString(const ImageResizeMode& value) {
159+
switch (value) {
160+
case ImageResizeMode::Cover:
161+
return "cover";
162+
case ImageResizeMode::Contain:
163+
return "contain";
164+
case ImageResizeMode::Stretch:
165+
return "stretch";
166+
case ImageResizeMode::Center:
167+
return "center";
168+
case ImageResizeMode::Repeat:
169+
return "repeat";
170+
case ImageResizeMode::None:
171+
return "none";
172+
default:
173+
LOG(ERROR) << "Unsupported ImageResizeMode value: " << (int)value;
174+
react_native_expect(false);
175+
// "cover" is default in non-Fabric web and iOS
176+
return "cover";
177+
}
178+
}
179+
180+
} // namespace facebook::react

0 commit comments

Comments
 (0)