Skip to content

Commit

Permalink
[cookies] Refetch cookies when requested from UI
Browse files Browse the repository at this point in the history
Bug: 348683488
Change-Id: I341cb4eae63762dcc45ba24777fdfddda2b0af99
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5675323
Reviewed-by: Wolfgang Beyer <wolfi@chromium.org>
Commit-Queue: Danil Somsikov <dsv@chromium.org>
Auto-Submit: Danil Somsikov <dsv@chromium.org>
  • Loading branch information
danilsomsikov authored and Devtools-frontend LUCI CQ committed Jul 4, 2024
1 parent f06aac4 commit 067880c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
31 changes: 31 additions & 0 deletions front_end/core/sdk/CookieModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ describeWithMockConnection('CookieModel', () => {
await expectCalled(eventListener);
});

it('does not refetch cookies while listening unless requested', async () => {
const cookie = PROTOCOL_COOKIE;
setMockConnectionResponseHandler('Network.getCookies', () => ({cookies: [cookie]}));

const target = createTarget();
const dispatchLoadingFinished = () => target.model(SDK.NetworkManager.NetworkManager)!.dispatchEventToListeners(
SDK.NetworkManager.Events.LoadingFinished, createNetworkRequest('1'));

const mainFrame = getMainFrame(target);
const model = target.model(SDK.CookieModel.CookieModel)!;

createResource(mainFrame, `https://${DOMAIN}/main_resource` as Platform.DevToolsPath.UrlString, 'text/html', '');
dispatchLoadingFinished();

let [readCookie] = await model.getCookiesForDomain(`https://${DOMAIN}`);
assert.strictEqual(readCookie.value(), 'value');

cookie.value = 'new value';

model.addEventListener(SDK.CookieModel.Events.CookieListUpdated, () => {});

[readCookie] = await model.getCookiesForDomain(`https://${DOMAIN}`);
assert.strictEqual(readCookie.value(), 'value');

[readCookie] = await model.getCookiesForDomain(`https://${DOMAIN}`);
assert.strictEqual(readCookie.value(), 'value');

[readCookie] = await model.getCookiesForDomain(`https://${DOMAIN}`, true);
assert.strictEqual(readCookie.value(), 'new value');
});

it('clears stored blocked cookies on primary page change', async () => {
const target = createTarget();
const cookieModel = new SDK.CookieModel.CookieModel(target);
Expand Down
4 changes: 2 additions & 2 deletions front_end/core/sdk/CookieModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export class CookieModel extends SDKModel<EventTypes> {
/**
* Returns cookies needed by current page's frames whose security origins are |domain|.
*/
async getCookiesForDomain(domain: string): Promise<Cookie[]> {
if (!this.#isRefreshing()) {
async getCookiesForDomain(domain: string, forceUpdate?: boolean): Promise<Cookie[]> {
if (!this.#isRefreshing() || forceUpdate) {
await this.#refreshThrottled();
}
const normalCookies = this.#cookies.get(domain) || [];
Expand Down
10 changes: 7 additions & 3 deletions front_end/panels/application/CookieItemsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ export class CookieItemsView extends StorageItemsView {
}

setCookiesDomain(model: SDK.CookieModel.CookieModel, domain: string): void {
this.model.removeEventListener(SDK.CookieModel.Events.CookieListUpdated, this.refreshItems, this);
this.model.removeEventListener(SDK.CookieModel.Events.CookieListUpdated, this.onCookieListUpdate, this);
this.model = model;
this.cookieDomain = domain;
this.refreshItems();
this.model.addEventListener(SDK.CookieModel.Events.CookieListUpdated, this.refreshItems, this);
this.model.addEventListener(SDK.CookieModel.Events.CookieListUpdated, this.onCookieListUpdate, this);
}

private showPreview(cookie: SDK.Cookie.Cookie|null): void {
Expand Down Expand Up @@ -312,10 +312,14 @@ export class CookieItemsView extends StorageItemsView {
}
}

override refreshItems(): void {
private onCookieListUpdate(): void {
void this.model.getCookiesForDomain(this.cookieDomain).then(this.updateWithCookies.bind(this));
}

override refreshItems(): void {
void this.model.getCookiesForDomain(this.cookieDomain, true).then(this.updateWithCookies.bind(this));
}

override wasShown(): void {
super.wasShown();
this.registerCSSFiles([cookieItemsViewStyles]);
Expand Down

0 comments on commit 067880c

Please sign in to comment.