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

Side by Side Diff: chrome/browser/resources/settings/route.js

Issue 2957153003: MD Settings: remove unsupported routes from guest-mode. (Closed)
Patch Set: merge Created 3 years, 5 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /**
6 * Specifies all possible routes in settings.
7 *
8 * @typedef {{
9 * ABOUT: (undefined|!settings.Route),
10 * ABOUT_ABOUT: (undefined|!settings.Route),
11 * ACCESSIBILITY: (undefined|!settings.Route),
12 * ACCOUNTS: (undefined|!settings.Route),
13 * ADVANCED: (undefined|!settings.Route),
14 * ANDROID_APPS: (undefined|!settings.Route),
15 * ANDROID_APPS_DETAILS: (undefined|!settings.Route),
16 * APPEARANCE: (undefined|!settings.Route),
17 * AUTOFILL: (undefined|!settings.Route),
18 * BASIC: (undefined|!settings.Route),
19 * BLUETOOTH: (undefined|!settings.Route),
20 * BLUETOOTH_DEVICES: (undefined|!settings.Route),
21 * CERTIFICATES: (undefined|!settings.Route),
22 * CHANGE_PICTURE: (undefined|!settings.Route),
23 * CLEAR_BROWSER_DATA: (undefined|!settings.Route),
24 * CLOUD_PRINTERS: (undefined|!settings.Route),
25 * CUPS_PRINTER_DETAIL: (undefined|!settings.Route),
26 * CUPS_PRINTERS: (undefined|!settings.Route),
27 * DATETIME: (undefined|!settings.Route),
28 * DEFAULT_BROWSER: (undefined|!settings.Route),
29 * DETAILED_BUILD_INFO: (undefined|!settings.Route),
30 * DEVICE: (undefined|!settings.Route),
31 * DISPLAY: (undefined|!settings.Route),
32 * DOWNLOADS: (undefined|!settings.Route),
33 * EDIT_DICTIONARY: (undefined|!settings.Route),
34 * FINGERPRINT: (undefined|!settings.Route),
35 * FONTS: (undefined|!settings.Route),
36 * IMPORT_DATA: (undefined|!settings.Route),
37 * INPUT_METHODS: (undefined|!settings.Route),
38 * INTERNET: (undefined|!settings.Route),
39 * INTERNET_NETWORKS: (undefined|!settings.Route),
40 * KEYBOARD: (undefined|!settings.Route),
41 * KNOWN_NETWORKS: (undefined|!settings.Route),
42 * LANGUAGES: (undefined|!settings.Route),
43 * LOCK_SCREEN: (undefined|!settings.Route),
44 * MANAGE_ACCESSIBILITY: (undefined|!settings.Route),
45 * MANAGE_PASSWORDS: (undefined|!settings.Route),
46 * MANAGE_PROFILE: (undefined|!settings.Route),
47 * NETWORK_CONFIG: (undefined|!settings.Route),
48 * NETWORK_DETAIL: (undefined|!settings.Route),
49 * ON_STARTUP: (undefined|!settings.Route),
50 * PASSWORDS: (undefined|!settings.Route),
51 * PEOPLE: (undefined|!settings.Route),
52 * POINTERS: (undefined|!settings.Route),
53 * POWER: (undefined|!settings.Route),
54 * PRINTING: (undefined|!settings.Route),
55 * PRIVACY: (undefined|!settings.Route),
56 * RESET: (undefined|!settings.Route),
57 * RESET_DIALOG: (undefined|!settings.Route),
58 * SEARCH: (undefined|!settings.Route),
59 * SEARCH_ENGINES: (undefined|!settings.Route),
60 * SIGN_OUT: (undefined|!settings.Route),
61 * SITE_SETTINGS: (undefined|!settings.Route),
62 * SITE_SETTINGS_ADS: (undefined|!settings.Route),
63 * SITE_SETTINGS_ALL: (undefined|!settings.Route),
64 * SITE_SETTINGS_AUTOMATIC_DOWNLOADS: (undefined|!settings.Route),
65 * SITE_SETTINGS_BACKGROUND_SYNC: (undefined|!settings.Route),
66 * SITE_SETTINGS_CAMERA: (undefined|!settings.Route),
67 * SITE_SETTINGS_COOKIES: (undefined|!settings.Route),
68 * SITE_SETTINGS_DATA_DETAILS: (undefined|!settings.Route),
69 * SITE_SETTINGS_FLASH: (undefined|!settings.Route),
70 * SITE_SETTINGS_HANDLERS: (undefined|!settings.Route),
71 * SITE_SETTINGS_IMAGES: (undefined|!settings.Route),
72 * SITE_SETTINGS_JAVASCRIPT: (undefined|!settings.Route),
73 * SITE_SETTINGS_LOCATION: (undefined|!settings.Route),
74 * SITE_SETTINGS_MICROPHONE: (undefined|!settings.Route),
75 * SITE_SETTINGS_MIDI_DEVICES: (undefined|!settings.Route),
76 * SITE_SETTINGS_NOTIFICATIONS: (undefined|!settings.Route),
77 * SITE_SETTINGS_PDF_DOCUMENTS: (undefined|!settings.Route),
78 * SITE_SETTINGS_POPUPS: (undefined|!settings.Route),
79 * SITE_SETTINGS_PROTECTED_CONTENT: (undefined|!settings.Route),
80 * SITE_SETTINGS_SITE_DETAILS: (undefined|!settings.Route),
81 * SITE_SETTINGS_UNSANDBOXED_PLUGINS: (undefined|!settings.Route),
82 * SITE_SETTINGS_USB_DEVICES: (undefined|!settings.Route),
83 * SITE_SETTINGS_ZOOM_LEVELS: (undefined|!settings.Route),
84 * STORAGE: (undefined|!settings.Route),
85 * STYLUS: (undefined|!settings.Route),
86 * SYNC: (undefined|!settings.Route),
87 * SYSTEM: (undefined|!settings.Route),
88 * TRIGGERED_RESET_DIALOG: (undefined|!settings.Route),
89 * }}
90 */
91 var SettingsRoutes;
92
5 cr.define('settings', function() { 93 cr.define('settings', function() {
94
6 /** 95 /**
7 * Class for navigable routes. May only be instantiated within this file. 96 * Class for navigable routes. May only be instantiated within this file.
8 * @constructor
9 * @param {string} path
10 * @private
11 */ 97 */
12 var Route = function(path) { 98 class Route {
13 this.path = path; 99 /** @param {string} path */
100 constructor(path) {
101 /** @type {string} */
102 this.path = path;
14 103
15 /** @type {?settings.Route} */ 104 /** @type {?settings.Route} */
16 this.parent = null; 105 this.parent = null;
17 106
18 /** @type {number} */ 107 /** @type {number} */
19 this.depth = 0; 108 this.depth = 0;
20 109
21 /** 110 /**
22 * @type {boolean} Whether this route corresponds to a navigable 111 * @type {boolean} Whether this route corresponds to a navigable
23 * dialog. Those routes don't belong to a "section". 112 * dialog. Those routes don't belong to a "section".
24 */ 113 */
25 this.isNavigableDialog = false; 114 this.isNavigableDialog = false;
26 115
27 // Below are all legacy properties to provide compatibility with the old 116 // Below are all legacy properties to provide compatibility with the old
28 // routing system. 117 // routing system.
29 118
30 /** @type {string} */ 119 /** @type {string} */
31 this.section = ''; 120 this.section = '';
32 }; 121 }
33 122
34 Route.prototype = {
35 /** 123 /**
36 * Returns a new Route instance that's a child of this route. 124 * Returns a new Route instance that's a child of this route.
37 * @param {string} path Extends this route's path if it doesn't contain a 125 * @param {string} path Extends this route's path if it doesn't contain a
38 * leading slash. 126 * leading slash.
39 * @return {!settings.Route} 127 * @return {!settings.Route}
40 * @private 128 * @private
41 */ 129 */
42 createChild: function(path) { 130 createChild(path) {
43 assert(path); 131 assert(path);
44 132
45 // |path| extends this route's path if it doesn't have a leading slash. 133 // |path| extends this route's path if it doesn't have a leading slash.
46 // If it does have a leading slash, it's just set as the new route's URL. 134 // If it does have a leading slash, it's just set as the new route's URL.
47 var newUrl = path[0] == '/' ? path : this.path + '/' + path; 135 var newUrl = path[0] == '/' ? path : this.path + '/' + path;
48 136
49 var route = new Route(newUrl); 137 var route = new Route(newUrl);
50 route.parent = this; 138 route.parent = this;
51 route.section = this.section; 139 route.section = this.section;
52 route.depth = this.depth + 1; 140 route.depth = this.depth + 1;
53 141
54 return route; 142 return route;
55 }, 143 }
56 144
57 /** 145 /**
58 * Returns a new Route instance that's a child section of this route. 146 * Returns a new Route instance that's a child section of this route.
59 * TODO(tommycli): Remove once we've obsoleted the concept of sections. 147 * TODO(tommycli): Remove once we've obsoleted the concept of sections.
60 * @param {string} path 148 * @param {string} path
61 * @param {string} section 149 * @param {string} section
62 * @return {!settings.Route} 150 * @return {!settings.Route}
63 * @private 151 * @private
64 */ 152 */
65 createSection: function(path, section) { 153 createSection(path, section) {
66 var route = this.createChild(path); 154 var route = this.createChild(path);
67 route.section = section; 155 route.section = section;
68 return route; 156 return route;
69 }, 157 }
70 158
71 /** 159 /**
72 * Returns true if this route matches or is an ancestor of the parameter. 160 * Returns true if this route matches or is an ancestor of the parameter.
73 * @param {!settings.Route} route 161 * @param {!settings.Route} route
74 * @return {boolean} 162 * @return {boolean}
75 */ 163 */
76 contains: function(route) { 164 contains(route) {
77 for (var r = route; r != null; r = r.parent) { 165 for (var r = route; r != null; r = r.parent) {
78 if (this == r) 166 if (this == r)
79 return true; 167 return true;
80 } 168 }
81 return false; 169 return false;
82 }, 170 }
83 171
84 /** 172 /**
85 * Returns true if this route is a subpage of a section. 173 * Returns true if this route is a subpage of a section.
86 * @return {boolean} 174 * @return {boolean}
87 */ 175 */
88 isSubpage: function() { 176 isSubpage() {
89 return !!this.parent && !!this.section && 177 return !!this.parent && !!this.section &&
90 this.parent.section == this.section; 178 this.parent.section == this.section;
91 }, 179 }
180 }
181
182 /**
183 * Computes and return all available routes based on settings.pageVisibility.
184 * @return {!SettingsRoutes}
185 */
186 var computeAvailableRoutes = function() {
187 var pageVisibility = settings.pageVisibility || {};
188
189 /** @type {!SettingsRoutes} */
190 var r = {};
191
192 // Root pages.
193 r.BASIC = new Route('/');
194 r.ABOUT = new Route('/help');
195
196 // Navigable dialogs. These are the only non-section children of root
197 // pages. These are disfavored. If we add anymore, we should add explicit
198 // support.
199 r.IMPORT_DATA = r.BASIC.createChild('/importData');
200 r.IMPORT_DATA.isNavigableDialog = true;
201 r.SIGN_OUT = r.BASIC.createChild('/signOut');
202 r.SIGN_OUT.isNavigableDialog = true;
203
204 // <if expr="chromeos">
205 r.INTERNET = r.BASIC.createSection('/internet', 'internet');
206 r.INTERNET_NETWORKS = r.INTERNET.createChild('/networks');
207 r.NETWORK_CONFIG = r.INTERNET.createChild('/networkConfig');
208 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail');
209 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks');
210 r.BLUETOOTH = r.BASIC.createSection('/bluetooth', 'bluetooth');
211 r.BLUETOOTH_DEVICES = r.BLUETOOTH.createChild('/bluetoothDevices');
212 // </if>
213
214 if (pageVisibility.appearance !== false) {
215 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance');
216 r.FONTS = r.APPEARANCE.createChild('/fonts');
217 }
218
219 if (pageVisibility.defaultBrowser !== false) {
220 r.DEFAULT_BROWSER =
221 r.BASIC.createSection('/defaultBrowser', 'defaultBrowser');
222 }
223
224 r.SEARCH = r.BASIC.createSection('/search', 'search');
225 r.SEARCH_ENGINES = r.SEARCH.createChild('/searchEngines');
226
227 // <if expr="chromeos">
228 r.ANDROID_APPS = r.BASIC.createSection('/androidApps', 'androidApps');
229 r.ANDROID_APPS_DETAILS = r.ANDROID_APPS.createChild('/androidApps/details');
230 // </if>
231
232 if (pageVisibility.onStartup !== false) {
233 r.ON_STARTUP = r.BASIC.createSection('/onStartup', 'onStartup');
234 r.STARTUP_URLS = r.ON_STARTUP.createChild('/startupUrls');
235 }
236
237 if (pageVisibility.people !== false) {
238 r.PEOPLE = r.BASIC.createSection('/people', 'people');
239 r.SYNC = r.PEOPLE.createChild('/syncSetup');
240 // <if expr="not chromeos">
241 r.MANAGE_PROFILE = r.PEOPLE.createChild('/manageProfile');
242 // </if>
243 // <if expr="chromeos">
244 r.CHANGE_PICTURE = r.PEOPLE.createChild('/changePicture');
245 r.ACCOUNTS = r.PEOPLE.createChild('/accounts');
246 r.LOCK_SCREEN = r.PEOPLE.createChild('/lockScreen');
247 r.FINGERPRINT = r.LOCK_SCREEN.createChild('/lockScreen/fingerprint');
248 // </if>
249 }
250
251 // <if expr="chromeos">
252 r.DEVICE = r.BASIC.createSection('/device', 'device');
253 r.POINTERS = r.DEVICE.createChild('/pointer-overlay');
254 r.KEYBOARD = r.DEVICE.createChild('/keyboard-overlay');
255 r.STYLUS = r.DEVICE.createChild('/stylus');
256 r.DISPLAY = r.DEVICE.createChild('/display');
257 r.STORAGE = r.DEVICE.createChild('/storage');
258 r.POWER = r.DEVICE.createChild('/power');
259 // </if>
260
261 // Advacned Routes
262 if (pageVisibility.advancedSettings !== false) {
263 r.ADVANCED = new Route('/advanced');
264
265 r.CLEAR_BROWSER_DATA = r.ADVANCED.createChild('/clearBrowserData');
266 r.CLEAR_BROWSER_DATA.isNavigableDialog = true;
267
268 if (pageVisibility.privacy !== false) {
269 r.PRIVACY = r.ADVANCED.createSection('/privacy', 'privacy');
270 r.CERTIFICATES = r.PRIVACY.createChild('/certificates');
271 r.SITE_SETTINGS = r.PRIVACY.createChild('/content');
272 }
273
274 if (loadTimeData.getBoolean('enableSiteSettings')) {
275 r.SITE_SETTINGS_ALL = r.SITE_SETTINGS.createChild('all');
276 r.SITE_SETTINGS_SITE_DETAILS =
277 r.SITE_SETTINGS_ALL.createChild('/content/siteDetails');
278 } else if (loadTimeData.getBoolean('enableSiteDetails')) {
279 // When there is no "All Sites", pressing 'back' from "Site Details"
280 // should return to "Content Settings". This should only occur when
281 // |kSiteSettings| is off and |kSiteDetails| is on.
282 r.SITE_SETTINGS_SITE_DETAILS =
283 r.SITE_SETTINGS.createChild('/content/siteDetails');
284 }
285
286 r.SITE_SETTINGS_HANDLERS = r.SITE_SETTINGS.createChild('/handlers');
287
288 // TODO(tommycli): Find a way to refactor these repetitive category
289 // routes.
290 r.SITE_SETTINGS_ADS = r.SITE_SETTINGS.createChild('ads');
291 r.SITE_SETTINGS_AUTOMATIC_DOWNLOADS =
292 r.SITE_SETTINGS.createChild('automaticDownloads');
293 r.SITE_SETTINGS_BACKGROUND_SYNC =
294 r.SITE_SETTINGS.createChild('backgroundSync');
295 r.SITE_SETTINGS_CAMERA = r.SITE_SETTINGS.createChild('camera');
296 r.SITE_SETTINGS_COOKIES = r.SITE_SETTINGS.createChild('cookies');
297 r.SITE_SETTINGS_DATA_DETAILS =
298 r.SITE_SETTINGS_COOKIES.createChild('/cookies/detail');
299 r.SITE_SETTINGS_IMAGES = r.SITE_SETTINGS.createChild('images');
300 r.SITE_SETTINGS_JAVASCRIPT = r.SITE_SETTINGS.createChild('javascript');
301 r.SITE_SETTINGS_LOCATION = r.SITE_SETTINGS.createChild('location');
302 r.SITE_SETTINGS_MICROPHONE = r.SITE_SETTINGS.createChild('microphone');
303 r.SITE_SETTINGS_NOTIFICATIONS =
304 r.SITE_SETTINGS.createChild('notifications');
305 r.SITE_SETTINGS_FLASH = r.SITE_SETTINGS.createChild('flash');
306 r.SITE_SETTINGS_POPUPS = r.SITE_SETTINGS.createChild('popups');
307 r.SITE_SETTINGS_UNSANDBOXED_PLUGINS =
308 r.SITE_SETTINGS.createChild('unsandboxedPlugins');
309 r.SITE_SETTINGS_MIDI_DEVICES = r.SITE_SETTINGS.createChild('midiDevices');
310 r.SITE_SETTINGS_USB_DEVICES = r.SITE_SETTINGS.createChild('usbDevices');
311 r.SITE_SETTINGS_ZOOM_LEVELS = r.SITE_SETTINGS.createChild('zoomLevels');
312 r.SITE_SETTINGS_PDF_DOCUMENTS =
313 r.SITE_SETTINGS.createChild('pdfDocuments');
314 r.SITE_SETTINGS_PROTECTED_CONTENT =
315 r.SITE_SETTINGS.createChild('protectedContent');
316
317 // <if expr="chromeos">
318 if (pageVisibility.dateTime !== false) {
319 r.DATETIME = r.ADVANCED.createSection('/dateTime', 'dateTime');
320 }
321 // </if>
322
323 if (pageVisibility.passwordsAndForms !== false) {
324 r.PASSWORDS =
325 r.ADVANCED.createSection('/passwordsAndForms', 'passwordsAndForms');
326 r.AUTOFILL = r.PASSWORDS.createChild('/autofill');
327 r.MANAGE_PASSWORDS = r.PASSWORDS.createChild('/passwords');
328 }
329
330 r.LANGUAGES = r.ADVANCED.createSection('/languages', 'languages');
331 // <if expr="chromeos">
332 r.INPUT_METHODS = r.LANGUAGES.createChild('/inputMethods');
333 // </if>
334 // <if expr="not is_macosx">
335 r.EDIT_DICTIONARY = r.LANGUAGES.createChild('/editDictionary');
336 // </if>
337
338 if (pageVisibility.downloads !== false) {
339 r.DOWNLOADS = r.ADVANCED.createSection('/downloads', 'downloads');
340 }
341
342 r.PRINTING = r.ADVANCED.createSection('/printing', 'printing');
343 r.CLOUD_PRINTERS = r.PRINTING.createChild('/cloudPrinters');
344 // <if expr="chromeos">
345 r.CUPS_PRINTERS = r.PRINTING.createChild('/cupsPrinters');
346 r.CUPS_PRINTER_DETAIL =
347 r.CUPS_PRINTERS.createChild('/cupsPrinterDetails');
348 // </if>
349
350 r.ACCESSIBILITY = r.ADVANCED.createSection('/accessibility', 'a11y');
351 // <if expr="chromeos">
352 r.MANAGE_ACCESSIBILITY =
353 r.ACCESSIBILITY.createChild('/manageAccessibility');
354 // </if>
355
356 r.SYSTEM = r.ADVANCED.createSection('/system', 'system');
357
358 if (pageVisibility.reset !== false) {
359 r.RESET = r.ADVANCED.createSection('/reset', 'reset');
360 r.RESET_DIALOG = r.ADVANCED.createChild('/resetProfileSettings');
361 r.RESET_DIALOG.isNavigableDialog = true;
362 r.TRIGGERED_RESET_DIALOG =
363 r.ADVANCED.createChild('/triggeredResetProfileSettings');
364 r.TRIGGERED_RESET_DIALOG.isNavigableDialog = true;
365 }
366 }
367
368 // <if expr="chromeos">
369 // "About" is the only section in About, but we still need to create the
370 // route in order to show the subpage on Chrome OS.
371 r.ABOUT_ABOUT = r.ABOUT.createSection('/help/about', 'about');
372 r.DETAILED_BUILD_INFO = r.ABOUT_ABOUT.createChild('/help/details');
373 // </if>
374
375 return r;
92 }; 376 };
93 377
94 // Abbreviated variable for easier definitions. 378 class Router {
95 var r = Route; 379 constructor() {
96 380 /**
97 // Root pages. 381 * List of available routes. This is populated taking into account current
98 r.BASIC = new Route('/'); 382 * state (like guest mode).
99 r.ADVANCED = new Route('/advanced'); 383 * @private {!SettingsRoutes}
100 r.ABOUT = new Route('/help'); 384 */
101 385 this.routes_ = computeAvailableRoutes();
102 // Navigable dialogs. These are the only non-section children of root pages. 386
103 // These are disfavored. If we add anymore, we should add explicit support. 387 /**
104 r.IMPORT_DATA = r.BASIC.createChild('/importData'); 388 * The current active route. This updated is only by settings.navigateTo
105 r.IMPORT_DATA.isNavigableDialog = true; 389 * or settings.initializeRouteFromUrl.
106 r.SIGN_OUT = r.BASIC.createChild('/signOut'); 390 * @type {!settings.Route}
107 r.SIGN_OUT.isNavigableDialog = true; 391 */
108 r.CLEAR_BROWSER_DATA = r.ADVANCED.createChild('/clearBrowserData'); 392 this.currentRoute = /** @type {!settings.Route} */ (this.routes_.BASIC);
109 r.CLEAR_BROWSER_DATA.isNavigableDialog = true; 393
110 r.RESET_DIALOG = r.ADVANCED.createChild('/resetProfileSettings'); 394 /**
111 r.RESET_DIALOG.isNavigableDialog = true; 395 * The current query parameters. This is updated only by
112 r.TRIGGERED_RESET_DIALOG = 396 * settings.navigateTo or settings.initializeRouteFromUrl.
113 r.ADVANCED.createChild('/triggeredResetProfileSettings'); 397 * @private {!URLSearchParams}
114 r.TRIGGERED_RESET_DIALOG.isNavigableDialog = true; 398 */
115 399 this.currentQueryParameters_ = new URLSearchParams();
116 // <if expr="chromeos"> 400
117 r.INTERNET = r.BASIC.createSection('/internet', 'internet'); 401 /** @private {boolean} */
118 r.INTERNET_NETWORKS = r.INTERNET.createChild('/networks'); 402 this.wasLastRouteChangePopstate_ = false;
119 r.NETWORK_CONFIG = r.INTERNET.createChild('/networkConfig'); 403
120 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail'); 404 /** @private {boolean}*/
121 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks'); 405 this.initializeRouteFromUrlCalled_ = false;
122 r.BLUETOOTH = r.BASIC.createSection('/bluetooth', 'bluetooth'); 406 }
123 r.BLUETOOTH_DEVICES = r.BLUETOOTH.createChild('/bluetoothDevices'); 407
124 // </if> 408 /** @return {settings.Route} */
125 409 getRoute(routeName) {
126 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance'); 410 return this.routes_[routeName];
127 r.FONTS = r.APPEARANCE.createChild('/fonts'); 411 }
128 412
129 r.DEFAULT_BROWSER = 413 /** @return {!SettingsRoutes} */
130 r.BASIC.createSection('/defaultBrowser', 'defaultBrowser'); 414 getRoutes() {
131 415 return this.routes_;
132 r.SEARCH = r.BASIC.createSection('/search', 'search'); 416 }
133 r.SEARCH_ENGINES = r.SEARCH.createChild('/searchEngines'); 417
134 418 /**
135 // <if expr="chromeos"> 419 * Helper function to set the current route and notify all observers.
136 r.ANDROID_APPS = r.BASIC.createSection('/androidApps', 'androidApps'); 420 * @param {!settings.Route} route
137 r.ANDROID_APPS_DETAILS = r.ANDROID_APPS.createChild('/androidApps/details'); 421 * @param {!URLSearchParams} queryParameters
138 // </if> 422 * @param {boolean} isPopstate
139 423 */
140 r.ON_STARTUP = r.BASIC.createSection('/onStartup', 'onStartup'); 424 setCurrentRoute(route, queryParameters, isPopstate) {
141 r.STARTUP_URLS = r.ON_STARTUP.createChild('/startupUrls'); 425 var oldRoute = this.currentRoute;
142 426 this.currentRoute = route;
143 r.PEOPLE = r.BASIC.createSection('/people', 'people'); 427 this.currentQueryParameters_ = queryParameters;
144 r.SYNC = r.PEOPLE.createChild('/syncSetup'); 428 this.wasLastRouteChangePopstate_ = isPopstate;
145 // <if expr="not chromeos"> 429 routeObservers.forEach(function(observer) {
146 r.MANAGE_PROFILE = r.PEOPLE.createChild('/manageProfile'); 430 observer.currentRouteChanged(this.currentRoute, oldRoute);
147 // </if> 431 }.bind(this));
148 // <if expr="chromeos"> 432 }
149 r.CHANGE_PICTURE = r.PEOPLE.createChild('/changePicture'); 433
150 r.ACCOUNTS = r.PEOPLE.createChild('/accounts'); 434 /** @return {!settings.Route} */
151 r.LOCK_SCREEN = r.PEOPLE.createChild('/lockScreen'); 435 getCurrentRoute() {
152 r.FINGERPRINT = r.LOCK_SCREEN.createChild('/lockScreen/fingerprint'); 436 return this.currentRoute;
153 437 }
154 r.DEVICE = r.BASIC.createSection('/device', 'device'); 438
155 r.POINTERS = r.DEVICE.createChild('/pointer-overlay'); 439 /** @return {!URLSearchParams} */
156 r.KEYBOARD = r.DEVICE.createChild('/keyboard-overlay'); 440 getQueryParameters() {
157 r.STYLUS = r.DEVICE.createChild('/stylus'); 441 return new URLSearchParams(
158 r.DISPLAY = r.DEVICE.createChild('/display'); 442 this.currentQueryParameters_); // Defensive copy.
159 r.STORAGE = r.DEVICE.createChild('/storage'); 443 }
160 r.POWER = r.DEVICE.createChild('/power'); 444
161 // </if> 445 /** @return {boolean} */
162 446 lastRouteChangeWasPopstate() {
163 r.PRIVACY = r.ADVANCED.createSection('/privacy', 'privacy'); 447 return this.wasLastRouteChangePopstate_;
164 r.CERTIFICATES = r.PRIVACY.createChild('/certificates'); 448 }
165 449
166 r.SITE_SETTINGS = r.PRIVACY.createChild('/content'); 450 /**
167 451 * @param {string} path
168 if (loadTimeData.getBoolean('enableSiteSettings')) { 452 * @return {?settings.Route} The matching canonical route, or null if none
169 r.SITE_SETTINGS_ALL = r.SITE_SETTINGS.createChild('all'); 453 * matches.
170 r.SITE_SETTINGS_SITE_DETAILS = 454 */
171 r.SITE_SETTINGS_ALL.createChild('/content/siteDetails'); 455 getRouteForPath(path) {
172 } else if (loadTimeData.getBoolean('enableSiteDetails')) { 456 // Allow trailing slash in paths.
173 // When there is no "All Sites", pressing 'back' from "Site Details" should 457 var canonicalPath = path.replace(CANONICAL_PATH_REGEX, '$1$2');
174 // return to "Content Settings". This should only occur when |kSiteSettings| 458
175 // is off and |kSiteDetails| is on. 459 // TODO(tommycli): Use Object.values once Closure compilation supports it.
176 r.SITE_SETTINGS_SITE_DETAILS = 460 var matchingKey = Object.keys(this.routes_).find(function(key) {
177 r.SITE_SETTINGS.createChild('/content/siteDetails'); 461 return this.routes_[key].path == canonicalPath;
462 }.bind(this));
463
464 return !!matchingKey ? this.routes_[matchingKey] : null;
465 }
466
467 /**
468 * Navigates to a canonical route and pushes a new history entry.
469 * @param {!settings.Route} route
470 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same
471 * URL parameters in a different order will still push to history.
472 * @param {boolean=} opt_removeSearch Whether to strip the 'search' URL
473 * parameter during navigation. Defaults to false.
474 */
475 navigateTo(route, opt_dynamicParameters, opt_removeSearch) {
476 // The ADVANCED route only serves as a parent of subpages, and should not
477 // be possible to navigate to it directly.
478 if (route == this.routes_.ADVANCED)
479 route = /** @type {!settings.Route} */ (this.routes_.BASIC);
480
481 var params = opt_dynamicParameters || new URLSearchParams();
482 var removeSearch = !!opt_removeSearch;
483
484 var oldSearchParam = this.getQueryParameters().get('search') || '';
485 var newSearchParam = params.get('search') || '';
486
487 if (!removeSearch && oldSearchParam && !newSearchParam)
488 params.append('search', oldSearchParam);
489
490 var url = route.path;
491 var queryString = params.toString();
492 if (queryString)
493 url += '?' + queryString;
494
495 // History serializes the state, so we don't push the actual route object.
496 window.history.pushState(this.currentRoute.path, '', url);
497 this.setCurrentRoute(route, params, false);
498 }
499
500 /**
501 * Navigates to the previous route if it has an equal or lesser depth.
502 * If there is no previous route in history meeting those requirements,
503 * this navigates to the immediate parent. This will never exit Settings.
504 */
505 navigateToPreviousRoute() {
506 var previousRoute = window.history.state &&
507 assert(this.getRouteForPath(
508 /** @type {string} */ (window.history.state)));
509
510 if (previousRoute && previousRoute.depth <= this.currentRoute.depth)
511 window.history.back();
512 else
513 this.navigateTo(
514 this.currentRoute.parent ||
515 /** @type {!settings.Route} */ (this.routes_.BASIC));
516 }
517
518 /**
519 * Initialize the route and query params from the URL.
520 */
521 initializeRouteFromUrl() {
522 assert(!this.initializeRouteFromUrlCalled_);
523 this.initializeRouteFromUrlCalled_ = true;
524
525 var route = this.getRouteForPath(window.location.pathname);
526 // Never allow direct navigation to ADVANCED.
527 if (route && route != this.routes_.ADVANCED) {
528 this.currentRoute = route;
529 this.currentQueryParameters_ =
530 new URLSearchParams(window.location.search);
531 } else {
532 window.history.replaceState(undefined, '', this.routes_.BASIC.path);
533 }
534 }
535
536 resetRouteForTesting() {
537 this.initializeRouteFromUrlCalled_ = false;
538 this.wasLastRouteChangePopstate_ = false;
539 this.currentRoute = /** @type {!settings.Route} */ (this.routes_.BASIC);
540 this.currentQueryParameters_ = new URLSearchParams();
541 }
178 } 542 }
179 543
180 r.SITE_SETTINGS_HANDLERS = r.SITE_SETTINGS.createChild('/handlers'); 544 var routerInstance = new Router();
181
182 // TODO(tommycli): Find a way to refactor these repetitive category routes.
183 r.SITE_SETTINGS_ADS = r.SITE_SETTINGS.createChild('ads');
184 r.SITE_SETTINGS_AUTOMATIC_DOWNLOADS =
185 r.SITE_SETTINGS.createChild('automaticDownloads');
186 r.SITE_SETTINGS_BACKGROUND_SYNC =
187 r.SITE_SETTINGS.createChild('backgroundSync');
188 r.SITE_SETTINGS_CAMERA = r.SITE_SETTINGS.createChild('camera');
189 r.SITE_SETTINGS_COOKIES = r.SITE_SETTINGS.createChild('cookies');
190 r.SITE_SETTINGS_DATA_DETAILS =
191 r.SITE_SETTINGS_COOKIES.createChild('/cookies/detail');
192 r.SITE_SETTINGS_IMAGES = r.SITE_SETTINGS.createChild('images');
193 r.SITE_SETTINGS_JAVASCRIPT = r.SITE_SETTINGS.createChild('javascript');
194 r.SITE_SETTINGS_LOCATION = r.SITE_SETTINGS.createChild('location');
195 r.SITE_SETTINGS_MICROPHONE = r.SITE_SETTINGS.createChild('microphone');
196 r.SITE_SETTINGS_NOTIFICATIONS = r.SITE_SETTINGS.createChild('notifications');
197 r.SITE_SETTINGS_FLASH = r.SITE_SETTINGS.createChild('flash');
198 r.SITE_SETTINGS_POPUPS = r.SITE_SETTINGS.createChild('popups');
199 r.SITE_SETTINGS_UNSANDBOXED_PLUGINS =
200 r.SITE_SETTINGS.createChild('unsandboxedPlugins');
201 r.SITE_SETTINGS_MIDI_DEVICES = r.SITE_SETTINGS.createChild('midiDevices');
202 r.SITE_SETTINGS_USB_DEVICES = r.SITE_SETTINGS.createChild('usbDevices');
203 r.SITE_SETTINGS_ZOOM_LEVELS = r.SITE_SETTINGS.createChild('zoomLevels');
204 r.SITE_SETTINGS_PDF_DOCUMENTS = r.SITE_SETTINGS.createChild('pdfDocuments');
205 r.SITE_SETTINGS_PROTECTED_CONTENT =
206 r.SITE_SETTINGS.createChild('protectedContent');
207
208 // <if expr="chromeos">
209 r.DATETIME = r.ADVANCED.createSection('/dateTime', 'dateTime');
210 // </if>
211
212 r.PASSWORDS =
213 r.ADVANCED.createSection('/passwordsAndForms', 'passwordsAndForms');
214 r.AUTOFILL = r.PASSWORDS.createChild('/autofill');
215 r.MANAGE_PASSWORDS = r.PASSWORDS.createChild('/passwords');
216
217 r.LANGUAGES = r.ADVANCED.createSection('/languages', 'languages');
218 // <if expr="chromeos">
219 r.INPUT_METHODS = r.LANGUAGES.createChild('/inputMethods');
220 // </if>
221 // <if expr="not is_macosx">
222 r.EDIT_DICTIONARY = r.LANGUAGES.createChild('/editDictionary');
223 // </if>
224
225 r.DOWNLOADS = r.ADVANCED.createSection('/downloads', 'downloads');
226
227 r.PRINTING = r.ADVANCED.createSection('/printing', 'printing');
228 r.CLOUD_PRINTERS = r.PRINTING.createChild('/cloudPrinters');
229 // <if expr="chromeos">
230 r.CUPS_PRINTERS = r.PRINTING.createChild('/cupsPrinters');
231 r.CUPS_PRINTER_DETAIL = r.CUPS_PRINTERS.createChild('/cupsPrinterDetails');
232 // </if>
233
234 r.ACCESSIBILITY = r.ADVANCED.createSection('/accessibility', 'a11y');
235 // <if expr="chromeos">
236 r.MANAGE_ACCESSIBILITY = r.ACCESSIBILITY.createChild('/manageAccessibility');
237 // </if>
238
239 r.SYSTEM = r.ADVANCED.createSection('/system', 'system');
240 r.RESET = r.ADVANCED.createSection('/reset', 'reset');
241
242 // <if expr="chromeos">
243 // "About" is the only section in About, but we still need to create the route
244 // in order to show the subpage on Chrome OS.
245 r.ABOUT_ABOUT = r.ABOUT.createSection('/help/about', 'about');
246 r.DETAILED_BUILD_INFO = r.ABOUT_ABOUT.createChild('/help/details');
247 // </if>
248 545
249 var routeObservers = new Set(); 546 var routeObservers = new Set();
250 547
251 /** @polymerBehavior */ 548 /** @polymerBehavior */
252 var RouteObserverBehavior = { 549 var RouteObserverBehavior = {
253 /** @override */ 550 /** @override */
254 attached: function() { 551 attached: function() {
255 assert(!routeObservers.has(this)); 552 assert(!routeObservers.has(this));
256 routeObservers.add(this); 553 routeObservers.add(this);
257 554
258 // Emulating Polymer data bindings, the observer is called when the 555 // Emulating Polymer data bindings, the observer is called when the
259 // element starts observing the route. 556 // element starts observing the route.
260 this.currentRouteChanged(currentRoute, undefined); 557 this.currentRouteChanged(routerInstance.currentRoute, undefined);
261 }, 558 },
262 559
263 /** @override */ 560 /** @override */
264 detached: function() { 561 detached: function() {
265 assert(routeObservers.delete(this)); 562 assert(routeObservers.delete(this));
266 }, 563 },
267 564
268 /** 565 /**
269 * @param {!settings.Route|undefined} opt_newRoute 566 * @param {!settings.Route|undefined} opt_newRoute
270 * @param {!settings.Route|undefined} opt_oldRoute 567 * @param {!settings.Route|undefined} opt_oldRoute
271 * @abstract 568 * @abstract
272 */ 569 */
273 currentRouteChanged: function(opt_newRoute, opt_oldRoute) { 570 currentRouteChanged: function(opt_newRoute, opt_oldRoute) {
274 assertNotReached(); 571 assertNotReached();
275 }, 572 },
276 }; 573 };
277 574
278 /** 575 /**
279 * Regular expression that captures the leading slash, the content and the 576 * Regular expression that captures the leading slash, the content and the
280 * trailing slash in three different groups. 577 * trailing slash in three different groups.
281 * @const {!RegExp} 578 * @const {!RegExp}
282 */ 579 */
283 var CANONICAL_PATH_REGEX = /(^\/)([\/-\w]+)(\/$)/; 580 var CANONICAL_PATH_REGEX = /(^\/)([\/-\w]+)(\/$)/;
284 581
285 /**
286 * @param {string} path
287 * @return {?settings.Route} The matching canonical route, or null if none
288 * matches.
289 */
290 var getRouteForPath = function(path) {
291 // Allow trailing slash in paths.
292 var canonicalPath = path.replace(CANONICAL_PATH_REGEX, '$1$2');
293
294 // TODO(tommycli): Use Object.values once Closure compilation supports it.
295 var matchingKey = Object.keys(Route).find(function(key) {
296 return Route[key].path == canonicalPath;
297 });
298
299 return !!matchingKey ? Route[matchingKey] : null;
300 };
301
302 /**
303 * The current active route. This updated is only by settings.navigateTo or
304 * settings.initializeRouteFromUrl.
305 * @private {!settings.Route}
306 */
307 var currentRoute = Route.BASIC;
308
309 /**
310 * The current query parameters. This is updated only by settings.navigateTo
311 * or settings.initializeRouteFromUrl.
312 * @private {!URLSearchParams}
313 */
314 var currentQueryParameters = new URLSearchParams();
315
316 /** @private {boolean} */
317 var wasLastRouteChangePopstate = false;
318
319 /** @private */
320 var initializeRouteFromUrlCalled = false;
321
322 /**
323 * Initialize the route and query params from the URL.
324 */
325 var initializeRouteFromUrl = function() {
326 assert(!initializeRouteFromUrlCalled);
327 initializeRouteFromUrlCalled = true;
328
329 var route = getRouteForPath(window.location.pathname);
330 // Never allow direct navigation to ADVANCED.
331 if (route && route != Route.ADVANCED) {
332 currentRoute = route;
333 currentQueryParameters = new URLSearchParams(window.location.search);
334 } else {
335 window.history.replaceState(undefined, '', Route.BASIC.path);
336 }
337 };
338
339 function resetRouteForTesting() {
340 initializeRouteFromUrlCalled = false;
341 wasLastRouteChangePopstate = false;
342 currentRoute = Route.BASIC;
343 currentQueryParameters = new URLSearchParams();
344 }
345
346 /**
347 * Helper function to set the current route and notify all observers.
348 * @param {!settings.Route} route
349 * @param {!URLSearchParams} queryParameters
350 * @param {boolean} isPopstate
351 */
352 var setCurrentRoute = function(route, queryParameters, isPopstate) {
353 var oldRoute = currentRoute;
354 currentRoute = route;
355 currentQueryParameters = queryParameters;
356 wasLastRouteChangePopstate = isPopstate;
357 routeObservers.forEach(function(observer) {
358 observer.currentRouteChanged(currentRoute, oldRoute);
359 });
360 };
361
362 /** @return {!settings.Route} */
363 var getCurrentRoute = function() {
364 return currentRoute;
365 };
366
367 /** @return {!URLSearchParams} */
368 var getQueryParameters = function() {
369 return new URLSearchParams(currentQueryParameters); // Defensive copy.
370 };
371
372 /** @return {boolean} */
373 var lastRouteChangeWasPopstate = function() {
374 return wasLastRouteChangePopstate;
375 };
376
377 /**
378 * Navigates to a canonical route and pushes a new history entry.
379 * @param {!settings.Route} route
380 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same
381 * URL parameters in a different order will still push to history.
382 * @param {boolean=} opt_removeSearch Whether to strip the 'search' URL
383 * parameter during navigation. Defaults to false.
384 */
385 var navigateTo = function(route, opt_dynamicParameters, opt_removeSearch) {
386 // The ADVANCED route only serves as a parent of subpages, and should not
387 // be possible to navigate to it directly.
388 if (route == settings.Route.ADVANCED)
389 route = settings.Route.BASIC;
390
391 var params = opt_dynamicParameters || new URLSearchParams();
392 var removeSearch = !!opt_removeSearch;
393
394 var oldSearchParam = getQueryParameters().get('search') || '';
395 var newSearchParam = params.get('search') || '';
396
397 if (!removeSearch && oldSearchParam && !newSearchParam)
398 params.append('search', oldSearchParam);
399
400 var url = route.path;
401 var queryString = params.toString();
402 if (queryString)
403 url += '?' + queryString;
404
405 // History serializes the state, so we don't push the actual route object.
406 window.history.pushState(currentRoute.path, '', url);
407 setCurrentRoute(route, params, false);
408 };
409
410 /**
411 * Navigates to the previous route if it has an equal or lesser depth.
412 * If there is no previous route in history meeting those requirements,
413 * this navigates to the immediate parent. This will never exit Settings.
414 */
415 var navigateToPreviousRoute = function() {
416 var previousRoute = window.history.state &&
417 assert(getRouteForPath(/** @type {string} */ (window.history.state)));
418
419 if (previousRoute && previousRoute.depth <= currentRoute.depth)
420 window.history.back();
421 else
422 navigateTo(currentRoute.parent || Route.BASIC);
423 };
424
425 window.addEventListener('popstate', function(event) { 582 window.addEventListener('popstate', function(event) {
426 // On pop state, do not push the state onto the window.history again. 583 // On pop state, do not push the state onto the window.history again.
427 setCurrentRoute( 584 routerInstance.setCurrentRoute(
428 getRouteForPath(window.location.pathname) || Route.BASIC, 585 /** @type {!settings.Route} */ (
586 routerInstance.getRouteForPath(window.location.pathname) ||
587 routerInstance.getRoutes().BASIC),
429 new URLSearchParams(window.location.search), true); 588 new URLSearchParams(window.location.search), true);
430 }); 589 });
431 590
591 // TODO(scottchen): Change to 'get routes() {}' in export when we fix a bug in
592 // ChromePass that limits the syntax of what can be returned from cr.define().
593 var routes = routerInstance.getRoutes();
594
595 // TODO(scottchen): Stop exposing all those methods directly on settings.*,
596 // and instead update all clients to use the singleton instance directly
597 var getCurrentRoute = routerInstance.getCurrentRoute.bind(routerInstance);
598 var getRouteForPath = routerInstance.getRouteForPath.bind(routerInstance);
599 var initializeRouteFromUrl =
600 routerInstance.initializeRouteFromUrl.bind(routerInstance);
601 var resetRouteForTesting =
602 routerInstance.resetRouteForTesting.bind(routerInstance);
603 var getQueryParameters =
604 routerInstance.getQueryParameters.bind(routerInstance);
605 var lastRouteChangeWasPopstate =
606 routerInstance.lastRouteChangeWasPopstate.bind(routerInstance);
607 var navigateTo = routerInstance.navigateTo.bind(routerInstance);
608 var navigateToPreviousRoute =
609 routerInstance.navigateToPreviousRoute.bind(routerInstance);
610
432 return { 611 return {
433 Route: Route, 612 Route: Route, // The Route class definition.
613 Router: Router, // The Router class definition.
614 router: routerInstance, // the singleton.
615 routes: routes,
434 RouteObserverBehavior: RouteObserverBehavior, 616 RouteObserverBehavior: RouteObserverBehavior,
435 getRouteForPath: getRouteForPath, 617 getRouteForPath: getRouteForPath,
436 initializeRouteFromUrl: initializeRouteFromUrl, 618 initializeRouteFromUrl: initializeRouteFromUrl,
437 resetRouteForTesting: resetRouteForTesting, 619 resetRouteForTesting: resetRouteForTesting,
438 getCurrentRoute: getCurrentRoute, 620 getCurrentRoute: getCurrentRoute,
439 getQueryParameters: getQueryParameters, 621 getQueryParameters: getQueryParameters,
440 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate, 622 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate,
441 navigateTo: navigateTo, 623 navigateTo: navigateTo,
442 navigateToPreviousRoute: navigateToPreviousRoute, 624 navigateToPreviousRoute: navigateToPreviousRoute,
443 }; 625 };
444 }); 626 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698