Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/event_router.cc

Issue 507293002: Enrich fileBrowserPrivate.onFileTransfersUpdated event to support displaying total number of jobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
74 // is kicked. 76 // is kicked.
75 const size_t kDirectoryChangeEventMaxDetailInfoSize = 1000; 77 const size_t kDirectoryChangeEventMaxDetailInfoSize = 1000;
76 78
79 // This time(millisecond) is used for confirm following event exists.
80 const int64 kFileTransferEventDelayTimeInMilliseconds = 500;
81
82 // This time(millisecond) is margin for PostDelayedTask.
83 // PostDelayedTask do not ensure the specified time to be elapsed. This time
84 // is necessary to avoid unexpected discard.
85 const int64 kFileTransferEventMarginTimeForDelay = 20;
86
77 // Utility function to check if |job_info| is a file uploading job. 87 // Utility function to check if |job_info| is a file uploading job.
78 bool IsUploadJob(drive::JobType type) { 88 bool IsUploadJob(drive::JobType type) {
79 return (type == drive::TYPE_UPLOAD_NEW_FILE || 89 return (type == drive::TYPE_UPLOAD_NEW_FILE ||
80 type == drive::TYPE_UPLOAD_EXISTING_FILE); 90 type == drive::TYPE_UPLOAD_EXISTING_FILE);
81 } 91 }
82 92
83 // Converts the job info to a IDL generated type. 93 // Converts the job info to a IDL generated type.
84 void JobInfoToTransferStatus( 94 void JobInfoToTransferStatus(
85 Profile* profile, 95 Profile* profile,
86 const std::string& extension_id, 96 const std::string& extension_id,
87 const std::string& job_status, 97 const std::string& job_status,
88 const drive::JobInfo& job_info, 98 const drive::JobInfo& job_info,
89 file_browser_private::FileTransferStatus* status) { 99 file_browser_private::FileTransferStatus* status) {
90 DCHECK(IsActiveFileTransferJobInfo(job_info)); 100 DCHECK(IsActiveFileTransferJobInfo(job_info));
91 101
92 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); 102 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
93 GURL url = util::ConvertDrivePathToFileSystemUrl( 103 GURL url = util::ConvertDrivePathToFileSystemUrl(
94 profile, job_info.file_path, extension_id); 104 profile, job_info.file_path, extension_id);
95 status->file_url = url.spec(); 105 status->file_url = url.spec();
96 status->transfer_state = file_browser_private::ParseTransferState(job_status); 106 status->transfer_state = file_browser_private::ParseTransferState(job_status);
97 status->transfer_type = 107 status->transfer_type =
98 IsUploadJob(job_info.job_type) ? 108 IsUploadJob(job_info.job_type) ?
99 file_browser_private::TRANSFER_TYPE_UPLOAD : 109 file_browser_private::TRANSFER_TYPE_UPLOAD :
100 file_browser_private::TRANSFER_TYPE_DOWNLOAD; 110 file_browser_private::TRANSFER_TYPE_DOWNLOAD;
111 DriveIntegrationService* const integration_service =
112 DriveIntegrationServiceFactory::FindForProfile(profile);
113 status->num_total_jobs =
114 integration_service->job_list()->GetJobInfoList().size();
kinaba 2014/09/03 05:42:24 This includes the number of jobs that is not drive
iseki 2014/09/03 12:29:00 Done.
101 // JavaScript does not have 64-bit integers. Instead we use double, which 115 // 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 116 // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice
103 // in C++. Larger values are rounded. 117 // in C++. Larger values are rounded.
104 status->processed.reset( 118 status->processed.reset(
105 new double(static_cast<double>(job_info.num_completed_bytes))); 119 new double(static_cast<double>(job_info.num_completed_bytes)));
106 status->total.reset( 120 status->total.reset(
107 new double(static_cast<double>(job_info.num_total_bytes))); 121 new double(static_cast<double>(job_info.num_total_bytes)));
108 } 122 }
109 123
110 // Checks if the Recovery Tool is running. This is a temporary solution. 124 // Checks if the Recovery Tool is running. This is a temporary solution.
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } 627 }
614 628
615 BroadcastEvent( 629 BroadcastEvent(
616 profile_, 630 profile_,
617 file_browser_private::OnPreferencesChanged::kEventName, 631 file_browser_private::OnPreferencesChanged::kEventName,
618 file_browser_private::OnPreferencesChanged::Create()); 632 file_browser_private::OnPreferencesChanged::Create());
619 } 633 }
620 634
621 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) { 635 void EventRouter::OnJobAdded(const drive::JobInfo& job_info) {
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
623 OnJobUpdated(job_info); 637 if (!drive::IsActiveFileTransferJobInfo(job_info))
638 return;
639 ScheduleDriveFileTransferEvent(
640 job_info, kFileTransferStateAdded, false /* immediate */);
624 } 641 }
625 642
626 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) { 643 void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) {
627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 644 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
628 if (!drive::IsActiveFileTransferJobInfo(job_info)) 645 if (!drive::IsActiveFileTransferJobInfo(job_info))
629 return; 646 return;
630 647
631 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end()); 648 bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end());
632 649
650 const std::string status =
651 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress;
652
633 // Replace with the latest job info. 653 // Replace with the latest job info.
634 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( 654 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus(job_info, status);
635 job_info,
636 is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress);
637 655
638 // Fire event if needed. 656 ScheduleDriveFileTransferEvent(job_info, status, false /* immediate */);
639 bool always = is_new_job;
640 SendDriveFileTransferEvent(always);
641 } 657 }
642 658
643 void EventRouter::OnJobDone(const drive::JobInfo& job_info, 659 void EventRouter::OnJobDone(const drive::JobInfo& job_info,
644 drive::FileError error) { 660 drive::FileError error) {
645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 661 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
646 if (!drive::IsActiveFileTransferJobInfo(job_info)) 662 if (!drive::IsActiveFileTransferJobInfo(job_info))
647 return; 663 return;
648 664
649 // Replace with the latest job info. 665 const std::string status = error == drive::FILE_ERROR_OK
650 drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus( 666 ? kFileTransferStateCompleted
651 job_info, 667 : kFileTransferStateFailed;
652 error == drive::FILE_ERROR_OK ? kFileTransferStateCompleted
653 : kFileTransferStateFailed);
654 668
655 // Fire event if needed. 669 ScheduleDriveFileTransferEvent(job_info, status, true /* immediate */);
656 bool always = true;
657 SendDriveFileTransferEvent(always);
658 670
659 // Forget about the job. 671 // Forget about the job.
660 drive_jobs_.erase(job_info.job_id); 672 drive_jobs_.erase(job_info.job_id);
661 } 673 }
662 674
663 void EventRouter::SendDriveFileTransferEvent(bool always) { 675 void EventRouter::ScheduleDriveFileTransferEvent(const drive::JobInfo& job_info,
664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 676 const std::string& status,
677 bool immediate) {
678 // Update next send time.
679 const base::Time now = base::Time::Now();
680 if (immediate) {
681 next_send_file_transfer_event_ = now;
682 } else if (!drive_job_info_for_scheduled_event_) {
683 // The first job is delayed to confirm whether only job or many jobs.
684 next_send_file_transfer_event_ =
685 std::max(now + base::TimeDelta::FromMilliseconds(
686 kFileTransferEventDelayTimeInMilliseconds),
687 next_send_file_transfer_event_);
688 }
665 689
666 // When |always| flag is not set, we don't send the event until certain 690 // Update the latest event.
667 // amount of time passes after the previous one. This is to avoid 691 drive_job_info_for_scheduled_event_.reset(
668 // flooding the IPC between extensions by many onFileTransferUpdated events. 692 new DriveJobInfoWithStatus(job_info, status));
669 if (!ShouldSendProgressEvent(always, &last_file_transfer_event_)) 693
694 // Schedule event.
695 if (next_send_file_transfer_event_ < now) {
kinaba 2014/09/03 05:42:24 <=, rather than < ("immediate" events should fire
iseki 2014/09/03 12:29:00 Done.
696 SendDriveFileTransferEvent();
697 } else {
698 const base::TimeDelta delta = next_send_file_transfer_event_ - now;
699 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
700 FROM_HERE,
701 base::Bind(&EventRouter::SendDriveFileTransferEvent,
702 weak_factory_.GetWeakPtr()),
703 delta);
704 }
705 }
706
707 void EventRouter::SendDriveFileTransferEvent() {
708 const base::Time now = base::Time::Now();
709 if (now < next_send_file_transfer_event_ -
710 base::TimeDelta::FromMilliseconds(
711 kFileTransferEventMarginTimeForDelay) ||
712 !drive_job_info_for_scheduled_event_) {
670 return; 713 return;
714 }
671 715
672 // Convert the current |drive_jobs_| to IDL type. 716 // Convert the drive_job_info_for_scheduled_event_ to IDL type.
673 std::vector<linked_ptr<file_browser_private::FileTransferStatus> > 717 std::vector<linked_ptr<file_browser_private::FileTransferStatus> >
674 status_list; 718 status_list;
675 for (std::map<drive::JobID, DriveJobInfoWithStatus>::iterator 719
676 iter = drive_jobs_.begin(); iter != drive_jobs_.end(); ++iter) { 720 linked_ptr<file_browser_private::FileTransferStatus> status(
677 linked_ptr<file_browser_private::FileTransferStatus> status( 721 new file_browser_private::FileTransferStatus());
678 new file_browser_private::FileTransferStatus()); 722 JobInfoToTransferStatus(profile_,
679 JobInfoToTransferStatus(profile_, 723 kFileManagerAppId,
680 kFileManagerAppId, 724 drive_job_info_for_scheduled_event_->status,
681 iter->second.status, 725 drive_job_info_for_scheduled_event_->job_info,
682 iter->second.job_info, 726 status.get());
683 status.get()); 727 status_list.push_back(status);
684 status_list.push_back(status); 728
685 } 729 drive_job_info_for_scheduled_event_.reset();
730
686 BroadcastEvent( 731 BroadcastEvent(
687 profile_, 732 profile_,
688 file_browser_private::OnFileTransfersUpdated::kEventName, 733 file_browser_private::OnFileTransfersUpdated::kEventName,
689 file_browser_private::OnFileTransfersUpdated::Create(status_list)); 734 file_browser_private::OnFileTransfersUpdated::Create(status_list));
690 } 735 }
691 736
692 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { 737 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) {
693 HandleFileWatchNotification(NULL, drive_path, false); 738 HandleFileWatchNotification(NULL, drive_path, false);
694 } 739 }
695 740
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 } 1010 }
966 } 1011 }
967 1012
968 void EventRouter::OnOwnerEntryChanged(aura::Window* window) { 1013 void EventRouter::OnOwnerEntryChanged(aura::Window* window) {
969 BroadcastEvent(profile_, 1014 BroadcastEvent(profile_,
970 file_browser_private::OnDesktopChanged::kEventName, 1015 file_browser_private::OnDesktopChanged::kEventName,
971 file_browser_private::OnDesktopChanged::Create()); 1016 file_browser_private::OnDesktopChanged::Create());
972 } 1017 }
973 1018
974 } // namespace file_manager 1019 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698