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