Skip to content

Commit 62d0549

Browse files
ergunshDevtools-frontend LUCI CQ
authored andcommitted
[GdpIntegration] Make getProfile request and render the profile settings
Drive-by: * Added a check to `GdpClient` to make sure to not make a request to backend when the feature is not available. Bug: 436202677 Change-Id: Ica058e2fd22b8a53334fbd538554e6d5ce6ce85a Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6903164 Commit-Queue: Ergün Erdoğmuş <[email protected]> Reviewed-by: Simon Zünd <[email protected]>
1 parent b41d9fc commit 62d0549

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

front_end/core/host/GdpClient.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import {updateHostConfig} from '../../testing/EnvironmentHelpers.js';
6+
57
import * as Host from './host.js';
68

79
describe('GdpClient', () => {
810
let dispatchHttpRequestStub:
911
sinon.SinonStub<Parameters<typeof Host.InspectorFrontendHost.InspectorFrontendHostInstance.dispatchHttpRequest>>;
1012
beforeEach(() => {
13+
updateHostConfig({
14+
devToolsGdpProfiles: {
15+
enabled: true,
16+
}
17+
});
18+
1119
dispatchHttpRequestStub =
1220
sinon.stub(Host.InspectorFrontendHost.InspectorFrontendHostInstance, 'dispatchHttpRequest')
1321
.callsFake((request, cb) => {
@@ -32,4 +40,19 @@ describe('GdpClient', () => {
3240

3341
sinon.assert.calledOnce(dispatchHttpRequestStub);
3442
});
43+
44+
describe('when the integration is disabled', () => {
45+
it('should not make a request', async () => {
46+
updateHostConfig({
47+
devToolsGdpProfiles: {
48+
enabled: false,
49+
},
50+
});
51+
52+
const profile = await Host.GdpClient.GdpClient.instance({forceNew: true}).getProfile();
53+
54+
assert.isNull(profile);
55+
sinon.assert.notCalled(dispatchHttpRequestStub);
56+
});
57+
});
3558
});

front_end/core/host/GdpClient.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import * as Root from '../root/root.js';
6+
57
import {InspectorFrontendHostInstance} from './InspectorFrontendHost.js';
68
import type {DispatchHttpRequestRequest, DispatchHttpRequestResult} from './InspectorFrontendHostAPI.js';
79

@@ -47,7 +49,11 @@ export interface Profile {
4749
};
4850
}
4951

50-
async function dispatchHttpRequestPromise<R extends object>(request: DispatchHttpRequestRequest): Promise<R|null> {
52+
async function makeHttpRequest<R extends object>(request: DispatchHttpRequestRequest): Promise<R|null> {
53+
if (!Root.Runtime.hostConfig.devToolsGdpProfiles?.enabled) {
54+
return null;
55+
}
56+
5157
const response = await new Promise<DispatchHttpRequestResult>(resolve => {
5258
InspectorFrontendHostInstance.dispatchHttpRequest(request, resolve);
5359
});
@@ -78,12 +84,17 @@ export class GdpClient {
7884
return gdpClientInstance;
7985
}
8086

87+
async initialize(): Promise<void> {
88+
void this.getProfile();
89+
void this.checkEligibility();
90+
}
91+
8192
async getProfile(): Promise<Profile|null> {
8293
if (this.#cachedProfilePromise) {
8394
return await this.#cachedProfilePromise;
8495
}
8596

86-
this.#cachedProfilePromise = dispatchHttpRequestPromise({
97+
this.#cachedProfilePromise = makeHttpRequest({
8798
service: SERVICE_NAME,
8899
path: '/v1beta1/profile:get',
89100
method: 'GET',
@@ -97,13 +108,13 @@ export class GdpClient {
97108
}
98109

99110
this.#cachedEligibilityPromise =
100-
dispatchHttpRequestPromise({service: SERVICE_NAME, path: '/v1beta1/eligibility:check', method: 'GET'});
111+
makeHttpRequest({service: SERVICE_NAME, path: '/v1beta1/eligibility:check', method: 'GET'});
101112

102113
return await this.#cachedEligibilityPromise;
103114
}
104115

105116
createProfile({user, emailPreference}: {user: string, emailPreference: EmailPreference}): Promise<Profile|null> {
106-
return dispatchHttpRequestPromise({
117+
return makeHttpRequest({
107118
service: SERVICE_NAME,
108119
path: '/v1beta1/profiles',
109120
method: 'POST',

front_end/entrypoints/main/MainImpl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ export class MainImpl {
524524
UI.ShortcutRegistry.ShortcutRegistry.instance({forceNew: true, actionRegistry: actionRegistryInstance});
525525
this.#registerMessageSinkListener();
526526

527+
// Initialize `GDPClient` for Google Developer Program integration
528+
if (Root.Runtime.hostConfig.devToolsGdpProfiles?.enabled) {
529+
void Host.GdpClient.GdpClient.instance().initialize();
530+
}
531+
527532
MainImpl.timeEnd('Main._createAppUI');
528533

529534
const appProvider = Common.AppProvider.getRegisteredAppProviders()[0];

front_end/panels/settings/SettingsScreen.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,25 @@ export class GenericSettingsTab extends UI.Widget.VBox implements SettingsTab {
335335
window.clearTimeout(this.#updateSyncSectionTimerId);
336336
this.#updateSyncSectionTimerId = -1;
337337
}
338-
Host.InspectorFrontendHost.InspectorFrontendHostInstance.getSyncInformation(syncInfo => {
339-
this.syncSection.data = {
340-
syncInfo,
341-
syncSetting: Common.Settings.moduleSetting('sync-preferences') as Common.Settings.Setting<boolean>,
342-
receiveBadgesSetting: Common.Settings.Settings.instance().moduleSetting('receive-gdp-badges'),
343-
gdpProfile: undefined,
344-
};
345-
if (!syncInfo.isSyncActive || !syncInfo.arePreferencesSynced) {
346-
this.#updateSyncSectionTimerId = window.setTimeout(this.updateSyncSection.bind(this), 500);
347-
}
348-
});
338+
339+
void Promise
340+
.all([
341+
new Promise<Host.InspectorFrontendHostAPI.SyncInformation>(
342+
resolve => Host.InspectorFrontendHost.InspectorFrontendHostInstance.getSyncInformation(resolve)),
343+
Root.Runtime.hostConfig.devToolsGdpProfiles?.enabled ? Host.GdpClient.GdpClient.instance().getProfile() :
344+
Promise.resolve(undefined),
345+
])
346+
.then(([syncInfo, gdpProfile]) => {
347+
this.syncSection.data = {
348+
syncInfo,
349+
syncSetting: Common.Settings.moduleSetting('sync-preferences') as Common.Settings.Setting<boolean>,
350+
receiveBadgesSetting: Common.Settings.Settings.instance().moduleSetting('receive-gdp-badges'),
351+
gdpProfile: gdpProfile ?? undefined,
352+
};
353+
if (!syncInfo.isSyncActive || !syncInfo.arePreferencesSynced) {
354+
this.#updateSyncSectionTimerId = window.setTimeout(this.updateSyncSection.bind(this), 500);
355+
}
356+
});
349357
}
350358

351359
private createExtensionSection(settings: Common.Settings.SettingRegistration[]): void {

0 commit comments

Comments
 (0)