-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reload failed IPFS tabs when API becomes available (#1092)
- Loading branch information
Showing
11 changed files
with
372 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root=true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { InternalTabReloader } = require('./internalTabReloader') | ||
const { LocalGatewayReloader } = require('./localGatewayReloader') | ||
const { WebUiReloader } = require('./webUiReloader') | ||
|
||
/** | ||
* Prepares extension by creating an instance and awaiting for init. | ||
* | ||
* @param {Array.[InternalTabReloader|LocalGatewayReloader|WebUiReloader]} extensions | ||
* @param {Browser} browserInstance | ||
* @param {Logger} loggerInstance | ||
* @returns {Promise<Array.[InternalTabReloader|LocalGatewayReloader|WebUiReloader]>} | ||
*/ | ||
function prepareReloadExtensions (extensions, browserInstance, loggerInstance) { | ||
const reloadExtensions = Array.isArray(extensions) ? extensions : [extensions] | ||
return Promise.all(reloadExtensions | ||
.map(async Ext => { | ||
try { | ||
const ext = new Ext(browserInstance, loggerInstance) | ||
await ext.init() | ||
return ext | ||
} catch (e) { | ||
loggerInstance(`Extension Instance Failed to Initialize with error: ${e}. Extension: ${Ext}`) | ||
} | ||
}) | ||
) | ||
} | ||
|
||
module.exports = { | ||
InternalTabReloader, | ||
LocalGatewayReloader, | ||
WebUiReloader, | ||
prepareReloadExtensions | ||
} |
37 changes: 37 additions & 0 deletions
37
add-on/src/lib/ipfs-client/reloaders/internalTabReloader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { ReloaderBase } = require('./reloaderBase') | ||
|
||
class InternalTabReloader extends ReloaderBase { | ||
/** | ||
* Setting up the extension origin. | ||
*/ | ||
init () { | ||
this.extensionOrigin = this._browserInstance.runtime.getURL('/') | ||
this._log('InternalTabReloader Ready for use.') | ||
} | ||
|
||
/** | ||
* Performs url validation for the tab. If tab is a WebUI tab. | ||
* | ||
* @param {Object} tab | ||
* @param {string} tab.url | ||
* @returns {boolean} | ||
*/ | ||
validation ({ url }) { | ||
return url.startsWith(this.extensionOrigin) | ||
} | ||
|
||
/** | ||
* Returns message when reloading the tab. | ||
* | ||
* @param {Object} tab | ||
* @param {string} tab.url | ||
* @returns {string} message. | ||
*/ | ||
message ({ url }) { | ||
return `reloading internal extension page at ${url}` | ||
} | ||
} | ||
|
||
module.exports = { | ||
InternalTabReloader | ||
} |
42 changes: 42 additions & 0 deletions
42
add-on/src/lib/ipfs-client/reloaders/localGatewayReloader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { ReloaderBase } = require('./reloaderBase') | ||
const isIPFS = require('is-ipfs') | ||
|
||
class LocalGatewayReloader extends ReloaderBase { | ||
/** | ||
* Performs url validation for the tab. If tab is loaded via local gateway. | ||
* | ||
* @param {Object} tab | ||
* @param {string} tab.url | ||
* @param {string} tab.url | ||
* @returns {boolean} | ||
*/ | ||
validation ({ url, title }) { | ||
/** | ||
* Check if the url is the local gateway url and if the title is contained within the url then it was not loaded. | ||
* - This assumes that the title of most pages on the web will be set and hence when not reachable, the browser | ||
* will set title to the url/host (both chrome and brave) and 'problem loading page' for firefox. | ||
* - There is probability that this might be true in case the <title> tag is omitted, but worst case it only reloads | ||
* those pages. | ||
* - The benefit we get from this approach is the static nature of just observing the tabs in their current state | ||
* which reduces the overhead of injecting content scripts to track urls that were loaded after the connection | ||
* was offline, it may also need extra permissions to inject code on error pages. | ||
*/ | ||
return (isIPFS.url(url) || isIPFS.subdomain(url)) && | ||
(url.includes(title) || title.toLowerCase() === 'problem loading page') | ||
} | ||
|
||
/** | ||
* Returns message when reloading the tab. | ||
* | ||
* @param {Object} tab | ||
* @param {string} tab.url | ||
* @returns {string} message. | ||
*/ | ||
message ({ url }) { | ||
return `reloading local gateway at ${url}` | ||
} | ||
} | ||
|
||
module.exports = { | ||
LocalGatewayReloader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class ReloaderBase { | ||
/** | ||
* Constructor for reloader base class. | ||
* | ||
* @param {Browser} browser | ||
* @param {Logger} log | ||
*/ | ||
constructor (browser, log) { | ||
if (!browser || !log) { | ||
throw new Error('Instances of browser and logger are needed!') | ||
} | ||
this._browserInstance = browser | ||
this._log = log | ||
}; | ||
|
||
/** | ||
* Initializes the instance. | ||
*/ | ||
init () { | ||
this._log('Initialized without additional config.') | ||
} | ||
|
||
/** | ||
* To be implemented in child class. | ||
*/ | ||
validation () { | ||
throw new Error('Validation: Method Not Implemented') | ||
} | ||
|
||
/** | ||
* To be implemented in child class. | ||
*/ | ||
message () { | ||
throw new Error('Message: Method Not Implemented') | ||
} | ||
|
||
/** | ||
* Handles reload for all tabs. | ||
* params {Array<tabs>} | ||
*/ | ||
reload (tabs) { | ||
tabs | ||
.filter(tab => this.validation(tab)) | ||
.forEach(tab => { | ||
this._log(this.message(tab)) | ||
this._browserInstance.tabs.reload(tab.id) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
ReloaderBase | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { ReloaderBase } = require('./reloaderBase') | ||
|
||
class WebUiReloader extends ReloaderBase { | ||
/** | ||
* Performs url validation for the tab. If tab is a WebUI tab. | ||
* | ||
* @param {Object} tab | ||
* @returns {boolean} | ||
*/ | ||
validation ({ url }) { | ||
const bundled = !url.startsWith('http') && url.includes('/webui/index.html#/') | ||
const ipns = url.includes('/webui.ipfs.io/#/') | ||
return bundled || ipns | ||
} | ||
|
||
/** | ||
* Returns message when reloading the tab. | ||
* | ||
* @param {Object} tab | ||
* @param {string} tab.url | ||
* @returns {string} message. | ||
*/ | ||
message ({ url }) { | ||
return `reloading webui at ${url}` | ||
} | ||
} | ||
|
||
module.exports = { | ||
WebUiReloader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.