Bug 1178513 - Add <extapp> element and interfaces to be used by ACL. r=khuey

This commit is contained in:
William Chen 2015-06-30 11:27:57 -07:00
parent d8e47d6d68
commit b672752886
18 changed files with 366 additions and 1 deletions

View File

@ -542,6 +542,12 @@ this.PermissionsTable = { geolocation: {
trusted: DENY_ACTION,
privileged: DENY_ACTION,
certified: ALLOW_ACTION
},
"external-app": {
app: DENY_ACTION,
trusted: DENY_ACTION,
privileged: ALLOW_ACTION,
certified: ALLOW_ACTION
}
};

View File

@ -366,6 +366,7 @@ GK_ATOM(excludeResultPrefixes, "exclude-result-prefixes")
GK_ATOM(excludes, "excludes")
GK_ATOM(expr, "expr")
GK_ATOM(expectingSystemMessage, "expecting-system-message")
GK_ATOM(extapp, "extapp")
GK_ATOM(extends, "extends")
GK_ATOM(extensionElementPrefixes, "extension-element-prefixes")
GK_ATOM(face, "face")

View File

@ -0,0 +1,185 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/dom/HTMLExtAppElement.h"
#include "mozilla/dom/HTMLUnknownElement.h"
#include "mozilla/dom/HTMLExtAppElementBinding.h"
#include "mozilla/dom/ExternalAppEvent.h"
#include "nsGkAtoms.h"
#include "nsIAtom.h"
#include "nsIPermissionManager.h"
#include "nsStyleConsts.h"
#include "nsRuleData.h"
nsGenericHTMLElement*
NS_NewHTMLExtAppElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
mozilla::dom::FromParser aFromParser) {
// Return HTMLUnknownElement if the document doesn't have the 'external-app' permission.
nsCOMPtr<nsIPermissionManager> permissionManager =
mozilla::services::GetPermissionManager();
nsRefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
nsIPrincipal* principal = ni->GetDocument()->NodePrincipal();
already_AddRefed<mozilla::dom::NodeInfo> aarni = ni.forget();
if (!permissionManager) {
return new HTMLUnknownElement(aarni);
}
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
permissionManager->TestExactPermissionFromPrincipal(principal,
"external-app",
&perm);
if (perm != nsIPermissionManager::ALLOW_ACTION) {
return new HTMLUnknownElement(aarni);
}
return new HTMLExtAppElement(aarni);
}
namespace mozilla {
namespace dom {
HTMLExtAppElement::HTMLExtAppElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsGenericHTMLElement(aNodeInfo)
{
mCustomEventDispatch = new nsCustomEventDispatch(this);
mCustomPropertyBag = new nsCustomPropertyBag();
nsCOMPtr<nsIExternalApplication> app =
do_CreateInstance(NS_EXTERNALAPP_CONTRACTID);
if (app) {
nsresult rv = app->Init(OwnerDoc()->GetInnerWindow(), mCustomPropertyBag, mCustomEventDispatch);
if (NS_SUCCEEDED(rv)) {
mApp = app;
}
}
}
HTMLExtAppElement::~HTMLExtAppElement()
{
mCustomEventDispatch->ClearEventTarget();
}
void
HTMLExtAppElement::GetCustomProperty(const nsAString& aName, nsString& aReturn)
{
mCustomPropertyBag->GetCustomProperty(aName, aReturn);
}
void
HTMLExtAppElement::PostMessage(const nsAString& aMessage, ErrorResult& aRv)
{
if (!mApp) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
aRv = mApp->PostMessage(aMessage);
}
NS_IMPL_ADDREF_INHERITED(HTMLExtAppElement, Element)
NS_IMPL_RELEASE_INHERITED(HTMLExtAppElement, Element)
NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLExtAppElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLExtAppElement,
nsGenericHTMLElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLExtAppElement,
nsGenericHTMLElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
// QueryInterface implementation for HTMLExtAppElement
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(HTMLExtAppElement)
NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement)
NS_IMPL_ELEMENT_CLONE(HTMLExtAppElement)
JSObject*
HTMLExtAppElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
{
return HTMLExtAppElementBinding::Wrap(aCx, this, aGivenProto);
}
} // namespace dom
} // namespace mozilla
NS_IMPL_ISUPPORTS(nsCustomPropertyBag, nsICustomPropertyBag)
nsCustomPropertyBag::nsCustomPropertyBag()
{
}
nsCustomPropertyBag::~nsCustomPropertyBag()
{
}
NS_IMETHODIMP
nsCustomPropertyBag::SetProperty(const nsAString& aName, const nsAString& aValue)
{
mBag.Put(nsString(aName), new nsString(aValue));
return NS_OK;
}
NS_IMETHODIMP
nsCustomPropertyBag::RemoveProperty(const nsAString& aName)
{
mBag.Remove(nsString(aName));
return NS_OK;
}
void
nsCustomPropertyBag::GetCustomProperty(const nsAString& aName, nsString& aReturn)
{
nsString* value;
if (!mBag.Get(nsString(aName), &value)) {
aReturn.Truncate();
return;
}
MOZ_ASSERT(value);
aReturn.Assign(*value);
}
NS_IMPL_ISUPPORTS(nsCustomEventDispatch, nsICustomEventDispatch)
nsCustomEventDispatch::nsCustomEventDispatch(mozilla::dom::EventTarget* aEventTarget)
: mEventTarget(aEventTarget)
{
MOZ_ASSERT(mEventTarget);
}
void
nsCustomEventDispatch::ClearEventTarget()
{
mEventTarget = nullptr;
}
nsCustomEventDispatch::~nsCustomEventDispatch()
{
}
NS_IMETHODIMP
nsCustomEventDispatch::DispatchExternalEvent(const nsAString& value)
{
if (!mEventTarget) {
return NS_OK;
}
mozilla::dom::ExternalAppEventInit init;
init.mData = value;
nsRefPtr<mozilla::dom::ExternalAppEvent> event =
mozilla::dom::ExternalAppEvent::Constructor(mEventTarget,
NS_LITERAL_STRING("externalappevent"),
init);
bool defaultActionEnabled;
return mEventTarget->DispatchEvent(event, &defaultActionEnabled);
}

View File

@ -0,0 +1,79 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_HTMLExtAppElement_h
#define mozilla_dom_HTMLExtAppElement_h
#include "nsGenericHTMLElement.h"
#include "nsIExternalApplication.h"
class nsCustomEventDispatch;
class nsCustomPropertyBag;
namespace mozilla {
namespace dom {
class HTMLExtAppElement final : public nsGenericHTMLElement
{
public:
explicit HTMLExtAppElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
// nsISupports
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLExtAppElement,
nsGenericHTMLElement)
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const override;
void GetCustomProperty(const nsAString& aName, nsString& aReturn);
void PostMessage(const nsAString& aMessage, ErrorResult& aRv);
protected:
virtual ~HTMLExtAppElement();
virtual JSObject* WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
nsRefPtr<nsCustomEventDispatch> mCustomEventDispatch;
nsRefPtr<nsCustomPropertyBag> mCustomPropertyBag;
nsCOMPtr<nsIExternalApplication> mApp;
};
} // namespace dom
} // namespace mozilla
class nsCustomEventDispatch final : public nsICustomEventDispatch
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICUSTOMEVENTDISPATCH
explicit nsCustomEventDispatch(mozilla::dom::EventTarget* aEventTarget);
void ClearEventTarget();
private:
~nsCustomEventDispatch();
// Weak pointer, this object is owned by the event target.
mozilla::dom::EventTarget* mEventTarget;
};
class nsCustomPropertyBag final : public nsICustomPropertyBag
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICUSTOMPROPERTYBAG
nsCustomPropertyBag();
void GetCustomProperty(const nsAString& aName, nsString& aReturn);
private:
~nsCustomPropertyBag();
nsClassHashtable<nsStringHashKey, nsString> mBag;
};
#endif // mozilla_dom_HTMLExtAppElement_h

View File

@ -57,6 +57,7 @@ EXPORTS.mozilla.dom += [
'HTMLDataElement.h',
'HTMLDataListElement.h',
'HTMLDivElement.h',
'HTMLExtAppElement.h',
'HTMLFieldSetElement.h',
'HTMLFontElement.h',
'HTMLFormControlsCollection.h',
@ -134,6 +135,7 @@ UNIFIED_SOURCES += [
'HTMLDataListElement.cpp',
'HTMLDivElement.cpp',
'HTMLElement.cpp',
'HTMLExtAppElement.cpp',
'HTMLFieldSetElement.cpp',
'HTMLFontElement.cpp',
'HTMLFormControlsCollection.cpp',

View File

@ -1742,6 +1742,7 @@ NS_DECLARE_NS_NEW_HTML_ELEMENT(Mod)
NS_DECLARE_NS_NEW_HTML_ELEMENT(Data)
NS_DECLARE_NS_NEW_HTML_ELEMENT(DataList)
NS_DECLARE_NS_NEW_HTML_ELEMENT(Div)
NS_DECLARE_NS_NEW_HTML_ELEMENT(ExtApp)
NS_DECLARE_NS_NEW_HTML_ELEMENT(FieldSet)
NS_DECLARE_NS_NEW_HTML_ELEMENT(Font)
NS_DECLARE_NS_NEW_HTML_ELEMENT(Form)

View File

@ -594,4 +594,5 @@ support-files = file_bug871161-1.html file_bug871161-2.html
[test_window_open_close.html]
skip-if = buildapp == 'b2g' # bug 1129014
[test_img_complete.html]
[test_viewport_resize.html]
[test_viewport_resize.html]
[test_extapp.html]

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Test for extapp element being HTMLUnknownElement when permission is not available</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
assert_true(document.createElement("extapp") instanceof HTMLUnknownElement);
}, "extapp should be HTMLUnknownElement when external-app permission is not enabled.");
</script>

View File

@ -440,6 +440,8 @@ var interfaceNamesInGlobalScope =
"EventTarget",
// IMPORTANT: Do not change this list without review from a DOM peer!
{name: "External", b2g: false},
// IMPORTANT: Do not change this list without review from a DOM peer!
{name: "ExternalAppEvent", b2g: true, permission: ["external-app"]},
// IMPORTANT: Do not change this list without review from a DOM peer!
"File",
// IMPORTANT: Do not change this list without review from a DOM peer!
@ -518,6 +520,8 @@ var interfaceNamesInGlobalScope =
"HTMLElement",
// IMPORTANT: Do not change this list without review from a DOM peer!
"HTMLEmbedElement",
// IMPORTANT: Do not change this list without review from a DOM peer!
{name: "HTMLExternalAppElement", b2g: true, permission: ["external-app"]},
// IMPORTANT: Do not change this list without review from a DOM peer!
"HTMLFieldSetElement",
// IMPORTANT: Do not change this list without review from a DOM peer!

View File

@ -0,0 +1,17 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*/
[Constructor(DOMString type, optional ExternalAppEventInit eventInitDict),
CheckPermissions="external-app"]
interface ExternalAppEvent : Event
{
readonly attribute DOMString data;
};
dictionary ExternalAppEventInit : EventInit
{
DOMString data = "";
};

View File

@ -0,0 +1,16 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*/
[CheckPermissions="external-app"]
interface HTMLExtAppElement : HTMLElement {
// Gets the value of the property from a property bag
// that was provided to the external application.
DOMString getCustomProperty(DOMString name);
// Posts a message to the external application.
[Throws]
void postMessage(DOMString name);
};

View File

@ -184,6 +184,7 @@ WEBIDL_FILES = [
'HTMLDocument.webidl',
'HTMLElement.webidl',
'HTMLEmbedElement.webidl',
'HTMLExtAppElement.webidl',
'HTMLFieldSetElement.webidl',
'HTMLFontElement.webidl',
'HTMLFormControlsCollection.webidl',
@ -744,6 +745,7 @@ GENERATED_EVENTS_WEBIDL_FILES = [
'DOMTransactionEvent.webidl',
'DownloadEvent.webidl',
'ErrorEvent.webidl',
'ExternalAppEvent.webidl',
'HashChangeEvent.webidl',
'IccChangeEvent.webidl',
'ImageCaptureErrorEvent.webidl',

View File

@ -664,6 +664,7 @@ static const nsElementInfo kElements[eHTMLTag_userdefined] = {
ELEM(dt, true, true, GROUP_DL_CONTENT, GROUP_INLINE_ELEMENT),
ELEM(em, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(embed, false, false, GROUP_NONE, GROUP_NONE),
ELEM(extapp, true, true, GROUP_NONE, GROUP_NONE),
ELEM(fieldset, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(figcaption, true, false, GROUP_FIGCAPTION, GROUP_FLOW_ELEMENT),
ELEM(figure, true, true, GROUP_BLOCK,

View File

@ -171,6 +171,10 @@ const nsHTMLElement gHTMLElements[] = {
/*tag*/ eHTMLTag_embed,
/*parent,leaf*/ kSpecial, true
},
{
/*tag*/ eHTMLTag_extapp,
/*parent,leaf*/ kNone, false
},
{
/*tag*/ eHTMLTag_fieldset,
/*parent,leaf*/ kBlock, false

View File

@ -73,6 +73,7 @@ HTML_TAG(dl, SharedList)
HTML_HTMLELEMENT_TAG(dt)
HTML_HTMLELEMENT_TAG(em)
HTML_TAG(embed, SharedObject)
HTML_TAG(extapp, ExtApp)
HTML_TAG(fieldset, FieldSet)
HTML_HTMLELEMENT_TAG(figcaption)
HTML_HTMLELEMENT_TAG(figure)

View File

@ -93,6 +93,8 @@ static const char16_t sHTMLTagUnicodeName_em[] =
{'e', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_embed[] =
{'e', 'm', 'b', 'e', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_extapp[] =
{'e', 'x', 't', 'a', 'p', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_fieldset[] =
{'f', 'i', 'e', 'l', 'd', 's', 'e', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_figcaption[] =

View File

@ -7,6 +7,7 @@
XPIDL_SOURCES += [
'nsIBlocklistService.idl',
'nsIDeviceSensors.idl',
'nsIExternalApplication.idl',
'nsIGConfService.idl',
'nsIGeolocationProvider.idl',
'nsIGIOService.idl',

View File

@ -0,0 +1,31 @@
/* 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/. */
#include "nsISupports.idl"
interface nsPIDOMWindow;
[builtinclass, uuid(adf6b501-609a-4f54-ac16-39b54d6358b5)]
interface nsICustomEventDispatch : nsISupports {
// Dispatches an event named "externalappevent" with the data
// in a property named "data" on the event.
void dispatchExternalEvent(in AString data);
};
[builtinclass, uuid(4d797f32-a24d-4cbc-b608-1eb0fc8e442b)]
interface nsICustomPropertyBag : nsISupports {
void setProperty(in AString name, in AString value);
void removeProperty(in AString name);
};
[builtinclass, uuid(c100de94-e699-4941-b528-eaff1af5b15c)]
interface nsIExternalApplication : nsISupports {
void init(in nsPIDOMWindow window, in nsICustomPropertyBag bag, in nsICustomEventDispatch callback);
void postMessage(in AString message);
};
%{C++
#define NS_EXTERNALAPP_CONTRACTID "@mozilla.org/externalapp;1"
#define NS_EXTERNALAPP_CID {0x8ec5dce2, 0x67e1, 0x49cd, {0xa0, 0x12, 0xf9, 0xfe, 0x32, 0x00, 0xd0, 0x8e}}
%}