OLD | NEW |
---|---|
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 // Custom binding for <webview> contextMenus API. | |
6 // Note that this file mimics custom bindings for chrome.contextMenus API | |
7 // which resides in context_menus_custom_bindings.js. The functions in this file | |
8 // have an extra instanceId parameter in the beginning, which corresponds to the | |
9 // id of the <webview>. | |
10 // | |
11 // TODO(lazyboy): Share common code /w context_menus_custom_bindings.js. | |
12 | |
13 var EventBindings = require('event_bindings'); | |
14 var binding = require('binding').Binding.create('chromeWebViewInternal'); | 5 var binding = require('binding').Binding.create('chromeWebViewInternal'); |
15 var contextMenuNatives = requireNative('context_menus'); | 6 var createContextMenusHandlers = require('contextMenusHandlers').create; |
not at google - send to devlin
2015/02/25 19:03:53
I'd rather imports import the module, not a method
robwu
2015/02/26 09:33:46
Done.
| |
16 var sendRequest = require('sendRequest').sendRequest; | |
17 var lastError = require('lastError'); | |
18 | 7 |
19 binding.registerCustomHook(function(bindingsAPI) { | 8 binding.registerCustomHook(function(bindingsAPI) { |
20 var apiFunctions = bindingsAPI.apiFunctions; | 9 var apiFunctions = bindingsAPI.apiFunctions; |
21 | 10 |
22 var webviewContextMenus = {}; | 11 var impl = createContextMenusHandlers(/* isWebview = */ true); |
lazyboy
2015/02/25 17:58:42
nit: the most followed pattern here is:
createCont
robwu
2015/02/26 09:33:46
Done.
| |
23 webviewContextMenus.generatedIdHandlers = {}; | |
24 webviewContextMenus.stringIdHandlers = {}; | |
25 | 12 |
26 // Per item event handler. | 13 apiFunctions.setHandleRequest('contextMenusCreate', |
27 var ename = 'webViewInternal.contextMenus'; | 14 impl.requestHandlers.create); |
not at google - send to devlin
2015/02/25 19:03:53
Can we call this "handlers" rather than "impl"?
robwu
2015/02/26 09:33:46
Done.
| |
28 webviewContextMenus.event = new EventBindings.Event(ename); | |
29 | 15 |
30 webviewContextMenus.getIdFromCreateProperties = function(prop) { | 16 apiFunctions.setCustomCallback('contextMenusCreate', impl.callbacks.create); |
31 if (typeof(prop.id) !== 'undefined') | |
32 return prop.id; | |
33 return prop.generatedId; | |
34 }; | |
35 | 17 |
36 webviewContextMenus.handlersForId = function(instanceId, id) { | 18 apiFunctions.setCustomCallback('contextMenusUpdate', impl.callbacks.update); |
37 if (typeof(id) === 'number') { | |
38 if (!webviewContextMenus.generatedIdHandlers[instanceId]) { | |
39 webviewContextMenus.generatedIdHandlers[instanceId] = {}; | |
40 } | |
41 return webviewContextMenus.generatedIdHandlers[instanceId]; | |
42 } | |
43 | 19 |
44 if (!webviewContextMenus.stringIdHandlers[instanceId]) { | 20 apiFunctions.setCustomCallback('contextMenusRemove', impl.callbacks.remove); |
45 webviewContextMenus.stringIdHandlers[instanceId] = {}; | |
46 } | |
47 return webviewContextMenus.stringIdHandlers[instanceId]; | |
48 }; | |
49 | |
50 webviewContextMenus.ensureListenerSetup = function() { | |
51 if (webviewContextMenus.listening) { | |
52 return; | |
53 } | |
54 webviewContextMenus.listening = true; | |
55 webviewContextMenus.event.addListener(function() { | |
56 // An extension context menu item has been clicked on - fire the onclick | |
57 // if there is one. | |
58 var id = arguments[0].menuItemId; | |
59 var instanceId = arguments[0].webviewInstanceId; | |
60 delete arguments[0].webviewInstanceId; | |
61 var onclick = webviewContextMenus.handlersForId(instanceId, id)[id]; | |
62 if (onclick) { | |
63 $Function.apply(onclick, null, arguments); | |
64 } | |
65 }); | |
66 }; | |
67 | |
68 apiFunctions.setHandleRequest('contextMenusCreate', function() { | |
69 var args = arguments; | |
70 var id = contextMenuNatives.GetNextContextMenuId(); | |
71 args[1].generatedId = id; | |
72 | |
73 var optArgs = { | |
74 customCallback: this.customCallback, | |
75 }; | |
76 | |
77 sendRequest(this.name, args, this.definition.parameters, optArgs); | |
78 return webviewContextMenus.getIdFromCreateProperties(args[1]); | |
79 }); | |
80 | |
81 apiFunctions.setCustomCallback('contextMenusCreate', | |
82 function(name, request, callback, response) { | |
83 if (lastError.hasError(chrome)) { | |
84 if (callback) | |
85 callback(); | |
86 return; | |
87 } | |
88 | |
89 var instanceId = request.args[0]; | |
90 var id = webviewContextMenus.getIdFromCreateProperties(request.args[1]); | |
91 var onclick = request.args.length ? request.args[1].onclick : null; | |
92 if (onclick) { | |
93 webviewContextMenus.ensureListenerSetup(); | |
94 webviewContextMenus.handlersForId(instanceId, id)[id] = onclick; | |
95 } | |
96 if (callback) | |
97 callback(); | |
98 }); | |
99 | |
100 apiFunctions.setCustomCallback('contextMenusUpdate', | |
101 function(name, request, callback, response) { | |
102 if (lastError.hasError(chrome)) { | |
103 if (callback) | |
104 callback(); | |
105 return; | |
106 } | |
107 var instanceId = request.args[0]; | |
108 var id = request.args[1]; | |
109 if (request.args[2].onclick) { | |
110 webviewContextMenus.handlersForId(instanceId, id)[id] = | |
111 request.args[2].onclick; | |
112 } | |
113 if (callback) | |
114 callback(); | |
115 }); | |
116 | |
117 apiFunctions.setCustomCallback('contextMenusRemove', | |
118 function(name, request, callback, response) { | |
119 if (lastError.hasError(chrome)) { | |
120 if (callback) | |
121 callback(); | |
122 return; | |
123 } | |
124 var instanceId = request.args[0]; | |
125 var id = request.args[1]; | |
126 delete webviewContextMenus.handlersForId(instanceId, id)[id]; | |
127 if (callback) | |
128 callback(); | |
129 }); | |
130 | 21 |
131 apiFunctions.setCustomCallback('contextMenusRemoveAll', | 22 apiFunctions.setCustomCallback('contextMenusRemoveAll', |
132 function(name, request, callback, response) { | 23 impl.callbacks.removeAll); |
133 if (lastError.hasError(chrome)) { | |
134 if (callback) | |
135 callback(); | |
136 return; | |
137 } | |
138 var instanceId = request.args[0]; | |
139 webviewContextMenus.stringIdHandlers[instanceId] = {}; | |
140 webviewContextMenus.generatedIdHandlers[instanceId] = {}; | |
141 if (callback) | |
142 callback(); | |
143 }); | |
144 | 24 |
145 }); | 25 }); |
146 | 26 |
147 exports.ChromeWebView = binding.generate(); | 27 exports.ChromeWebView = binding.generate(); |
OLD | NEW |