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

Side by Side Diff: extensions/renderer/resources/context_menus_handlers.js

Issue 948243005: Merge custom bindings for context menus (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: $Array.unshift -> $Array.concat Created 5 years, 10 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
(Empty)
1 // Copyright 2015 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.
4
5 // Implementation of custom bindings for the contextMenus API.
lazyboy 2015/02/25 17:58:42 A little bit detail here would help. Note that we
robwu 2015/02/26 00:15:11 Done.
6
7 var contextMenuNatives = requireNative('context_menus');
8 var sendRequest = require('sendRequest').sendRequest;
9 var Event = require('event_bindings').Event;
10 var lastError = require('lastError');
11
12 // Add the bindings to the contextMenus API.
13 function createContextMenusHandlers(isWebview) {
14 var eventName = isWebview ? 'webViewInternal.contextMenus' : 'contextMenus';
15 // Some dummy value for chrome.contextMenus instances.
16 var INSTANCEID_NONWEBVIEW = -1;
lazyboy 2015/02/25 17:58:42 Most places in the code base refers to this as kIn
robwu 2015/02/25 18:25:30 I've picked -1 because "0" could be a value associ
robwu 2015/02/26 00:15:11 I've added a comment to clarify my intention.
17
18 // Generates a customCallback for a given method. |handleCallback| will be
19 // invoked with |request.args| as parameters.
20 function createCustomCallback(handleCallback) {
21 return function(name, request, callback) {
22 if (lastError.hasError(chrome)) {
23 if (callback)
24 callback();
25 return;
26 }
27 var args = request.args;
28 if (!isWebview) {
29 // <webviews> have an extra item in front of the parameter list, which
lazyboy 2015/02/25 22:52:48 nit: <webview>s ...
robwu 2015/02/26 00:15:11 Done.
30 // specifies the ID of the webview. This is used to hide context menu
lazyboy 2015/02/25 22:52:47 s/ID/viewInstanceId
robwu 2015/02/26 00:15:11 Done.
31 // events in one webview from another.
32 // The non-webview chrome.contextMenus API is not called with such an
33 // ID, so we prepend an ID to match the function signature.
34 args = $Array.concat([INSTANCEID_NONWEBVIEW], args);
35 }
36 $Function.apply(handleCallback, null, args);
37 if (callback)
38 callback();
39 };
40 }
41
42 var contextMenus = {};
43 contextMenus.handlers = {};
44 contextMenus.event = new Event(eventName);
45
46 contextMenus.getIdFromCreateProperties = function(createProperties) {
47 if (typeof createProperties.id !== 'undefined')
48 return createProperties.id;
49 return createProperties.generatedId;
50 };
51
52 contextMenus.handlersForId = function(instanceid, id) {
lazyboy 2015/02/25 22:52:47 here and in all other places: s/instanceid/instanc
robwu 2015/02/26 00:15:11 Done.
53 if (!contextMenus.handlers[instanceid]) {
54 contextMenus.handlers[instanceid] = {
55 generated: {},
56 string: {}
57 };
58 }
59 if (typeof id === 'number')
60 return contextMenus.handlers[instanceid].generated;
61 return contextMenus.handlers[instanceid].string;
62 };
63
64 contextMenus.ensureListenerSetup = function() {
65 if (contextMenus.listening) {
66 return;
67 }
68 contextMenus.listening = true;
69 contextMenus.event.addListener(function(info) {
70 var instanceid = INSTANCEID_NONWEBVIEW;
71 if (isWebview) {
72 instanceid = info.webviewInstanceId;
73 delete info.webviewInstanceId;
lazyboy 2015/02/25 22:52:47 Add a note here saying we don't expose "webviewIns
robwu 2015/02/26 00:15:11 Done.
74 }
75
76 var id = info.menuItemId;
77 var onclick = contextMenus.handlersForId(instanceid, id)[id];
78 if (onclick) {
79 $Function.apply(onclick, null, arguments);
80 }
81 });
82 };
83
84 // To be used with apiFunctions.setHandleRequest
85 var requestHandlers = {};
86 // To be used with apiFunctions.setCustomCallback
87 var callbacks = {};
88
89 requestHandlers.create = function() {
90 var createProperties = isWebview ? arguments[1] : arguments[0];
91 createProperties.generatedId = contextMenuNatives.GetNextContextMenuId();
92 var optArgs = {
93 customCallback: this.customCallback,
94 };
95 sendRequest(this.name, arguments, this.definition.parameters, optArgs);
96 return contextMenus.getIdFromCreateProperties(createProperties);
97 };
98
99 callbacks.create =
100 createCustomCallback(function(instanceid, createProperties) {
101 var id = contextMenus.getIdFromCreateProperties(createProperties);
102 var onclick = createProperties.onclick;
103 if (onclick) {
104 contextMenus.ensureListenerSetup();
105 contextMenus.handlersForId(instanceid, id)[id] = onclick;
106 }
107 });
108
109 callbacks.remove = createCustomCallback(function(instanceid, id) {
110 delete contextMenus.handlersForId(instanceid, id)[id];
111 });
112
113 callbacks.update =
114 createCustomCallback(function(instanceid, id, updateProperties) {
115 var onclick = updateProperties.onclick;
116 if (onclick) {
117 contextMenus.ensureListenerSetup();
118 contextMenus.handlersForId(instanceid, id)[id] = onclick;
119 } else if (onclick === null) {
120 // When onclick is explicitly set to null, remove the event listener.
lazyboy 2015/02/25 17:58:42 Great, can you file a bug for this and add it to t
robwu 2015/02/25 18:25:30 Done. https://crbug.com/461869
lazyboy 2015/02/25 22:52:47 Please write a test for this behavior :)
robwu 2015/02/26 00:15:11 Done.
121 delete contextMenus.handlersForId(instanceid, id)[id];
122 }
123 });
124
125 callbacks.removeAll = createCustomCallback(function(instanceid) {
126 delete contextMenus.handlers[instanceid];
127 });
128
129 return {
lazyboy 2015/02/25 17:58:42 This is a bit brittle, the json schema compiler do
robwu 2015/02/25 18:25:30 This return value is not validated at all. This me
lazyboy 2015/02/25 22:52:47 Not significantly different, but there are differe
robwu 2015/02/26 00:15:11 If that's all, then merging would make maintenance
130 requestHandlers: requestHandlers,
131 callbacks: callbacks
132 };
133 }
134
135 exports.create = createContextMenusHandlers;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698