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 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_handlers.
h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_handlers.
h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 } | 520 } |
521 | 521 |
522 bool IsFallbackFileBrowserHandler(const file_tasks::TaskDescriptor& task) { | 522 bool IsFallbackFileBrowserHandler(const file_tasks::TaskDescriptor& task) { |
523 return (task.task_type == file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER && | 523 return (task.task_type == file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER && |
524 (task.app_id == kFileManagerAppId || | 524 (task.app_id == kFileManagerAppId || |
525 task.app_id == extension_misc::kQuickOfficeComponentExtensionId || | 525 task.app_id == extension_misc::kQuickOfficeComponentExtensionId || |
526 task.app_id == extension_misc::kQuickOfficeDevExtensionId || | 526 task.app_id == extension_misc::kQuickOfficeDevExtensionId || |
527 task.app_id == extension_misc::kQuickOfficeExtensionId)); | 527 task.app_id == extension_misc::kQuickOfficeExtensionId)); |
528 } | 528 } |
529 | 529 |
530 FileBrowserHandlerList FindDefaultFileBrowserHandlers( | 530 FileBrowserHandlerList FindFileBrowserHandlers( |
531 const PrefService& pref_service, | |
532 const std::vector<base::FilePath>& file_list, | |
533 const FileBrowserHandlerList& common_handlers) { | |
534 FileBrowserHandlerList default_handlers; | |
535 | |
536 std::set<std::string> default_ids; | |
537 for (std::vector<base::FilePath>::const_iterator it = file_list.begin(); | |
538 it != file_list.end(); ++it) { | |
539 std::string task_id = file_tasks::GetDefaultTaskIdFromPrefs( | |
540 pref_service, "", it->Extension()); | |
541 if (!task_id.empty()) | |
542 default_ids.insert(task_id); | |
543 } | |
544 | |
545 const FileBrowserHandler* fallback_handler = NULL; | |
546 // Convert the default task IDs collected above to one of the handler pointers | |
547 // from common_handlers. | |
548 for (size_t i = 0; i < common_handlers.size(); ++i) { | |
549 const FileBrowserHandler* handler = common_handlers[i]; | |
550 const file_tasks::TaskDescriptor task_descriptor( | |
551 handler->extension_id(), | |
552 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER, | |
553 handler->id()); | |
554 const std::string task_id = | |
555 file_tasks::TaskDescriptorToId(task_descriptor); | |
556 std::set<std::string>::iterator default_iter = default_ids.find(task_id); | |
557 if (default_iter != default_ids.end()) { | |
558 default_handlers.push_back(handler); | |
559 continue; | |
560 } | |
561 | |
562 // Remember the first fallback handler. | |
563 if (!fallback_handler && IsFallbackFileBrowserHandler(task_descriptor)) | |
564 fallback_handler = handler; | |
565 } | |
566 | |
567 // If there are no default handlers found, use fallback as default. | |
568 if (fallback_handler && default_handlers.empty()) | |
569 default_handlers.push_back(fallback_handler); | |
570 | |
571 return default_handlers; | |
572 } | |
573 | |
574 FileBrowserHandlerList FindCommonFileBrowserHandlers( | |
575 Profile* profile, | 531 Profile* profile, |
576 const std::vector<GURL>& file_list) { | 532 const std::vector<GURL>& file_list) { |
577 FileBrowserHandlerList common_handlers; | 533 FileBrowserHandlerList common_handlers; |
578 for (std::vector<GURL>::const_iterator it = file_list.begin(); | 534 for (std::vector<GURL>::const_iterator it = file_list.begin(); |
579 it != file_list.end(); ++it) { | 535 it != file_list.end(); ++it) { |
580 FileBrowserHandlerList handlers = | 536 FileBrowserHandlerList handlers = |
581 FindFileBrowserHandlersForURL(profile, *it); | 537 FindFileBrowserHandlersForURL(profile, *it); |
582 // If there is nothing to do for one file, the intersection of handlers | 538 // If there is nothing to do for one file, the intersection of handlers |
583 // for all files will be empty at the end, so no need to check further. | 539 // for all files will be empty at the end, so no need to check further. |
584 if (handlers.empty()) | 540 if (handlers.empty()) |
(...skipping 30 matching lines...) Expand all Loading... |
615 // for multiple selection). | 571 // for multiple selection). |
616 if (file_list.size() == 1) | 572 if (file_list.size() == 1) |
617 common_handlers.erase(gallery_iter); | 573 common_handlers.erase(gallery_iter); |
618 else | 574 else |
619 common_handlers.erase(watch_iter); | 575 common_handlers.erase(watch_iter); |
620 } | 576 } |
621 | 577 |
622 return common_handlers; | 578 return common_handlers; |
623 } | 579 } |
624 | 580 |
625 const FileBrowserHandler* FindFileBrowserHandlerForURLAndPath( | |
626 Profile* profile, | |
627 const GURL& url, | |
628 const base::FilePath& file_path) { | |
629 std::vector<GURL> file_urls; | |
630 file_urls.push_back(url); | |
631 | |
632 FileBrowserHandlerList common_handlers = | |
633 FindCommonFileBrowserHandlers(profile, file_urls); | |
634 if (common_handlers.empty()) | |
635 return NULL; | |
636 | |
637 std::vector<base::FilePath> file_paths; | |
638 file_paths.push_back(file_path); | |
639 | |
640 FileBrowserHandlerList default_handlers = | |
641 FindDefaultFileBrowserHandlers(*profile->GetPrefs(), | |
642 file_paths, | |
643 common_handlers); | |
644 | |
645 // If there's none, or more than one, then we don't have a canonical default. | |
646 if (!default_handlers.empty()) { | |
647 // There should not be multiple default handlers for a single URL. | |
648 DCHECK_EQ(1u, default_handlers.size()); | |
649 | |
650 return *default_handlers.begin(); | |
651 } | |
652 | |
653 // If there are no default handlers, use first handler in the list (file | |
654 // manager does the same in this situation). TODO(tbarzic): This is not so | |
655 // optimal behaviour. | |
656 return *common_handlers.begin(); | |
657 } | |
658 | |
659 } // namespace file_browser_handlers | 581 } // namespace file_browser_handlers |
660 } // namespace file_manager | 582 } // namespace file_manager |
OLD | NEW |