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 // This module implements helper objects for the dialog, newwindow, and
6 // permissionrequest <webview> events.
8 var MessagingNatives = requireNative('messaging_natives');
9 var WebViewConstants = require('webViewConstants').WebViewConstants;
10 var WebViewInternal = require('webViewInternal').WebViewInternal;
12 var PERMISSION_TYPES = ['media',
20 // -----------------------------------------------------------------------------
21 // WebViewActionRequest object.
23 // Default partial implementation of a webview action request.
24 function WebViewActionRequest(webViewImpl, event, webViewEvent, interfaceName) {
25 this.webViewImpl = webViewImpl;
27 this.webViewEvent = webViewEvent;
28 this.interfaceName = interfaceName;
29 this.guestInstanceId = this.webViewImpl.guest.getId();
30 this.requestId = event.requestId;
31 this.actionTaken = false;
34 // Performs the default action for the request.
35 WebViewActionRequest.prototype.defaultAction = function() {
36 // Do nothing if the action has already been taken or the requester is
37 // already gone (in which case its guestInstanceId will be stale).
38 if (this.actionTaken ||
39 this.guestInstanceId != this.webViewImpl.guest.getId()) {
43 this.actionTaken = true;
44 WebViewInternal.setPermission(this.guestInstanceId, this.requestId,
45 'default', '', function(allowed) {
49 this.showWarningMessage();
53 // Called to handle the action request's event.
54 WebViewActionRequest.prototype.handleActionRequestEvent = function() {
55 // Construct the interface object and attach it to |webViewEvent|.
56 var request = this.getInterfaceObject();
57 this.webViewEvent[this.interfaceName] = request;
59 var defaultPrevented = !this.webViewImpl.dispatchEvent(this.webViewEvent);
60 // Set |webViewEvent| to null to break the circular reference to |request| so
61 // that the garbage collector can eventually collect it.
62 this.webViewEvent = null;
63 if (this.actionTaken) {
67 if (defaultPrevented) {
68 // Track the lifetime of |request| with the garbage collector.
69 var portId = -1; // (hack) there is no Extension Port to release
70 MessagingNatives.BindToGC(request, this.defaultAction.bind(this), portId);
76 // Displays a warning message when an action request is blocked by default.
77 WebViewActionRequest.prototype.showWarningMessage = function() {
78 window.console.warn(this.WARNING_MSG_REQUEST_BLOCKED);
81 // This function ensures that each action is taken at most once.
82 WebViewActionRequest.prototype.validateCall = function() {
83 if (this.actionTaken) {
84 throw new Error(this.ERROR_MSG_ACTION_ALREADY_TAKEN);
86 this.actionTaken = true;
89 // The following are implemented by the specific action request.
91 // Returns the interface object for this action request.
92 WebViewActionRequest.prototype.getInterfaceObject = undefined;
94 // Error/warning messages.
95 WebViewActionRequest.prototype.ERROR_MSG_ACTION_ALREADY_TAKEN = undefined;
96 WebViewActionRequest.prototype.WARNING_MSG_REQUEST_BLOCKED = undefined;
98 // -----------------------------------------------------------------------------
101 // Represents a dialog box request (e.g. alert()).
102 function Dialog(webViewImpl, event, webViewEvent) {
103 WebViewActionRequest.call(this, webViewImpl, event, webViewEvent, 'dialog');
105 this.handleActionRequestEvent();
108 Dialog.prototype.__proto__ = WebViewActionRequest.prototype;
110 Dialog.prototype.getInterfaceObject = function() {
112 ok: function(user_input) {
114 user_input = user_input || '';
115 WebViewInternal.setPermission(
116 this.guestInstanceId, this.requestId, 'allow', user_input);
120 WebViewInternal.setPermission(
121 this.guestInstanceId, this.requestId, 'deny');
126 Dialog.prototype.showWarningMessage = function() {
127 var VOWELS = ['a', 'e', 'i', 'o', 'u'];
128 var dialogType = this.event.messageType;
129 var article = (VOWELS.indexOf(dialogType.charAt(0)) >= 0) ? 'An' : 'A';
130 this.WARNING_MSG_REQUEST_BLOCKED = this.WARNING_MSG_REQUEST_BLOCKED.
131 replace('%1', article).replace('%2', dialogType);
132 window.console.warn(this.WARNING_MSG_REQUEST_BLOCKED);
135 Dialog.prototype.ERROR_MSG_ACTION_ALREADY_TAKEN =
136 WebViewConstants.ERROR_MSG_DIALOG_ACTION_ALREADY_TAKEN;
137 Dialog.prototype.WARNING_MSG_REQUEST_BLOCKED =
138 WebViewConstants.WARNING_MSG_DIALOG_REQUEST_BLOCKED;
140 // -----------------------------------------------------------------------------
143 // Represents a new window request.
144 function NewWindow(webViewImpl, event, webViewEvent) {
145 WebViewActionRequest.call(this, webViewImpl, event, webViewEvent, 'window');
147 this.handleActionRequestEvent();
150 NewWindow.prototype.__proto__ = WebViewActionRequest.prototype;
152 NewWindow.prototype.getInterfaceObject = function() {
154 attach: function(webview) {
156 if (!webview || !webview.tagName || webview.tagName != 'WEBVIEW') {
157 throw new Error(ERROR_MSG_WEBVIEW_EXPECTED);
160 var webViewImpl = privates(webview).internal;
161 // Update the partition.
162 if (this.event.partition) {
163 webViewImpl.onAttach(this.event.partition);
166 var attached = webViewImpl.attachWindow(this.event.windowId);
168 window.console.error(ERROR_MSG_NEWWINDOW_UNABLE_TO_ATTACH);
171 if (this.guestInstanceId != this.webViewImpl.guest.getId()) {
172 // If the opener is already gone, then its guestInstanceId will be
177 // If the object being passed into attach is not a valid <webview>
178 // then we will fail and it will be treated as if the new window
179 // was rejected. The permission API plumbing is used here to clean
180 // up the state created for the new window if attaching fails.
181 WebViewInternal.setPermission(this.guestInstanceId, this.requestId,
182 attached ? 'allow' : 'deny');
184 discard: function() {
186 if (!this.guestInstanceId) {
187 // If the opener is already gone, then we won't have its
191 WebViewInternal.setPermission(
192 this.guestInstanceId, this.requestId, 'deny');
197 NewWindow.prototype.ERROR_MSG_ACTION_ALREADY_TAKEN =
198 WebViewConstants.ERROR_MSG_NEWWINDOW_ACTION_ALREADY_TAKEN;
199 NewWindow.prototype.WARNING_MSG_REQUEST_BLOCKED =
200 WebViewConstants.WARNING_MSG_NEWWINDOW_REQUEST_BLOCKED;
202 // -----------------------------------------------------------------------------
203 // PermissionRequest object.
205 // Represents a permission request (e.g. to access the filesystem).
206 function PermissionRequest(webViewImpl, event, webViewEvent) {
207 WebViewActionRequest.call(this, webViewImpl, event, webViewEvent, 'request');
209 if (!this.validPermissionCheck()) {
213 this.handleActionRequestEvent();
216 PermissionRequest.prototype.__proto__ = WebViewActionRequest.prototype;
218 PermissionRequest.prototype.getInterfaceObject = function() {
222 WebViewInternal.setPermission(
223 this.guestInstanceId, this.requestId, 'allow');
227 WebViewInternal.setPermission(
228 this.guestInstanceId, this.requestId, 'deny');
233 PermissionRequest.prototype.showWarningMessage = function() {
235 this.WARNING_MSG_REQUEST_BLOCKED.replace('%1', this.event.permission));
238 // Checks that the requested permission is valid. Returns true if valid.
239 PermissionRequest.prototype.validPermissionCheck = function() {
240 if (PERMISSION_TYPES.indexOf(this.event.permission) < 0) {
241 // The permission type is not allowed. Trigger the default response.
242 this.defaultAction();
248 PermissionRequest.prototype.ERROR_MSG_ACTION_ALREADY_TAKEN =
249 WebViewConstants.ERROR_MSG_PERMISSION_ACTION_ALREADY_TAKEN;
250 PermissionRequest.prototype.WARNING_MSG_REQUEST_BLOCKED =
251 WebViewConstants.WARNING_MSG_PERMISSION_REQUEST_BLOCKED;
253 // -----------------------------------------------------------------------------
255 // FullscreenPermissionRequest object.
257 // Represents a fullscreen permission request.
258 function FullscreenPermissionRequest(webViewImpl, event, webViewEvent) {
259 PermissionRequest.call(this, webViewImpl, event, webViewEvent);
262 FullscreenPermissionRequest.prototype.__proto__ = PermissionRequest.prototype;
264 FullscreenPermissionRequest.prototype.getInterfaceObject = function() {
268 WebViewInternal.setPermission(
269 this.guestInstanceId, this.requestId, 'allow');
270 // Now make the <webview> element go fullscreen.
271 this.webViewImpl.makeElementFullscreen();
275 WebViewInternal.setPermission(
276 this.guestInstanceId, this.requestId, 'deny');
281 var WebViewActionRequests = {
282 WebViewActionRequest: WebViewActionRequest,
284 NewWindow: NewWindow,
285 PermissionRequest: PermissionRequest,
286 FullscreenPermissionRequest: FullscreenPermissionRequest
290 exports.WebViewActionRequests = WebViewActionRequests;