| 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/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 // | 27 // |
| 26 // TODO(zork): Provide an interface for querying the number of jobs, and state | 28 // TODO(zork): Provide an interface for querying the number of jobs, and state |
| 27 // of each. See: crbug.com/154243 | 29 // of each. See: crbug.com/154243 |
| 28 class DriveScheduler | 30 class DriveScheduler |
| 29 : public net::NetworkChangeNotifier::ConnectionTypeObserver { | 31 : public net::NetworkChangeNotifier::ConnectionTypeObserver, |
| 32 public DriveJobListInterface { |
| 30 public: | 33 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, | 34 DriveScheduler(Profile* profile, |
| 92 google_apis::DriveServiceInterface* drive_service); | 35 google_apis::DriveServiceInterface* drive_service); |
| 93 virtual ~DriveScheduler(); | 36 virtual ~DriveScheduler(); |
| 94 | 37 |
| 95 // Initializes the object. This function should be called before any | 38 // Initializes the object. This function should be called before any |
| 96 // other functions. | 39 // other functions. |
| 97 void Initialize(); | 40 void Initialize(); |
| 98 | 41 |
| 99 // Returns the list of jobs currently managed by the scheduler. | 42 // DriveJobListInterface overrides. |
| 100 std::vector<JobInfo> GetJobInfoList(); | 43 virtual std::vector<JobInfo> GetJobInfoList() OVERRIDE; |
| 44 virtual void AddObserver(DriveJobListObserver* observer) OVERRIDE; |
| 45 virtual void RemoveObserver(DriveJobListObserver* observer) OVERRIDE; |
| 101 | 46 |
| 102 // Adds a GetAccountMetadata operation to the queue. | 47 // Adds a GetAccountMetadata operation to the queue. |
| 103 // |callback| must not be null. | 48 // |callback| must not be null. |
| 104 void GetAccountMetadata( | 49 void GetAccountMetadata( |
| 105 const google_apis::GetAccountMetadataCallback& callback); | 50 const google_apis::GetAccountMetadataCallback& callback); |
| 106 | 51 |
| 107 // Adds a GetAppList operation to the queue. | 52 // Adds a GetAppList operation to the queue. |
| 108 // |callback| must not be null. | 53 // |callback| must not be null. |
| 109 void GetAppList(const google_apis::GetAppListCallback& callback); | 54 void GetAppList(const google_apis::GetAppListCallback& callback); |
| 110 | 55 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 // net::NetworkChangeNotifier::ConnectionTypeObserver override. | 370 // net::NetworkChangeNotifier::ConnectionTypeObserver override. |
| 426 virtual void OnConnectionTypeChanged( | 371 virtual void OnConnectionTypeChanged( |
| 427 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 372 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 428 | 373 |
| 429 // Get the type of queue the specified job should be put in. | 374 // Get the type of queue the specified job should be put in. |
| 430 QueueType GetJobQueueType(JobType type); | 375 QueueType GetJobQueueType(JobType type); |
| 431 | 376 |
| 432 // For testing only. Disables throttling so that testing is faster. | 377 // For testing only. Disables throttling so that testing is faster. |
| 433 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } | 378 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } |
| 434 | 379 |
| 380 // Notifies updates to observers. |
| 381 void NotifyJobAdded(const JobInfo& job_id); |
| 382 void NotifyJobDone(const JobInfo& job_id); |
| 383 void NotifyJobUpdated(const JobInfo& job_id); |
| 384 |
| 435 // Number of jobs in flight for each queue. | 385 // Number of jobs in flight for each queue. |
| 436 int jobs_running_[NUM_QUEUES]; | 386 int jobs_running_[NUM_QUEUES]; |
| 437 | 387 |
| 438 // The number of times operations have failed in a row, capped at | 388 // The number of times operations have failed in a row, capped at |
| 439 // kMaxThrottleCount. This is used to calculate the delay before running the | 389 // kMaxThrottleCount. This is used to calculate the delay before running the |
| 440 // next task. | 390 // next task. |
| 441 int throttle_count_; | 391 int throttle_count_; |
| 442 | 392 |
| 443 // Disables throttling for testing. | 393 // Disables throttling for testing. |
| 444 bool disable_throttling_; | 394 bool disable_throttling_; |
| 445 | 395 |
| 446 // The queues of jobs. | 396 // The queues of jobs. |
| 447 std::list<QueueEntry*> queue_[NUM_QUEUES]; | 397 std::list<QueueEntry*> queue_[NUM_QUEUES]; |
| 448 | 398 |
| 449 // The list of unfinished (= queued or running) job info indexed by job IDs. | 399 // The list of unfinished (= queued or running) job info indexed by job IDs. |
| 450 typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; | 400 typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; |
| 451 JobIDMap job_map_; | 401 JobIDMap job_map_; |
| 452 | 402 |
| 403 // The list of observers for the scheduler. |
| 404 ObserverList<DriveJobListObserver> observer_list_; |
| 405 |
| 453 google_apis::DriveServiceInterface* drive_service_; | 406 google_apis::DriveServiceInterface* drive_service_; |
| 454 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; | 407 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; |
| 455 | 408 |
| 456 Profile* profile_; | 409 Profile* profile_; |
| 457 | 410 |
| 458 // Whether this instance is initialized or not. | 411 // Whether this instance is initialized or not. |
| 459 bool initialized_; | 412 bool initialized_; |
| 460 | 413 |
| 461 // Note: This should remain the last member so it'll be destroyed and | 414 // Note: This should remain the last member so it'll be destroyed and |
| 462 // invalidate its weak pointers before any other members are destroyed. | 415 // invalidate its weak pointers before any other members are destroyed. |
| 463 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; | 416 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; |
| 464 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); | 417 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); |
| 465 }; | 418 }; |
| 466 | 419 |
| 467 } // namespace drive | 420 } // namespace drive |
| 468 | 421 |
| 469 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ | 422 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ |
| OLD | NEW |