Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 /** | |
| 6 * @fileoverview Orchestrates loading of suggestion content in several | |
| 7 * chrome-search://suggestion iframes. | |
| 8 */ | |
| 9 | |
| 10 /** | |
| 11 * The origin of the embedding page. | |
| 12 * This string literal must be in double quotes for proper escaping. | |
| 13 * @type {string} | |
| 14 * @const | |
| 15 */ | |
| 16 var EMBEDDER_ORIGIN = "%s"; | |
| 17 | |
| 18 /** | |
| 19 * Converts an RGB color number to a hex color string if valid. | |
| 20 * @param {number} color A 6-digit hex RGB color code as a number. | |
| 21 * @return {?string} A CSS representation of the color or null if invalid. | |
| 22 */ | |
| 23 function convertColor(color) { | |
|
palmer
2013/04/05 18:43:40
Much better, thanks.
Jered
2013/04/05 18:51:20
Ack np.
| |
| 24 if (typeof(color) == 'number' && !isNaN(color) && | |
| 25 (color % 1) == 0 && color >= 0 && color <= 0xffffff) { | |
| 26 var hexColor = color.toString(16); | |
| 27 return '#000000'.substr(0, 7 - hexColor.length) + hexColor; | |
| 28 } | |
| 29 return null; | |
| 30 } | |
| 31 | |
| 32 /** | |
| 33 * Checks and returns suggestion style. | |
| 34 * @param {!Object} pageStyle Instant page-specified overrides for suggestion | |
| 35 * styles. | |
| 36 * @return {!Object} Checked styles or defaults. | |
| 37 */ | |
| 38 function getStyle(pageStyle) { | |
| 39 var apiHandle = chrome.embeddedSearch.searchBox; | |
| 40 var style = { | |
| 41 queryColor: 0x000000, | |
| 42 urlColor: 0x009933, | |
| 43 titleColor: 0x666666, | |
| 44 font: apiHandle.font, | |
| 45 fontSize: apiHandle.fontSize | |
| 46 }; | |
| 47 if ('queryColor' in pageStyle) | |
| 48 style.queryColor = convertColor(pageStyle.queryColor) || style.queryColor; | |
| 49 if ('urlColor' in pageStyle) | |
| 50 style.urlColor = convertColor(pageStyle.urlColor) || style.urlColor; | |
| 51 if ('titleColor' in pageStyle) | |
| 52 style.titleColor = convertColor(pageStyle.titleColor) || style.titleColor; | |
| 53 return style; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Renders a native history suggestion. | |
| 58 * @param {!Document} resultDoc The suggestion template document. | |
| 59 * @param {!Object} suggestion The NativeSuggestion to render. | |
| 60 * @param {!Object} pageStyle Page-specificed styles. | |
| 61 */ | |
| 62 function updateResult(resultDoc, suggestion, pageStyle) { | |
| 63 var style = getStyle(pageStyle || {}); | |
| 64 resultDoc.body.style.fontSize = style.fontSize + 'px'; | |
| 65 resultDoc.body.style.fontFamily = style.font; | |
| 66 var contentsNode = resultDoc.querySelector('#contents'); | |
| 67 contentsNode.textContent = suggestion.contents; | |
| 68 contentsNode.style.color = suggestion.is_search ? | |
| 69 style.queryColor : style.urlColor; | |
| 70 var optionalNode = resultDoc.querySelector('#optional'); | |
| 71 if (suggestion.description) { | |
| 72 var titleNode = resultDoc.querySelector('#title'); | |
| 73 titleNode.textContent = suggestion.description; | |
| 74 optionalNode.style.color = style.titleColor; | |
| 75 optionalNode.classList.remove('hide'); | |
| 76 } else { | |
| 77 optionalNode.classList.add('hide'); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Handles a postMessage from the embedding page requesting to populate history | |
| 83 * suggestion iframes. | |
| 84 * @param {!Object} message The message. | |
| 85 */ | |
| 86 function handleMessage(message) { | |
| 87 // Only allow messages from the embedding page, which should be an Instant | |
| 88 // search provider or the local omnibox dropdown (and not e.g. a site which | |
| 89 // it has iframed.) | |
| 90 if (message.origin != EMBEDDER_ORIGIN) | |
| 91 return; | |
| 92 | |
| 93 var apiHandle = chrome.embeddedSearch.searchBox; | |
| 94 if ('load' in message.data) { | |
| 95 var loaded = []; | |
| 96 for (var id in message.data.load) { | |
| 97 var restrictedId = message.data.load[id]; | |
| 98 var suggestion = apiHandle.getSuggestionData(restrictedId); | |
| 99 var iframe = window.parent.frames[id]; | |
| 100 if (iframe) { | |
| 101 updateResult(iframe.document, suggestion, message.data.style || {}); | |
| 102 loaded.push(id); | |
| 103 } | |
| 104 } | |
| 105 message.source.postMessage( | |
| 106 {'loaded': message.data.requestId}, message.origin); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 window.addEventListener('message', handleMessage, false); | |
| OLD | NEW |