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