Commit 68954c5c authored by hirono's avatar hirono Committed by Commit bot

Files.app: Replace job related code in EventRouter with new JobEventRoter class.

BUG=469039
TEST=None

Review URL: https://codereview.chromium.org/1023093002

Cr-Commit-Position: refs/heads/master@{#321542}
parent cfc48a48
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "chrome/browser/chromeos/drive/file_change.h" #include "chrome/browser/chromeos/drive/file_change.h"
#include "chrome/browser/chromeos/drive/file_system_interface.h" #include "chrome/browser/chromeos/drive/file_system_interface.h"
#include "chrome/browser/chromeos/drive/file_system_util.h" #include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h"
#include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
#include "chrome/browser/chromeos/file_manager/app_id.h" #include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h" #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
...@@ -59,12 +58,6 @@ namespace file_manager_private = extensions::api::file_manager_private; ...@@ -59,12 +58,6 @@ namespace file_manager_private = extensions::api::file_manager_private;
namespace file_manager { namespace file_manager {
namespace { namespace {
// Constants for the "transferState" field of onFileTransferUpdated event.
const char kFileTransferStateAdded[] = "added";
const char kFileTransferStateStarted[] = "started";
const char kFileTransferStateInProgress[] = "in_progress";
const char kFileTransferStateCompleted[] = "completed";
const char kFileTransferStateFailed[] = "failed";
// Frequency of sending onFileTransferUpdated. // Frequency of sending onFileTransferUpdated.
const int64 kProgressEventFrequencyInMilliseconds = 1000; const int64 kProgressEventFrequencyInMilliseconds = 1000;
...@@ -77,53 +70,6 @@ const size_t kDirectoryChangeEventMaxDetailInfoSize = 1000; ...@@ -77,53 +70,6 @@ const size_t kDirectoryChangeEventMaxDetailInfoSize = 1000;
// This time(millisecond) is used for confirm following event exists. // This time(millisecond) is used for confirm following event exists.
const int64 kFileTransferEventDelayTimeInMilliseconds = 300; const int64 kFileTransferEventDelayTimeInMilliseconds = 300;
// Utility function to check if |job_info| is a file uploading job.
bool IsUploadJob(drive::JobType type) {
return (type == drive::TYPE_UPLOAD_NEW_FILE ||
type == drive::TYPE_UPLOAD_EXISTING_FILE);
}
size_t CountActiveFileTransferJobInfo(
const std::vector<drive::JobInfo>& job_info_list) {
size_t num_active_file_transfer_job_info = 0;
for (size_t i = 0; i < job_info_list.size(); ++i) {
if (IsActiveFileTransferJobInfo(job_info_list[i]))
++num_active_file_transfer_job_info;
}
return num_active_file_transfer_job_info;
}
// Converts the job info to a IDL generated type.
void JobInfoToTransferStatus(
Profile* profile,
const std::string& extension_id,
const std::string& job_status,
const drive::JobInfo& job_info,
file_manager_private::FileTransferStatus* status) {
DCHECK(IsActiveFileTransferJobInfo(job_info));
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
GURL url = util::ConvertDrivePathToFileSystemUrl(
profile, job_info.file_path, extension_id);
status->file_url = url.spec();
status->transfer_state = file_manager_private::ParseTransferState(job_status);
status->transfer_type =
IsUploadJob(job_info.job_type) ?
file_manager_private::TRANSFER_TYPE_UPLOAD :
file_manager_private::TRANSFER_TYPE_DOWNLOAD;
DriveIntegrationService* const integration_service =
DriveIntegrationServiceFactory::FindForProfile(profile);
status->num_total_jobs = CountActiveFileTransferJobInfo(
integration_service->job_list()->GetJobInfoList());
// JavaScript does not have 64-bit integers. Instead we use double, which
// is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice
// in C++. Larger values are rounded.
status->processed.reset(
new double(static_cast<double>(job_info.num_completed_bytes)));
status->total.reset(
new double(static_cast<double>(job_info.num_total_bytes)));
}
// Checks if the Recovery Tool is running. This is a temporary solution. // Checks if the Recovery Tool is running. This is a temporary solution.
// TODO(mtomasz): Replace with crbug.com/341902 solution. // TODO(mtomasz): Replace with crbug.com/341902 solution.
bool IsRecoveryToolRunning(Profile* profile) { bool IsRecoveryToolRunning(Profile* profile) {
...@@ -364,22 +310,37 @@ class DeviceEventRouterImpl : public DeviceEventRouter { ...@@ -364,22 +310,37 @@ class DeviceEventRouterImpl : public DeviceEventRouter {
DISALLOW_COPY_AND_ASSIGN(DeviceEventRouterImpl); DISALLOW_COPY_AND_ASSIGN(DeviceEventRouterImpl);
}; };
} // namespace class JobEventRouterImpl : public JobEventRouter {
public:
explicit JobEventRouterImpl(Profile* profile)
: JobEventRouter(base::TimeDelta::FromMilliseconds(
kFileTransferEventDelayTimeInMilliseconds)),
profile_(profile) {}
// Pass dummy value to JobInfo's constructor for make it default constructible. protected:
EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus() GURL ConvertDrivePathToFileSystemUrl(const base::FilePath& path,
: job_info(drive::TYPE_DOWNLOAD_FILE) { const std::string& id) const override {
} return file_manager::util::ConvertDrivePathToFileSystemUrl(profile_, path,
id);
}
void BroadcastEvent(const std::string& event_name,
scoped_ptr<base::ListValue> event_args) override {
::file_manager::BroadcastEvent(profile_, event_name, event_args.Pass());
}
EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( private:
const drive::JobInfo& info, const std::string& status) Profile* const profile_;
: job_info(info), status(status) {
} DISALLOW_COPY_AND_ASSIGN(JobEventRouterImpl);
};
} // namespace
EventRouter::EventRouter(Profile* profile) EventRouter::EventRouter(Profile* profile)
: pref_change_registrar_(new PrefChangeRegistrar), : pref_change_registrar_(new PrefChangeRegistrar),
profile_(profile), profile_(profile),
device_event_router_(new DeviceEventRouterImpl(profile)), device_event_router_(new DeviceEventRouterImpl(profile)),
job_event_router_(new JobEventRouterImpl(profile)),
dispatch_directory_change_event_impl_( dispatch_directory_change_event_impl_(
base::Bind(&EventRouter::DispatchDirectoryChangeEventImpl, base::Bind(&EventRouter::DispatchDirectoryChangeEventImpl,
base::Unretained(this))), base::Unretained(this))),
...@@ -415,7 +376,7 @@ void EventRouter::Shutdown() { ...@@ -415,7 +376,7 @@ void EventRouter::Shutdown() {
if (integration_service) { if (integration_service) {
integration_service->file_system()->RemoveObserver(this); integration_service->file_system()->RemoveObserver(this);
integration_service->drive_service()->RemoveObserver(this); integration_service->drive_service()->RemoveObserver(this);
integration_service->job_list()->RemoveObserver(this); integration_service->job_list()->RemoveObserver(job_event_router_.get());
} }
VolumeManager* const volume_manager = VolumeManager::Get(profile_); VolumeManager* const volume_manager = VolumeManager::Get(profile_);
...@@ -462,7 +423,7 @@ void EventRouter::ObserveEvents() { ...@@ -462,7 +423,7 @@ void EventRouter::ObserveEvents() {
if (integration_service) { if (integration_service) {
integration_service->drive_service()->AddObserver(this); integration_service->drive_service()->AddObserver(this);
integration_service->file_system()->AddObserver(this); integration_service->file_system()->AddObserver(this);
integration_service->job_list()->AddObserver(this); integration_service->job_list()->AddObserver(job_event_router_.get());
} }
if (NetworkHandler::IsInitialized()) { if (NetworkHandler::IsInitialized()) {
...@@ -632,84 +593,6 @@ void EventRouter::OnFileManagerPrefsChanged() { ...@@ -632,84 +593,6 @@ void EventRouter::OnFileManagerPrefsChanged() {
file_manager_private::OnPreferencesChanged::Create()); file_manager_private::OnPreferencesChanged::Create());
} }
void EventRouter::OnJobAdded(const drive::JobInfo& job_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!drive::IsActiveFileTransferJobInfo(job_info))
return;
ScheduleDriveFileTransferEvent(
job_info, kFileTransferStateAdded, false /* immediate */);
}
void EventRouter::OnJobUpdated(const drive::JobInfo& job_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!drive::IsActiveFileTransferJobInfo(job_info))
return;
bool is_new_job = (drive_jobs_.find(job_info.job_id) == drive_jobs_.end());
const std::string status =
is_new_job ? kFileTransferStateStarted : kFileTransferStateInProgress;
// Replace with the latest job info.
drive_jobs_[job_info.job_id] = DriveJobInfoWithStatus(job_info, status);
ScheduleDriveFileTransferEvent(job_info, status, false /* immediate */);
}
void EventRouter::OnJobDone(const drive::JobInfo& job_info,
drive::FileError error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!drive::IsActiveFileTransferJobInfo(job_info))
return;
const std::string status = error == drive::FILE_ERROR_OK
? kFileTransferStateCompleted
: kFileTransferStateFailed;
ScheduleDriveFileTransferEvent(job_info, status, true /* immediate */);
// Forget about the job.
drive_jobs_.erase(job_info.job_id);
}
void EventRouter::ScheduleDriveFileTransferEvent(const drive::JobInfo& job_info,
const std::string& status,
bool immediate) {
const bool no_pending_task = !drive_job_info_for_scheduled_event_;
// Update the latest event.
drive_job_info_for_scheduled_event_.reset(
new DriveJobInfoWithStatus(job_info, status));
if (immediate) {
SendDriveFileTransferEvent();
} else if (no_pending_task) {
const base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
kFileTransferEventDelayTimeInMilliseconds);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&EventRouter::SendDriveFileTransferEvent,
weak_factory_.GetWeakPtr()),
delay);
}
}
void EventRouter::SendDriveFileTransferEvent() {
if (!drive_job_info_for_scheduled_event_)
return;
file_manager_private::FileTransferStatus status;
JobInfoToTransferStatus(profile_,
kFileManagerAppId,
drive_job_info_for_scheduled_event_->status,
drive_job_info_for_scheduled_event_->job_info,
&status);
drive_job_info_for_scheduled_event_.reset();
BroadcastEvent(profile_,
file_manager_private::OnFileTransfersUpdated::kEventName,
file_manager_private::OnFileTransfersUpdated::Create(status));
}
void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) {
HandleFileWatchNotification(NULL, drive_path, false); HandleFileWatchNotification(NULL, drive_path, false);
} }
......
...@@ -15,8 +15,9 @@ ...@@ -15,8 +15,9 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h" #include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/file_system_observer.h" #include "chrome/browser/chromeos/drive/file_system_observer.h"
#include "chrome/browser/chromeos/drive/job_list.h"
#include "chrome/browser/chromeos/drive/sync_client.h" #include "chrome/browser/chromeos/drive/sync_client.h"
#include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h"
#include "chrome/browser/chromeos/extensions/file_manager/job_event_router.h"
#include "chrome/browser/chromeos/file_manager/file_watcher.h" #include "chrome/browser/chromeos/file_manager/file_watcher.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h" #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/volume_manager.h" #include "chrome/browser/chromeos/file_manager/volume_manager.h"
...@@ -46,14 +47,12 @@ class FileChange; ...@@ -46,14 +47,12 @@ class FileChange;
} }
namespace file_manager { namespace file_manager {
class DeviceEventRouter;
// Monitors changes in disk mounts, network connection state and preferences // Monitors changes in disk mounts, network connection state and preferences
// affecting File Manager. Dispatches appropriate File Browser events. // affecting File Manager. Dispatches appropriate File Browser events.
class EventRouter : public KeyedService, class EventRouter : public KeyedService,
public chromeos::NetworkStateHandlerObserver, public chromeos::NetworkStateHandlerObserver,
public drive::FileSystemObserver, public drive::FileSystemObserver,
public drive::JobListObserver,
public drive::DriveServiceObserver, public drive::DriveServiceObserver,
public VolumeManagerObserver { public VolumeManagerObserver {
public: public:
...@@ -112,12 +111,6 @@ class EventRouter : public KeyedService, ...@@ -112,12 +111,6 @@ class EventRouter : public KeyedService,
// chromeos::NetworkStateHandlerObserver overrides. // chromeos::NetworkStateHandlerObserver overrides.
void DefaultNetworkChanged(const chromeos::NetworkState* network) override; void DefaultNetworkChanged(const chromeos::NetworkState* network) override;
// drive::JobListObserver overrides.
void OnJobAdded(const drive::JobInfo& job_info) override;
void OnJobUpdated(const drive::JobInfo& job_info) override;
void OnJobDone(const drive::JobInfo& job_info,
drive::FileError error) override;
// drive::DriveServiceObserver overrides. // drive::DriveServiceObserver overrides.
void OnRefreshTokenInvalid() override; void OnRefreshTokenInvalid() override;
void OnReadyToSendRequests() override; void OnReadyToSendRequests() override;
...@@ -197,15 +190,6 @@ class EventRouter : public KeyedService, ...@@ -197,15 +190,6 @@ class EventRouter : public KeyedService,
void ShowRemovableDeviceInFileManager(VolumeType type, void ShowRemovableDeviceInFileManager(VolumeType type,
const base::FilePath& mount_path); const base::FilePath& mount_path);
// Manages the list of currently active Drive file transfer jobs.
struct DriveJobInfoWithStatus {
DriveJobInfoWithStatus();
DriveJobInfoWithStatus(const drive::JobInfo& info,
const std::string& status);
drive::JobInfo job_info;
std::string status;
};
// Sends onFileTransferUpdate event right now if |immediate| is set. Otherwise // Sends onFileTransferUpdate event right now if |immediate| is set. Otherwise
// it refrains from sending for a short while, and after that it sends the // it refrains from sending for a short while, and after that it sends the
// most recently scheduled event once. // most recently scheduled event once.
...@@ -221,16 +205,14 @@ class EventRouter : public KeyedService, ...@@ -221,16 +205,14 @@ class EventRouter : public KeyedService,
// This is used for implementing ScheduledDriveFileTransferEvent(). // This is used for implementing ScheduledDriveFileTransferEvent().
void SendDriveFileTransferEvent(); void SendDriveFileTransferEvent();
std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
scoped_ptr<DriveJobInfoWithStatus> drive_job_info_for_scheduled_event_;
base::Time last_copy_progress_event_; base::Time last_copy_progress_event_;
base::Time next_send_file_transfer_event_;
WatcherMap file_watchers_; WatcherMap file_watchers_;
scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
Profile* profile_; Profile* profile_;
scoped_ptr<DeviceEventRouter> device_event_router_; scoped_ptr<DeviceEventRouter> device_event_router_;
scoped_ptr<JobEventRouter> job_event_router_;
DispatchDirectoryChangeEventImplCallback DispatchDirectoryChangeEventImplCallback
dispatch_directory_change_event_impl_; dispatch_directory_change_event_impl_;
......
...@@ -124,8 +124,8 @@ void JobEventRouter::SendDriveFileTransferEvent() { ...@@ -124,8 +124,8 @@ void JobEventRouter::SendDriveFileTransferEvent() {
// is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice // is in IEEE 754 formant and accurate up to 52-bits in JS, and in practice
// in C++. Larger values are rounded. // in C++. Larger values are rounded.
pending_event_->num_total_jobs = drive_jobs_.size(); pending_event_->num_total_jobs = drive_jobs_.size();
pending_event_->processed.reset(new double(num_completed_bytes_)); pending_event_->processed = num_completed_bytes_;
pending_event_->total.reset(new double(num_total_bytes_)); pending_event_->total = num_total_bytes_;
BroadcastEvent( BroadcastEvent(
file_manager_private::OnFileTransfersUpdated::kEventName, file_manager_private::OnFileTransfersUpdated::kEventName,
......
...@@ -49,7 +49,7 @@ enum MountCompletedStatus { ...@@ -49,7 +49,7 @@ enum MountCompletedStatus {
}; };
// File transfer progress state. // File transfer progress state.
enum TransferState { added, started, in_progress, completed, failed }; enum TransferState { in_progress, completed, failed };
// Defines file transfer direction. // Defines file transfer direction.
enum TransferType { upload, download }; enum TransferType { upload, download };
...@@ -352,10 +352,10 @@ dictionary FileTransferStatus { ...@@ -352,10 +352,10 @@ dictionary FileTransferStatus {
TransferType transferType; TransferType transferType;
// Approximated completed portion of the transfer operation. // Approximated completed portion of the transfer operation.
double? processed; double processed;
// Approximated total size of transfer operation. // Approximated total size of transfer operation.
double? total; double total;
// Total number of jobs. // Total number of jobs.
long num_total_jobs; long num_total_jobs;
......
...@@ -89,8 +89,8 @@ var MountCompletedEvent; ...@@ -89,8 +89,8 @@ var MountCompletedEvent;
* fileUrl: string, * fileUrl: string,
* transferState: string, * transferState: string,
* transferType: string, * transferType: string,
* processed: (number|undefined), * processed: number,
* total: (number|undefined), * total: number,
* num_total_jobs: number * num_total_jobs: number
* }} * }}
*/ */
......
...@@ -141,9 +141,7 @@ DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() { ...@@ -141,9 +141,7 @@ DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {
*/ */
DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) { DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
switch (status.transferState) { switch (status.transferState) {
case 'added':
case 'in_progress': case 'in_progress':
case 'started':
this.updateItem_(status); this.updateItem_(status);
break; break;
case 'completed': case 'completed':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment