Skip to content

Commit 356f623

Browse files
Samiya CaurDevtools-frontend LUCI CQ
authored andcommitted
Move code for calling dispatchHttpRequest to a common file
We will be using `dispatchHttpRequest` for AI Code Generation as well. Hence, moving it out of GdpClient Bug: 448793181 Change-Id: Ic9224bbea94cde4d0c6ace0e82996a6131c51db4 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7094290 Reviewed-by: Danil Somsikov <[email protected]> Reviewed-by: Ergün Erdoğmuş <[email protected]> Commit-Queue: Samiya Caur <[email protected]>
1 parent 3e000c9 commit 356f623

File tree

5 files changed

+74
-57
lines changed

5 files changed

+74
-57
lines changed

config/gni/devtools_grd_files.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ grd_files_unbundled_sources = [
865865
"front_end/core/common/Worker.js",
866866
"front_end/core/dom_extension/DOMExtension.js",
867867
"front_end/core/host/AidaClient.js",
868+
"front_end/core/host/DispatchHttpRequestClient.js",
868869
"front_end/core/host/GdpClient.js",
869870
"front_end/core/host/InspectorFrontendHost.js",
870871
"front_end/core/host/InspectorFrontendHostAPI.js",

front_end/core/host/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import("../../../scripts/build/ninja/devtools_module.gni")
88
devtools_module("host") {
99
sources = [
1010
"AidaClient.ts",
11+
"DispatchHttpRequestClient.ts",
1112
"GdpClient.ts",
1213
"InspectorFrontendHost.ts",
1314
"InspectorFrontendHostAPI.ts",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 The Chromium Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import {InspectorFrontendHostInstance} from './InspectorFrontendHost.js';
6+
import type {DispatchHttpRequestRequest, DispatchHttpRequestResult} from './InspectorFrontendHostAPI.js';
7+
8+
export enum ErrorType {
9+
HTTP_RESPONSE_UNAVAILABLE = 'HTTP_RESPONSE_UNAVAILABLE',
10+
NOT_FOUND = 'NOT_FOUND',
11+
}
12+
13+
export class DispatchHttpRequestError extends Error {
14+
constructor(readonly type: ErrorType, options?: ErrorOptions) {
15+
super(undefined, options);
16+
}
17+
}
18+
19+
export async function makeHttpRequest<R>(request: DispatchHttpRequestRequest): Promise<R> {
20+
const response = await new Promise<DispatchHttpRequestResult>(resolve => {
21+
InspectorFrontendHostInstance.dispatchHttpRequest(request, resolve);
22+
});
23+
24+
debugLog({request, response});
25+
if (response.statusCode === 404) {
26+
throw new DispatchHttpRequestError(ErrorType.NOT_FOUND);
27+
}
28+
29+
if ('response' in response && response.statusCode === 200) {
30+
try {
31+
return JSON.parse(response.response) as R;
32+
} catch (err) {
33+
throw new DispatchHttpRequestError(ErrorType.HTTP_RESPONSE_UNAVAILABLE, {cause: err});
34+
}
35+
}
36+
37+
throw new DispatchHttpRequestError(ErrorType.HTTP_RESPONSE_UNAVAILABLE);
38+
}
39+
40+
function isDebugMode(): boolean {
41+
return Boolean(localStorage.getItem('debugDispatchHttpRequestEnabled'));
42+
}
43+
44+
function debugLog(...log: unknown[]): void {
45+
if (!isDebugMode()) {
46+
return;
47+
}
48+
49+
// eslint-disable-next-line no-console
50+
console.log('debugLog', ...log);
51+
}
52+
53+
function setDebugDispatchHttpRequestEnabled(enabled: boolean): void {
54+
if (enabled) {
55+
localStorage.setItem('debugDispatchHttpRequestEnabled', 'true');
56+
} else {
57+
localStorage.removeItem('debugDispatchHttpRequestEnabled');
58+
}
59+
}
60+
61+
// @ts-expect-error
62+
globalThis.setDebugDispatchHttpRequestEnabled = setDebugDispatchHttpRequestEnabled;

front_end/core/host/GdpClient.ts

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import * as Root from '../root/root.js';
66

7-
import {InspectorFrontendHostInstance} from './InspectorFrontendHost.js';
8-
import type {DispatchHttpRequestRequest, DispatchHttpRequestResult} from './InspectorFrontendHostAPI.js';
7+
import * as DispatchHttpRequestClient from './DispatchHttpRequestClient.js';
8+
import type {DispatchHttpRequestRequest} from './InspectorFrontendHostAPI.js';
99

1010
export enum SubscriptionStatus {
1111
ENABLED = 'SUBSCRIPTION_STATE_ENABLED',
@@ -73,17 +73,6 @@ export interface GetProfileResponse {
7373
isEligible: boolean;
7474
}
7575

76-
export enum GdpErrorType {
77-
HTTP_RESPONSE_UNAVAILABLE = 'HTTP_RESPONSE_UNAVAILABLE',
78-
NOT_FOUND = 'NOT_FOUND',
79-
}
80-
81-
class GdpError extends Error {
82-
constructor(readonly type: GdpErrorType, options?: ErrorOptions) {
83-
super(undefined, options);
84-
}
85-
}
86-
8776
/**
8877
* The `batchGet` awards endpoint returns badge names with an
8978
* obfuscated user ID (e.g., `profiles/12345/awards/badge-name`).
@@ -99,27 +88,12 @@ export const GOOGLE_DEVELOPER_PROGRAM_PROFILE_LINK = 'https://developers.google.
9988

10089
async function makeHttpRequest<R>(request: DispatchHttpRequestRequest): Promise<R> {
10190
if (!isGdpProfilesAvailable()) {
102-
throw new GdpError(GdpErrorType.HTTP_RESPONSE_UNAVAILABLE);
103-
}
104-
105-
const response = await new Promise<DispatchHttpRequestResult>(resolve => {
106-
InspectorFrontendHostInstance.dispatchHttpRequest(request, resolve);
107-
});
108-
109-
debugLog({request, response});
110-
if (response.statusCode === 404) {
111-
throw new GdpError(GdpErrorType.NOT_FOUND);
112-
}
113-
114-
if ('response' in response && response.statusCode === 200) {
115-
try {
116-
return JSON.parse(response.response) as R;
117-
} catch (err) {
118-
throw new GdpError(GdpErrorType.HTTP_RESPONSE_UNAVAILABLE, {cause: err});
119-
}
91+
throw new DispatchHttpRequestClient.DispatchHttpRequestError(
92+
DispatchHttpRequestClient.ErrorType.HTTP_RESPONSE_UNAVAILABLE);
12093
}
12194

122-
throw new GdpError(GdpErrorType.HTTP_RESPONSE_UNAVAILABLE);
95+
const response = await DispatchHttpRequestClient.makeHttpRequest(request) as R;
96+
return response;
12397
}
12498

12599
const SERVICE_NAME = 'gdpService';
@@ -158,7 +132,8 @@ export class GdpClient {
158132
isEligible: true,
159133
};
160134
} catch (err: unknown) {
161-
if (err instanceof GdpError && err.type === GdpErrorType.HTTP_RESPONSE_UNAVAILABLE) {
135+
if (err instanceof DispatchHttpRequestClient.DispatchHttpRequestError &&
136+
err.type === DispatchHttpRequestClient.ErrorType.HTTP_RESPONSE_UNAVAILABLE) {
162137
return null;
163138
}
164139
}
@@ -265,27 +240,6 @@ export class GdpClient {
265240
}
266241
}
267242

268-
function isDebugMode(): boolean {
269-
return Boolean(localStorage.getItem('debugGdpIntegrationEnabled'));
270-
}
271-
272-
function debugLog(...log: unknown[]): void {
273-
if (!isDebugMode()) {
274-
return;
275-
}
276-
277-
// eslint-disable-next-line no-console
278-
console.log('debugLog', ...log);
279-
}
280-
281-
function setDebugGdpIntegrationEnabled(enabled: boolean): void {
282-
if (enabled) {
283-
localStorage.setItem('debugGdpIntegrationEnabled', 'true');
284-
} else {
285-
localStorage.removeItem('debugGdpIntegrationEnabled');
286-
}
287-
}
288-
289243
export function isGdpProfilesAvailable(): boolean {
290244
const isBaseFeatureEnabled = Boolean(Root.Runtime.hostConfig.devToolsGdpProfiles?.enabled);
291245
const isBrandedBuild = Boolean(Root.Runtime.hostConfig.devToolsGdpProfilesAvailability?.enabled);
@@ -311,6 +265,3 @@ export function isBadgesEnabled(): boolean {
311265
export function isStarterBadgeEnabled(): boolean {
312266
return Boolean(Root.Runtime.hostConfig.devToolsGdpProfiles?.starterBadgeEnabled);
313267
}
314-
315-
// @ts-expect-error
316-
globalThis.setDebugGdpIntegrationEnabled = setDebugGdpIntegrationEnabled;

front_end/core/host/host.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import * as AidaClient from './AidaClient.js';
6+
import * as DispatchHttpRequestClient from './DispatchHttpRequestClient.js';
67
import * as GdpClient from './GdpClient.js';
78
import * as InspectorFrontendHost from './InspectorFrontendHost.js';
89
import * as InspectorFrontendHostAPI from './InspectorFrontendHostAPI.js';
@@ -12,6 +13,7 @@ import * as UserMetrics from './UserMetrics.js';
1213

1314
export {
1415
AidaClient,
16+
DispatchHttpRequestClient,
1517
GdpClient,
1618
InspectorFrontendHost,
1719
InspectorFrontendHostAPI,

0 commit comments

Comments
 (0)