| OLD | NEW |
| 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 binding for the omnibox API. Only injected into the v8 contexts | 5 // Custom bindings for the omnibox API. Only injected into the v8 contexts |
| 6 // for extensions which have permission for the omnibox API. | 6 // for extensions which have permission for the omnibox API. |
| 7 | 7 |
| 8 var binding = require('binding').Binding.create('omnibox'); | |
| 9 | |
| 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 11 var sendRequest = require('sendRequest').sendRequest; | 9 var sendRequest = require('sendRequest').sendRequest; |
| 12 | 10 |
| 13 // Remove invalid characters from |text| so that it is suitable to use | 11 // Remove invalid characters from |text| so that it is suitable to use |
| 14 // for |AutocompleteMatch::contents|. | 12 // for |AutocompleteMatch::contents|. |
| 15 function sanitizeString(text, shouldTrim) { | 13 function sanitizeString(text, shouldTrim) { |
| 16 // NOTE: This logic mirrors |AutocompleteMatch::SanitizeString()|. | 14 // NOTE: This logic mirrors |AutocompleteMatch::SanitizeString()|. |
| 17 // 0x2028 = line separator; 0x2029 = paragraph separator. | 15 // 0x2028 = line separator; 0x2029 = paragraph separator. |
| 18 var kRemoveChars = /(\r|\n|\t|\u2028|\u2029)/gm; | 16 var kRemoveChars = /(\r|\n|\t|\u2028|\u2029)/gm; |
| 19 if (shouldTrim) | 17 if (shouldTrim) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 // Descend into all other nodes, even if they are unrecognized, for | 72 // Descend into all other nodes, even if they are unrecognized, for |
| 75 // forward compat. | 73 // forward compat. |
| 76 walk(child); | 74 walk(child); |
| 77 } | 75 } |
| 78 }; | 76 }; |
| 79 walk(root); | 77 walk(root); |
| 80 | 78 |
| 81 return result; | 79 return result; |
| 82 } | 80 } |
| 83 | 81 |
| 84 binding.registerCustomHook(function(bindingsAPI) { | 82 chromeHidden.registerCustomHook('omnibox', function(bindingsAPI) { |
| 85 var apiFunctions = bindingsAPI.apiFunctions; | 83 var apiFunctions = bindingsAPI.apiFunctions; |
| 86 | 84 |
| 87 apiFunctions.setHandleRequest('setDefaultSuggestion', function(details) { | 85 apiFunctions.setHandleRequest('setDefaultSuggestion', function(details) { |
| 88 var parseResult = parseOmniboxDescription(details.description); | 86 var parseResult = parseOmniboxDescription(details.description); |
| 89 sendRequest(this.name, [parseResult], this.definition.parameters); | 87 sendRequest(this.name, [parseResult], this.definition.parameters); |
| 90 }); | 88 }); |
| 91 | 89 |
| 92 apiFunctions.setUpdateArgumentsPostValidate( | 90 apiFunctions.setUpdateArgumentsPostValidate( |
| 93 'sendSuggestions', function(requestId, userSuggestions) { | 91 'sendSuggestions', function(requestId, userSuggestions) { |
| 94 var suggestions = []; | 92 var suggestions = []; |
| 95 for (var i = 0; i < userSuggestions.length; i++) { | 93 for (var i = 0; i < userSuggestions.length; i++) { |
| 96 var parseResult = parseOmniboxDescription( | 94 var parseResult = parseOmniboxDescription( |
| 97 userSuggestions[i].description); | 95 userSuggestions[i].description); |
| 98 parseResult.content = userSuggestions[i].content; | 96 parseResult.content = userSuggestions[i].content; |
| 99 suggestions.push(parseResult); | 97 suggestions.push(parseResult); |
| 100 } | 98 } |
| 101 return [requestId, suggestions]; | 99 return [requestId, suggestions]; |
| 102 }); | 100 }); |
| 103 }); | 101 }); |
| 104 | 102 |
| 105 chromeHidden.Event.registerArgumentMassager('omnibox.onInputChanged', | 103 chromeHidden.Event.registerArgumentMassager('omnibox.onInputChanged', |
| 106 function(args, dispatch) { | 104 function(args, dispatch) { |
| 107 var text = args[0]; | 105 var text = args[0]; |
| 108 var requestId = args[1]; | 106 var requestId = args[1]; |
| 109 var suggestCallback = function(suggestions) { | 107 var suggestCallback = function(suggestions) { |
| 110 chrome.omnibox.sendSuggestions(requestId, suggestions); | 108 chrome.omnibox.sendSuggestions(requestId, suggestions); |
| 111 }; | 109 }; |
| 112 dispatch([text, suggestCallback]); | 110 dispatch([text, suggestCallback]); |
| 113 }); | 111 }); |
| 114 | |
| 115 exports.binding = binding.generate(); | |
| OLD | NEW |