Chromium Code Reviews| Index: chrome/browser/resources/omnibox_result_loader.js |
| diff --git a/chrome/browser/resources/omnibox_result_loader.js b/chrome/browser/resources/omnibox_result_loader.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..322cebd0a38388ca20b69397b4aff5021925a6e2 |
| --- /dev/null |
| +++ b/chrome/browser/resources/omnibox_result_loader.js |
| @@ -0,0 +1,93 @@ |
| +/** |
| + * @fileoverview Orchestrates loading of suggestion content in several |
| + * chrome-search://suggestion iframes. |
| + */ |
| + |
| +/** |
| + * The origin of the embedding page. |
| + * Must be in double quotes for proper escaping. |
| + * @type {string} |
| + * @const |
| + */ |
| +var EMBEDDER_ORIGIN = "%s"; |
| + |
| +/** |
| + * Checks whether a string is a valid color code. |
| + * @param {string} color A color code. |
| + * @return {boolean} True if color is a valid color code and false otherwise. |
| + */ |
| +function isValidColor(color) { |
| + // Accept 3 or 6 digit hex colors preceded by '#'. |
| + return /^#[0-9A-Fa-f]{3}|#[0-9A-Fa-f]{6}$/.test(color); |
| +} |
| + |
| +/** |
| + * Checks and returns suggestion style. |
| + * @param {Object} userStyle User-specified overrides for suggestion styles. |
| + * @return {Object} Checked styles or defaults. |
| + */ |
| +function getStyle(userStyle) { |
| + var apiHandle = chrome.embeddedSearch.searchBox; |
| + var safeStyle = { |
| + urlColor: '#093', |
| + titleColor: '#666', |
| + font: apiHandle.font, |
| + fontSize: apiHandle.fontSize |
| + }; |
| + if ('urlColor' in userStyle && isValidColor(userStyle.urlColor)) |
| + safeStyle.urlColor = userStyle.urlColor; |
| + if ('titleColor' in userStyle && isValidColor(userStyle.titleColor)) |
| + safeStyle.titleColor = userStyle.titleColor; |
| + return safeStyle; |
| +} |
| + |
| +/** |
| + * Renders a native history suggestion. |
| + * @param {Document} resultDoc The suggestion template document. |
| + * @param {Object} suggestion The NativeSuggestion to render. |
| + * @param {Object} style Checked (not user-set) result style. |
| + */ |
| +function updateResult(resultDoc, suggestion, style) { |
| + resultDoc.body.style.font = style.fontSize + 'px "' + style.font + '";'; |
| + var urlNode = resultDoc.querySelector('#url'); |
| + urlNode.textContent = suggestion.destination_url; |
| + urlNode.style.color = style.urlColor; |
| + var optionalNode = resultDoc.querySelector('#optional'); |
| + if (suggestion.description) { |
| + var titleNode = resultDoc.querySelector('#title'); |
| + titleNode.textContent = suggestion.description; |
| + optionalNode.style.color = style.titleColor; |
| + optionalNode.classList.remove('hide'); |
| + } else { |
| + optionalNode.classList.add('hide'); |
| + } |
| +} |
| + |
| +/** |
| + * Handles a postMessage from the embedding page requesting to populate history |
| + * suggestion iframes. |
| + * @param {Object} message The message. |
| + */ |
| +function handleMessage(message) { |
| + // Only allow messages from the embedding page, which should be an Instant |
| + // search provider or the local omnibox dropdown (and not e.g. a site which |
| + // it has iframed.) |
| + if (message.origin != EMBEDDER_ORIGIN) |
| + return; |
| + var apiHandle = chrome.embeddedSearch.searchBox; |
| + if ('load' in message.data) { |
| + var loaded = []; |
| + for (var id in message.data.load) { |
| + var restrictedId = message.data.load[id]; |
| + var suggestion = apiHandle.getSuggestionData(restrictedId); |
| + updateResult(window.parent.frames[id].document, suggestion, |
|
sreeram
2013/04/01 21:03:27
Where's the "frames" array defined?
Jered
2013/04/01 21:29:18
This is referencing window.frames
https://develope
sreeram
2013/04/01 21:50:41
Correct me if I am wrong: window.parent refers to
|
| + getStyle(message.data.style || {})); |
| + loaded.push(id); |
| + } |
| + message.source.postMessage( |
| + {'loaded': message.data.requestId}, |
| + message.origin); |
| + } |
| +} |
| + |
| +window.addEventListener('message', handleMessage, false); |