OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Shim extension to provide permission request API (and possibly other future | 5 // Shim extension to provide permission request API (and possibly other future |
6 // experimental APIs) for <webview> tag. | 6 // experimental APIs) for <webview> tag. |
7 // See web_view.js for details. | 7 // See web_view.js for details. |
8 // | 8 // |
9 // We want to control the permission API feature in <webview> separately from | 9 // We want to control the permission API feature in <webview> separately from |
10 // the <webview> feature itself. <webview> is available in stable channel, but | 10 // the <webview> feature itself. <webview> is available in stable channel, but |
11 // permission API would only be available for channels CHANNEL_DEV and | 11 // permission API would only be available for channels CHANNEL_DEV and |
12 // CHANNEL_CANARY. | 12 // CHANNEL_CANARY. |
13 | 13 |
14 var WebRequestEvent = require('webRequestInternal').WebRequestEvent; | 14 var WebRequestEvent = require('webRequestInternal').WebRequestEvent; |
15 var webRequestSchema = | 15 var webRequestSchema = |
16 requireNative('schema_registry').GetSchema('webRequest'); | 16 requireNative('schema_registry').GetSchema('webRequest'); |
17 var WebView = require('webView').WebView; | 17 var WebView = require('webView').WebView; |
18 | 18 |
19 /** | 19 /** |
20 * @private | 20 * @private |
21 */ | 21 */ |
22 WebView.prototype.maybeSetupExperimentalAPI_ = function() { | 22 WebView.prototype.maybeSetupExperimentalAPI_ = function() { |
23 this.setupWebRequestEvents_(); | 23 this.setupWebRequestEvents_(); |
| 24 this.setupDialogEvent_(); |
24 }; | 25 }; |
25 | 26 |
26 /** | 27 /** |
27 * @private | 28 * @private |
28 */ | 29 */ |
29 WebView.prototype.maybeGetExperimentalPermissionTypes_ = function() { | 30 WebView.prototype.maybeGetExperimentalPermissionTypes_ = function() { |
30 var PERMISSION_TYPES = ['download']; | 31 var PERMISSION_TYPES = ['download']; |
31 return PERMISSION_TYPES; | 32 return PERMISSION_TYPES; |
32 }; | 33 }; |
33 | 34 |
(...skipping 17 matching lines...) Expand all Loading... |
51 self.browserPluginNode_.getInstanceId()); | 52 self.browserPluginNode_.getInstanceId()); |
52 } | 53 } |
53 return self[webRequestEvent.name + '_']; | 54 return self[webRequestEvent.name + '_']; |
54 } | 55 } |
55 }(webRequestSchema.events[i]), | 56 }(webRequestSchema.events[i]), |
56 // No setter. | 57 // No setter. |
57 enumerable: true | 58 enumerable: true |
58 }); | 59 }); |
59 } | 60 } |
60 }; | 61 }; |
| 62 |
| 63 /** |
| 64 * @private |
| 65 */ |
| 66 WebView.prototype.setupDialogEvent_ = function() { |
| 67 var ERROR_MSG_DIALOG_ACTION_ALREADY_TAKEN = '<webview>: ' + |
| 68 'An action has already been taken for this "dialog" event.'; |
| 69 |
| 70 var WARNING_MSG_DIALOG_BLOCKED = '<webview>: A dialog was blocked.'; |
| 71 |
| 72 var DIALOG_EVENT_ATTRIBUTES = [ |
| 73 'defaultPromptText', |
| 74 'messageText', |
| 75 'messageType', |
| 76 'url' |
| 77 ]; |
| 78 |
| 79 var self = this; |
| 80 var node = this.webviewNode_; |
| 81 var browserPluginNode = this.browserPluginNode_; |
| 82 |
| 83 var onTrackedObjectGone = function(requestId, e) { |
| 84 var detail = e.detail ? JSON.parse(e.detail) : {}; |
| 85 if (detail.id != requestId) |
| 86 return; |
| 87 browserPluginNode['-internal-setPermission'](requestId, false, ''); |
| 88 } |
| 89 |
| 90 browserPluginNode.addEventListener('-internal-dialog', function(e) { |
| 91 var evt = new Event('dialog', { bubbles: true, cancelable: true }); |
| 92 var detail = e.detail ? JSON.parse(e.detail) : {}; |
| 93 |
| 94 $Array.forEach(DIALOG_EVENT_ATTRIBUTES, function(attribName) { |
| 95 evt[attribName] = detail[attribName]; |
| 96 }); |
| 97 var requestId = detail.requestId; |
| 98 var actionTaken = false; |
| 99 |
| 100 var validateCall = function () { |
| 101 if (actionTaken) |
| 102 throw new Error(ERROR_MSG_DIALOG_ACTION_ALREADY_TAKEN); |
| 103 actionTaken = true; |
| 104 }; |
| 105 |
| 106 var dialog = { |
| 107 ok: function(user_input) { |
| 108 validateCall(); |
| 109 browserPluginNode['-internal-setPermission']( |
| 110 requestId, true, user_input); |
| 111 }, |
| 112 cancel: function() { |
| 113 validateCall(); |
| 114 browserPluginNode['-internal-setPermission'](requestId, false, ''); |
| 115 } |
| 116 }; |
| 117 evt.dialog = dialog; |
| 118 // Make browser plugin track lifetime of |window|. |
| 119 var onTrackedObjectGoneWithRequestId = |
| 120 $Function.bind(onTrackedObjectGone, self, requestId); |
| 121 browserPluginNode.addEventListener('-internal-trackedobjectgone', |
| 122 onTrackedObjectGoneWithRequestId); |
| 123 browserPluginNode['-internal-trackObjectLifetime'](dialog, requestId); |
| 124 |
| 125 var defaultPrevented = !node.dispatchEvent(evt); |
| 126 if (!actionTaken && !defaultPrevented) { |
| 127 actionTaken = true; |
| 128 // The default action is equivalent to canceling the dialog. |
| 129 browserPluginNode['-internal-setPermission'](requestId, false, ''); |
| 130 console.warn(WARNING_MSG_DIALOG_BLOCKED); |
| 131 } |
| 132 }); |
| 133 }; |
OLD | NEW |