Chromium Code Reviews| 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 "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" | 10 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 // concurrency as appropriate. | 25 // concurrency as appropriate. |
| 26 // | 26 // |
| 27 // TODO(zork): Provide an interface for querying the number of jobs, and state | 27 // TODO(zork): Provide an interface for querying the number of jobs, and state |
| 28 // of each. See: crbug.com/154243 | 28 // of each. See: crbug.com/154243 |
| 29 class DriveScheduler | 29 class DriveScheduler |
| 30 : public net::NetworkChangeNotifier::ConnectionTypeObserver { | 30 : public net::NetworkChangeNotifier::ConnectionTypeObserver { |
| 31 public: | 31 public: |
| 32 | 32 |
| 33 // Enum representing the type of job. | 33 // Enum representing the type of job. |
| 34 enum JobType { | 34 enum JobType { |
| 35 TYPE_MOVE, | |
| 35 TYPE_REMOVE, | 36 TYPE_REMOVE, |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 // Current state of the job. | 39 // Current state of the job. |
| 39 enum JobState { | 40 enum JobState { |
| 40 // The job is queued, but not yet executed. | 41 // The job is queued, but not yet executed. |
| 41 STATE_NONE, | 42 STATE_NONE, |
| 42 | 43 |
| 43 // The job is in the process of being handled. | 44 // The job is in the process of being handled. |
| 44 STATE_RUNNING, | 45 STATE_RUNNING, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 DriveScheduler(Profile* profile, | 74 DriveScheduler(Profile* profile, |
| 74 file_system::DriveOperations* drive_operations); | 75 file_system::DriveOperations* drive_operations); |
| 75 virtual ~DriveScheduler(); | 76 virtual ~DriveScheduler(); |
| 76 | 77 |
| 77 // Initializes the object. This function should be called before any | 78 // Initializes the object. This function should be called before any |
| 78 // other functions. | 79 // other functions. |
| 79 void Initialize(); | 80 void Initialize(); |
| 80 | 81 |
| 82 // Adds a move operation to the queue. | |
| 83 void Move(const FilePath& src_file_path, | |
| 84 const FilePath& dest_file_path, | |
| 85 const FileOperationCallback& callback); | |
| 86 | |
| 81 // Adds a remove operation to the queue. | 87 // Adds a remove operation to the queue. |
| 82 void Remove(const FilePath& file_path, | 88 void Remove(const FilePath& file_path, |
| 83 bool is_recursive, | 89 bool is_recursive, |
| 84 const FileOperationCallback& callback); | 90 const FileOperationCallback& callback); |
| 85 | 91 |
| 86 private: | 92 private: |
| 87 friend class DriveSchedulerTest; | 93 friend class DriveSchedulerTest; |
| 88 | 94 |
| 89 // Data specific to a remove operation. | |
| 90 struct RemoveJobPrivate { | |
| 91 RemoveJobPrivate(bool in_is_recursive, FileOperationCallback in_callback); | |
| 92 ~RemoveJobPrivate(); | |
| 93 | |
| 94 bool is_recursive; | |
| 95 FileOperationCallback callback; | |
| 96 }; | |
| 97 | |
| 98 // Represents a single entry in the job queue. | 95 // Represents a single entry in the job queue. |
| 99 struct QueueEntry { | 96 struct QueueEntry { |
| 100 QueueEntry(JobType in_job_type, FilePath in_file_path); | 97 QueueEntry(JobType in_job_type, |
| 98 FilePath in_file_path, | |
| 99 FileOperationCallback in_callback); | |
| 101 ~QueueEntry(); | 100 ~QueueEntry(); |
| 102 | 101 |
| 103 JobInfo job_info; | 102 JobInfo job_info; |
| 104 | 103 |
| 105 scoped_ptr<RemoveJobPrivate> remove_private; | 104 // Callback for when the operation completes. |
| 105 // Used by: TYPE_MOVE, TYPE_REMOVE | |
| 106 FileOperationCallback callback; | |
| 107 | |
| 108 // Destination of the operation. Used by: TYPE_MOVE | |
| 109 FilePath dest_file_path; | |
| 110 | |
| 111 // Whether the operation is recursive. Used by: TYPE_REMOVE | |
| 112 bool is_recursive; | |
| 106 }; | 113 }; |
| 107 | 114 |
| 108 // Adds the specified job to the queue. Takes ownership of |job| | 115 // Adds the specified job to the queue. Takes ownership of |job| |
| 109 int QueueJob(QueueEntry* job); | 116 int QueueJob(QueueEntry* job); |
| 110 | 117 |
| 111 // Starts the job loop, if it is not already running. | 118 // Starts the job loop, if it is not already running. |
| 112 void StartJobLoop(); | 119 void StartJobLoop(); |
| 113 | 120 |
| 114 // Detemines the next job that should run, and starts it. | 121 // Detemines the next job that should run, and starts it. |
| 115 void DoJobLoop(); | 122 void DoJobLoop(); |
| 116 | 123 |
| 117 // Checks if operations should be suspended, such as if the network is | 124 // Checks if operations should be suspended, such as if the network is |
| 118 // disconnected. | 125 // disconnected. |
| 119 // | 126 // |
| 120 // Returns true when it should stop, and false if it should continue. | 127 // Returns true when it should stop, and false if it should continue. |
| 121 bool ShouldStopJobLoop(); | 128 bool ShouldStopJobLoop(); |
| 122 | 129 |
| 123 // Increases the throttle delay if it's below the maximum value, and posts a | 130 // Increases the throttle delay if it's below the maximum value, and posts a |
| 124 // task to continue the loop after the delay. | 131 // task to continue the loop after the delay. |
| 125 void ThrottleAndContinueJobLoop(); | 132 void ThrottleAndContinueJobLoop(); |
| 126 | 133 |
| 127 // Resets the throttle delay to the initial value, and continues the job loop. | 134 // Resets the throttle delay to the initial value, and continues the job loop. |
| 128 void ResetThrottleAndContinueJobLoop(); | 135 void ResetThrottleAndContinueJobLoop(); |
| 129 | 136 |
| 130 // Callback for job of TYPE_REMOVE finishing. Retries the job if needed, | 137 // Callback for job finishing. Retries the job if needed, otherwise cleans up |
| 131 // otherwise cleans up the job, invokes the callback, and continues the job | 138 // the job, invokes the callback, and continues the job loop. |
| 132 // loop. | 139 void OnJobDone(int job_id, DriveFileError error); |
| 133 void OnRemoveDone(int job_id, DriveFileError error); | |
| 134 | 140 |
| 135 // net::NetworkChangeNotifier::ConnectionTypeObserver override. | 141 // net::NetworkChangeNotifier::ConnectionTypeObserver override. |
| 136 virtual void OnConnectionTypeChanged( | 142 virtual void OnConnectionTypeChanged( |
| 137 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 143 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 138 | 144 |
| 139 // For testing only. Disables throttling so that testing is faster. | 145 // For testing only. Disables throttling so that testing is faster. |
| 140 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } | 146 void SetDisableThrottling(bool disable) { disable_throttling_ = disable; } |
| 141 | 147 |
| 142 // True when there is a job running. Inidicates that new jobs should wait to | 148 // True when there is a job running. Inidicates that new jobs should wait to |
| 143 // be executed. | 149 // be executed. |
| 144 bool job_loop_is_running_; | 150 bool job_loop_is_running_; |
| 145 | 151 |
| 146 // Next value that should be assigned as a job id. | 152 // Next value that should be assigned as a job id. |
| 147 int next_job_id_; | 153 int next_job_id_; |
| 148 | 154 |
| 149 // The number of times operations have failed in a row, capped at | 155 // The number of times operations have failed in a row, capped at |
| 150 // kMaxThrottleCount. This is used to calculate the delay before running the | 156 // kMaxThrottleCount. This is used to calculate the delay before running the |
| 151 // next task. | 157 // next task. |
| 152 int throttle_count_; | 158 int throttle_count_; |
| 153 | 159 |
| 154 // Disables throttling for testing. | 160 // Disables throttling for testing. |
| 155 bool disable_throttling_; | 161 bool disable_throttling_; |
| 156 | 162 |
| 157 // Mapping of id to QueueEntry. | 163 // Mapping of id to QueueEntry. |
| 158 typedef std::map<int, linked_ptr<QueueEntry> > JobMap; | 164 typedef std::map<int, linked_ptr<QueueEntry> > JobMap; |
| 159 JobMap job_info_; | 165 JobMap job_info_; |
|
satorux1
2012/10/16 04:34:01
job_info_map_ would be more descriptive.
Zachary Kuznia
2012/10/16 04:59:10
Done.
| |
| 160 | 166 |
| 161 // The queue of jobs id. Sorted by priority. | 167 // The queue of jobs id. Sorted by priority. |
| 162 std::deque<int> queue_; | 168 std::deque<int> queue_; |
| 163 | 169 |
| 164 // Drive operations. | 170 // Drive operations. |
| 165 file_system::DriveOperations* drive_operations_; | 171 file_system::DriveOperations* drive_operations_; |
| 166 | 172 |
| 167 Profile* profile_; | 173 Profile* profile_; |
| 168 | 174 |
| 169 // Note: This should remain the last member so it'll be destroyed and | 175 // Note: This should remain the last member so it'll be destroyed and |
| 170 // invalidate its weak pointers before any other members are destroyed. | 176 // invalidate its weak pointers before any other members are destroyed. |
| 171 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; | 177 base::WeakPtrFactory<DriveScheduler> weak_ptr_factory_; |
| 172 | 178 |
| 173 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); | 179 DISALLOW_COPY_AND_ASSIGN(DriveScheduler); |
| 174 }; | 180 }; |
| 175 | 181 |
| 176 } // namespace drive | 182 } // namespace drive |
| 177 | 183 |
| 178 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ | 184 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_SCHEDULER_H_ |
| OLD | NEW |