зеркало из https://github.com/mozilla/gecko-dev.git
Bug 883002 - Add opensearch support to the mozbrowser API r=jlebar
This commit is contained in:
Родитель
cd96dae673
Коммит
c5953171fa
|
@ -171,7 +171,7 @@ BrowserElementChild.prototype = {
|
|||
/* wantsUntrusted = */ false);
|
||||
|
||||
addEventListener('DOMLinkAdded',
|
||||
this._iconChangedHandler.bind(this),
|
||||
this._linkAddedHandler.bind(this),
|
||||
/* useCapture = */ true,
|
||||
/* wantsUntrusted = */ false);
|
||||
|
||||
|
@ -447,22 +447,45 @@ BrowserElementChild.prototype = {
|
|||
},
|
||||
|
||||
_iconChangedHandler: function(e) {
|
||||
debug("Got iconchanged: (" + e.target.href + ")");
|
||||
var hasIcon = e.target.rel.split(' ').some(function(x) {
|
||||
return x.toLowerCase() === 'icon';
|
||||
});
|
||||
debug('Got iconchanged: (' + e.target.href + ')');
|
||||
|
||||
if (hasIcon) {
|
||||
var win = e.target.ownerDocument.defaultView;
|
||||
// Ignore iconchanges which don't come from the top-level
|
||||
// <iframe mozbrowser> window.
|
||||
if (win == content) {
|
||||
sendAsyncMsg('iconchange', { _payload_: e.target.href });
|
||||
}
|
||||
else {
|
||||
debug("Not top level!");
|
||||
}
|
||||
sendAsyncMsg('iconchange', { _payload_: e.target.href });
|
||||
},
|
||||
|
||||
_openSearchHandler: function(e) {
|
||||
debug('Got opensearch: (' + e.target.href + ')');
|
||||
|
||||
if (e.target.type !== "application/opensearchdescription+xml") {
|
||||
return;
|
||||
}
|
||||
|
||||
sendAsyncMsg('opensearch', { title: e.target.title,
|
||||
href: e.target.href });
|
||||
|
||||
},
|
||||
|
||||
// Processes the "rel" field in <link> tags and forward to specific handlers.
|
||||
_linkAddedHandler: function(e) {
|
||||
let win = e.target.ownerDocument.defaultView;
|
||||
// Ignore links which don't come from the top-level
|
||||
// <iframe mozbrowser> window.
|
||||
if (win != content) {
|
||||
debug('Not top level!');
|
||||
return;
|
||||
}
|
||||
|
||||
let handlers = {
|
||||
'icon': this._iconChangedHandler,
|
||||
'search': this._openSearchHandler
|
||||
};
|
||||
|
||||
debug('Got linkAdded: (' + e.target.href + ') ' + e.target.rel);
|
||||
e.target.rel.split(' ').forEach(function(x) {
|
||||
let token = x.toLowerCase();
|
||||
if (handlers[token]) {
|
||||
handlers[token](e);
|
||||
}
|
||||
}, this);
|
||||
},
|
||||
|
||||
_addMozAfterPaintHandler: function(callback) {
|
||||
|
|
|
@ -113,6 +113,7 @@ function BrowserElementParent(frameLoader, hasRemoteFrame) {
|
|||
"titlechange": this._fireEventFromMsg,
|
||||
"iconchange": this._fireEventFromMsg,
|
||||
"close": this._fireEventFromMsg,
|
||||
"opensearch": this._fireEventFromMsg,
|
||||
"securitychange": this._fireEventFromMsg,
|
||||
"error": this._fireEventFromMsg,
|
||||
"scroll": this._fireEventFromMsg,
|
||||
|
|
|
@ -42,7 +42,9 @@ MOCHITEST_FILES = \
|
|||
test_browserElement_inproc_BrowserWindowNamespace.html \
|
||||
file_browserElement_BrowserWindowNamespace.html \
|
||||
browserElement_Iconchange.js \
|
||||
browserElement_Opensearch.js \
|
||||
test_browserElement_inproc_Iconchange.html \
|
||||
test_browserElement_inproc_Opensearch.html \
|
||||
browserElement_GetScreenshot.js \
|
||||
test_browserElement_inproc_GetScreenshot.html \
|
||||
browserElement_BadScreenshot.js \
|
||||
|
@ -196,6 +198,7 @@ MOCHITEST_FILES += \
|
|||
test_browserElement_oop_BrowserWindowNamespace.html \
|
||||
test_browserElement_oop_TopBarrier.html \
|
||||
test_browserElement_oop_Iconchange.html \
|
||||
test_browserElement_oop_Opensearch.html \
|
||||
test_browserElement_oop_GetScreenshot.html \
|
||||
test_browserElement_oop_BadScreenshot.html \
|
||||
test_browserElement_oop_XFrameOptions.html \
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
/* Any copyright is dedicated to the public domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
// Test that the onmozbrowseropensearch event works.
|
||||
"use strict";
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
browserElementTestHelpers.setEnabledPref(true);
|
||||
browserElementTestHelpers.addPermission();
|
||||
|
||||
function createHtml(link) {
|
||||
return 'data:text/html,<html><head>' + link + '<body></body></html>';
|
||||
}
|
||||
|
||||
function createLink(name) {
|
||||
return '<link rel="search" title="Test OpenSearch" type="application/opensearchdescription+xml" href="http://example.com/' + name + '.xml">';
|
||||
}
|
||||
|
||||
function runTest() {
|
||||
var iframe1 = document.createElement('iframe');
|
||||
SpecialPowers.wrap(iframe1).mozbrowser = true;
|
||||
document.body.appendChild(iframe1);
|
||||
|
||||
// iframe2 is a red herring; we modify its link but don't listen for
|
||||
// opensearch; we want to make sure that its opensearch events aren't
|
||||
// picked up by the listener on iframe1.
|
||||
var iframe2 = document.createElement('iframe');
|
||||
SpecialPowers.wrap(iframe2).mozbrowser = true;
|
||||
document.body.appendChild(iframe2);
|
||||
|
||||
// iframe3 is another red herring. It's not a mozbrowser, so we shouldn't
|
||||
// get any opensearch events on it.
|
||||
var iframe3 = document.createElement('iframe');
|
||||
document.body.appendChild(iframe3);
|
||||
|
||||
var numLinkChanges = 0;
|
||||
|
||||
iframe1.addEventListener('mozbrowseropensearch', function(e) {
|
||||
|
||||
numLinkChanges++;
|
||||
|
||||
if (numLinkChanges == 1) {
|
||||
is(e.detail.title, 'Test OpenSearch');
|
||||
is(e.detail.href, 'http://example.com/mysearch.xml');
|
||||
|
||||
// We should recieve opensearch events when the user creates new links
|
||||
// to a search engine, but only when we listen for them
|
||||
SpecialPowers.getBrowserFrameMessageManager(iframe1)
|
||||
.loadFrameScript("data:,content.document.title='New title';",
|
||||
/* allowDelayedLoad = */ false);
|
||||
|
||||
SpecialPowers.getBrowserFrameMessageManager(iframe1)
|
||||
.loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
|
||||
/* allowDelayedLoad = */ false);
|
||||
|
||||
SpecialPowers.getBrowserFrameMessageManager(iframe2)
|
||||
.loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
|
||||
/* allowDelayedLoad = */ false);
|
||||
}
|
||||
else if (numLinkChanges == 2) {
|
||||
is(e.detail.href, 'http://example.com/newsearch.xml');
|
||||
|
||||
// Full new pages should trigger opensearch events
|
||||
iframe1.src = createHtml(createLink('3rdsearch'));
|
||||
}
|
||||
else if (numLinkChanges == 3) {
|
||||
is(e.detail.href, 'http://example.com/3rdsearch.xml');
|
||||
|
||||
// the rel attribute can have various space seperated values, make
|
||||
// sure we only pick up correct values for 'opensearch'
|
||||
SpecialPowers.getBrowserFrameMessageManager(iframe1)
|
||||
.loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=someopensearch type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
|
||||
/* allowDelayedLoad = */ false);
|
||||
// Test setting a page with multiple links elements
|
||||
iframe1.src = createHtml(createLink('another') + createLink('search'));
|
||||
}
|
||||
else if (numLinkChanges == 4) {
|
||||
is(e.detail.href, 'http://example.com/another.xml');
|
||||
// 2 events will be triggered by previous test, wait for next
|
||||
}
|
||||
else if (numLinkChanges == 5) {
|
||||
is(e.detail.href, 'http://example.com/search.xml');
|
||||
|
||||
// Make sure opensearch check is case insensitive
|
||||
SpecialPowers.getBrowserFrameMessageManager(iframe1)
|
||||
.loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/ucasesearch.xml>')",
|
||||
/* allowDelayedLoad = */ false);
|
||||
}
|
||||
else if (numLinkChanges == 6) {
|
||||
is(e.detail.href, 'http://example.com/ucasesearch.xml');
|
||||
SimpleTest.finish();
|
||||
} else {
|
||||
ok(false, 'Too many opensearch events.');
|
||||
}
|
||||
});
|
||||
|
||||
iframe3.addEventListener('mozbrowseropensearch', function(e) {
|
||||
ok(false, 'Should not get a opensearch event for iframe3.');
|
||||
});
|
||||
|
||||
|
||||
iframe1.src = createHtml(createLink('mysearch'));
|
||||
// We should not recieve opensearch change events for either of the below iframes
|
||||
iframe2.src = createHtml(createLink('mysearch'));
|
||||
iframe3.src = createHtml(createLink('mysearch'));
|
||||
|
||||
}
|
||||
|
||||
addEventListener('testready', runTest);
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<!--
|
||||
https://bugzilla.mozilla.org/show_bug.cgi?id=883002
|
||||
-->
|
||||
<head>
|
||||
<title>Test for Bug 883002</title>
|
||||
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=883002">Mozilla Bug 883002</a>
|
||||
|
||||
<script type="application/javascript;version=1.7" src="browserElement_Opensearch.js">
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<!--
|
||||
https://bugzilla.mozilla.org/show_bug.cgi?id=883002
|
||||
-->
|
||||
<head>
|
||||
<title>Test for Bug 883002</title>
|
||||
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=883002">Mozilla Bug 883002</a>
|
||||
|
||||
<script type="application/javascript;version=1.7" src="browserElement_Opensearch.js">
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче