Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /** | |
| 2 * @fileoverview Orchestrates loading of suggestion content in several | |
| 3 * chrome-search://suggestion iframes. | |
| 4 */ | |
| 5 | |
| 6 /** | |
| 7 * The origin of the embedding page. | |
| 8 * Must be in double quotes for proper escaping. | |
| 9 * @type {string} | |
| 10 * @const | |
| 11 */ | |
| 12 var EMBEDDER_ORIGIN = "%s"; | |
|
Dan Beam
2013/04/05 01:07:37
can you share with with the other file?
Jered
2013/04/05 15:30:23
Because I've got CSP disallowing inline script, I
| |
| 13 | |
| 14 /** | |
| 15 * Checks whether a string is a valid color code. | |
| 16 * @param {string} color A color code. | |
| 17 * @return {boolean} True if color is a valid color code and false otherwise. | |
| 18 */ | |
| 19 function isValidColor(color) { | |
| 20 // Accept 3 or 6 digit hex colors preceded by '#'. | |
| 21 return /^#[0-9A-Fa-f]{3}|#[0-9A-Fa-f]{6}$/.test(color); | |
|
Dan Beam
2013/04/05 01:07:37
'\m/d(^o^)b\m/#123456' passes, '#123_OMGWTFBBQ' pa
palmer
2013/04/05 01:30:32
This is too permissive.
Express color as an integ
Dan Beam
2013/04/05 01:38:45
^ what can you do with this?
Jered
2013/04/05 15:30:23
Sorry, I got this wrong.
Jered
2013/04/05 15:30:23
That is a good pattern, but it's rather awkward in
| |
| 22 } | |
| 23 | |
| 24 /** | |
| 25 * Checks and returns suggestion style. | |
| 26 * @param {Object} pageStyle Instant page-specified overrides for suggestion | |
| 27 * styles. | |
| 28 * @return {Object} Checked styles or defaults. | |
|
Dan Beam
2013/04/05 01:07:37
^ can these be null?
Jered
2013/04/05 15:30:23
Done.
Jered
2013/04/05 15:30:23
Done.
| |
| 29 */ | |
| 30 function getStyle(pageStyle) { | |
| 31 var apiHandle = chrome.embeddedSearch.searchBox; | |
| 32 var safeStyle = { | |
| 33 queryColor: '#000', | |
| 34 urlColor: '#093', | |
| 35 titleColor: '#666', | |
| 36 font: apiHandle.font, | |
| 37 fontSize: apiHandle.fontSize | |
| 38 }; | |
| 39 if ('queryColor' in pageStyle && isValidColor(pageStyle.queryColor)) | |
| 40 safeStyle.queryColor = pageStyle.queryColor; | |
| 41 if ('urlColor' in pageStyle && isValidColor(pageStyle.urlColor)) | |
| 42 safeStyle.urlColor = pageStyle.urlColor; | |
| 43 if ('titleColor' in pageStyle && isValidColor(pageStyle.titleColor)) | |
| 44 safeStyle.titleColor = pageStyle.titleColor; | |
| 45 return safeStyle; | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Renders a native history suggestion. | |
| 50 * @param {Document} resultDoc The suggestion template document. | |
| 51 * @param {Object} suggestion The NativeSuggestion to render. | |
| 52 * @param {Object} style Checked (not user-set) result style. | |
|
Dan Beam
2013/04/05 01:07:37
^ can any of these be null?
Jered
2013/04/05 15:30:23
Done.
| |
| 53 */ | |
| 54 function updateResult(resultDoc, suggestion, style) { | |
| 55 resultDoc.body.style.font = style.fontSize + 'px "' + style.font + '"'; | |
|
palmer
2013/04/05 01:30:32
Can this function be absolutely sure that |font| a
Dan Beam
2013/04/05 01:38:45
resultDoc.body.style.fontSize = style.fontSize + '
Jered
2013/04/05 15:30:23
Done.
Jered
2013/04/05 15:30:23
Done.
| |
| 56 var contentsNode = resultDoc.querySelector('#contents'); | |
| 57 contentsNode.textContent = suggestion.contents; | |
| 58 contentsNode.style.color = suggestion.is_search ? | |
| 59 style.queryColor : style.urlColor; | |
| 60 var optionalNode = resultDoc.querySelector('#optional'); | |
| 61 if (suggestion.description) { | |
| 62 var titleNode = resultDoc.querySelector('#title'); | |
| 63 titleNode.textContent = suggestion.description; | |
| 64 optionalNode.style.color = style.titleColor; | |
| 65 optionalNode.classList.remove('hide'); | |
| 66 } else { | |
| 67 optionalNode.classList.add('hide'); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * Handles a postMessage from the embedding page requesting to populate history | |
| 73 * suggestion iframes. | |
| 74 * @param {Object} message The message. | |
| 75 */ | |
| 76 function handleMessage(message) { | |
| 77 // Only allow messages from the embedding page, which should be an Instant | |
| 78 // search provider or the local omnibox dropdown (and not e.g. a site which | |
| 79 // it has iframed.) | |
| 80 if (message.origin != EMBEDDER_ORIGIN) | |
| 81 return; | |
|
Dan Beam
2013/04/05 01:07:37
\n
Jered
2013/04/05 15:30:23
Done.
| |
| 82 var apiHandle = chrome.embeddedSearch.searchBox; | |
| 83 if ('load' in message.data) { | |
| 84 var loaded = []; | |
| 85 for (var id in message.data.load) { | |
| 86 var restrictedId = message.data.load[id]; | |
| 87 var suggestion = apiHandle.getSuggestionData(restrictedId); | |
| 88 updateResult(window.parent.frames[id].document, suggestion, | |
| 89 getStyle(message.data.style || {})); | |
| 90 loaded.push(id); | |
| 91 } | |
| 92 message.source.postMessage( | |
| 93 {'loaded': message.data.requestId}, | |
|
Dan Beam
2013/04/05 01:07:37
nit: this could probably all fit in one line
Jered
2013/04/05 15:30:23
Done.
| |
| 94 message.origin); | |
| 95 } | |
| 96 } | |
|
Dan Beam
2013/04/05 01:07:37
why are these in the global namespace?
Jered
2013/04/05 15:30:23
Let's discuss in other comment, and I'll fix it in
| |
| 97 | |
| 98 window.addEventListener('message', handleMessage, false); | |
| OLD | NEW |