OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/event_router.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/prefs/pref_change_registrar.h" | 10 #include "base/prefs/pref_change_registrar.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/thread_task_runner_handle.h" | |
13 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/browser/app_mode/app_mode_utils.h" | 16 #include "chrome/browser/app_mode/app_mode_utils.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 18 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
18 #include "chrome/browser/chromeos/drive/file_change.h" | 19 #include "chrome/browser/chromeos/drive/file_change.h" |
19 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 20 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
20 #include "chrome/browser/chromeos/drive/file_system_util.h" | 21 #include "chrome/browser/chromeos/drive/file_system_util.h" |
21 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" | 22 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" |
22 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" | 23 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 using drive::DriveIntegrationService; | 55 using drive::DriveIntegrationService; |
55 using drive::DriveIntegrationServiceFactory; | 56 using drive::DriveIntegrationServiceFactory; |
56 using file_manager::util::EntryDefinition; | 57 using file_manager::util::EntryDefinition; |
57 using file_manager::util::FileDefinition; | 58 using file_manager::util::FileDefinition; |
58 | 59 |
59 namespace file_browser_private = extensions::api::file_browser_private; | 60 namespace file_browser_private = extensions::api::file_browser_private; |
60 | 61 |
61 namespace file_manager { | 62 namespace file_manager { |
62 namespace { | 63 namespace { |
63 // Constants for the "transferState" field of onFileTransferUpdated event. | 64 // Constants for the "transferState" field of onFileTransferUpdated event. |
65 const char kFileTransferStateAdded[] = "added"; | |
64 const char kFileTransferStateStarted[] = "started"; | 66 const char kFileTransferStateStarted[] = "started"; |
65 const char kFileTransferStateInProgress[] = "in_progress"; | 67 const char kFileTransferStateInProgress[] = "in_progress"; |
66 const char kFileTransferStateCompleted[] = "completed"; | 68 const char kFileTransferStateCompleted[] = "completed"; |
67 const char kFileTransferStateFailed[] = "failed"; | 69 const char kFileTransferStateFailed[] = "failed"; |
68 | 70 |
69 // Frequency of sending onFileTransferUpdated. | 71 // Frequency of sending onFileTransferUpdated. |
70 const int64 kProgressEventFrequencyInMilliseconds = 1000; | 72 const int64 kProgressEventFrequencyInMilliseconds = 1000; |
71 | 73 |
72 // Maximim size of detailed change info on directory change event. If the size | 74 // Maximim size of detailed change info on directory change event. If the size |
73 // exceeds the maximum size, the detailed info is omitted and the force refresh | 75 // exceeds the maximum size, the detailed info is omitted and the force refresh |
(...skipping 17 matching lines...) Expand all Loading... | |
91 | 93 |
92 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | 94 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
93 GURL url = util::ConvertDrivePathToFileSystemUrl( | 95 GURL url = util::ConvertDrivePathToFileSystemUrl( |
94 profile, job_info.file_path, extension_id); | 96 profile, job_info.file_path, extension_id); |
95 status->file_url = url.spec(); | 97 status->file_url = url.spec(); |
96 status->transfer_state = file_browser_private::ParseTransferState(job_status); | 98 status->transfer_state = file_browser_private::ParseTransferState(job_status); |
97 status->transfer_type = | 99 status->transfer_type = |
98 IsUploadJob(job_info.job_type) ? | 100 IsUploadJob(job_info.job_type) ? |
99 file_browser_private::TRANSFER_TYPE_UPLOAD : | 101 file_browser_private::TRANSFER_TYPE_UPLOAD : |
100 file_browser_private::TRANSFER_TYPE_DOWNLOAD; | 102 file_browser_private::TRANSFER_TYPE_DOWNLOAD; |
103 status->num_total_jobs = job_info.num_total_jobs; | |
101 // JavaScript does not have 64-bit integers. Instead we use double, which | 104 // JavaScript does not have 64-bit integers. Instead we use double, which |
102 // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice | 105 // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice |
103 // in C++. Larger values are rounded. | 106 // in C++. Larger values are rounded. |
104 status->processed.reset( | 107 status->processed.reset( |
105 new double(static_cast<double>(job_info.num_completed_bytes))); | 108 new double(static_cast<double>(job_info.num_completed_bytes))); |
106 status->total.reset( | 109 status->total.reset( |
107 new double(static_cast<double>(job_info.num_total_bytes))); | 110 new double(static_cast<double>(job_info.num_total_bytes))); |
108 } | 111 } |
109 | 112 |
110 // Checks if the Recovery Tool is running. This is a temporary solution. | 113 // Checks if the Recovery Tool is running. This is a temporary solution. |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 } | 616 } |
614 | 617 |
615 BroadcastEvent( | 618 BroadcastEvent( |
616 profile_, | 619 profile_, |
617 file_browser_private::OnPreferencesChanged::kEventName, | 620 file_browser_private::OnPreferencesChanged::kEventName, |
618 file_browser_private::OnPreferencesChanged::Create()); | 621 file_browser_private::OnPreferencesChanged::Create()); |
619 } | 622 } |
620 | 623 |
621 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { | 624 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { |
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
623 OnJobUpdated(job_info); | 626 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
627 return; | |
628 ScheduleDriveFileTransferEvent( | |
629 job_info, kFileTransferStateAdded, false /* immediate */); | |
624 } | 630 } |
625 | 631 |
626 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { | 632 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { |
627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 633 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
628 if (!drive::IsActiveFileTransferJobInfo(job_info)) | 634 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
629 return; | 635 return; |
630 | 636 |
631 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); | 637 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); |
632 | 638 |
639 const std::string status = | |
640 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress; | |
641 | |
633 // Replace with the latest job info. | 642 // Replace with the latest job info. |
634 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( | 643 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus(job_info, status); |
635 job_info, | |
636 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress); | |
637 | 644 |
638 // Fire event if needed. | 645 ScheduleDriveFileTransferEvent(job_info, status, false /* immediate */); |
639 bool always = is_new_job; | |
640 SendDriveFileTransferEvent(always); | |
641 } | 646 } |
642 | 647 |
643 void EventRouter::OnJobDone(const drive::JobInfo& job_info, | 648 void EventRouter::OnJobDone(const drive::JobInfo& job_info, |
644 drive::FileError error) { | 649 drive::FileError error) { |
645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 650 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
646 if (!drive::IsActiveFileTransferJobInfo(job_info)) | 651 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
647 return; | 652 return; |
648 | 653 |
649 // Replace with the latest job info. | 654 const std::string status = error == drive::FILE_ERROR_OK |
650 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( | 655 ? kFileTransferStateCompleted |
651 job_info, | 656 : kFileTransferStateFailed; |
652 error == drive::FILE_ERROR_OK ? kFileTransferStateCompleted | |
653 : kFileTransferStateFailed); | |
654 | 657 |
655 // Fire event if needed. | 658 ScheduleDriveFileTransferEvent(job_info, status, true /* immediate */); |
656 bool always = true; | |
657 SendDriveFileTransferEvent(always); | |
658 | 659 |
659 // Forget about the job. | 660 // Forget about the job. |
660 drive_jobs_.erase(job_info.job_id); | 661 drive_jobs_.erase(job_info.job_id); |
661 } | 662 } |
662 | 663 |
663 void EventRouter::SendDriveFileTransferEvent(bool always) { | 664 void EventRouter::ScheduleDriveFileTransferEvent(const drive::JobInfo& job_info, |
664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 665 const std::string& status, |
666 bool immediate) { | |
667 // Update next send time. | |
668 base::Time now = base::Time::Now(); | |
669 if (immediate) { | |
670 next_send_time_ = now; | |
671 } else if (!drive_job_info_with_status_) { | |
672 next_send_time_ = | |
hirono
2014/09/01 07:29:01
Please add comment why we need to delay the events
iseki
2014/09/01 09:26:39
Done.
| |
673 std::max(now + base::TimeDelta::FromMilliseconds(100), next_send_time_); | |
hirono
2014/09/01 07:29:01
Please turn the number into constant.
note: We don
iseki
2014/09/01 09:26:39
Done.
| |
674 } | |
665 | 675 |
666 // When |always| flag is not set, we don't send the event until certain | 676 // Update the latest event. |
667 // amount of time passes after the previous one. This is to avoid | 677 drive_job_info_with_status_.reset( |
668 // flooding the IPC between extensions by many onFileTransferUpdated events. | 678 new DriveJobInfoWithStatus(job_info, status)); |
669 if (!ShouldSendProgressEvent(always, &last_file_transfer_event_)) | 679 |
680 // Schedule event. | |
681 if (next_send_time_ < now) { | |
682 SendDriveFileTransferEvent(); | |
683 } else { | |
684 base::TimeDelta delta = next_send_time_ - now; | |
hirono
2014/09/01 07:29:01
const
iseki
2014/09/01 09:26:39
Done.
| |
685 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
686 FROM_HERE, | |
687 base::Bind(&EventRouter::SendDriveFileTransferEvent, | |
688 weak_factory_.GetWeakPtr()), | |
689 delta); | |
690 } | |
691 } | |
692 | |
693 void EventRouter::SendDriveFileTransferEvent() { | |
694 base::Time now = base::Time::Now(); | |
695 if (now < next_send_time_ || !drive_job_info_with_status_) | |
670 return; | 696 return; |
671 | 697 |
672 // Convert the current |drive_jobs_| to IDL type. | 698 // Convert the drive_job_info_with_status_ to IDL type. |
673 std::vector<linked_ptr<file_browser_private::FileTransferStatus> > | 699 std::vector<linked_ptr<file_browser_private::FileTransferStatus> > |
674 status_list; | 700 status_list; |
675 for (std::map<drive::JobID, DriveJobInfoWithStatus>::iterator | 701 |
676 iter = drive_jobs_.begin(); iter != drive_jobs_.end(); ++iter) { | 702 linked_ptr<file_browser_private::FileTransferStatus> status( |
677 linked_ptr<file_browser_private::FileTransferStatus> status( | 703 new file_browser_private::FileTransferStatus()); |
678 new file_browser_private::FileTransferStatus()); | 704 JobInfoToTransferStatus(profile_, |
679 JobInfoToTransferStatus(profile_, | 705 kFileManagerAppId, |
680 kFileManagerAppId, | 706 drive_job_info_with_status_->status, |
681 iter->second.status, | 707 drive_job_info_with_status_->job_info, |
682 iter->second.job_info, | 708 status.get()); |
683 status.get()); | 709 status_list.push_back(status); |
684 status_list.push_back(status); | 710 |
685 } | 711 drive_job_info_with_status_.reset(); |
712 | |
686 BroadcastEvent( | 713 BroadcastEvent( |
687 profile_, | 714 profile_, |
688 file_browser_private::OnFileTransfersUpdated::kEventName, | 715 file_browser_private::OnFileTransfersUpdated::kEventName, |
689 file_browser_private::OnFileTransfersUpdated::Create(status_list)); | 716 file_browser_private::OnFileTransfersUpdated::Create(status_list)); |
717 | |
718 next_send_time_ = now + base::TimeDelta::FromMilliseconds(500); | |
hirono
2014/09/01 07:29:01
ditto: Please add comment why we need to delay the
iseki
2014/09/01 09:26:39
Done.
iseki
2014/09/01 09:26:39
Done.
| |
690 } | 719 } |
691 | 720 |
692 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { | 721 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { |
693 HandleFileWatchNotification(NULL, drive_path, false); | 722 HandleFileWatchNotification(NULL, drive_path, false); |
694 } | 723 } |
695 | 724 |
696 void EventRouter::OnFileChanged(const drive::FileChange& changed_files) { | 725 void EventRouter::OnFileChanged(const drive::FileChange& changed_files) { |
697 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; | 726 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; |
698 | 727 |
699 FileChangeMap map; | 728 FileChangeMap map; |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
966 } | 995 } |
967 } | 996 } |
968 | 997 |
969 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { | 998 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { |
970 BroadcastEvent(profile_, | 999 BroadcastEvent(profile_, |
971 file_browser_private::OnDesktopChanged::kEventName, | 1000 file_browser_private::OnDesktopChanged::kEventName, |
972 file_browser_private::OnDesktopChanged::Create()); | 1001 file_browser_private::OnDesktopChanged::Create()); |
973 } | 1002 } |
974 | 1003 |
975 } // namespace file_manager | 1004 } // namespace file_manager |
OLD | NEW |