| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/id_map.h" | 11 #include "base/id_map.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/observer_list.h" |
| 13 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" | 14 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" |
| 15 #include "chrome/browser/chromeos/drive/job_list_interface.h" |
| 14 #include "chrome/browser/google_apis/drive_service_interface.h" | 16 #include "chrome/browser/google_apis/drive_service_interface.h" |
| 15 #include "chrome/browser/google_apis/drive_uploader.h" | 17 #include "chrome/browser/google_apis/drive_uploader.h" |
| 16 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
| 17 | 19 |
| 18 class Profile; | 20 class Profile; |
| 19 | 21 |
| 20 namespace drive { | 22 namespace drive { |
| 21 | 23 |
| 22 // The DriveScheduler is responsible for queuing and scheduling drive | 24 // The DriveScheduler is responsible for queuing and scheduling drive |
| 23 // operations. It is responsible for handling retry logic, rate limiting, as | 25 // operations. It is responsible for handling retry logic, rate limiting, as |
| 24 // concurrency as appropriate. | 26 // concurrency as appropriate. |
| 25 // | |
| 26 // TODO(zork): Provide an interface for querying the number of jobs, and state | |
| 27 // of each. See: crbug.com/154243 | |
| 28 class DriveScheduler | 27 class DriveScheduler |
| 29 : public net::NetworkChangeNotifier::ConnectionTypeObserver { | 28 : public net::NetworkChangeNotifier::ConnectionTypeObserver, |
| 29 public JobListInterface { |
| 30 public: | 30 public: |
| 31 // Enum representing the type of job. | |
| 32 enum JobType { | |
| 33 TYPE_GET_ABOUT_RESOURCE, | |
| 34 TYPE_GET_ACCOUNT_METADATA, | |
| 35 TYPE_GET_APP_LIST, | |
| 36 TYPE_GET_ALL_RESOURCE_LIST, | |
| 37 TYPE_GET_RESOURCE_LIST_IN_DIRECTORY, | |
| 38 TYPE_SEARCH, | |
| 39 TYPE_GET_CHANGE_LIST, | |
| 40 TYPE_CONTINUE_GET_RESOURCE_LIST, | |
| 41 TYPE_GET_RESOURCE_ENTRY, | |
| 42 TYPE_DELETE_RESOURCE, | |
| 43 TYPE_COPY_HOSTED_DOCUMENT, | |
| 44 TYPE_RENAME_RESOURCE, | |
| 45 TYPE_ADD_RESOURCE_TO_DIRECTORY, | |
| 46 TYPE_REMOVE_RESOURCE_FROM_DIRECTORY, | |
| 47 TYPE_ADD_NEW_DIRECTORY, | |
| 48 TYPE_DOWNLOAD_FILE, | |
| 49 TYPE_UPLOAD_NEW_FILE, | |
| 50 TYPE_UPLOAD_EXISTING_FILE, | |
| 51 }; | |
| 52 | |
| 53 // Current state of the job. | |
| 54 enum JobState { | |
| 55 // The job is queued, but not yet executed. | |
| 56 STATE_NONE, | |
| 57 | |
| 58 // The job is in the process of being handled. | |
| 59 STATE_RUNNING, | |
| 60 | |
| 61 // The job failed, but has been re-added to the queue. | |
| 62 STATE_RETRY, | |
| 63 }; | |
| 64 | |
| 65 // Unique ID assigned to each job. It is base::IDMap<JobInfo>::KeyType. | |
| 66 typedef int32 JobID; | |
| 67 | |
| 68 // Information about a specific job that is visible to other systems. | |
| 69 struct JobInfo { | |
| 70 explicit JobInfo(JobType in_job_type); | |
| 71 | |
| 72 // Type of the job. | |
| 73 JobType job_type; | |
| 74 | |
| 75 // Id of the job, which can be used to query or modify it. | |
| 76 JobID job_id; | |
| 77 | |
| 78 // Number of bytes completed, if applicable. | |
| 79 int completed_bytes; | |
| 80 | |
| 81 // Total bytes of this operation, if applicable. | |
| 82 int total_bytes; | |
| 83 | |
| 84 // Drive path of the file that this job acts on. | |
| 85 base::FilePath file_path; | |
| 86 | |
| 87 // Current state of the operation. | |
| 88 JobState state; | |
| 89 }; | |
| 90 | |
| 91 DriveScheduler(Profile* profile, | 31 DriveScheduler(Profile* profile, |
| 92 google_apis::DriveServiceInterface* drive_service); | 32 google_apis::DriveServiceInterface* drive_service); |
| 93 virtual ~DriveScheduler(); | 33 virtual ~DriveScheduler(); |
| 94 | 34 |
| 95 // Initializes the object. This function should be called before any | 35 // Initializes the object. This function should be called before any |
| 96 // other functions. | 36 // other functions. |
| 97 void Initialize(); | 37 void Initialize(); |
| 98 | 38 |
| 99 // Returns the list of jobs currently managed by the scheduler. | 39 // JobListInterface overrides. |
| 100 std::vector<JobInfo> GetJobInfoList(); | 40 virtual std::vector<JobInfo> GetJobInfoList() OVERRIDE; |
| 41 virtual void AddObserver(JobListObserver* observer) OVERRIDE; |
| 42 virtual void RemoveObserver(JobListObserver* observer) OVERRIDE; |
| 101 | 43 |
| 102 // Adds a GetAccountMetadata operation to the queue. | 44 // Adds a GetAccountMetadata operation to the queue. |
| 103 // |callback| must not be null. | 45 // |callback| must not be null. |
| 104 void GetAccountMetadata( | 46 void GetAccountMetadata( |
| 105 const google_apis::GetAccountMetadataCallback& callback); | 47 const google_apis::GetAccountMetadataCallback& callback); |
| 106 | 48 |
| 107 // Adds a GetAppList operation to the queue. | 49 // Adds a GetAppList operation to the queue. |
| 108 // |callback| must not be null. | 50 // |callback| must not be null. |
| 109 void GetAppList(const google_apis::GetAppListCallback& callback); | 51 void GetAppList(const google_apis::GetAppListCallback& callback); |
| 110 | 52 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 // net::NetworkChangeNotifier::ConnectionTypeObserver override. | 367 // net::NetworkChangeNotifier::ConnectionTypeObserver override. |
| 426 virtual void OnConnectionTypeChanged( | 368 virtual void OnConnectionTypeChanged( |
| 427 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 369 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 428 | 370 |
| 429 // Get the type of queue the specified job should be put in. | 371 // Get the type of queue the specified job should be put in. |
| 430 QueueType GetJobQueueType(JobType type); | 372 QueueType GetJobQueueType(JobType type); |
| 431 | 373 |
| 432 // For testing only. Disables throttling so that testing is faster. | 374 // For testing only. Disables throttling so that testing is faster. |
| 433 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } | 375 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } |
| 434 | 376 |
| 377 // Notifies updates to observers. |
| 378 void NotifyJobAdded(const JobInfo& job_info); |
| 379 void NotifyJobDone(const JobInfo& job_info); |
| 380 void NotifyJobUpdated(const JobInfo& job_info); |
| 381 |
| 435 // Number of jobs in flight for each queue. | 382 // Number of jobs in flight for each queue. |
| 436 int jobs_running_[NUM_QUEUES]; | 383 int jobs_running_[NUM_QUEUES]; |
| 437 | 384 |
| 438 // The number of times operations have failed in a row, capped at | 385 // The number of times operations have failed in a row, capped at |
| 439 // kMaxThrottleCount. This is used to calculate the delay before running the | 386 // kMaxThrottleCount. This is used to calculate the delay before running the |
| 440 // next task. | 387 // next task. |
| 441 int throttle_count_; | 388 int throttle_count_; |
| 442 | 389 |
| 443 // Disables throttling for testing. | 390 // Disables throttling for testing. |
| 444 bool disable_throttling_; | 391 bool disable_throttling_; |
| 445 | 392 |
| 446 // The queues of jobs. | 393 // The queues of jobs. |
| 447 std::list<QueueEntry*> queue_[NUM_QUEUES]; | 394 std::list<QueueEntry*> queue_[NUM_QUEUES]; |
| 448 | 395 |
| 449 // The list of unfinished (= queued or running) job info indexed by job IDs. | 396 // The list of unfinished (= queued or running) job info indexed by job IDs. |
| 450 typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; | 397 typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; |
| 451 JobIDMap job_map_; | 398 JobIDMap job_map_; |
| 452 | 399 |
| 400 // The list of observers for the scheduler. |
| 401 ObserverList<JobListObserver> observer_list_; |
| 402 |
| 453 google_apis::DriveServiceInterface* drive_service_; | 403 google_apis::DriveServiceInterface* drive_service_; |
| 454 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; | 404 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; |
| 455 | 405 |
| 456 Profile* profile_; | 406 Profile* profile_; |
| 457 | 407 |
| 458 // Whether this instance is initialized or not. | 408 // Whether this instance is initialized or not. |
| 459 bool initialized_; | 409 bool initialized_; |
| 460 | 410 |
| 461 // Note: This should remain the last member so it'll be destroyed and | 411 // Note: This should remain the last member so it'll be destroyed and |
| 462 // invalidate its weak pointers before any other members are destroyed. | 412 // invalidate its weak pointers before any other members are destroyed. |
| 463 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; | 413 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; |
| 464 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); | 414 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); |
| 465 }; | 415 }; |
| 466 | 416 |
| 467 } // namespace drive | 417 } // namespace drive |
| 468 | 418 |
| 469 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ | 419 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ |
| OLD | NEW |