1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This module implements the registration of guestview elements when
6 // permissions are not available. These elements exist only to provide a useful
7 // error message when developers attempt to use them.
9 var DocumentNatives
= requireNative('document_natives');
10 var GuestViewContainer
= require('guestViewContainer').GuestViewContainer
;
12 var ERROR_MESSAGE
= 'You do not have permission to use the %1 element.' +
13 ' Be sure to declare the "%1" permission in your manifest file.';
15 // A list of view types that will have custom elements registered if they are
16 // not already registered by the time this module is loaded.
24 // Registers a GuestView custom element.
25 function registerGuestViewElement(viewType
) {
26 var proto
= Object
.create(HTMLElement
.prototype);
28 proto
.createdCallback = function() {
29 window
.console
.error(ERROR_MESSAGE
.replace(/%1/g, viewType
.toLowerCase()));
32 window
[viewType
] = DocumentNatives
.RegisterElement(viewType
.toLowerCase(),
35 // Delete the callbacks so developers cannot call them and produce unexpected
37 delete proto
.createdCallback
;
38 delete proto
.attachedCallback
;
39 delete proto
.detachedCallback
;
40 delete proto
.attributeChangedCallback
;
43 var useCapture
= true;
44 window
.addEventListener('readystatechange', function listener(event
) {
45 if (document
.readyState
== 'loading')
48 for (var i
= 0; i
!= VIEW_TYPES
.length
; ++i
) {
49 // Register the error-providing custom element only for those view types
50 // that have not already been registered. Since this module is always loaded
51 // last, all the view types that are available (i.e. have the proper
52 // permissions) will have already been registered on |window|.
53 if (!window
[VIEW_TYPES
[i
]])
54 registerGuestViewElement(VIEW_TYPES
[i
]);
57 window
.removeEventListener(event
.type
, listener
, useCapture
);