Skip to content

Commit

Permalink
Issue mozilla-mobile#2080: Extract website icon resources via WebExte…
Browse files Browse the repository at this point in the history
…nsion.
  • Loading branch information
pocmo authored and georgf committed Mar 7, 2019
1 parent 05d0fe5 commit c3971bf
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/browser/icons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ android {

dependencies {
implementation project(':support-ktx')
implementation project(':concept-engine')

implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* This web extension looks for known icon tags, collects URLs and available
* meta data (e.g. sizes) and passes that to the app code.
*/

function collect_link_icons(icons, rel) {
document.querySelectorAll('link[rel="' + rel + '"]').forEach(
function(currentValue, currentIndex, listObj) {
icons.push({
'type': rel,
'href': currentValue.href,
'sizes': currentValue.sizes
});
})
}

function collect_meta_property_icons(icons, property) {
document.querySelectorAll('meta[property="' + property + '"]').forEach(
function(currentValue, currentIndex, listObj) {
icons.push({
'type': property,
'href': currentValue.content
})
}
)
}

function collect_meta_name_icons(icons, name) {
document.querySelectorAll('meta[name="' + name + '"]').forEach(
function(currentValue, currentIndex, listObj) {
icons.push({
'type': name,
'href': currentValue.content
})
}
)
}

let icons = [];

collect_link_icons(icons, 'icon');
collect_link_icons(icons, 'shortcut icon');
collect_link_icons(icons, 'fluid-icon')
collect_link_icons(icons, 'apple-touch-icon')
collect_link_icons(icons, 'image_src')
collect_link_icons(icons, 'apple-touch-icon image_src')
collect_link_icons(icons, 'apple-touch-icon-precomposed')

collect_meta_property_icons(icons, 'og:image')
collect_meta_property_icons(icons, 'og:image:url')
collect_meta_property_icons(icons, 'og:image:secure_url')

collect_meta_name_icons(icons, 'twitter:image')
collect_meta_name_icons(icons, 'msapplication-TileImage')

// TODO: For now we are just logging the icons we found here. We need the Messaging API
// to actually be able to pass them to the Android world.
// https://github.com/mozilla-mobile/android-components/issues/2243
// https://bugzilla.mozilla.org/show_bug.cgi?id=1518843
console.log("browser-icons: (" + icons.length + ")", document.location.href, icons)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"manifest_version": 2,
"name": "Mozilla Android Components - Browser Icons",
"version": "1.0",
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["icons.js"],
"run_at": "document_end"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import mozilla.components.browser.icons.generator.DefaultIconGenerator
import mozilla.components.browser.icons.generator.IconGenerator
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.webextension.WebExtension
import mozilla.components.support.base.log.logger.Logger
import java.util.concurrent.Executors

// Number of worker threads we are using internally.
Expand All @@ -34,4 +37,20 @@ class BrowserIcons(
// For now we only generate an icon.
generator.generate(context, request)
}

/**
* Installs the "icons" extension in the engine in order to dynamically load icons for loaded websites.
*/
fun install(engine: Engine) {
engine.installWebExtension(
WebExtension(
id = "browser-icons",
url = "resource://android/assets/extensions/browser-icons/"),
onSuccess = {
Logger.debug("Installed browser-icons extension")
},
onError = { _, throwable ->
Logger.error("Could not install browser-icons extension", throwable)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Components(private val applicationContext: Context) : DefaultComponents(ap
installWebExtension(WebExtension("mozac-borderify", "resource://android/assets/extensions/borderify/")) {
ext, throwable -> Log.log(Log.Priority.ERROR, "SampleBrowser", throwable, "Failed to install ${ext.id}")
}

icons.install(this)
}
}
}

0 comments on commit c3971bf

Please sign in to comment.