1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('options', function() {
const OptionsPage = options.OptionsPage;
const ArrayDataModel = cr.ui.ArrayDataModel;
const ListSelectionModel = cr.ui.ListSelectionModel;
//
// BrowserOptions class
// Encapsulated handling of browser options page.
//
function BrowserOptions() {
OptionsPage.call(this, 'browser', templateData.browserPage, 'browserPage');
}
cr.addSingletonGetter(BrowserOptions);
BrowserOptions.prototype = {
// Inherit BrowserOptions from OptionsPage.
__proto__: options.OptionsPage.prototype,
homepage_pref_: {
'name': 'homepage',
'value': '',
'managed': false
},
homepage_is_newtabpage_pref_: {
'name': 'homepage_is_newtabpage',
'value': true,
'managed': false
},
/**
* Initialize BrowserOptions page.
*/
initializePage: function() {
// Call base class implementation to start preference initialization.
OptionsPage.prototype.initializePage.call(this);
// Wire up controls.
$('startupUseCurrentButton').onclick = function(event) {
chrome.send('setStartupPagesToCurrentPages');
};
$('startupPageManagerButton').onclick = function(event) {
OptionsPage.showPageByName('startupPages');
};
$('defaultSearchManageEnginesButton').onclick = function(event) {
OptionsPage.showPageByName('searchEngines');
chrome.send('coreOptionsUserMetricsAction',
['Options_ManageSearchEngines']);
};
$('instantEnableCheckbox').onclick = function(event) {
var alreadyConfirmed = $('instantDialogShown').checked;
if (this.checked && !alreadyConfirmed) {
// Leave disabled for now. The PrefCheckbox handler already set it to
// true so undo that.
Preferences.setBooleanPref(this.pref, false, this.metric);
OptionsPage.showOverlay('instantConfirmOverlay');
}
};
$('defaultSearchEngine').onchange = this.setDefaultSearchEngine;
var homepageField = $('homepageURL');
$('homepageUseNTPButton').onchange =
this.handleHomepageUseNTPButtonChange_.bind(this);
$('homepageUseURLButton').onchange =
this.handleHomepageUseURLButtonChange_.bind(this);
homepageField.onchange =
this.handleHomepageURLChange_.bind(this);
// Ensure that changes are committed when closing the page.
window.addEventListener('unload', function() {
if (document.activeElement == homepageField)
homepageField.blur();
});
if (!cr.isChromeOS) {
$('defaultBrowserUseAsDefaultButton').onclick = function(event) {
chrome.send('becomeDefaultBrowser');
};
}
var list = $('startupPagesShortList');
options.browser_options.StartupPageList.decorate(list);
list.autoExpands = true;
list.selectionModel = new ListSelectionModel;
// Check if we are in the guest mode.
if (cr.commandLine.options['--bwsi']) {
// Hide the startup section.
$('startupSection').classList.add('hidden');
} else {
// Initialize control enabled states.
Preferences.getInstance().addEventListener('session.restore_on_startup',
this.updateCustomStartupPageControlStates_.bind(this));
Preferences.getInstance().addEventListener('homepage_is_newtabpage',
this.handleHomepageIsNewTabPageChange_.bind(this));
Preferences.getInstance().addEventListener('homepage',
this.handleHomepageChange_.bind(this));
this.updateCustomStartupPageControlStates_();
}
},
/**
* Update the Default Browsers section based on the current state.
* @private
* @param {string} statusString Description of the current default state.
* @param {boolean} isDefault Whether or not the browser is currently
* default.
* @param {boolean} canBeDefault Whether or not the browser can be default.
*/
updateDefaultBrowserState_: function(statusString, isDefault,
canBeDefault) {
var label = $('defaultBrowserState');
label.textContent = statusString;
$('defaultBrowserUseAsDefaultButton').disabled = !canBeDefault ||
isDefault;
},
/**
* Clears the search engine popup.
* @private
*/
clearSearchEngines_: function() {
$('defaultSearchEngine').textContent = '';
},
/**
* Updates the search engine popup with the given entries.
* @param {Array} engines List of available search engines.
* @param {number} defaultValue The value of the current default engine.
*/
updateSearchEngines_: function(engines, defaultValue) {
this.clearSearchEngines_();
engineSelect = $('defaultSearchEngine');
engineCount = engines.length;
var defaultIndex = -1;
for (var i = 0; i < engineCount; i++) {
var engine = engines[i];
var option = new Option(engine['name'], engine['index']);
if (defaultValue == option.value)
defaultIndex = i;
engineSelect.appendChild(option);
}
if (defaultIndex >= 0)
engineSelect.selectedIndex = defaultIndex;
},
/**
* Returns true if the custom startup page control block should
* be enabled.
* @private
* @returns {boolean} Whether the startup page controls should be
* enabled.
*/
shouldEnableCustomStartupPageControls_: function(pages) {
return $('startupShowPagesButton').checked;
},
/**
* Updates the startup pages list with the given entries.
* @private
* @param {Array} pages List of startup pages.
*/
updateStartupPages_: function(pages) {
var list = $('startupPagesShortList');
list.dataModel = new ArrayDataModel(pages);
if (pages.length > 0 && pages.length <= 10) {
list.classList.remove("hidden");
$('startupPageManagement').classList.add('settings-list');
$('startupShowPagesLabel').textContent =
localStrings.getStringWithoutAccelerator('startupShowPages');
} else {
list.classList.add("hidden");
$('startupPageManagement').classList.remove('settings-list');
$('startupShowPagesLabel').textContent =
localStrings.getStringWithoutAccelerator('startupShowManyPages');
}
},
/**
* Handles change events of the radio button 'homepageUseURLButton'.
* @private
* @param {event} change event.
*/
handleHomepageUseURLButtonChange_: function(event) {
Preferences.setBooleanPref('homepage_is_newtabpage', false);
},
/**
* Handles change events of the radio button 'homepageUseNTPButton'.
* @private
* @param {event} change event.
*/
handleHomepageUseNTPButtonChange_: function(event) {
Preferences.setBooleanPref('homepage_is_newtabpage', true);
},
/**
* Handles change events of the text field 'homepageURL'.
* @private
* @param {event} change event.
*/
handleHomepageURLChange_: function(event) {
Preferences.setStringPref('homepage', $('homepageURL').value);
},
/**
* Handle change events of the preference 'homepage'.
* @private
* @param {event} preference changed event.
*/
handleHomepageChange_: function(event) {
this.homepage_pref_.value = event.value['value'];
this.homepage_pref_.managed = event.value['managed'];
if (this.isHomepageURLNewTabPageURL_() && !this.homepage_pref_.managed &&
!this.homepage_is_newtabpage_pref_.managed) {
var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_();
Preferences.setStringPref('homepage', '')
Preferences.setBooleanPref('homepage_is_newtabpage', useNewTabPage)
}
this.updateHomepageControlStates_();
},
/**
* Handle change events of the preference homepage_is_newtabpage.
* @private
* @param {event} preference changed event.
*/
handleHomepageIsNewTabPageChange_: function(event) {
this.homepage_is_newtabpage_pref_.value = event.value['value'];
this.homepage_is_newtabpage_pref_.managed = event.value['managed'];
this.updateHomepageControlStates_();
},
/**
* Update homepage preference UI controls. Here's a table describing the
* desired characteristics of the homepage choice radio value, its enabled
* state and the URL field enabled state. They depend on the values of the
* managed bits for homepage (m_hp) and homepageIsNewTabPage (m_ntp)
* preferences, as well as the value of the homepageIsNewTabPage preference
* (ntp) and whether the homepage preference is equal to the new tab page
* URL (hpisntp).
*
* m_hp m_ntp ntp hpisntp| choice value| choice enabled| URL field enabled
* ------------------------------------------------------------------------
* 0 0 0 0 | homepage | 1 | 1
* 0 0 0 1 | new tab page| 1 | 0
* 0 0 1 0 | new tab page| 1 | 0
* 0 0 1 1 | new tab page| 1 | 0
* 0 1 0 0 | homepage | 0 | 1
* 0 1 0 1 | homepage | 0 | 1
* 0 1 1 0 | new tab page| 0 | 0
* 0 1 1 1 | new tab page| 0 | 0
* 1 0 0 0 | homepage | 1 | 0
* 1 0 0 1 | new tab page| 0 | 0
* 1 0 1 0 | new tab page| 1 | 0
* 1 0 1 1 | new tab page| 0 | 0
* 1 1 0 0 | homepage | 0 | 0
* 1 1 0 1 | new tab page| 0 | 0
* 1 1 1 0 | new tab page| 0 | 0
* 1 1 1 1 | new tab page| 0 | 0
*
* thus, we have:
*
* choice value is new tab page === ntp || (hpisntp && (m_hp || !m_ntp))
* choice enabled === !m_ntp && !(m_hp && hpisntp)
* URL field enabled === !ntp && !mhp && !(hpisntp && !m_ntp)
*
* which also make sense if you think about them.
* @private
*/
updateHomepageControlStates_: function() {
var homepageField = $('homepageURL');
homepageField.disabled = !this.isHomepageURLFieldEnabled_();
homepageField.value = this.homepage_pref_.value;
homepageField.style.backgroundImage = url('chrome://favicon/' +
this.homepage_pref_.value);
var disableChoice = !this.isHomepageChoiceEnabled_();
$('homepageUseURLButton').disabled = disableChoice;
$('homepageUseNTPButton').disabled = disableChoice;
var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_();
$('homepageUseNTPButton').checked = useNewTabPage;
$('homepageUseURLButton').checked = !useNewTabPage;
},
/**
* Tests whether the value of the 'homepage' preference equls the new tab
* page url (chrome://newtab).
* @private
* @returns {boolean} True if the 'homepage' value equals the new tab page
* url.
*/
isHomepageURLNewTabPageURL_ : function() {
return (this.homepage_pref_.value.toLowerCase() == 'chrome://newtab');
},
/**
* Tests whether the Homepage choice "Use New Tab Page" is selected.
* @private
* @returns {boolean} True if "Use New Tab Page" is selected.
*/
isHomepageIsNewTabPageChoiceSelected_: function() {
return (this.homepage_is_newtabpage_pref_.value ||
(this.isHomepageURLNewTabPageURL_() &&
(this.homepage_pref_.managed ||
!this.homepage_is_newtabpage_pref_.managed)));
},
/**
* Tests whether the home page choice controls are enabled.
* @private
* @returns {boolean} True if the home page choice controls are enabled.
*/
isHomepageChoiceEnabled_: function() {
return (!this.homepage_is_newtabpage_pref_.managed &&
!(this.homepage_pref_.managed &&
this.isHomepageURLNewTabPageURL_()));
},
/**
* Checks whether the home page field should be enabled.
* @private
* @returns {boolean} True if the home page field should be enabled.
*/
isHomepageURLFieldEnabled_: function() {
return (!this.homepage_is_newtabpage_pref_.value &&
!this.homepage_pref_.managed &&
!(this.isHomepageURLNewTabPageURL_() &&
!this.homepage_is_newtabpage_pref_.managed));
},
/**
* Sets the enabled state of the custom startup page list controls
* based on the current startup radio button selection.
* @private
*/
updateCustomStartupPageControlStates_: function() {
var disable = !this.shouldEnableCustomStartupPageControls_();
$('startupPagesShortList').disabled = disable;
$('startupUseCurrentButton').disabled = disable;
$('startupPageManagerButton').disabled = disable;
},
/**
* Set the default search engine based on the popup selection.
*/
setDefaultSearchEngine: function() {
var engineSelect = $('defaultSearchEngine');
var selectedIndex = engineSelect.selectedIndex;
if (selectedIndex >= 0) {
var selection = engineSelect.options[selectedIndex];
chrome.send('setDefaultSearchEngine', [String(selection.value)]);
}
},
};
BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault,
canBeDefault) {
if (!cr.isChromeOS) {
BrowserOptions.getInstance().updateDefaultBrowserState_(statusString,
isDefault,
canBeDefault);
}
};
BrowserOptions.updateSearchEngines = function(engines, defaultValue) {
BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue);
};
BrowserOptions.updateStartupPages = function(pages) {
BrowserOptions.getInstance().updateStartupPages_(pages);
StartupPageManager.getInstance().updateStartupPages_(pages);
};
// Export
return {
BrowserOptions: BrowserOptions
};
});
|