Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: chrome/renderer/resources/extensions/web_view/chrome_web_view.js

Issue 2912883004: [Extensions Bindings] Don't allow `event` module with native bindings (Closed)
Patch Set: jbroman's Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 // This module implements chrome-specific <webview> API. 5 // This module implements chrome-specific <webview> API.
6 // See web_view_api_methods.js for details. 6 // See web_view_api_methods.js for details.
7 7
8 var ChromeWebView = getInternalApi ? 8 var ChromeWebView = getInternalApi ?
9 getInternalApi('chromeWebViewInternal') : 9 getInternalApi('chromeWebViewInternal') :
10 require('chromeWebViewInternal').ChromeWebView; 10 require('chromeWebViewInternal').ChromeWebView;
11 var ChromeWebViewSchema = 11 var ChromeWebViewSchema =
12 requireNative('schema_registry').GetSchema('chromeWebViewInternal'); 12 requireNative('schema_registry').GetSchema('chromeWebViewInternal');
13 var CreateEvent = require('guestViewEvents').CreateEvent; 13 var CreateEvent = require('guestViewEvents').CreateEvent;
14 var EventBindings = require('event_bindings');
15 var GuestViewInternalNatives = requireNative('guest_view_internal'); 14 var GuestViewInternalNatives = requireNative('guest_view_internal');
16 var idGeneratorNatives = requireNative('id_generator'); 15 var idGeneratorNatives = requireNative('id_generator');
17 var utils = require('utils'); 16 var utils = require('utils');
18 var WebViewImpl = require('webView').WebViewImpl; 17 var WebViewImpl = require('webView').WebViewImpl;
19 18
20 // This is the only "webViewInternal.onClicked" named event for this renderer. 19 // This is the only "webViewInternal.onClicked" named event for this renderer.
21 // 20 //
22 // Since we need an event per <webview>, we define events with suffix 21 // Since we need an event per <webview>, we define events with suffix
23 // (subEventName) in each of the <webview>. Behind the scenes, this event is 22 // (subEventName) in each of the <webview>. Behind the scenes, this event is
24 // registered as a ContextMenusEvent, with filter set to the webview's 23 // registered as a ContextMenusEvent, with filter set to the webview's
25 // |viewInstanceId|. Any time a ContextMenusEvent is dispatched, we re-dispatch 24 // |viewInstanceId|. Any time a ContextMenusEvent is dispatched, we re-dispatch
26 // it to the subEvent's listeners. This way 25 // it to the subEvent's listeners. This way
27 // <webview>.contextMenus.onClicked behave as a regular chrome Event type. 26 // <webview>.contextMenus.onClicked behave as a regular chrome Event type.
28 var ContextMenusEvent = CreateEvent('chromeWebViewInternal.onClicked'); 27 var ContextMenusEvent = CreateEvent('chromeWebViewInternal.onClicked');
29 // See comment above. 28 // See comment above.
30 var ContextMenusHandlerEvent = 29 var ContextMenusHandlerEvent =
31 CreateEvent('chromeWebViewInternal.onContextMenuShow'); 30 CreateEvent('chromeWebViewInternal.onContextMenuShow');
32 31
33 // ----------------------------------------------------------------------------- 32 var jsEvent;
34 // ContextMenusOnClickedEvent object. 33 function createCustomEvent(name, schema, options, webviewId) {
34 if (bindingUtil)
35 return bindingUtil.createCustomEvent(name, undefined, false);
36 if (!jsEvent)
37 jsEvent = require('event_bindings').Event;
38 return new jsEvent(name, schema, options, webviewId);
39 }
35 40
36 // This event is exposed as <webview>.contextMenus.onClicked. 41 // This event is exposed as <webview>.contextMenus.onClicked.
37 function ContextMenusOnClickedEvent(webViewInstanceId, 42 function getContextMenusOnClickedEvent(webViewInstanceId,
lazyboy 2017/06/13 17:46:00 nit: s/get/create
Devlin 2017/06/13 22:30:53 Done.
38 opt_eventName, 43 opt_eventName,
39 opt_argSchemas, 44 opt_argSchemas,
40 opt_eventOptions) { 45 opt_eventOptions) {
41 var subEventName = GetUniqueSubEventName(opt_eventName); 46 var subEventName = GetUniqueSubEventName(opt_eventName);
42 $Function.call(EventBindings.Event, 47 var newEvent = createCustomEvent(subEventName, opt_argSchemas,
43 this, 48 opt_eventOptions, webViewInstanceId);
44 subEventName,
45 opt_argSchemas,
46 opt_eventOptions,
47 webViewInstanceId);
48 49
49 var view = GuestViewInternalNatives.GetViewFromID(webViewInstanceId); 50 var view = GuestViewInternalNatives.GetViewFromID(webViewInstanceId);
50 if (!view) { 51 if (view) {
51 return; 52 view.events.addScopedListener(
53 ContextMenusEvent,
54 $Function.bind(function() {
55 // Re-dispatch to subEvent's listeners.
56 $Function.apply(newEvent.dispatch, newEvent, $Array.slice(arguments));
57 }, newEvent),
58 {instanceId: webViewInstanceId});
52 } 59 }
53 view.events.addScopedListener(ContextMenusEvent, $Function.bind(function() { 60 return newEvent;
54 // Re-dispatch to subEvent's listeners.
55 $Function.apply(this.dispatch, this, $Array.slice(arguments));
56 }, this), {instanceId: webViewInstanceId});
57 } 61 }
58 $Object.setPrototypeOf(ContextMenusOnClickedEvent.prototype,
59 EventBindings.Event.prototype);
60 62
61 // This event is exposed as <webview>.contextMenus.onShow. 63 // This event is exposed as <webview>.contextMenus.onShow.
62 function ContextMenusOnContextMenuEvent(webViewInstanceId, 64 function getContextMenusOnContextMenuEvent(webViewInstanceId,
63 opt_eventName, 65 opt_eventName,
64 opt_argSchemas, 66 opt_argSchemas,
65 opt_eventOptions) { 67 opt_eventOptions) {
66 var subEventName = GetUniqueSubEventName(opt_eventName); 68 var subEventName = GetUniqueSubEventName(opt_eventName);
67 $Function.call(EventBindings.Event, 69 var newEvent = createCustomEvent(subEventName, opt_argSchemas,
68 this, 70 opt_eventOptions, webViewInstanceId);
69 subEventName,
70 opt_argSchemas,
71 opt_eventOptions,
72 webViewInstanceId);
73 71
74 var view = GuestViewInternalNatives.GetViewFromID(webViewInstanceId); 72 var view = GuestViewInternalNatives.GetViewFromID(webViewInstanceId);
75 if (!view) { 73 if (view) {
76 return; 74 view.events.addScopedListener(
75 ContextMenusHandlerEvent,
76 $Function.bind(function(e) {
77 var defaultPrevented = false;
78 var event = {
79 preventDefault: function() { defaultPrevented = true; }
80 };
81
82 // Re-dispatch to subEvent's listeners.
83 $Function.apply(newEvent.dispatch, newEvent, [event]);
84
85 if (!defaultPrevented) {
86 // TODO(lazyboy): Remove |items| parameter completely from
87 // ChromeWebView.showContextMenu as we don't do anything useful with
88 // it currently.
89 var items = [];
90 var guestInstanceId = GuestViewInternalNatives.
91 GetViewFromID(webViewInstanceId).guest.getId();
92 ChromeWebView.showContextMenu(guestInstanceId, e.requestId, items);
93 }
94 }, newEvent),
95 {instanceId: webViewInstanceId});
77 } 96 }
78 view.events.addScopedListener(
79 ContextMenusHandlerEvent, $Function.bind(function(e) {
80 var defaultPrevented = false;
81 var event = {
82 'preventDefault': function() { defaultPrevented = true; }
83 };
84 97
85 // Re-dispatch to subEvent's listeners. 98 return newEvent;
86 $Function.apply(this.dispatch, this, [event]);
87
88 if (!defaultPrevented) {
89 // TODO(lazyboy): Remove |items| parameter completely from
90 // ChromeWebView.showContextMenu as we don't do anything useful with it
91 // currently.
92 var items = [];
93 var guestInstanceId = GuestViewInternalNatives.
94 GetViewFromID(webViewInstanceId).guest.getId();
95 ChromeWebView.showContextMenu(guestInstanceId, e.requestId, items);
96 }
97 }, this), {instanceId: webViewInstanceId});
98 } 99 }
99 100
100 $Object.setPrototypeOf(ContextMenusOnContextMenuEvent.prototype,
101 EventBindings.Event.prototype);
102
103 // ----------------------------------------------------------------------------- 101 // -----------------------------------------------------------------------------
104 // WebViewContextMenusImpl object. 102 // WebViewContextMenusImpl object.
105 103
106 // An instance of this class is exposed as <webview>.contextMenus. 104 // An instance of this class is exposed as <webview>.contextMenus.
107 function WebViewContextMenusImpl(viewInstanceId) { 105 function WebViewContextMenusImpl(viewInstanceId) {
108 this.viewInstanceId_ = viewInstanceId; 106 this.viewInstanceId_ = viewInstanceId;
109 } 107 }
110 $Object.setPrototypeOf(WebViewContextMenusImpl.prototype, null); 108 $Object.setPrototypeOf(WebViewContextMenusImpl.prototype, null);
111 109
112 WebViewContextMenusImpl.prototype.create = function() { 110 WebViewContextMenusImpl.prototype.create = function() {
(...skipping 29 matching lines...) Expand all
142 }); 140 });
143 141
144 // ----------------------------------------------------------------------------- 142 // -----------------------------------------------------------------------------
145 143
146 WebViewImpl.prototype.maybeSetupContextMenus = function() { 144 WebViewImpl.prototype.maybeSetupContextMenus = function() {
147 if (!this.contextMenusOnContextMenuEvent_) { 145 if (!this.contextMenusOnContextMenuEvent_) {
148 var eventName = 'chromeWebViewInternal.onContextMenuShow'; 146 var eventName = 'chromeWebViewInternal.onContextMenuShow';
149 var eventSchema = 147 var eventSchema =
150 utils.lookup(ChromeWebViewSchema.events, 'name', 'onShow'); 148 utils.lookup(ChromeWebViewSchema.events, 'name', 'onShow');
151 var eventOptions = {supportsListeners: true}; 149 var eventOptions = {supportsListeners: true};
152 this.contextMenusOnContextMenuEvent_ = new ContextMenusOnContextMenuEvent( 150 this.contextMenusOnContextMenuEvent_ = getContextMenusOnContextMenuEvent(
153 this.viewInstanceId, eventName, eventSchema, eventOptions); 151 this.viewInstanceId, eventName, eventSchema, eventOptions);
154 } 152 }
155 153
156 var createContextMenus = $Function.bind(function() { 154 var createContextMenus = $Function.bind(function() {
157 return this.weakWrapper(function() { 155 return this.weakWrapper(function() {
158 if (this.contextMenus_) { 156 if (this.contextMenus_) {
159 return this.contextMenus_; 157 return this.contextMenus_;
160 } 158 }
161 159
162 this.contextMenus_ = new WebViewContextMenus(this.viewInstanceId); 160 this.contextMenus_ = new WebViewContextMenus(this.viewInstanceId);
163 161
164 // Define 'onClicked' event property on |this.contextMenus_|. 162 // Define 'onClicked' event property on |this.contextMenus_|.
165 var getOnClickedEvent = $Function.bind(function() { 163 var getOnClickedEvent = $Function.bind(function() {
166 return this.weakWrapper(function() { 164 return this.weakWrapper(function() {
167 if (!this.contextMenusOnClickedEvent_) { 165 if (!this.contextMenusOnClickedEvent_) {
168 var eventName = 'chromeWebViewInternal.onClicked'; 166 var eventName = 'chromeWebViewInternal.onClicked';
169 var eventSchema = 167 var eventSchema =
170 utils.lookup(ChromeWebViewSchema.events, 'name', 'onClicked'); 168 utils.lookup(ChromeWebViewSchema.events, 'name', 'onClicked');
171 var eventOptions = {supportsListeners: true}; 169 var eventOptions = {supportsListeners: true};
172 var onClickedEvent = new ContextMenusOnClickedEvent( 170 var onClickedEvent = getContextMenusOnClickedEvent(
173 this.viewInstanceId, eventName, eventSchema, eventOptions); 171 this.viewInstanceId, eventName, eventSchema, eventOptions);
174 this.contextMenusOnClickedEvent_ = onClickedEvent; 172 this.contextMenusOnClickedEvent_ = onClickedEvent;
175 return onClickedEvent; 173 return onClickedEvent;
176 } 174 }
177 return this.contextMenusOnClickedEvent_; 175 return this.contextMenusOnClickedEvent_;
178 }); 176 });
179 }, this); 177 }, this);
180 $Object.defineProperty( 178 $Object.defineProperty(
181 this.contextMenus_, 179 this.contextMenus_,
182 'onClicked', 180 'onClicked',
(...skipping 17 matching lines...) Expand all
200 'contextMenus', 198 'contextMenus',
201 { 199 {
202 get: createContextMenus(), 200 get: createContextMenus(),
203 enumerable: true 201 enumerable: true
204 }); 202 });
205 }; 203 };
206 204
207 function GetUniqueSubEventName(eventName) { 205 function GetUniqueSubEventName(eventName) {
208 return eventName + '/' + idGeneratorNatives.GetNextId(); 206 return eventName + '/' + idGeneratorNatives.GetNextId();
209 } 207 }
OLDNEW
« no previous file with comments | « no previous file | extensions/renderer/api_binding.cc » ('j') | extensions/renderer/api_binding_js_util.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698