Skip to content

Commit

Permalink
feat(Service): Add method to clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Jan 4, 2022
1 parent fe69274 commit 06e6d65
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/electron/ipc-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import focusState from './focusState';
import serviceWebView from '../../features/serviceWebview/ipc';
import fullscreenStatus from './fullscreen';
import subscriptionWindow from './subscriptionWindow';
import serviceCache from './serviceCache';

export default (params) => {
settings(params);
Expand All @@ -22,4 +23,5 @@ export default (params) => {
serviceWebView(params);
fullscreenStatus(params);
subscriptionWindow(params);
serviceCache();
};
15 changes: 15 additions & 0 deletions src/electron/ipc-api/serviceCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ipcMain } from 'electron';

const debug = require('debug')('Franz:ipcApi:serviceCache');

export default () => {
ipcMain.handle('clearServiceCache', ({ sender: webContents }) => {
debug('Clearing cache for service');
const { session } = webContents;

session.flushStorageData();
session.clearStorageData({
storages: ['appcache', 'serviceworkers', 'cachestorage', 'websql', 'indexdb'],
});
});
};
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,29 @@ ipcMain.on('modifyRequestHeaders', (e, { modifiedRequestHeaders, serviceId }) =>
});
});

ipcMain.on('cleanServiceCache', (e, { modifiedRequestHeaders, serviceId }) => {
debug('Received modifyRequestHeaders', modifiedRequestHeaders, serviceId);
modifiedRequestHeaders.forEach((headerFilterSet) => {
const { headers, requestFilters } = headerFilterSet;
session.fromPartition(`persist:service-${serviceId}`).webRequest.onBeforeSendHeaders(requestFilters, (details, callback) => {
for (const key in headers) {
if (Object.prototype.hasOwnProperty.call(headers, key)) {
const value = headers[key];
if (value === 'RefererHost') {
if (Object.prototype.hasOwnProperty.call(details.requestHeaders, 'Referer')) {
const { hostname } = new URL(details.requestHeaders.Referer);
details.requestHeaders[key] = `https://${hostname}`;
}
} else {
details.requestHeaders[key] = value;
}
}
}
callback({ requestHeaders: details.requestHeaders });
});
});
});

ipcMain.on('feature-basic-auth-cancel', () => {
debug('Cancel basic auth');

Expand Down
9 changes: 9 additions & 0 deletions src/webview/lib/RecipeWebview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class RecipeWebview {
// communicating with the client
ipcRenderer.sendToHost('alive');
});

window.FranzAPI = {
clearCache: RecipeWebview.clearCache,
};
}

loopFunc = () => null;
Expand Down Expand Up @@ -76,6 +80,7 @@ class RecipeWebview {
});
}


onNotify(fn) {
if (typeof fn === 'function') {
window.Notification.prototype.onNotify = fn;
Expand All @@ -87,6 +92,10 @@ class RecipeWebview {
fn();
}
}

static clearCache() {
ipcRenderer.invoke('clearServiceCache');
}
}

module.exports = RecipeWebview;
2 changes: 2 additions & 0 deletions src/webview/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { DEFAULT_APP_SETTINGS_VANILLA } from '../configVanilla';

const debug = require('debug')('Franz:Plugin');

window.FranzAPI = {};

class RecipeController {
@observable settings = {
app: DEFAULT_APP_SETTINGS_VANILLA,
Expand Down

0 comments on commit 06e6d65

Please sign in to comment.