Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / renderer / resources / guest_view / web_view / web_view_events.js
bloba72189e1120db4025a420b30b92a3e03c5310654
1 // Copyright 2014 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 // Event management for WebView.
7 var CreateEvent = require('guestViewEvents').CreateEvent;
8 var DeclarativeWebRequestSchema =
9     requireNative('schema_registry').GetSchema('declarativeWebRequest');
10 var EventBindings = require('event_bindings');
11 var GuestViewEvents = require('guestViewEvents').GuestViewEvents;
12 var IdGenerator = requireNative('id_generator');
13 var WebRequestEvent = require('webRequestInternal').WebRequestEvent;
14 var WebRequestSchema =
15     requireNative('schema_registry').GetSchema('webRequest');
16 var WebViewActionRequests =
17     require('webViewActionRequests').WebViewActionRequests;
19 var WebRequestMessageEvent = CreateEvent('webViewInternal.onMessage');
21 function WebViewEvents(webViewImpl) {
22   GuestViewEvents.call(this, webViewImpl);
24   this.setupWebRequestEvents();
25   this.view.maybeSetupContextMenus();
28 WebViewEvents.prototype.__proto__ = GuestViewEvents.prototype;
30 // A dictionary of <webview> extension events to be listened for. This
31 // dictionary augments |GuestViewEvents.EVENTS| in guest_view_events.js. See the
32 // documentation there for details.
33 WebViewEvents.EVENTS = {
34   'close': {
35     evt: CreateEvent('webViewInternal.onClose')
36   },
37   'consolemessage': {
38     evt: CreateEvent('webViewInternal.onConsoleMessage'),
39     fields: ['level', 'message', 'line', 'sourceId']
40   },
41   'contentload': {
42     evt: CreateEvent('webViewInternal.onContentLoad')
43   },
44   'dialog': {
45     cancelable: true,
46     evt: CreateEvent('webViewInternal.onDialog'),
47     fields: ['defaultPromptText', 'messageText', 'messageType', 'url'],
48     handler: 'handleDialogEvent'
49   },
50   'droplink': {
51     evt: CreateEvent('webViewInternal.onDropLink'),
52     fields: ['url']
53   },
54   'exit': {
55     evt: CreateEvent('webViewInternal.onExit'),
56     fields: ['processId', 'reason']
57   },
58   'exitfullscreen': {
59     evt: CreateEvent('webViewInternal.onExitFullscreen'),
60     fields: ['url'],
61     handler: 'handleFullscreenExitEvent',
62     internal: true
63   },
64   'findupdate': {
65     evt: CreateEvent('webViewInternal.onFindReply'),
66     fields: [
67       'searchText',
68       'numberOfMatches',
69       'activeMatchOrdinal',
70       'selectionRect',
71       'canceled',
72       'finalUpdate'
73     ]
74   },
75   'framenamechanged': {
76     evt: CreateEvent('webViewInternal.onFrameNameChanged'),
77     handler: 'handleFrameNameChangedEvent',
78     internal: true
79   },
80   'loadabort': {
81     cancelable: true,
82     evt: CreateEvent('webViewInternal.onLoadAbort'),
83     fields: ['url', 'isTopLevel', 'code', 'reason'],
84     handler: 'handleLoadAbortEvent'
85   },
86   'loadcommit': {
87     evt: CreateEvent('webViewInternal.onLoadCommit'),
88     fields: ['url', 'isTopLevel'],
89     handler: 'handleLoadCommitEvent'
90   },
91   'loadprogress': {
92     evt: CreateEvent('webViewInternal.onLoadProgress'),
93     fields: ['url', 'progress']
94   },
95   'loadredirect': {
96     evt: CreateEvent('webViewInternal.onLoadRedirect'),
97     fields: ['isTopLevel', 'oldUrl', 'newUrl']
98   },
99   'loadstart': {
100     evt: CreateEvent('webViewInternal.onLoadStart'),
101     fields: ['url', 'isTopLevel']
102   },
103   'loadstop': {
104     evt: CreateEvent('webViewInternal.onLoadStop')
105   },
106   'newwindow': {
107     cancelable: true,
108     evt: CreateEvent('webViewInternal.onNewWindow'),
109     fields: [
110       'initialHeight',
111       'initialWidth',
112       'targetUrl',
113       'windowOpenDisposition',
114       'name'
115     ],
116     handler: 'handleNewWindowEvent'
117   },
118   'permissionrequest': {
119     cancelable: true,
120     evt: CreateEvent('webViewInternal.onPermissionRequest'),
121     fields: [
122       'identifier',
123       'lastUnlockedBySelf',
124       'name',
125       'permission',
126       'requestMethod',
127       'url',
128       'userGesture'
129     ],
130     handler: 'handlePermissionEvent'
131   },
132   'responsive': {
133     evt: CreateEvent('webViewInternal.onResponsive'),
134     fields: ['processId']
135   },
136   'sizechanged': {
137     evt: CreateEvent('webViewInternal.onSizeChanged'),
138     fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'],
139     handler: 'handleSizeChangedEvent'
140   },
141   'unresponsive': {
142     evt: CreateEvent('webViewInternal.onUnresponsive'),
143     fields: ['processId']
144   },
145   'zoomchange': {
146     evt: CreateEvent('webViewInternal.onZoomChange'),
147     fields: ['oldZoomFactor', 'newZoomFactor']
148   }
151 WebViewEvents.prototype.setupWebRequestEvents = function() {
152   var request = {};
153   var createWebRequestEvent = function(webRequestEvent) {
154     return function() {
155       if (!this[webRequestEvent.name]) {
156         this[webRequestEvent.name] =
157             new WebRequestEvent(
158                 'webViewInternal.' + webRequestEvent.name,
159                 webRequestEvent.parameters,
160                 webRequestEvent.extraParameters, webRequestEvent.options,
161                 this.view.viewInstanceId);
162       }
163       return this[webRequestEvent.name];
164     }.bind(this);
165   }.bind(this);
167   var createDeclarativeWebRequestEvent = function(webRequestEvent) {
168     return function() {
169       if (!this[webRequestEvent.name]) {
170         // The onMessage event gets a special event type because we want
171         // the listener to fire only for messages targeted for this particular
172         // <webview>.
173         var EventClass = webRequestEvent.name === 'onMessage' ?
174             DeclarativeWebRequestEvent : EventBindings.Event;
175         this[webRequestEvent.name] =
176             new EventClass(
177                 'webViewInternal.declarativeWebRequest.' + webRequestEvent.name,
178                 webRequestEvent.parameters,
179                 webRequestEvent.options,
180                 this.view.viewInstanceId);
181       }
182       return this[webRequestEvent.name];
183     }.bind(this);
184   }.bind(this);
186   for (var i = 0; i < DeclarativeWebRequestSchema.events.length; ++i) {
187     var eventSchema = DeclarativeWebRequestSchema.events[i];
188     var webRequestEvent = createDeclarativeWebRequestEvent(eventSchema);
189     Object.defineProperty(
190         request,
191         eventSchema.name,
192         {
193           get: webRequestEvent,
194           enumerable: true
195         }
196         );
197   }
199   // Populate the WebRequest events from the API definition.
200   for (var i = 0; i < WebRequestSchema.events.length; ++i) {
201     var webRequestEvent = createWebRequestEvent(WebRequestSchema.events[i]);
202     Object.defineProperty(
203         request,
204         WebRequestSchema.events[i].name,
205         {
206           get: webRequestEvent,
207           enumerable: true
208         }
209         );
210   }
212   this.view.setRequestPropertyOnWebViewElement(request);
215 WebViewEvents.prototype.getEvents = function() {
216   return WebViewEvents.EVENTS;
219 WebViewEvents.prototype.handleDialogEvent = function(event, eventName) {
220   var webViewEvent = this.makeDomEvent(event, eventName);
221   new WebViewActionRequests.Dialog(this.view, event, webViewEvent);
224 WebViewEvents.prototype.handleFrameNameChangedEvent = function(event) {
225   this.view.onFrameNameChanged(event.name);
228 WebViewEvents.prototype.handleFullscreenExitEvent = function(event, eventName) {
229   document.webkitCancelFullScreen();
232 WebViewEvents.prototype.handleLoadAbortEvent = function(event, eventName) {
233   var showWarningMessage = function(code, reason) {
234     var WARNING_MSG_LOAD_ABORTED = '<webview>: ' +
235         'The load has aborted with error %1: %2.';
236     window.console.warn(
237         WARNING_MSG_LOAD_ABORTED.replace('%1', code).replace('%2', reason));
238   };
239   var webViewEvent = this.makeDomEvent(event, eventName);
240   if (this.view.dispatchEvent(webViewEvent)) {
241     showWarningMessage(event.code, event.reason);
242   }
245 WebViewEvents.prototype.handleLoadCommitEvent = function(event, eventName) {
246   this.view.onLoadCommit(event.baseUrlForDataUrl,
247                          event.currentEntryIndex,
248                          event.entryCount,
249                          event.processId,
250                          event.url,
251                          event.isTopLevel);
252   var webViewEvent = this.makeDomEvent(event, eventName);
253   this.view.dispatchEvent(webViewEvent);
256 WebViewEvents.prototype.handleNewWindowEvent = function(event, eventName) {
257   var webViewEvent = this.makeDomEvent(event, eventName);
258   new WebViewActionRequests.NewWindow(this.view, event, webViewEvent);
261 WebViewEvents.prototype.handlePermissionEvent = function(event, eventName) {
262   var webViewEvent = this.makeDomEvent(event, eventName);
263   if (event.permission === 'fullscreen') {
264     new WebViewActionRequests.FullscreenPermissionRequest(
265         this.view, event, webViewEvent);
266   } else {
267     new WebViewActionRequests.PermissionRequest(this.view, event, webViewEvent);
268   }
271 WebViewEvents.prototype.handleSizeChangedEvent = function(event, eventName) {
272   var webViewEvent = this.makeDomEvent(event, eventName);
273   this.view.onSizeChanged(webViewEvent);
276 function DeclarativeWebRequestEvent(opt_eventName,
277                                     opt_argSchemas,
278                                     opt_eventOptions,
279                                     opt_webViewInstanceId) {
280   var subEventName = opt_eventName + '/' + IdGenerator.GetNextId();
281   EventBindings.Event.call(this,
282                            subEventName,
283                            opt_argSchemas,
284                            opt_eventOptions,
285                            opt_webViewInstanceId);
287   // TODO(lazyboy): When do we dispose this listener?
288   WebRequestMessageEvent.addListener(function() {
289     // Re-dispatch to subEvent's listeners.
290     $Function.apply(this.dispatch, this, $Array.slice(arguments));
291   }.bind(this), {instanceId: opt_webViewInstanceId || 0});
294 DeclarativeWebRequestEvent.prototype.__proto__ = EventBindings.Event.prototype;
296 // Exports.
297 exports.WebViewEvents = WebViewEvents;