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

Side by Side Diff: chrome/renderer/resources/extensions/context_menus_custom_bindings.js

Issue 9423049: Make registering custom hooks with schema_generated_bindings.js safer and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 bindings for the contextMenus API. 5 // Custom bindings for the contextMenus API.
6 6
7 (function() { 7 (function() {
8 8
9 native function GetChromeHidden(); 9 native function GetChromeHidden();
10 native function GetNextContextMenuId(); 10 native function GetNextContextMenuId();
11 11
12 var chromeHidden = GetChromeHidden(); 12 var chromeHidden = GetChromeHidden();
13 13
14 chromeHidden.registerCustomHook('contextMenus', function(bindingsAPI) { 14 chromeHidden.registerCustomHook('contextMenus', function(bindingsAPI) {
15 var apiFunctions = bindingsAPI.apiFunctions; 15 var apiFunctions = bindingsAPI.apiFunctions;
16 var sendRequest = bindingsAPI.sendRequest; 16 var sendRequest = bindingsAPI.sendRequest;
17 17
18 chromeHidden.contextMenus = {}; 18 chromeHidden.contextMenus = {};
19 chromeHidden.contextMenus.handlers = {}; 19 chromeHidden.contextMenus.handlers = {};
20 var eventName = "contextMenus"; 20 var eventName = 'contextMenus';
21 chromeHidden.contextMenus.event = new chrome.Event(eventName); 21 chromeHidden.contextMenus.event = new chrome.Event(eventName);
22 chromeHidden.contextMenus.ensureListenerSetup = function() { 22 chromeHidden.contextMenus.ensureListenerSetup = function() {
23 if (chromeHidden.contextMenus.listening) { 23 if (chromeHidden.contextMenus.listening) {
24 return; 24 return;
25 } 25 }
26 chromeHidden.contextMenus.listening = true; 26 chromeHidden.contextMenus.listening = true;
27 chromeHidden.contextMenus.event.addListener(function() { 27 chromeHidden.contextMenus.event.addListener(function() {
28 // An extension context menu item has been clicked on - fire the onclick 28 // An extension context menu item has been clicked on - fire the onclick
29 // if there is one. 29 // if there is one.
30 var id = arguments[0].menuItemId; 30 var id = arguments[0].menuItemId;
31 var onclick = chromeHidden.contextMenus.handlers[id]; 31 var onclick = chromeHidden.contextMenus.handlers[id];
32 if (onclick) { 32 if (onclick) {
33 onclick.apply(null, arguments); 33 onclick.apply(null, arguments);
34 } 34 }
35 }); 35 });
36 }; 36 };
37 37
38 apiFunctions.setHandleRequest("contextMenus.create", function() { 38 apiFunctions.setHandleRequest('create', function() {
39 var args = arguments; 39 var args = arguments;
40 var id = GetNextContextMenuId(); 40 var id = GetNextContextMenuId();
41 args[0].generatedId = id; 41 args[0].generatedId = id;
42 sendRequest(this.name, 42 sendRequest(this.name,
43 args, 43 args,
44 this.definition.parameters, 44 this.definition.parameters,
45 {customCallback: this.customCallback}); 45 {customCallback: this.customCallback});
46 return id; 46 return id;
47 }); 47 });
48 48
49 apiFunctions.setCustomCallback("contextMenus.create", 49 apiFunctions.setCustomCallback('create', function(name, request, response) {
50 function(name, request, response) {
51 if (chrome.extension.lastError) { 50 if (chrome.extension.lastError) {
52 return; 51 return;
53 } 52 }
54 53
55 var id = request.args[0].generatedId; 54 var id = request.args[0].generatedId;
56 55
57 // Set up the onclick handler if we were passed one in the request. 56 // Set up the onclick handler if we were passed one in the request.
58 var onclick = request.args.length ? request.args[0].onclick : null; 57 var onclick = request.args.length ? request.args[0].onclick : null;
59 if (onclick) { 58 if (onclick) {
60 chromeHidden.contextMenus.ensureListenerSetup(); 59 chromeHidden.contextMenus.ensureListenerSetup();
61 chromeHidden.contextMenus.handlers[id] = onclick; 60 chromeHidden.contextMenus.handlers[id] = onclick;
62 } 61 }
63 }); 62 });
64 63
65 apiFunctions.setCustomCallback("contextMenus.remove", 64 apiFunctions.setCustomCallback('remove', function(name, request, response) {
66 function(name, request, response) {
67 if (chrome.extension.lastError) { 65 if (chrome.extension.lastError) {
68 return; 66 return;
69 } 67 }
70 var id = request.args[0]; 68 var id = request.args[0];
71 delete chromeHidden.contextMenus.handlers[id]; 69 delete chromeHidden.contextMenus.handlers[id];
72 }); 70 });
73 71
74 apiFunctions.setCustomCallback("contextMenus.update", 72 apiFunctions.setCustomCallback('update', function(name, request, response) {
75 function(name, request, response) {
76 if (chrome.extension.lastError) { 73 if (chrome.extension.lastError) {
77 return; 74 return;
78 } 75 }
79 var id = request.args[0]; 76 var id = request.args[0];
80 if (request.args[1].onclick) { 77 if (request.args[1].onclick) {
81 chromeHidden.contextMenus.handlers[id] = request.args[1].onclick; 78 chromeHidden.contextMenus.handlers[id] = request.args[1].onclick;
82 } 79 }
83 }); 80 });
84 81
85 apiFunctions.setCustomCallback("contextMenus.removeAll", 82 apiFunctions.setCustomCallback('removeAll',
86 function(name, request, response) { 83 function(name, request, response) {
87 if (chrome.extension.lastError) { 84 if (chrome.extension.lastError) {
88 return; 85 return;
89 } 86 }
90 chromeHidden.contextMenus.handlers = {}; 87 chromeHidden.contextMenus.handlers = {};
91 }); 88 });
92 }); 89 });
93 90
94 })(); 91 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698