Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: chrome/browser/resources/options/browser_options.js

Issue 7003007: Apply content-security-policy to the HTML options page. This is a (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 cr.define('options', function() {
6 const OptionsPage = options.OptionsPage;
7 const ArrayDataModel = cr.ui.ArrayDataModel;
8
9 //
10 // BrowserOptions class
11 // Encapsulated handling of browser options page.
12 //
13 function BrowserOptions() {
14 OptionsPage.call(this, 'browser',
15 templateData.browserPageTabTitle,
16 'browserPage');
17 }
18
19 cr.addSingletonGetter(BrowserOptions);
20
21 BrowserOptions.prototype = {
22 // Inherit BrowserOptions from OptionsPage.
23 __proto__: options.OptionsPage.prototype,
24
25 startup_pages_pref_: {
26 'name': 'session.urls_to_restore_on_startup',
27 'managed': false
28 },
29
30 homepage_pref_: {
31 'name': 'homepage',
32 'value': '',
33 'managed': false
34 },
35
36 homepage_is_newtabpage_pref_: {
37 'name': 'homepage_is_newtabpage',
38 'value': true,
39 'managed': false
40 },
41
42 /**
43 * At autocomplete list that can be attached to a text field during editing.
44 * @type {HTMLElement}
45 * @private
46 */
47 autocompleteList_: null,
48
49 // The cached value of the instant.confirm_dialog_shown preference.
50 instantConfirmDialogShown_: false,
51
52 /**
53 * Initialize BrowserOptions page.
54 */
55 initializePage: function() {
56 // Call base class implementation to start preference initialization.
57 OptionsPage.prototype.initializePage.call(this);
58
59 // Wire up controls.
60 $('startupUseCurrentButton').onclick = function(event) {
61 chrome.send('setStartupPagesToCurrentPages');
62 };
63 $('toolbarShowBookmarksBar').onchange = function() {
64 chrome.send('toggleShowBookmarksBar');
65 };
66 $('defaultSearchManageEnginesButton').onclick = function(event) {
67 OptionsPage.navigateToPage('searchEngines');
68 chrome.send('coreOptionsUserMetricsAction',
69 ['Options_ManageSearchEngines']);
70 };
71 $('defaultSearchEngine').onchange = this.setDefaultSearchEngine_;
72
73 var self = this;
74 $('instantEnableCheckbox').onclick = function(event) {
75 if (this.checked && !self.instantConfirmDialogShown_) {
76 // Leave disabled for now. The PrefCheckbox handler already set it to
77 // true so undo that.
78 Preferences.setBooleanPref(this.pref, false, this.metric);
79 OptionsPage.navigateToPage('instantConfirm');
80 }
81 };
82
83 Preferences.getInstance().addEventListener('instant.confirm_dialog_shown',
84 this.onInstantConfirmDialogShownChanged_.bind(this));
85
86 var homepageField = $('homepageURL');
87 $('homepageUseNTPButton').onchange =
88 this.handleHomepageUseNTPButtonChange_.bind(this);
89 $('homepageUseURLButton').onchange =
90 this.handleHomepageUseURLButtonChange_.bind(this);
91 var homepageChangeHandler = this.handleHomepageURLChange_.bind(this);
92 homepageField.addEventListener('change', homepageChangeHandler);
93 homepageField.addEventListener('input', homepageChangeHandler);
94 homepageField.addEventListener('focus', function(event) {
95 self.autocompleteList_.attachToInput(homepageField);
96 });
97 homepageField.addEventListener('blur', function(event) {
98 self.autocompleteList_.detach();
99 });
100 homepageField.addEventListener('keydown', function(event) {
101 // Remove focus when the user hits enter since people expect feedback
102 // indicating that they are done editing.
103 if (event.keyIdentifier == 'Enter')
104 homepageField.blur();
105 });
106 // Text fields may change widths when the window changes size, so make
107 // sure the suggestion list stays in sync.
108 window.addEventListener('resize', function() {
109 self.autocompleteList_.syncWidthToInput();
110 });
111
112 // Ensure that changes are committed when closing the page.
113 window.addEventListener('unload', function() {
114 if (document.activeElement == homepageField)
115 homepageField.blur();
116 });
117
118 if (!cr.isChromeOS) {
119 $('defaultBrowserUseAsDefaultButton').onclick = function(event) {
120 chrome.send('becomeDefaultBrowser');
121 };
122 }
123
124 var startupPagesList = $('startupPagesList');
125 options.browser_options.StartupPageList.decorate(startupPagesList);
126 startupPagesList.autoExpands = true;
127
128 // Check if we are in the guest mode.
129 if (cr.commandLine.options['--bwsi']) {
130 // Hide the startup section.
131 $('startupSection').classList.add('hidden');
132 } else {
133 // Initialize control enabled states.
134 Preferences.getInstance().addEventListener('session.restore_on_startup',
135 this.updateCustomStartupPageControlStates_.bind(this));
136 Preferences.getInstance().addEventListener(
137 this.startup_pages_pref_.name,
138 this.handleStartupPageListChange_.bind(this));
139 Preferences.getInstance().addEventListener(
140 this.homepage_pref_.name,
141 this.handleHomepageChange_.bind(this));
142 Preferences.getInstance().addEventListener(
143 this.homepage_is_newtabpage_pref_.name,
144 this.handleHomepageIsNewTabPageChange_.bind(this));
145
146 this.updateCustomStartupPageControlStates_();
147 }
148
149 var suggestionList = new options.AutocompleteList();
150 suggestionList.autoExpands = true;
151 suggestionList.suggestionUpdateRequestCallback =
152 this.requestAutocompleteSuggestions_.bind(this);
153 $('main-content').appendChild(suggestionList);
154 this.autocompleteList_ = suggestionList;
155 startupPagesList.autocompleteList = suggestionList;
156 },
157
158 /**
159 * Called when the value of the instant.confirm_dialog_shown preference
160 * changes. Cache this value.
161 * @param {Event} event Change event.
162 * @private
163 */
164 onInstantConfirmDialogShownChanged_: function(event) {
165 this.instantConfirmDialogShown_ = event.value['value'];
166 },
167
168 /**
169 * Update the Default Browsers section based on the current state.
170 * @param {string} statusString Description of the current default state.
171 * @param {boolean} isDefault Whether or not the browser is currently
172 * default.
173 * @param {boolean} canBeDefault Whether or not the browser can be default.
174 * @private
175 */
176 updateDefaultBrowserState_: function(statusString, isDefault,
177 canBeDefault) {
178 var label = $('defaultBrowserState');
179 label.textContent = statusString;
180
181 $('defaultBrowserUseAsDefaultButton').disabled = !canBeDefault ||
182 isDefault;
183 },
184
185 /**
186 * Clears the search engine popup.
187 * @private
188 */
189 clearSearchEngines_: function() {
190 $('defaultSearchEngine').textContent = '';
191 },
192
193 /**
194 * Updates the search engine popup with the given entries.
195 * @param {Array} engines List of available search engines.
196 * @param {number} defaultValue The value of the current default engine.
197 */
198 updateSearchEngines_: function(engines, defaultValue) {
199 this.clearSearchEngines_();
200 engineSelect = $('defaultSearchEngine');
201 engineCount = engines.length;
202 var defaultIndex = -1;
203 for (var i = 0; i < engineCount; i++) {
204 var engine = engines[i];
205 var option = new Option(engine['name'], engine['index']);
206 option.hasInstant = engine['hasInstant'];
207 if (defaultValue == option.value)
208 defaultIndex = i;
209 engineSelect.appendChild(option);
210 }
211 if (defaultIndex >= 0)
212 engineSelect.selectedIndex = defaultIndex;
213
214 this.setDefaultSearchEngine_();
215 },
216
217 /**
218 * Returns true if the custom startup page control block should
219 * be enabled.
220 * @returns {boolean} Whether the startup page controls should be
221 * enabled.
222 */
223 shouldEnableCustomStartupPageControls: function(pages) {
224 return $('startupShowPagesButton').checked &&
225 !this.startup_pages_pref_.managed;
226 },
227
228 /**
229 * Updates the startup pages list with the given entries.
230 * @param {Array} pages List of startup pages.
231 * @private
232 */
233 updateStartupPages_: function(pages) {
234 var model = new ArrayDataModel(pages);
235 // Add a "new page" row.
236 model.push({
237 'modelIndex': '-1'
238 });
239 $('startupPagesList').dataModel = model;
240 },
241
242 /**
243 * Handles change events of the radio button 'homepageUseURLButton'.
244 * @param {event} change event.
245 * @private
246 */
247 handleHomepageUseURLButtonChange_: function(event) {
248 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name, false);
249 },
250
251 /**
252 * Handles change events of the radio button 'homepageUseNTPButton'.
253 * @param {event} change event.
254 * @private
255 */
256 handleHomepageUseNTPButtonChange_: function(event) {
257 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name, true);
258 },
259
260 /**
261 * Handles input and change events of the text field 'homepageURL'.
262 * @param {event} input/change event.
263 * @private
264 */
265 handleHomepageURLChange_: function(event) {
266 var homepageField = $('homepageURL');
267 var doFixup = event.type == 'change' ? '1' : '0';
268 chrome.send('setHomePage', [homepageField.value, doFixup]);
269 },
270
271 /**
272 * Handle change events of the preference 'homepage'.
273 * @param {event} preference changed event.
274 * @private
275 */
276 handleHomepageChange_: function(event) {
277 this.homepage_pref_.value = event.value['value'];
278 this.homepage_pref_.managed = event.value['managed'];
279 if (this.isHomepageURLNewTabPageURL_() && !this.homepage_pref_.managed &&
280 !this.homepage_is_newtabpage_pref_.managed) {
281 var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_();
282 Preferences.setStringPref(this.homepage_pref_.name, '')
283 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name,
284 useNewTabPage)
285 }
286 this.updateHomepageControlStates_();
287 },
288
289 /**
290 * Handle change events of the preference homepage_is_newtabpage.
291 * @param {event} preference changed event.
292 * @private
293 */
294 handleHomepageIsNewTabPageChange_: function(event) {
295 this.homepage_is_newtabpage_pref_.value = event.value['value'];
296 this.homepage_is_newtabpage_pref_.managed = event.value['managed'];
297 this.updateHomepageControlStates_();
298 },
299
300 /**
301 * Update homepage preference UI controls. Here's a table describing the
302 * desired characteristics of the homepage choice radio value, its enabled
303 * state and the URL field enabled state. They depend on the values of the
304 * managed bits for homepage (m_hp) and homepageIsNewTabPage (m_ntp)
305 * preferences, as well as the value of the homepageIsNewTabPage preference
306 * (ntp) and whether the homepage preference is equal to the new tab page
307 * URL (hpisntp).
308 *
309 * m_hp m_ntp ntp hpisntp| choice value| choice enabled| URL field enabled
310 * ------------------------------------------------------------------------
311 * 0 0 0 0 | homepage | 1 | 1
312 * 0 0 0 1 | new tab page| 1 | 0
313 * 0 0 1 0 | new tab page| 1 | 0
314 * 0 0 1 1 | new tab page| 1 | 0
315 * 0 1 0 0 | homepage | 0 | 1
316 * 0 1 0 1 | homepage | 0 | 1
317 * 0 1 1 0 | new tab page| 0 | 0
318 * 0 1 1 1 | new tab page| 0 | 0
319 * 1 0 0 0 | homepage | 1 | 0
320 * 1 0 0 1 | new tab page| 0 | 0
321 * 1 0 1 0 | new tab page| 1 | 0
322 * 1 0 1 1 | new tab page| 0 | 0
323 * 1 1 0 0 | homepage | 0 | 0
324 * 1 1 0 1 | new tab page| 0 | 0
325 * 1 1 1 0 | new tab page| 0 | 0
326 * 1 1 1 1 | new tab page| 0 | 0
327 *
328 * thus, we have:
329 *
330 * choice value is new tab page === ntp || (hpisntp && (m_hp || !m_ntp))
331 * choice enabled === !m_ntp && !(m_hp && hpisntp)
332 * URL field enabled === !ntp && !mhp && !(hpisntp && !m_ntp)
333 *
334 * which also make sense if you think about them.
335 * @private
336 */
337 updateHomepageControlStates_: function() {
338 var homepageField = $('homepageURL');
339 homepageField.disabled = !this.isHomepageURLFieldEnabled_();
340 if (homepageField.value != this.homepage_pref_.value)
341 homepageField.value = this.homepage_pref_.value;
342 homepageField.style.backgroundImage = url('chrome://favicon/' +
343 this.homepage_pref_.value);
344 var disableChoice = !this.isHomepageChoiceEnabled_();
345 $('homepageUseURLButton').disabled = disableChoice;
346 $('homepageUseNTPButton').disabled = disableChoice;
347 var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_();
348 $('homepageUseNTPButton').checked = useNewTabPage;
349 $('homepageUseURLButton').checked = !useNewTabPage;
350 },
351
352 /**
353 * Tests whether the value of the 'homepage' preference equls the new tab
354 * page url (chrome://newtab).
355 * @returns {boolean} True if the 'homepage' value equals the new tab page
356 * url.
357 * @private
358 */
359 isHomepageURLNewTabPageURL_ : function() {
360 return (this.homepage_pref_.value.toLowerCase() == 'chrome://newtab');
361 },
362
363 /**
364 * Tests whether the Homepage choice "Use New Tab Page" is selected.
365 * @returns {boolean} True if "Use New Tab Page" is selected.
366 * @private
367 */
368 isHomepageIsNewTabPageChoiceSelected_: function() {
369 return (this.homepage_is_newtabpage_pref_.value ||
370 (this.isHomepageURLNewTabPageURL_() &&
371 (this.homepage_pref_.managed ||
372 !this.homepage_is_newtabpage_pref_.managed)));
373 },
374
375 /**
376 * Tests whether the home page choice controls are enabled.
377 * @returns {boolean} True if the home page choice controls are enabled.
378 * @private
379 */
380 isHomepageChoiceEnabled_: function() {
381 return (!this.homepage_is_newtabpage_pref_.managed &&
382 !(this.homepage_pref_.managed &&
383 this.isHomepageURLNewTabPageURL_()));
384 },
385
386 /**
387 * Checks whether the home page field should be enabled.
388 * @returns {boolean} True if the home page field should be enabled.
389 * @private
390 */
391 isHomepageURLFieldEnabled_: function() {
392 return (!this.homepage_is_newtabpage_pref_.value &&
393 !this.homepage_pref_.managed &&
394 !(this.isHomepageURLNewTabPageURL_() &&
395 !this.homepage_is_newtabpage_pref_.managed));
396 },
397
398 /**
399 * Sets the enabled state of the custom startup page list controls
400 * based on the current startup radio button selection.
401 * @private
402 */
403 updateCustomStartupPageControlStates_: function() {
404 var disable = !this.shouldEnableCustomStartupPageControls();
405 $('startupPagesList').disabled = disable;
406 $('startupUseCurrentButton').disabled = disable;
407 },
408
409 /**
410 * Handle change events of the preference
411 * 'session.urls_to_restore_on_startup'.
412 * @param {event} preference changed event.
413 * @private
414 */
415 handleStartupPageListChange_: function(event) {
416 this.startup_pages_pref_.managed = event.value['managed'];
417 this.updateCustomStartupPageControlStates_();
418 },
419
420 /**
421 * Set the default search engine based on the popup selection.
422 */
423 setDefaultSearchEngine_: function() {
424 var engineSelect = $('defaultSearchEngine');
425 var selectedIndex = engineSelect.selectedIndex;
426 if (selectedIndex >= 0) {
427 var selection = engineSelect.options[selectedIndex];
428 $('instantEnableCheckbox').disabled = !selection.hasInstant;
429 chrome.send('setDefaultSearchEngine', [String(selection.value)]);
430 }
431 },
432
433 /**
434 * Sends an asynchronous request for new autocompletion suggestions for the
435 * the given query. When new suggestions are available, the C++ handler will
436 * call updateAutocompleteSuggestions_.
437 * @param {string} query List of autocomplete suggestions.
438 * @private
439 */
440 requestAutocompleteSuggestions_: function(query) {
441 chrome.send('requestAutocompleteSuggestions', [query]);
442 },
443
444 /**
445 * Updates the autocomplete suggestion list with the given entries.
446 * @param {Array} pages List of autocomplete suggestions.
447 * @private
448 */
449 updateAutocompleteSuggestions_: function(suggestions) {
450 var list = this.autocompleteList_;
451 // If the trigger for this update was a value being selected from the
452 // current list, do nothing.
453 if (list.targetInput && list.selectedItem &&
454 list.selectedItem['url'] == list.targetInput.value)
455 return;
456 list.suggestions = suggestions;
457 },
458 };
459
460 BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault,
461 canBeDefault) {
462 if (!cr.isChromeOS) {
463 BrowserOptions.getInstance().updateDefaultBrowserState_(statusString,
464 isDefault,
465 canBeDefault);
466 }
467 };
468
469 BrowserOptions.updateSearchEngines = function(engines, defaultValue) {
470 BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue);
471 };
472
473 BrowserOptions.updateStartupPages = function(pages) {
474 BrowserOptions.getInstance().updateStartupPages_(pages);
475 };
476
477 BrowserOptions.updateAutocompleteSuggestions = function(suggestions) {
478 BrowserOptions.getInstance().updateAutocompleteSuggestions_(suggestions);
479 };
480
481 // Export
482 return {
483 BrowserOptions: BrowserOptions
484 };
485
486 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698