Skip to content

Commit ed0bcb0

Browse files
authored
Fix/trace process shell/error on firefox (#1302)
fix firefox with trace_process_shell issue: #1301
1 parent a523bf5 commit ed0bcb0

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

include/perfetto/ext/base/string_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ bool StartsWithAny(const std::string& str,
169169
const std::vector<std::string>& prefixes);
170170
bool Contains(const std::string& haystack, const std::string& needle);
171171
bool Contains(const std::string& haystack, char needle);
172+
bool Contains(const std::vector<std::string>& haystack,
173+
const std::string& needle);
172174
size_t Find(const StringView& needle, const StringView& haystack);
173175
bool CaseInsensitiveEqual(const std::string& first, const std::string& second);
174176
std::string Join(const std::vector<std::string>& parts,

src/base/http/http_server.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,12 @@ size_t HttpServer::ParseOneHttpRequest(HttpServerConnection* conn) {
236236
if (IsOriginAllowed(hdr_value))
237237
conn->origin_allowed_ = hdr_value.ToStdString();
238238
} else if (hdr_name.CaseInsensitiveEq("connection")) {
239-
conn->keepalive_ = hdr_value.CaseInsensitiveEq("keep-alive");
240-
http_req.is_websocket_handshake =
241-
hdr_value.CaseInsensitiveEq("upgrade");
239+
auto values = SplitString(hdr_value.ToStdString(), ",");
240+
for (auto& value : values) {
241+
value = ToLower(TrimWhitespace(value));
242+
}
243+
conn->keepalive_ = Contains(values, "keep-alive");
244+
http_req.is_websocket_handshake = Contains(values, "upgrade");
242245
}
243246
}
244247
}

src/base/string_utils.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ bool Contains(const std::string& haystack, const char needle) {
7373
return haystack.find(needle) != std::string::npos;
7474
}
7575

76+
bool Contains(const std::vector<std::string>& haystack,
77+
const std::string& needle) {
78+
return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
79+
}
80+
7681
size_t Find(const StringView& needle, const StringView& haystack) {
7782
if (needle.empty())
7883
return 0;

src/base/string_utils_unittest.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ TEST(StringUtilsTest, Contains) {
366366
EXPECT_FALSE(Contains("abc", "abcd"));
367367
EXPECT_FALSE(Contains("", "a"));
368368
EXPECT_FALSE(Contains("", "abc"));
369+
auto values = std::vector<std::string>{"abc", "def"};
370+
EXPECT_TRUE(Contains(values, "abc"));
371+
EXPECT_TRUE(Contains(values, "def"));
372+
EXPECT_FALSE(Contains(values, "abcdef"));
373+
EXPECT_FALSE(Contains(values, "ab"));
374+
EXPECT_FALSE(Contains(values, "ef"));
375+
EXPECT_FALSE(Contains(std::vector<std::string>{}, "abcdef"));
369376
}
370377

371378
TEST(StringUtilsTest, Find) {

ui/src/trace_processor/http_rpc_engine.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import protos from '../protos';
1616
import {fetchWithTimeout} from '../base/http_utils';
17-
import {assertExists} from '../base/logging';
17+
import {assertExists, reportError} from '../base/logging';
1818
import {EngineBase} from '../trace_processor/engine';
1919

2020
const RPC_CONNECT_TIMEOUT_MS = 2000;
@@ -32,6 +32,8 @@ export class HttpRpcEngine extends EngineBase {
3232
private websocket?: WebSocket;
3333
private connected = false;
3434
private disposed = false;
35+
private queue: Blob[] = [];
36+
private isProcessingQueue = false;
3537

3638
// Can be changed by frontend/index.ts when passing ?rpc_port=1234 .
3739
static rpcPort = '9001';
@@ -86,11 +88,24 @@ export class HttpRpcEngine extends EngineBase {
8688
}
8789

8890
private onWebsocketMessage(e: MessageEvent) {
89-
assertExists(e.data as Blob)
90-
.arrayBuffer()
91-
.then((buf) => {
91+
const blob = assertExists(e.data as Blob);
92+
this.queue.push(blob);
93+
this.processQueue();
94+
}
95+
96+
private async processQueue() {
97+
if (this.isProcessingQueue) return;
98+
this.isProcessingQueue = true;
99+
while (this.queue.length > 0) {
100+
try {
101+
const blob = assertExists(this.queue.shift());
102+
const buf = await blob.arrayBuffer();
92103
super.onRpcResponseBytes(new Uint8Array(buf));
93-
});
104+
} catch (e) {
105+
reportError(e);
106+
}
107+
}
108+
this.isProcessingQueue = false;
94109
}
95110

96111
static async checkConnection(): Promise<HttpRpcState> {

0 commit comments

Comments
 (0)