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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Namespace for utility functions. | 8 * Namespace for utility functions. |
9 */ | 9 */ |
10 var util = {}; | 10 var util = {}; |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 349 |
350 // Minimum values for the units above. | 350 // Minimum values for the units above. |
351 var STEPS = [0, | 351 var STEPS = [0, |
352 Math.pow(2, 10), | 352 Math.pow(2, 10), |
353 Math.pow(2, 20), | 353 Math.pow(2, 20), |
354 Math.pow(2, 30), | 354 Math.pow(2, 30), |
355 Math.pow(2, 40), | 355 Math.pow(2, 40), |
356 Math.pow(2, 50)]; | 356 Math.pow(2, 50)]; |
357 | 357 |
358 var str = function(n, u) { | 358 var str = function(n, u) { |
359 // TODO(rginda): Switch to v8Locale's number formatter when it's | |
360 // available. | |
361 return strf(u, n.toLocaleString()); | 359 return strf(u, n.toLocaleString()); |
362 }; | 360 }; |
363 | 361 |
364 var fmt = function(s, u) { | 362 var fmt = function(s, u) { |
365 var rounded = Math.round(bytes / s * 10) / 10; | 363 var rounded = Math.round(bytes / s * 10) / 10; |
366 return str(rounded, u); | 364 return str(rounded, u); |
367 }; | 365 }; |
368 | 366 |
369 // Less than 1KB is displayed like '80 bytes'. | 367 // Less than 1KB is displayed like '80 bytes'. |
370 if (bytes < STEPS[1]) { | 368 if (bytes < STEPS[1]) { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 * | 565 * |
568 * @param {string} id The id of the string to return. | 566 * @param {string} id The id of the string to return. |
569 * @param {...string} var_args The values to replace into the string. | 567 * @param {...string} var_args The values to replace into the string. |
570 * @return {string} The translated string with replaced values. | 568 * @return {string} The translated string with replaced values. |
571 */ | 569 */ |
572 function strf(id, var_args) { | 570 function strf(id, var_args) { |
573 return loadTimeData.getStringF.apply(loadTimeData, arguments); | 571 return loadTimeData.getStringF.apply(loadTimeData, arguments); |
574 } | 572 } |
575 | 573 |
576 /** | 574 /** |
577 * Adapter object that abstracts away the the difference between Chrome app APIs | 575 * @return {boolean} True if Files.app is running as an open files or a select |
578 * v1 and v2. Is only necessary while the migration to v2 APIs is in progress. | 576 * folder dialog. False otherwise. |
579 * TODO(mtomasz): Clean up this. crbug.com/240606. | |
580 */ | 577 */ |
581 util.platform = { | 578 util.runningInBrowser = function() { |
582 /** | 579 return !window.appID; |
583 * @return {boolean} True if Files.app is running as an open files or a select | |
584 * folder dialog. False otherwise. | |
585 */ | |
586 runningInBrowser: function() { | |
587 return !window.appID; | |
588 }, | |
589 | |
590 /** | |
591 * @param {function(Object)} callback Function accepting a preference map. | |
592 */ | |
593 getPreferences: function(callback) { | |
594 chrome.storage.local.get(callback); | |
595 }, | |
596 | |
597 /** | |
598 * @param {string} key Preference name. | |
599 * @param {function(string)} callback Function accepting the preference value. | |
600 */ | |
601 getPreference: function(key, callback) { | |
602 chrome.storage.local.get(key, function(items) { | |
603 callback(items[key]); | |
604 }); | |
605 }, | |
606 | |
607 /** | |
608 * @param {string} key Preference name. | |
609 * @param {string|Object} value Preference value. | |
610 * @param {function()=} opt_callback Completion callback. | |
611 */ | |
612 setPreference: function(key, value, opt_callback) { | |
613 if (typeof value != 'string') | |
614 value = JSON.stringify(value); | |
615 | |
616 var items = {}; | |
617 items[key] = value; | |
618 chrome.storage.local.set(items, opt_callback); | |
619 } | |
620 }; | 580 }; |
621 | 581 |
622 /** | 582 /** |
623 * Attach page load handler. | 583 * Attach page load handler. |
624 * @param {function()} handler Application-specific load handler. | 584 * @param {function()} handler Application-specific load handler. |
625 */ | 585 */ |
626 util.addPageLoadHandler = function(handler) { | 586 util.addPageLoadHandler = function(handler) { |
627 document.addEventListener('DOMContentLoaded', function() { | 587 document.addEventListener('DOMContentLoaded', function() { |
628 handler(); | 588 handler(); |
629 }); | 589 }); |
630 }; | 590 }; |
631 | 591 |
632 /** | 592 /** |
633 * Save app launch data to the local storage. | 593 * Save app launch data to the local storage. |
634 */ | 594 */ |
635 util.saveAppState = function() { | 595 util.saveAppState = function() { |
636 if (window.appState) | 596 if (!window.appState) |
637 util.platform.setPreference(window.appID, window.appState); | 597 return; |
| 598 var items = {}; |
| 599 |
| 600 items[window.appID] = JSON.stringify(window.appState); |
| 601 chrome.storage.local.set(items); |
638 }; | 602 }; |
639 | 603 |
640 /** | 604 /** |
641 * AppCache is a persistent timestamped key-value storage backed by | 605 * AppCache is a persistent timestamped key-value storage backed by |
642 * HTML5 local storage. | 606 * HTML5 local storage. |
643 * | 607 * |
644 * It is not designed for frequent access. In order to avoid costly | 608 * It is not designed for frequent access. In order to avoid costly |
645 * localStorage iteration all data is kept in a single localStorage item. | 609 * localStorage iteration all data is kept in a single localStorage item. |
646 * There is no in-memory caching, so concurrent access is _almost_ safe. | 610 * There is no in-memory caching, so concurrent access is _almost_ safe. |
647 * | 611 * |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 util.AppCache.write_(map); | 662 util.AppCache.write_(map); |
699 }); | 663 }); |
700 }; | 664 }; |
701 | 665 |
702 /** | 666 /** |
703 * @param {function(Object)} callback Callback accepting a map of timestamped | 667 * @param {function(Object)} callback Callback accepting a map of timestamped |
704 * key-value pairs. | 668 * key-value pairs. |
705 * @private | 669 * @private |
706 */ | 670 */ |
707 util.AppCache.read_ = function(callback) { | 671 util.AppCache.read_ = function(callback) { |
708 util.platform.getPreference(util.AppCache.KEY, function(json) { | 672 chrome.storage.local.getPreference(util.AppCache.KEY, function(json) { |
709 if (json) { | 673 if (json) { |
710 try { | 674 try { |
711 callback(JSON.parse(json)); | 675 callback(JSON.parse(json)); |
712 } catch (e) { | 676 } catch (e) { |
713 // The local storage item somehow got messed up, start fresh. | 677 // The local storage item somehow got messed up, start fresh. |
714 } | 678 } |
715 } | 679 } |
716 callback({}); | 680 callback({}); |
717 }); | 681 }); |
718 }; | 682 }; |
719 | 683 |
720 /** | 684 /** |
721 * @param {Object} map A map of timestamped key-value pairs. | 685 * @param {Object} map A map of timestamped key-value pairs. |
722 * @private | 686 * @private |
723 */ | 687 */ |
724 util.AppCache.write_ = function(map) { | 688 util.AppCache.write_ = function(map) { |
725 util.platform.setPreference(util.AppCache.KEY, JSON.stringify(map)); | 689 var items = {}; |
| 690 items[util.AppCache.KEY] = JSON.stringify(map); |
| 691 chrome.storage.local.set(items); |
726 }; | 692 }; |
727 | 693 |
728 /** | 694 /** |
729 * Remove over-capacity and obsolete items. | 695 * Remove over-capacity and obsolete items. |
730 * | 696 * |
731 * @param {Object} map A map of timestamped key-value pairs. | 697 * @param {Object} map A map of timestamped key-value pairs. |
732 * @private | 698 * @private |
733 */ | 699 */ |
734 util.AppCache.cleanup_ = function(map) { | 700 util.AppCache.cleanup_ = function(map) { |
735 // Sort keys by ascending timestamps. | 701 // Sort keys by ascending timestamps. |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1351 parentEntry.toURL(), | 1317 parentEntry.toURL(), |
1352 name, | 1318 name, |
1353 function(valid) { | 1319 function(valid) { |
1354 if (valid) | 1320 if (valid) |
1355 fulfill(); | 1321 fulfill(); |
1356 else | 1322 else |
1357 reject(str('ERROR_LONG_NAME')); | 1323 reject(str('ERROR_LONG_NAME')); |
1358 }); | 1324 }); |
1359 }); | 1325 }); |
1360 }; | 1326 }; |
OLD | NEW |