diff options
Diffstat (limited to 'src/models/Service.js')
-rw-r--r-- | src/models/Service.js | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/src/models/Service.js b/src/models/Service.js index 5656295da..fe56e5a76 100644 --- a/src/models/Service.js +++ b/src/models/Service.js | |||
@@ -2,7 +2,7 @@ import { autorun, computed, observable } from 'mobx'; | |||
2 | import { ipcRenderer } from 'electron'; | 2 | import { ipcRenderer } from 'electron'; |
3 | import { webContents } from '@electron/remote'; | 3 | import { webContents } from '@electron/remote'; |
4 | import normalizeUrl from 'normalize-url'; | 4 | import normalizeUrl from 'normalize-url'; |
5 | import path from 'path'; | 5 | import { join } from 'path'; |
6 | 6 | ||
7 | import { todosStore } from '../features/todos'; | 7 | import { todosStore } from '../features/todos'; |
8 | import { isValidExternalURL } from '../helpers/url-helpers'; | 8 | import { isValidExternalURL } from '../helpers/url-helpers'; |
@@ -10,11 +10,6 @@ import UserAgent from './UserAgent'; | |||
10 | 10 | ||
11 | const debug = require('debug')('Ferdi:Service'); | 11 | const debug = require('debug')('Ferdi:Service'); |
12 | 12 | ||
13 | export const RESTRICTION_TYPES = { | ||
14 | SERVICE_LIMIT: 0, | ||
15 | CUSTOM_URL: 1, | ||
16 | }; | ||
17 | |||
18 | export default class Service { | 13 | export default class Service { |
19 | id = ''; | 14 | id = ''; |
20 | 15 | ||
@@ -42,8 +37,6 @@ export default class Service { | |||
42 | 37 | ||
43 | @observable isMuted = false; | 38 | @observable isMuted = false; |
44 | 39 | ||
45 | @observable isHibernating = false; | ||
46 | |||
47 | @observable team = ''; | 40 | @observable team = ''; |
48 | 41 | ||
49 | @observable customUrl = ''; | 42 | @observable customUrl = ''; |
@@ -82,10 +75,12 @@ export default class Service { | |||
82 | 75 | ||
83 | @observable isHibernationEnabled = false; | 76 | @observable isHibernationEnabled = false; |
84 | 77 | ||
85 | @observable isHibernating = false; | 78 | @observable isHibernationRequested = false; |
86 | 79 | ||
87 | @observable lastUsed = Date.now(); // timestamp | 80 | @observable lastUsed = Date.now(); // timestamp |
88 | 81 | ||
82 | @observable lastHibernated = null; // timestamp | ||
83 | |||
89 | @observable lastPoll = Date.now(); | 84 | @observable lastPoll = Date.now(); |
90 | 85 | ||
91 | @observable lastPollAnswer = Date.now(); | 86 | @observable lastPollAnswer = Date.now(); |
@@ -150,14 +145,11 @@ export default class Service { | |||
150 | this.recipe = recipe; | 145 | this.recipe = recipe; |
151 | 146 | ||
152 | // Check if "Hibernate on Startup" is enabled and hibernate all services except active one | 147 | // Check if "Hibernate on Startup" is enabled and hibernate all services except active one |
153 | const { | 148 | const { hibernateOnStartup } = window.ferdi.stores.settings.app; |
154 | hibernate, | ||
155 | hibernateOnStartup, | ||
156 | } = window.ferdi.stores.settings.app; | ||
157 | // The service store is probably not loaded yet so we need to use localStorage data to get active service | 149 | // The service store is probably not loaded yet so we need to use localStorage data to get active service |
158 | const isActive = window.localStorage.service && JSON.parse(window.localStorage.service).activeService === this.id; | 150 | const isActive = window.localStorage.service && JSON.parse(window.localStorage.service).activeService === this.id; |
159 | if (hibernate && hibernateOnStartup && !isActive) { | 151 | if (hibernateOnStartup && !isActive) { |
160 | this.isHibernating = true; | 152 | this.isHibernationRequested = true; |
161 | } | 153 | } |
162 | 154 | ||
163 | autorun(() => { | 155 | autorun(() => { |
@@ -190,6 +182,14 @@ export default class Service { | |||
190 | return this.recipe.id === todosStore.todoRecipeId; | 182 | return this.recipe.id === todosStore.todoRecipeId; |
191 | } | 183 | } |
192 | 184 | ||
185 | @computed get canHibernate() { | ||
186 | return this.isHibernationEnabled; | ||
187 | } | ||
188 | |||
189 | @computed get isHibernating() { | ||
190 | return this.canHibernate && this.isHibernationRequested; | ||
191 | } | ||
192 | |||
193 | get webview() { | 193 | get webview() { |
194 | if (this.isTodosService) { | 194 | if (this.isTodosService) { |
195 | return todosStore.webview; | 195 | return todosStore.webview; |
@@ -230,7 +230,7 @@ export default class Service { | |||
230 | return this.iconUrl; | 230 | return this.iconUrl; |
231 | } | 231 | } |
232 | 232 | ||
233 | return path.join(this.recipe.path, 'icon.svg'); | 233 | return join(this.recipe.path, 'icon.svg'); |
234 | } | 234 | } |
235 | 235 | ||
236 | @computed get hasCustomIcon() { | 236 | @computed get hasCustomIcon() { |
@@ -275,11 +275,17 @@ export default class Service { | |||
275 | debug(this.name, 'modifyRequestHeaders is not defined in the recipe'); | 275 | debug(this.name, 'modifyRequestHeaders is not defined in the recipe'); |
276 | } | 276 | } |
277 | 277 | ||
278 | this.webview.addEventListener('ipc-message', e => handleIPCMessage({ | 278 | this.webview.addEventListener('ipc-message', async (e) => { |
279 | serviceId: this.id, | 279 | if (e.channel === 'inject-js-unsafe') { |
280 | channel: e.channel, | 280 | await Promise.all(e.args.map((script) => this.webview.executeJavaScript(`"use strict"; (() => { ${script} })();`))); |
281 | args: e.args, | 281 | } else { |
282 | })); | 282 | handleIPCMessage({ |
283 | serviceId: this.id, | ||
284 | channel: e.channel, | ||
285 | args: e.args, | ||
286 | }); | ||
287 | } | ||
288 | }); | ||
283 | 289 | ||
284 | this.webview.addEventListener('new-window', (event, url, frameName, options) => { | 290 | this.webview.addEventListener('new-window', (event, url, frameName, options) => { |
285 | debug('new-window', event, url, frameName, options); | 291 | debug('new-window', event, url, frameName, options); |
@@ -334,6 +340,11 @@ export default class Service { | |||
334 | this.hasCrashed = true; | 340 | this.hasCrashed = true; |
335 | }); | 341 | }); |
336 | 342 | ||
343 | this.webview.addEventListener('found-in-page', ({ result }) => { | ||
344 | debug('Found in page', result); | ||
345 | this.webview.send('found-in-page', result); | ||
346 | }); | ||
347 | |||
337 | webviewWebContents.on('login', (event, request, authInfo, callback) => { | 348 | webviewWebContents.on('login', (event, request, authInfo, callback) => { |
338 | // const authCallback = callback; | 349 | // const authCallback = callback; |
339 | debug('browser login event', authInfo); | 350 | debug('browser login event', authInfo); |