Commit 46158e39 authored by kinaba@chromium.org's avatar kinaba@chromium.org

Add an observer interface for DriveScheduler.

This patch is just for setting up the interface. Actual code
to fire each event will come soon.

BUG=154243
TBR=sky@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194551 0039d316-1c4b-4281-b951-d872f2087c98
parent 74f678ed
...@@ -30,15 +30,6 @@ const int DriveScheduler::kMaxJobCount[] = { ...@@ -30,15 +30,6 @@ const int DriveScheduler::kMaxJobCount[] = {
1, // FILE_QUEUE 1, // FILE_QUEUE
}; };
DriveScheduler::JobInfo::JobInfo(JobType in_job_type)
: job_type(in_job_type),
job_id(-1),
completed_bytes(0),
total_bytes(0),
state(STATE_NONE) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
DriveScheduler::QueueEntry::QueueEntry() DriveScheduler::QueueEntry::QueueEntry()
: job_id(-1), : job_id(-1),
context(DriveClientContext(USER_INITIATED)) { context(DriveClientContext(USER_INITIATED)) {
...@@ -101,13 +92,23 @@ void DriveScheduler::Initialize() { ...@@ -101,13 +92,23 @@ void DriveScheduler::Initialize() {
initialized_ = true; initialized_ = true;
} }
std::vector<DriveScheduler::JobInfo> DriveScheduler::GetJobInfoList() { std::vector<JobInfo> DriveScheduler::GetJobInfoList() {
std::vector<JobInfo> job_info_list; std::vector<JobInfo> job_info_list;
for (JobIDMap::iterator iter(&job_map_); !iter.IsAtEnd(); iter.Advance()) for (JobIDMap::iterator iter(&job_map_); !iter.IsAtEnd(); iter.Advance())
job_info_list.push_back(*iter.GetCurrentValue()); job_info_list.push_back(*iter.GetCurrentValue());
return job_info_list; return job_info_list;
} }
void DriveScheduler::AddObserver(JobListObserver* observer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
observer_list_.AddObserver(observer);
}
void DriveScheduler::RemoveObserver(JobListObserver* observer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
observer_list_.RemoveObserver(observer);
}
void DriveScheduler::GetAccountMetadata( void DriveScheduler::GetAccountMetadata(
const google_apis::GetAccountMetadataCallback& callback) { const google_apis::GetAccountMetadataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
...@@ -903,4 +904,19 @@ DriveScheduler::QueueType DriveScheduler::GetJobQueueType(JobType type) { ...@@ -903,4 +904,19 @@ DriveScheduler::QueueType DriveScheduler::GetJobQueueType(JobType type) {
return FILE_QUEUE; return FILE_QUEUE;
} }
void DriveScheduler::NotifyJobAdded(const JobInfo& job_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobAdded(job_info));
}
void DriveScheduler::NotifyJobDone(const JobInfo& job_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobDone(job_info));
}
void DriveScheduler::NotifyJobUpdated(const JobInfo& job_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobUpdated(job_info));
}
} // namespace drive } // namespace drive
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
#include "base/id_map.h" #include "base/id_map.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "chrome/browser/chromeos/drive/drive_file_system_interface.h" #include "chrome/browser/chromeos/drive/drive_file_system_interface.h"
#include "chrome/browser/chromeos/drive/job_list_interface.h"
#include "chrome/browser/google_apis/drive_service_interface.h" #include "chrome/browser/google_apis/drive_service_interface.h"
#include "chrome/browser/google_apis/drive_uploader.h" #include "chrome/browser/google_apis/drive_uploader.h"
#include "net/base/network_change_notifier.h" #include "net/base/network_change_notifier.h"
...@@ -22,72 +24,10 @@ namespace drive { ...@@ -22,72 +24,10 @@ namespace drive {
// The DriveScheduler is responsible for queuing and scheduling drive // The DriveScheduler is responsible for queuing and scheduling drive
// operations. It is responsible for handling retry logic, rate limiting, as // operations. It is responsible for handling retry logic, rate limiting, as
// concurrency as appropriate. // concurrency as appropriate.
//
// TODO(zork): Provide an interface for querying the number of jobs, and state
// of each. See: crbug.com/154243
class DriveScheduler class DriveScheduler
: public net::NetworkChangeNotifier::ConnectionTypeObserver { : public net::NetworkChangeNotifier::ConnectionTypeObserver,
public JobListInterface {
public: public:
// Enum representing the type of job.
enum JobType {
TYPE_GET_ABOUT_RESOURCE,
TYPE_GET_ACCOUNT_METADATA,
TYPE_GET_APP_LIST,
TYPE_GET_ALL_RESOURCE_LIST,
TYPE_GET_RESOURCE_LIST_IN_DIRECTORY,
TYPE_SEARCH,
TYPE_GET_CHANGE_LIST,
TYPE_CONTINUE_GET_RESOURCE_LIST,
TYPE_GET_RESOURCE_ENTRY,
TYPE_DELETE_RESOURCE,
TYPE_COPY_HOSTED_DOCUMENT,
TYPE_RENAME_RESOURCE,
TYPE_ADD_RESOURCE_TO_DIRECTORY,
TYPE_REMOVE_RESOURCE_FROM_DIRECTORY,
TYPE_ADD_NEW_DIRECTORY,
TYPE_DOWNLOAD_FILE,
TYPE_UPLOAD_NEW_FILE,
TYPE_UPLOAD_EXISTING_FILE,
};
// Current state of the job.
enum JobState {
// The job is queued, but not yet executed.
STATE_NONE,
// The job is in the process of being handled.
STATE_RUNNING,
// The job failed, but has been re-added to the queue.
STATE_RETRY,
};
// Unique ID assigned to each job. It is base::IDMap<JobInfo>::KeyType.
typedef int32 JobID;
// Information about a specific job that is visible to other systems.
struct JobInfo {
explicit JobInfo(JobType in_job_type);
// Type of the job.
JobType job_type;
// Id of the job, which can be used to query or modify it.
JobID job_id;
// Number of bytes completed, if applicable.
int completed_bytes;
// Total bytes of this operation, if applicable.
int total_bytes;
// Drive path of the file that this job acts on.
base::FilePath file_path;
// Current state of the operation.
JobState state;
};
DriveScheduler(Profile* profile, DriveScheduler(Profile* profile,
google_apis::DriveServiceInterface* drive_service); google_apis::DriveServiceInterface* drive_service);
virtual ~DriveScheduler(); virtual ~DriveScheduler();
...@@ -96,8 +36,10 @@ class DriveScheduler ...@@ -96,8 +36,10 @@ class DriveScheduler
// other functions. // other functions.
void Initialize(); void Initialize();
// Returns the list of jobs currently managed by the scheduler. // JobListInterface overrides.
std::vector<JobInfo> GetJobInfoList(); virtual std::vector<JobInfo> GetJobInfoList() OVERRIDE;
virtual void AddObserver(JobListObserver* observer) OVERRIDE;
virtual void RemoveObserver(JobListObserver* observer) OVERRIDE;
// Adds a GetAccountMetadata operation to the queue. // Adds a GetAccountMetadata operation to the queue.
// |callback| must not be null. // |callback| must not be null.
...@@ -432,6 +374,11 @@ class DriveScheduler ...@@ -432,6 +374,11 @@ class DriveScheduler
// For testing only. Disables throttling so that testing is faster. // For testing only. Disables throttling so that testing is faster.
void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } void SetDisableThrottling(bool disable) { disable_throttling_ = disable; }
// Notifies updates to observers.
void NotifyJobAdded(const JobInfo& job_info);
void NotifyJobDone(const JobInfo& job_info);
void NotifyJobUpdated(const JobInfo& job_info);
// Number of jobs in flight for each queue. // Number of jobs in flight for each queue.
int jobs_running_[NUM_QUEUES]; int jobs_running_[NUM_QUEUES];
...@@ -450,6 +397,9 @@ class DriveScheduler ...@@ -450,6 +397,9 @@ class DriveScheduler
typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap;
JobIDMap job_map_; JobIDMap job_map_;
// The list of observers for the scheduler.
ObserverList<JobListObserver> observer_list_;
google_apis::DriveServiceInterface* drive_service_; google_apis::DriveServiceInterface* drive_service_;
scoped_ptr<google_apis::DriveUploaderInterface> uploader_; scoped_ptr<google_apis::DriveUploaderInterface> uploader_;
......
...@@ -663,24 +663,24 @@ TEST_F(DriveSchedulerTest, JobInfo) { ...@@ -663,24 +663,24 @@ TEST_F(DriveSchedulerTest, JobInfo) {
scoped_ptr<google_apis::AccountMetadata> account_metadata; scoped_ptr<google_apis::AccountMetadata> account_metadata;
base::FilePath path; base::FilePath path;
std::set<DriveScheduler::JobType> expected_types; std::set<JobType> expected_types;
// Add many jobs. // Add many jobs.
expected_types.insert(DriveScheduler::TYPE_ADD_NEW_DIRECTORY); expected_types.insert(TYPE_ADD_NEW_DIRECTORY);
scheduler_->AddNewDirectory( scheduler_->AddNewDirectory(
fake_drive_service_->GetRootResourceId(), fake_drive_service_->GetRootResourceId(),
"New Directory", "New Directory",
google_apis::test_util::CreateCopyResultCallback(&error, &entry)); google_apis::test_util::CreateCopyResultCallback(&error, &entry));
expected_types.insert(DriveScheduler::TYPE_GET_ACCOUNT_METADATA); expected_types.insert(TYPE_GET_ACCOUNT_METADATA);
scheduler_->GetAccountMetadata( scheduler_->GetAccountMetadata(
google_apis::test_util::CreateCopyResultCallback( google_apis::test_util::CreateCopyResultCallback(
&error, &account_metadata)); &error, &account_metadata));
expected_types.insert(DriveScheduler::TYPE_RENAME_RESOURCE); expected_types.insert(TYPE_RENAME_RESOURCE);
scheduler_->RenameResource( scheduler_->RenameResource(
"file:2_file_resource_id", "file:2_file_resource_id",
"New Name", "New Name",
google_apis::test_util::CreateCopyResultCallback(&error)); google_apis::test_util::CreateCopyResultCallback(&error));
expected_types.insert(DriveScheduler::TYPE_DOWNLOAD_FILE); expected_types.insert(TYPE_DOWNLOAD_FILE);
scheduler_->DownloadFile( scheduler_->DownloadFile(
base::FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path base::FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
temp_dir.path().AppendASCII("whatever.txt"), temp_dir.path().AppendASCII("whatever.txt"),
...@@ -693,21 +693,21 @@ TEST_F(DriveSchedulerTest, JobInfo) { ...@@ -693,21 +693,21 @@ TEST_F(DriveSchedulerTest, JobInfo) {
EXPECT_EQ(4U, scheduler_->GetJobInfoList().size()); EXPECT_EQ(4U, scheduler_->GetJobInfoList().size());
// Add more jobs. // Add more jobs.
expected_types.insert(DriveScheduler::TYPE_ADD_RESOURCE_TO_DIRECTORY); expected_types.insert(TYPE_ADD_RESOURCE_TO_DIRECTORY);
scheduler_->AddResourceToDirectory( scheduler_->AddResourceToDirectory(
"folder:1_folder_resource_id", "folder:1_folder_resource_id",
"file:2_file_resource_id", "file:2_file_resource_id",
google_apis::test_util::CreateCopyResultCallback(&error)); google_apis::test_util::CreateCopyResultCallback(&error));
expected_types.insert(DriveScheduler::TYPE_COPY_HOSTED_DOCUMENT); expected_types.insert(TYPE_COPY_HOSTED_DOCUMENT);
scheduler_->CopyHostedDocument( scheduler_->CopyHostedDocument(
"document:5_document_resource_id", "document:5_document_resource_id",
"New Document", "New Document",
google_apis::test_util::CreateCopyResultCallback(&error, &entry)); google_apis::test_util::CreateCopyResultCallback(&error, &entry));
// 6 jobs in total were queued. // 6 jobs in total were queued.
std::vector<DriveScheduler::JobInfo> jobs = scheduler_->GetJobInfoList(); std::vector<JobInfo> jobs = scheduler_->GetJobInfoList();
EXPECT_EQ(6U, jobs.size()); EXPECT_EQ(6U, jobs.size());
std::set<DriveScheduler::JobType> actual_types; std::set<JobType> actual_types;
for (size_t i = 0; i < jobs.size(); ++i) for (size_t i = 0; i < jobs.size(); ++i)
actual_types.insert(jobs[i].job_type); actual_types.insert(jobs[i].job_type);
EXPECT_EQ(expected_types, actual_types); EXPECT_EQ(expected_types, actual_types);
...@@ -718,7 +718,7 @@ TEST_F(DriveSchedulerTest, JobInfo) { ...@@ -718,7 +718,7 @@ TEST_F(DriveSchedulerTest, JobInfo) {
// All jobs except the BACKGROUND job should have finished. // All jobs except the BACKGROUND job should have finished.
jobs = scheduler_->GetJobInfoList(); jobs = scheduler_->GetJobInfoList();
ASSERT_EQ(1U, jobs.size()); ASSERT_EQ(1U, jobs.size());
EXPECT_EQ(DriveScheduler::TYPE_DOWNLOAD_FILE, jobs[0].job_type); EXPECT_EQ(TYPE_DOWNLOAD_FILE, jobs[0].job_type);
// Run the background downloading job as well. // Run the background downloading job as well.
ConnectToWifi(); ConnectToWifi();
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_JOB_LIST_INTERFACE_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_JOB_LIST_INTERFACE_H_
#include "base/basictypes.h"
#include "base/files/file_path.h"
namespace drive {
// Enum representing the type of job.
enum JobType {
TYPE_GET_ABOUT_RESOURCE,
TYPE_GET_ACCOUNT_METADATA,
TYPE_GET_APP_LIST,
TYPE_GET_ALL_RESOURCE_LIST,
TYPE_GET_RESOURCE_LIST_IN_DIRECTORY,
TYPE_SEARCH,
TYPE_GET_CHANGE_LIST,
TYPE_CONTINUE_GET_RESOURCE_LIST,
TYPE_GET_RESOURCE_ENTRY,
TYPE_DELETE_RESOURCE,
TYPE_COPY_HOSTED_DOCUMENT,
TYPE_RENAME_RESOURCE,
TYPE_ADD_RESOURCE_TO_DIRECTORY,
TYPE_REMOVE_RESOURCE_FROM_DIRECTORY,
TYPE_ADD_NEW_DIRECTORY,
TYPE_DOWNLOAD_FILE,
TYPE_UPLOAD_NEW_FILE,
TYPE_UPLOAD_EXISTING_FILE,
};
// Current state of the job.
enum JobState {
// The job is queued, but not yet executed.
STATE_NONE,
// The job is in the process of being handled.
STATE_RUNNING,
// The job failed, but has been re-added to the queue.
STATE_RETRY,
};
// Unique ID assigned to each job.
typedef int32 JobID;
// Information about a specific job that is visible to other systems.
struct JobInfo {
explicit JobInfo(JobType in_job_type)
: job_type(in_job_type),
job_id(-1),
num_completed_bytes(0),
num_total_bytes(0),
state(STATE_NONE) {
}
// Type of the job.
JobType job_type;
// Id of the job, which can be used to query or modify it.
JobID job_id;
// Number of bytes completed, if applicable.
int64 num_completed_bytes;
// Total bytes of this operation, if applicable.
int64 num_total_bytes;
// Drive path of the file that this job acts on.
base::FilePath file_path;
// Current state of the operation.
JobState state;
};
// The interface for observing JobListInterface.
// All events are notified in the UI thread.
class JobListObserver {
public:
// Called when a new job id added.
virtual void OnJobAdded(const JobInfo& job_info) {}
// Called when a job id finished.
virtual void OnJobDone(const JobInfo& job_info) {}
// Called when a job status is updated.
virtual void OnJobUpdated(const JobInfo& job_info) {}
protected:
virtual ~JobListObserver() {}
};
// The interface to expose the list of issued Drive jobs.
class JobListInterface {
public:
virtual ~JobListInterface() {}
// Returns the list of jobs currently managed by the scheduler.
virtual std::vector<JobInfo> GetJobInfoList() = 0;
// Adds an observer.
virtual void AddObserver(JobListObserver* observer) = 0;
// Removes an observer.
virtual void RemoveObserver(JobListObserver* observer) = 0;
};
} // namespace drive
#endif // CHROME_BROWSER_CHROMEOS_DRIVE_JOB_LIST_INTERFACE_H_
...@@ -267,6 +267,7 @@ ...@@ -267,6 +267,7 @@
'browser/chromeos/drive/file_system/update_operation.h', 'browser/chromeos/drive/file_system/update_operation.h',
'browser/chromeos/drive/file_write_helper.cc', 'browser/chromeos/drive/file_write_helper.cc',
'browser/chromeos/drive/file_write_helper.h', 'browser/chromeos/drive/file_write_helper.h',
'browser/chromeos/drive/job_list_interface.h',
'browser/chromeos/drive/resource_entry_conversion.cc', 'browser/chromeos/drive/resource_entry_conversion.cc',
'browser/chromeos/drive/resource_entry_conversion.h', 'browser/chromeos/drive/resource_entry_conversion.h',
'browser/chromeos/drive/search_metadata.cc', 'browser/chromeos/drive/search_metadata.cc',
......
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