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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus() | 361 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus() |
359 : job_info(drive::TYPE_DOWNLOAD_FILE) { | 362 : job_info(drive::TYPE_DOWNLOAD_FILE) { |
360 } | 363 } |
361 | 364 |
362 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( | 365 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( |
363 const drive::JobInfo& info, const std::string& status) | 366 const drive::JobInfo& info, const std::string& status) |
364 : job_info(info), status(status) { | 367 : job_info(info), status(status) { |
365 } | 368 } |
366 | 369 |
367 EventRouter::EventRouter(Profile* profile) | 370 EventRouter::EventRouter(Profile* profile) |
368 : pref_change_registrar_(new PrefChangeRegistrar), | 371 : delay_time_(base::TimeDelta::FromMilliseconds(100)), |
372 force_send_time_(base::TimeDelta::FromMilliseconds(1000)), | |
373 pref_change_registrar_(new PrefChangeRegistrar), | |
369 profile_(profile), | 374 profile_(profile), |
370 multi_user_window_manager_observer_registered_(false), | 375 multi_user_window_manager_observer_registered_(false), |
371 device_event_router_(new DeviceEventRouterImpl(profile)), | 376 device_event_router_(new DeviceEventRouterImpl(profile)), |
372 weak_factory_(this) { | 377 weak_factory_(this) { |
373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
374 } | 379 } |
375 | 380 |
376 EventRouter::~EventRouter() { | 381 EventRouter::~EventRouter() { |
377 } | 382 } |
378 | 383 |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 } | 618 } |
614 | 619 |
615 BroadcastEvent( | 620 BroadcastEvent( |
616 profile_, | 621 profile_, |
617 file_browser_private::OnPreferencesChanged::kEventName, | 622 file_browser_private::OnPreferencesChanged::kEventName, |
618 file_browser_private::OnPreferencesChanged::Create()); | 623 file_browser_private::OnPreferencesChanged::Create()); |
619 } | 624 } |
620 | 625 |
621 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { | 626 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { |
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
623 OnJobUpdated(job_info); | 628 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
629 return; | |
630 const DriveJobInfoWithStatus job_info_with_status = | |
631 DriveJobInfoWithStatus(job_info, kFileTransferStateAdded); | |
632 SendDriveFileTransferEvent( | |
633 job_info_with_status, base::Time::Now(), true /* should_delay */); | |
624 } | 634 } |
625 | 635 |
626 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { | 636 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { |
627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
628 if (!drive::IsActiveFileTransferJobInfo(job_info)) | 638 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
629 return; | 639 return; |
630 | 640 |
631 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); | 641 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); |
632 | 642 |
633 // Replace with the latest job info. | 643 // Replace with the latest job info. |
634 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( | 644 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( |
635 job_info, | 645 job_info, |
636 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress); | 646 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress); |
637 | 647 |
638 // Fire event if needed. | 648 const DriveJobInfoWithStatus job_info_with_status = |
639 bool always = is_new_job; | 649 drive_jobs_[job_info.job_id]; |
640 SendDriveFileTransferEvent(always); | 650 SendDriveFileTransferEvent( |
651 job_info_with_status, base::Time::Now(), true /* should_delay */); | |
641 } | 652 } |
642 | 653 |
643 void EventRouter::OnJobDone(const drive::JobInfo& job_info, | 654 void EventRouter::OnJobDone(const drive::JobInfo& job_info, |
644 drive::FileError error) { | 655 drive::FileError error) { |
645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
646 if (!drive::IsActiveFileTransferJobInfo(job_info)) | 657 if (!drive::IsActiveFileTransferJobInfo(job_info)) |
647 return; | 658 return; |
648 | 659 |
649 // Replace with the latest job info. | 660 // Replace with the latest job info. |
650 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( | 661 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( |
651 job_info, | 662 job_info, |
652 error == drive::FILE_ERROR_OK ? kFileTransferStateCompleted | 663 error == drive::FILE_ERROR_OK ? kFileTransferStateCompleted |
653 : kFileTransferStateFailed); | 664 : kFileTransferStateFailed); |
654 | 665 |
655 // Fire event if needed. | 666 const DriveJobInfoWithStatus job_info_with_status = |
656 bool always = true; | 667 drive_jobs_[job_info.job_id]; |
657 SendDriveFileTransferEvent(always); | 668 base::Time now = base::Time::Now(); |
669 last_post_delayed_task_ = now; | |
670 SendDriveFileTransferEvent( | |
671 job_info_with_status, now, false /* should_delay */); | |
658 | 672 |
659 // Forget about the job. | 673 // Forget about the job. |
660 drive_jobs_.erase(job_info.job_id); | 674 drive_jobs_.erase(job_info.job_id); |
661 } | 675 } |
662 | 676 |
663 void EventRouter::SendDriveFileTransferEvent(bool always) { | 677 void EventRouter::SendDriveFileTransferEvent( |
678 const DriveJobInfoWithStatus& job_info_with_status, | |
679 base::Time call_time, | |
680 bool should_delay) { | |
664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
hirono
2014/08/29 11:50:46
How about doing this?
ScheduleEvent(Event event,
iseki
2014/09/01 02:24:47
Done.
| |
665 | 682 |
666 // When |always| flag is not set, we don't send the event until certain | 683 if (should_delay) { |
667 // amount of time passes after the previous one. This is to avoid | 684 last_post_delayed_task_ = base::Time::Now(); |
668 // flooding the IPC between extensions by many onFileTransferUpdated events. | 685 if (first_delay_task_.is_null() || first_delay_task_.is_max()) |
669 if (!ShouldSendProgressEvent(always, &last_file_transfer_event_)) | 686 first_delay_task_ = base::Time::Now(); |
687 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
688 FROM_HERE, | |
689 base::Bind(&EventRouter::SendDriveFileTransferEvent, | |
690 weak_factory_.GetWeakPtr(), | |
691 job_info_with_status, | |
692 last_post_delayed_task_, | |
693 false /* should_delay */), | |
694 delay_time_); | |
670 return; | 695 return; |
696 } | |
697 | |
698 if (!last_post_delayed_task_.is_null() && !first_delay_task_.is_null() && | |
699 call_time < last_post_delayed_task_ && | |
700 call_time < first_delay_task_ + force_send_time_) { | |
701 return; | |
702 } | |
703 | |
704 first_delay_task_ = base::Time::Max(); | |
671 | 705 |
672 // Convert the current |drive_jobs_| to IDL type. | 706 // Convert the current |drive_jobs_| to IDL type. |
673 std::vector<linked_ptr<file_browser_private::FileTransferStatus> > | 707 std::vector<linked_ptr<file_browser_private::FileTransferStatus> > |
674 status_list; | 708 status_list; |
675 for (std::map<drive::JobID, DriveJobInfoWithStatus>::iterator | 709 |
676 iter = drive_jobs_.begin(); iter != drive_jobs_.end(); ++iter) { | 710 linked_ptr<file_browser_private::FileTransferStatus> status( |
677 linked_ptr<file_browser_private::FileTransferStatus> status( | 711 new file_browser_private::FileTransferStatus()); |
678 new file_browser_private::FileTransferStatus()); | 712 JobInfoToTransferStatus(profile_, |
679 JobInfoToTransferStatus(profile_, | 713 kFileManagerAppId, |
680 kFileManagerAppId, | 714 job_info_with_status.status, |
681 iter->second.status, | 715 job_info_with_status.job_info, |
682 iter->second.job_info, | 716 status.get()); |
683 status.get()); | 717 status_list.push_back(status); |
684 status_list.push_back(status); | 718 |
685 } | |
686 BroadcastEvent( | 719 BroadcastEvent( |
687 profile_, | 720 profile_, |
688 file_browser_private::OnFileTransfersUpdated::kEventName, | 721 file_browser_private::OnFileTransfersUpdated::kEventName, |
689 file_browser_private::OnFileTransfersUpdated::Create(status_list)); | 722 file_browser_private::OnFileTransfersUpdated::Create(status_list)); |
690 } | 723 } |
691 | 724 |
692 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { | 725 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { |
693 HandleFileWatchNotification(NULL, drive_path, false); | 726 HandleFileWatchNotification(NULL, drive_path, false); |
694 } | 727 } |
695 | 728 |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
966 } | 999 } |
967 } | 1000 } |
968 | 1001 |
969 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { | 1002 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { |
970 BroadcastEvent(profile_, | 1003 BroadcastEvent(profile_, |
971 file_browser_private::OnDesktopChanged::kEventName, | 1004 file_browser_private::OnDesktopChanged::kEventName, |
972 file_browser_private::OnDesktopChanged::Create()); | 1005 file_browser_private::OnDesktopChanged::Create()); |
973 } | 1006 } |
974 | 1007 |
975 } // namespace file_manager | 1008 } // namespace file_manager |
OLD | NEW |