Skip to content

Commit 25293d8

Browse files
motiz88facebook-github-bot
authored andcommitted
Refactor HostAgent control flow (#52815)
Summary: Pull Request resolved: #52815 Changelog: [Internal] Refactors `HostAgent::handleRequest` to make the control flow more explicit and the state (`isFinishedHandlingRequest`, `shouldSendOKResponse`) immutable. Reviewed By: huntie Differential Revision: D78799898 fbshipit-source-id: 0bcf6c364466a91ad3075b67e4f2ac9a4e7a69a7
1 parent 81f3be1 commit 25293d8

File tree

1 file changed

+89
-40
lines changed
  • packages/react-native/ReactCommon/jsinspector-modern

1 file changed

+89
-40
lines changed

packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ class HostAgent::Impl final {
6363
}
6464
}
6565

66-
void handleRequest(const cdp::PreparsedRequest& req) {
67-
bool shouldSendOKResponse = false;
68-
bool isFinishedHandlingRequest = false;
66+
private:
67+
struct RequestHandlingState {
68+
bool isFinishedHandlingRequest{false};
69+
bool shouldSendOKResponse{false};
70+
};
6971

72+
RequestHandlingState tryHandleRequest(const cdp::PreparsedRequest& req) {
7073
// Domain enable/disable requests: write to state (because we're the
7174
// top-level Agent in the Session), trigger any side effects, and decide
7275
// whether we are finished handling the request (or need to delegate to the
@@ -85,14 +88,20 @@ class HostAgent::Impl final {
8588
*hostMetadata_.integrationName);
8689
}
8790

88-
shouldSendOKResponse = true;
89-
isFinishedHandlingRequest = false;
90-
} else if (req.method == "Log.disable") {
91+
return {
92+
.isFinishedHandlingRequest = false,
93+
.shouldSendOKResponse = true,
94+
};
95+
}
96+
if (req.method == "Log.disable") {
9197
sessionState_.isLogDomainEnabled = false;
9298

93-
shouldSendOKResponse = true;
94-
isFinishedHandlingRequest = false;
95-
} else if (req.method == "Runtime.enable") {
99+
return {
100+
.isFinishedHandlingRequest = false,
101+
.shouldSendOKResponse = true,
102+
};
103+
}
104+
if (req.method == "Runtime.enable") {
96105
sessionState_.isRuntimeDomainEnabled = true;
97106

98107
if (fuseboxClientType_ == FuseboxClientType::Unknown) {
@@ -103,27 +112,39 @@ class HostAgent::Impl final {
103112
sendNonFuseboxNotice();
104113
}
105114

106-
shouldSendOKResponse = true;
107-
isFinishedHandlingRequest = false;
108-
} else if (req.method == "Runtime.disable") {
115+
return {
116+
.isFinishedHandlingRequest = false,
117+
.shouldSendOKResponse = true,
118+
};
119+
}
120+
if (req.method == "Runtime.disable") {
109121
sessionState_.isRuntimeDomainEnabled = false;
110122

111-
shouldSendOKResponse = true;
112-
isFinishedHandlingRequest = false;
113-
} else if (req.method == "Debugger.enable") {
123+
return {
124+
.isFinishedHandlingRequest = false,
125+
.shouldSendOKResponse = true,
126+
};
127+
}
128+
if (req.method == "Debugger.enable") {
114129
sessionState_.isDebuggerDomainEnabled = true;
115130

116-
shouldSendOKResponse = true;
117-
isFinishedHandlingRequest = false;
118-
} else if (req.method == "Debugger.disable") {
131+
return {
132+
.isFinishedHandlingRequest = false,
133+
.shouldSendOKResponse = true,
134+
};
135+
}
136+
if (req.method == "Debugger.disable") {
119137
sessionState_.isDebuggerDomainEnabled = false;
120138

121-
shouldSendOKResponse = true;
122-
isFinishedHandlingRequest = false;
139+
return {
140+
.isFinishedHandlingRequest = false,
141+
.shouldSendOKResponse = true,
142+
};
123143
}
124144
// Methods other than domain enables/disables: handle anything we know how
125145
// to handle, and delegate to the InstanceAgent otherwise.
126-
else if (req.method == "Page.reload") {
146+
147+
if (req.method == "Page.reload") {
127148
targetController_.getDelegate().onReload({
128149
.ignoreCache =
129150
req.params.isObject() && (req.params.count("ignoreCache") != 0u)
@@ -136,9 +157,12 @@ class HostAgent::Impl final {
136157
: std::nullopt,
137158
});
138159

139-
shouldSendOKResponse = true;
140-
isFinishedHandlingRequest = true;
141-
} else if (req.method == "Overlay.setPausedInDebuggerMessage") {
160+
return {
161+
.isFinishedHandlingRequest = true,
162+
.shouldSendOKResponse = true,
163+
};
164+
}
165+
if (req.method == "Overlay.setPausedInDebuggerMessage") {
142166
auto message =
143167
req.params.isObject() && (req.params.count("message") != 0u)
144168
? std::optional(req.params.at("message").asString())
@@ -153,9 +177,12 @@ class HostAgent::Impl final {
153177
.message = message,
154178
});
155179

156-
shouldSendOKResponse = true;
157-
isFinishedHandlingRequest = true;
158-
} else if (req.method == "ReactNativeApplication.enable") {
180+
return {
181+
.isFinishedHandlingRequest = true,
182+
.shouldSendOKResponse = true,
183+
};
184+
}
185+
if (req.method == "ReactNativeApplication.enable") {
159186
sessionState_.isReactNativeApplicationDomainEnabled = true;
160187
fuseboxClientType_ = FuseboxClientType::Fusebox;
161188

@@ -167,44 +194,66 @@ class HostAgent::Impl final {
167194
"ReactNativeApplication.metadataUpdated",
168195
createHostMetadataPayload(hostMetadata_)));
169196

170-
shouldSendOKResponse = true;
171-
isFinishedHandlingRequest = true;
172-
} else if (req.method == "ReactNativeApplication.disable") {
197+
return {
198+
.isFinishedHandlingRequest = true,
199+
.shouldSendOKResponse = true,
200+
};
201+
}
202+
if (req.method == "ReactNativeApplication.disable") {
173203
sessionState_.isReactNativeApplicationDomainEnabled = false;
174204

175-
shouldSendOKResponse = true;
176-
isFinishedHandlingRequest = true;
177-
} else if (req.method == "Tracing.start") {
205+
return {
206+
.isFinishedHandlingRequest = true,
207+
.shouldSendOKResponse = true,
208+
};
209+
}
210+
if (req.method == "Tracing.start") {
178211
if (sessionState_.isDebuggerDomainEnabled) {
179212
frontendChannel_(cdp::jsonError(
180213
req.id,
181214
cdp::ErrorCode::InternalError,
182215
"Debugger domain is expected to be disabled before starting Tracing"));
183216

184-
return;
217+
return {
218+
.isFinishedHandlingRequest = true,
219+
.shouldSendOKResponse = false,
220+
};
185221
}
186222

187223
// We delegate handling of this request to TracingAgent. If not handled,
188224
// then something unexpected happened - don't send an OK response.
189-
shouldSendOKResponse = false;
190-
isFinishedHandlingRequest = false;
225+
return {
226+
.isFinishedHandlingRequest = false,
227+
.shouldSendOKResponse = false,
228+
};
191229
}
192230

193-
if (!isFinishedHandlingRequest &&
231+
return {
232+
.isFinishedHandlingRequest = false,
233+
.shouldSendOKResponse = false,
234+
};
235+
}
236+
237+
public:
238+
void handleRequest(const cdp::PreparsedRequest& req) {
239+
const RequestHandlingState requestState = tryHandleRequest(req);
240+
241+
if (!requestState.isFinishedHandlingRequest &&
194242
networkIOAgent_.handleRequest(req, targetController_.getDelegate())) {
195243
return;
196244
}
197245

198-
if (!isFinishedHandlingRequest && tracingAgent_.handleRequest(req)) {
246+
if (!requestState.isFinishedHandlingRequest &&
247+
tracingAgent_.handleRequest(req)) {
199248
return;
200249
}
201250

202-
if (!isFinishedHandlingRequest && instanceAgent_ &&
251+
if (!requestState.isFinishedHandlingRequest && instanceAgent_ &&
203252
instanceAgent_->handleRequest(req)) {
204253
return;
205254
}
206255

207-
if (shouldSendOKResponse) {
256+
if (requestState.shouldSendOKResponse) {
208257
frontendChannel_(cdp::jsonResult(req.id));
209258
return;
210259
}

0 commit comments

Comments
 (0)