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 /** | 5 /** |
6 * Namespace for utility functions. | 6 * Namespace for utility functions. |
7 */ | 7 */ |
8 var util = { | 8 var util = { |
9 /** | 9 /** |
10 * Returns a function that console.log's its arguments, prefixed by |msg|. | 10 * Returns a function that console.log's its arguments, prefixed by |msg|. |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 (event.shiftKey ? 'Shift-' : '') + | 507 (event.shiftKey ? 'Shift-' : '') + |
508 (event.metaKey ? 'Meta-' : ''); | 508 (event.metaKey ? 'Meta-' : ''); |
509 }, | 509 }, |
510 | 510 |
511 /** | 511 /** |
512 * A wrapper for navigator.onLine that allows for easy debug override. | 512 * A wrapper for navigator.onLine that allows for easy debug override. |
513 * @return {boolean} True if offline. | 513 * @return {boolean} True if offline. |
514 */ | 514 */ |
515 isOffline: function() { | 515 isOffline: function() { |
516 return !navigator.onLine; | 516 return !navigator.onLine; |
517 }, | |
518 | |
519 /** | |
520 * Given a list of gdata search result urls, generates a list of urls which | |
521 * gdata search result urls reference. | |
522 * | |
523 * @param {Array.<string>} urls List of gdata search result urls. | |
524 * @param {string} rootUrl Root url for resolved urls. Should be gdata root | |
525 * url. | |
526 * @param {function(Array.<string>)} callback Callback that will be invoked | |
527 * when the method finishes. It will be passed list of resolved urls. | |
528 */ | |
529 resolveGDataSearchUrls: function(urls, rootUrl, callback) { | |
530 var resolvedUrls = []; | |
531 // If the list of urls is empty, there's nothing to do. | |
532 if (urls.length == 0) { | |
533 callback([]); | |
dgozman
2012/05/15 11:25:06
return missed
tbarzic
2012/05/16 03:50:04
Done.
| |
534 } | |
535 | |
536 // Given relative file path, it will generate escaped file path that will | |
537 // be appended to rootUrl to form the resolved url. | |
538 function getURIEncodedPath(filePath) { | |
539 var components = filePath.split('/'); | |
540 // Root url should already contain path's root dir, so clear it. | |
541 components[0] = ''; | |
542 for (var i = 0; i < components.length; i++) | |
543 components[i] = encodeURIComponent(components[i]); | |
544 return components.join('/'); | |
545 } | |
546 | |
547 // Resolves single url at |urls[index]| and calls |onResolved| with |index|. | |
548 function resolveUrl(index, onResolved) { | |
549 chrome.fileBrowserPrivate.getPathForDriveSearchResult(urls[index], | |
550 function(filePath) { | |
551 if (filePath) { | |
552 resolvedUrls.push(rootUrl + getURIEncodedPath(filePath)); | |
553 } else { | |
554 // If we weren't able to resolve the url, fallback to using | |
555 // original url. | |
556 resolvedUrls.push(urls[index]); | |
557 } | |
558 onResolved(index); | |
559 }); | |
560 } | |
561 | |
562 // If not all urls are resolved, starts resolving next one, else calls | |
563 // callback | |
564 function resolveNextOrFinish(lastIndex) { | |
565 var nextIndex = lastIndex + 1; | |
566 if (nextIndex == urls.length) { | |
567 callback(resolvedUrls); | |
568 return; | |
569 } | |
570 resolveUrl(nextIndex, resolveNextOrFinish); | |
571 } | |
572 | |
573 // Start resolving first url. | |
574 resolveUrl(0, resolveNextOrFinish); | |
575 }, | |
576 | |
577 /** | |
578 * Tests if the given path is a gdata search result path, and if it is, | |
579 * returns file's fileName in virtual search file system, its gdata resourceId | |
580 * and the display name that should be used when the file is shown in file | |
581 * browser. | |
582 * | |
583 * @param {string} path The potential gdata search result path. | |
584 * @return {object.<string, stringi, string>} Object that will contain file's | |
585 * fileName, displayName and resourceId; or null if the path is not gdata | |
586 * search result path. | |
587 */ | |
588 getFileAndDisplayNameForGDataSearchResult: function(path) { | |
589 // Nothing to do if the path is not under gdata search root path. | |
590 if (path.search(DirectoryModel.GDATA_SEARCH_ROOT_PATH) != 0) | |
591 return null; | |
592 | |
593 var pathComponents = path.split('/'); | |
594 | |
595 // Search result should be formatted like: | |
596 // gdataSearchRoot/query/result | |
597 if (pathComponents.length != | |
598 DirectoryModel.GDATA_SEARCH_ROOT_COMPONENTS.length + 2) | |
599 return null; | |
600 for (var i = 0; | |
601 i < DirectoryModel.GDATA_SEARCH_ROOT_COMPONENTS.length; | |
602 i++) { | |
603 if (pathComponents[i] != DirectoryModel.GDATA_SEARCH_ROOT_COMPONENTS[i]) | |
604 return null; | |
605 } | |
606 | |
607 // Search result file name should be formatted like: | |
608 // resource_id.referenced_file_name | |
609 // We should display referenced file name only. | |
610 var result = {}; | |
611 result.fileName = pathComponents.pop(); | |
612 result.displayName = | |
613 result.fileName.slice(result.fileName.indexOf('.') + 1); | |
614 result.resourceId = | |
615 result.fileName.substr(0, result.fileName.indexOf('.')); | |
616 | |
617 if (result.fileName.length > 0 && result.displayName.length > 0) { | |
618 return result; | |
619 } else { | |
620 return null; | |
621 } | |
517 } | 622 } |
518 }; | 623 }; |
OLD | NEW |