| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('uber', function() { | 5 cr.define('uber', function() { |
| 6 /** | 6 /** |
| 7 * Options for how web history should be handled. | 7 * Options for how web history should be handled. |
| 8 */ | 8 */ |
| 9 var HISTORY_STATE_OPTION = { | 9 var HISTORY_STATE_OPTION = { |
| 10 PUSH: 1, // Push a new history state. | 10 PUSH: 1, // Push a new history state. |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 * { method : "methodToInvoke", | 142 * { method : "methodToInvoke", |
| 143 * params : {...} | 143 * params : {...} |
| 144 * } | 144 * } |
| 145 * | 145 * |
| 146 * |method| is required, while |params| is optional. Extra parameters required | 146 * |method| is required, while |params| is optional. Extra parameters required |
| 147 * by a method must be specified by that method's documentation. | 147 * by a method must be specified by that method's documentation. |
| 148 * | 148 * |
| 149 * @param {Event} e The posted object. | 149 * @param {Event} e The posted object. |
| 150 */ | 150 */ |
| 151 function handleWindowMessage(e) { | 151 function handleWindowMessage(e) { |
| 152 e = /** @type{!MessageEvent.<!{method: string, params: *}>} */(e); |
| 152 if (e.data.method === 'beginInterceptingEvents') { | 153 if (e.data.method === 'beginInterceptingEvents') { |
| 153 backgroundNavigation(); | 154 backgroundNavigation(); |
| 154 } else if (e.data.method === 'stopInterceptingEvents') { | 155 } else if (e.data.method === 'stopInterceptingEvents') { |
| 155 foregroundNavigation(); | 156 foregroundNavigation(); |
| 156 } else if (e.data.method === 'ready') { | 157 } else if (e.data.method === 'ready') { |
| 157 pageReady(e.origin); | 158 pageReady(e.origin); |
| 158 } else if (e.data.method === 'updateHistory') { | 159 } else if (e.data.method === 'updateHistory') { |
| 159 updateHistory(e.origin, e.data.params.state, e.data.params.path, | 160 updateHistory(e.origin, e.data.params.state, e.data.params.path, |
| 160 e.data.params.replace); | 161 e.data.params.replace); |
| 161 } else if (e.data.method === 'setTitle') { | 162 } else if (e.data.method === 'setTitle') { |
| 162 setTitle(e.origin, e.data.params.title); | 163 setTitle(e.origin, e.data.params.title); |
| 163 } else if (e.data.method === 'showPage') { | 164 } else if (e.data.method === 'showPage') { |
| 164 showPage(e.data.params.pageId, | 165 showPage(e.data.params.pageId, |
| 165 HISTORY_STATE_OPTION.PUSH, | 166 HISTORY_STATE_OPTION.PUSH, |
| 166 e.data.params.path); | 167 e.data.params.path); |
| 167 } else if (e.data.method === 'navigationControlsLoaded') { | 168 } else if (e.data.method === 'navigationControlsLoaded') { |
| 168 onNavigationControlsLoaded(); | 169 onNavigationControlsLoaded(); |
| 169 } else if (e.data.method === 'adjustToScroll') { | 170 } else if (e.data.method === 'adjustToScroll') { |
| 170 adjustToScroll(e.data.params); | 171 adjustToScroll(/** @type {number} */(e.data.params)); |
| 171 } else if (e.data.method === 'mouseWheel') { | 172 } else if (e.data.method === 'mouseWheel') { |
| 172 forwardMouseWheel(e.data.params); | 173 forwardMouseWheel(/** @type {Object} */(e.data.params)); |
| 173 } else { | 174 } else { |
| 174 console.error('Received unexpected message', e.data); | 175 console.error('Received unexpected message', e.data); |
| 175 } | 176 } |
| 176 } | 177 } |
| 177 | 178 |
| 178 /** | 179 /** |
| 179 * Sends the navigation iframe to the background. | 180 * Sends the navigation iframe to the background. |
| 180 */ | 181 */ |
| 181 function backgroundNavigation() { | 182 function backgroundNavigation() { |
| 182 navFrame.classList.add('background'); | 183 navFrame.classList.add('background'); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 196 /** | 197 /** |
| 197 * Enables or disables animated transitions when changing content while | 198 * Enables or disables animated transitions when changing content while |
| 198 * horizontally scrolled. | 199 * horizontally scrolled. |
| 199 * @param {boolean} enabled True if enabled, else false to disable. | 200 * @param {boolean} enabled True if enabled, else false to disable. |
| 200 */ | 201 */ |
| 201 function setContentChanging(enabled) { | 202 function setContentChanging(enabled) { |
| 202 navFrame.classList[enabled ? 'add' : 'remove']('changing-content'); | 203 navFrame.classList[enabled ? 'add' : 'remove']('changing-content'); |
| 203 | 204 |
| 204 if (isRTL()) { | 205 if (isRTL()) { |
| 205 uber.invokeMethodOnWindow(navFrame.firstChild.contentWindow, | 206 uber.invokeMethodOnWindow(navFrame.firstChild.contentWindow, |
| 206 'setContentChanging', | 207 'setContentChanging', enabled); |
| 207 enabled); | |
| 208 } | 208 } |
| 209 } | 209 } |
| 210 | 210 |
| 211 /** | 211 /** |
| 212 * Get an iframe based on the origin of a received post message. | 212 * Get an iframe based on the origin of a received post message. |
| 213 * @param {string} origin The origin of a post message. | 213 * @param {string} origin The origin of a post message. |
| 214 * @return {!HTMLElement} The frame associated to |origin| or null. | 214 * @return {!Element} The frame associated to |origin| or null. |
| 215 */ | 215 */ |
| 216 function getIframeFromOrigin(origin) { | 216 function getIframeFromOrigin(origin) { |
| 217 assert(origin.substr(-1) != '/', 'invalid origin given'); | 217 assert(origin.substr(-1) != '/', 'invalid origin given'); |
| 218 var query = '.iframe-container > iframe[src^="' + origin + '/"]'; | 218 var query = '.iframe-container > iframe[src^="' + origin + '/"]'; |
| 219 return document.querySelector(query); | 219 var element = assert(document.querySelector(query)); |
| 220 return /** @type {!Element} */(element); |
| 220 } | 221 } |
| 221 | 222 |
| 222 /** | 223 /** |
| 223 * Changes the path past the page title (i.e. chrome://chrome/settings/(.*)). | 224 * Changes the path past the page title (i.e. chrome://chrome/settings/(.*)). |
| 224 * @param {Object} state The page's state object for the navigation. | 225 * @param {Object} state The page's state object for the navigation. |
| 225 * @param {string} path The new /path/ to be set after the page name. | 226 * @param {string} path The new /path/ to be set after the page name. |
| 226 * @param {number} historyOption The type of history modification to make. | 227 * @param {number} historyOption The type of history modification to make. |
| 227 */ | 228 */ |
| 228 function changePathTo(state, path, historyOption) { | 229 function changePathTo(state, path, historyOption) { |
| 229 assert(!path || path.substr(-1) != '/', 'invalid path given'); | 230 assert(!path || path.substr(-1) != '/', 'invalid path given'); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 var queue = queuedInvokes[container.id] || []; | 307 var queue = queuedInvokes[container.id] || []; |
| 307 queuedInvokes[container.id] = undefined; | 308 queuedInvokes[container.id] = undefined; |
| 308 for (var i = 0; i < queue.length; i++) { | 309 for (var i = 0; i < queue.length; i++) { |
| 309 uber.invokeMethodOnWindow(frame.contentWindow, queue[i][0], queue[i][1]); | 310 uber.invokeMethodOnWindow(frame.contentWindow, queue[i][0], queue[i][1]); |
| 310 } | 311 } |
| 311 } | 312 } |
| 312 | 313 |
| 313 /** | 314 /** |
| 314 * Selects and navigates a subpage. This is called from uber-frame. | 315 * Selects and navigates a subpage. This is called from uber-frame. |
| 315 * @param {string} pageId Should match an id of one of the iframe containers. | 316 * @param {string} pageId Should match an id of one of the iframe containers. |
| 316 * @param {integer} historyOption Indicates whether we should push or replace | 317 * @param {number} historyOption Indicates whether we should push or replace |
| 317 * browser history. | 318 * browser history. |
| 318 * @param {string} path A sub-page path. | 319 * @param {string} path A sub-page path. |
| 319 */ | 320 */ |
| 320 function showPage(pageId, historyOption, path) { | 321 function showPage(pageId, historyOption, path) { |
| 321 var container = $(pageId); | 322 var container = $(pageId); |
| 322 | 323 |
| 323 // Lazy load of iframe contents. | 324 // Lazy load of iframe contents. |
| 324 var sourceUrl = container.dataset.url + (path || ''); | 325 var sourceUrl = container.dataset.url + (path || ''); |
| 325 var frame = container.querySelector('iframe'); | 326 var frame = container.querySelector('iframe'); |
| 326 if (!frame) { | 327 if (!frame) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 adjustToScroll(0); | 365 adjustToScroll(0); |
| 365 | 366 |
| 366 var selectedFrame = getSelectedIframe().querySelector('iframe'); | 367 var selectedFrame = getSelectedIframe().querySelector('iframe'); |
| 367 uber.invokeMethodOnWindow(selectedFrame.contentWindow, 'frameSelected'); | 368 uber.invokeMethodOnWindow(selectedFrame.contentWindow, 'frameSelected'); |
| 368 | 369 |
| 369 if (historyOption != HISTORY_STATE_OPTION.NONE) | 370 if (historyOption != HISTORY_STATE_OPTION.NONE) |
| 370 changePathTo({}, path, historyOption); | 371 changePathTo({}, path, historyOption); |
| 371 | 372 |
| 372 if (container.dataset.title) | 373 if (container.dataset.title) |
| 373 document.title = container.dataset.title; | 374 document.title = container.dataset.title; |
| 374 $('favicon').href = 'chrome://theme/' + container.dataset.favicon; | 375 assert('favicon' in container.dataset); |
| 375 $('favicon2x').href = 'chrome://theme/' + container.dataset.favicon + '@2x'; | 376 |
| 377 var dataset = /** @type {{favicon: string}} */(container.dataset); |
| 378 $('favicon').href = 'chrome://theme/' + dataset.favicon; |
| 379 $('favicon2x').href = 'chrome://theme/' + dataset.favicon + '@2x'; |
| 376 | 380 |
| 377 updateNavigationControls(); | 381 updateNavigationControls(); |
| 378 } | 382 } |
| 379 | 383 |
| 380 function onNavigationControlsLoaded() { | 384 function onNavigationControlsLoaded() { |
| 381 updateNavigationControls(); | 385 updateNavigationControls(); |
| 382 } | 386 } |
| 383 | 387 |
| 384 /** | 388 /** |
| 385 * Sends a message to uber-frame to update the appearance of the nav controls. | 389 * Sends a message to uber-frame to update the appearance of the nav controls. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 } | 450 } |
| 447 | 451 |
| 448 return { | 452 return { |
| 449 onLoad: onLoad, | 453 onLoad: onLoad, |
| 450 onPopHistoryState: onPopHistoryState | 454 onPopHistoryState: onPopHistoryState |
| 451 }; | 455 }; |
| 452 }); | 456 }); |
| 453 | 457 |
| 454 window.addEventListener('popstate', uber.onPopHistoryState); | 458 window.addEventListener('popstate', uber.onPopHistoryState); |
| 455 document.addEventListener('DOMContentLoaded', uber.onLoad); | 459 document.addEventListener('DOMContentLoaded', uber.onLoad); |
| OLD | NEW |