Guest View: Fix error message string replacement.
[chromium-blink-merge.git] / extensions / renderer / resources / guest_view / guest_view_deny.js
blob7b04f7f8e61957274bb6b149c662916d69018c1a
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.
17 var VIEW_TYPES = [
18   'AppView',
19   'ExtensionOptions',
20   'ExtensionView',
21   'SurfaceWorker',
22   'WebView'
25 // Registers a GuestView custom element.
26 function registerGuestViewElement(viewType) {
27   var proto = Object.create(HTMLElement.prototype);
29   proto.createdCallback = function() {
30     window.console.error(ERROR_MESSAGE.replace(/%1/g, viewType.toLowerCase()));
31   };
33   window[viewType] = DocumentNatives.RegisterElement(viewType.toLowerCase(),
34                                                      {prototype: proto});
36   // Delete the callbacks so developers cannot call them and produce unexpected
37   // behavior.
38   delete proto.createdCallback;
39   delete proto.attachedCallback;
40   delete proto.detachedCallback;
41   delete proto.attributeChangedCallback;
44 var useCapture = true;
45 window.addEventListener('readystatechange', function listener(event) {
46   if (document.readyState == 'loading')
47     return;
49   for (var i = 0; i != VIEW_TYPES.length; ++i) {
50     // Register the error-providing custom element only for those view types
51     // that have not already been registered. Since this module is always loaded
52     // last, all the view types that are available (i.e. have the proper
53     // permissions) will have already been registered on |window|.
54     if (!window[VIEW_TYPES[i]])
55       registerGuestViewElement(VIEW_TYPES[i]);
56   }
58   window.removeEventListener(event.type, listener, useCapture);
59 }, useCapture);