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_JOB_SCHEDULER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_JOB_SCHEDULER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_JOB_SCHEDULER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_JOB_SCHEDULER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 friend class JobSchedulerTest; | 156 friend class JobSchedulerTest; |
157 | 157 |
158 enum QueueType { | 158 enum QueueType { |
159 METADATA_QUEUE, | 159 METADATA_QUEUE, |
160 FILE_QUEUE, | 160 FILE_QUEUE, |
161 NUM_QUEUES | 161 NUM_QUEUES |
162 }; | 162 }; |
163 | 163 |
164 static const int kMaxJobCount[NUM_QUEUES]; | 164 static const int kMaxJobCount[NUM_QUEUES]; |
165 | 165 |
166 // Represents a single entry in the job queue. | 166 // Represents a single entry in the job map. |
167 struct QueueEntry { | 167 struct JobEntry { |
168 QueueEntry(); | 168 explicit JobEntry(JobType type); |
169 ~QueueEntry(); | 169 ~JobEntry(); |
170 | 170 |
171 // Compare the two entries, per job priorities. Used for sorting entries | 171 // Returns true when |left| is in higher priority than |right|. |
172 // from high priority to low priority. | 172 // Used for sorting entries from high priority to low priority. |
173 static bool Compare(const QueueEntry* left, const QueueEntry* right); | 173 static bool Less(const JobEntry& left, const JobEntry& right); |
174 | 174 |
175 JobID job_id; | 175 JobInfo job_info; |
176 | 176 |
177 // Context of the job. | 177 // Context of the job. |
178 DriveClientContext context; | 178 DriveClientContext context; |
179 | 179 |
180 // Resource ID to use for the operation. | 180 // Resource ID to use for the operation. |
181 // Used by: | 181 // Used by: |
182 // TYPE_GET_RESOURCE_ENTRY | 182 // TYPE_GET_RESOURCE_ENTRY |
183 // TYPE_DELETE_RESOURCE | 183 // TYPE_DELETE_RESOURCE |
184 // TYPE_RENAME_RESOURCE | 184 // TYPE_RENAME_RESOURCE |
185 // TYPE_ADD_RESOURCE_TO_DIRECTORY | 185 // TYPE_ADD_RESOURCE_TO_DIRECTORY |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 // TYPE_UPLOAD_NEW_FILE | 287 // TYPE_UPLOAD_NEW_FILE |
288 // TYPE_UPLOAD_EXISTING_FILE | 288 // TYPE_UPLOAD_EXISTING_FILE |
289 std::string content_type; | 289 std::string content_type; |
290 std::string etag; | 290 std::string etag; |
291 std::string title; | 291 std::string title; |
292 google_apis::UploadCompletionCallback upload_completion_callback; | 292 google_apis::UploadCompletionCallback upload_completion_callback; |
293 }; | 293 }; |
294 | 294 |
295 // Adds the specified job to the queue and starts the job loop for the queue | 295 // Adds the specified job to the queue and starts the job loop for the queue |
296 // if needed. Returns the job ID for the new job. | 296 // if needed. Returns the job ID for the new job. |
297 JobID StartNewJob(scoped_ptr<QueueEntry> job, JobType type); | 297 JobID StartNewJob(scoped_ptr<JobEntry> job); |
298 | 298 |
299 // Adds the specified job to the queue. Takes ownership of |job| | 299 // Adds the specified job to the queue. |
300 void QueueJob(scoped_ptr<QueueEntry> job); | 300 void QueueJob(JobID job_id); |
301 | 301 |
302 // Starts the job loop, if it is not already running. | 302 // Starts the job loop, if it is not already running. |
303 void StartJobLoop(QueueType queue_type); | 303 void StartJobLoop(QueueType queue_type); |
304 | 304 |
305 // Determines the next job that should run, and starts it. | 305 // Determines the next job that should run, and starts it. |
306 void DoJobLoop(QueueType queue_type); | 306 void DoJobLoop(QueueType queue_type); |
307 | 307 |
308 // Checks if operations should be suspended, such as if the network is | 308 // Checks if operations should be suspended, such as if the network is |
309 // disconnected. | 309 // disconnected. |
310 // | 310 // |
311 // Returns true when it should stop, and false if it should continue. | 311 // Returns true when it should stop, and false if it should continue. |
312 bool ShouldStopJobLoop(QueueType queue_type, | 312 bool ShouldStopJobLoop(QueueType queue_type, |
313 const DriveClientContext& context); | 313 const DriveClientContext& context); |
314 | 314 |
315 // Increases the throttle delay if it's below the maximum value, and posts a | 315 // Increases the throttle delay if it's below the maximum value, and posts a |
316 // task to continue the loop after the delay. | 316 // task to continue the loop after the delay. |
317 void ThrottleAndContinueJobLoop(QueueType queue_type); | 317 void ThrottleAndContinueJobLoop(QueueType queue_type); |
318 | 318 |
319 // Resets the throttle delay to the initial value, and continues the job loop. | 319 // Resets the throttle delay to the initial value, and continues the job loop. |
320 void ResetThrottleAndContinueJobLoop(QueueType queue_type); | 320 void ResetThrottleAndContinueJobLoop(QueueType queue_type); |
321 | 321 |
322 // Retries the |queue_entry| job if needed and returns false. | 322 // Retries the job if needed and returns false. Otherwise returns true. |
323 // Otherwise returns true. | 323 bool OnJobDone(JobID job_id, FileError error); |
324 bool OnJobDone(scoped_ptr<QueueEntry> queue_entry, FileError error); | |
325 | 324 |
326 // Callback for job finishing with a GetResourceListCallback. | 325 // Callback for job finishing with a GetResourceListCallback. |
327 void OnGetResourceListJobDone( | 326 void OnGetResourceListJobDone( |
328 scoped_ptr<QueueEntry> queue_entry, | 327 JobID job_id, |
| 328 const google_apis::GetResourceListCallback& callback, |
329 google_apis::GDataErrorCode error, | 329 google_apis::GDataErrorCode error, |
330 scoped_ptr<google_apis::ResourceList> resource_list); | 330 scoped_ptr<google_apis::ResourceList> resource_list); |
331 | 331 |
332 // Callback for job finishing with a GetResourceEntryCallback. | 332 // Callback for job finishing with a GetResourceEntryCallback. |
333 void OnGetResourceEntryJobDone( | 333 void OnGetResourceEntryJobDone( |
334 scoped_ptr<QueueEntry> queue_entry, | 334 JobID job_id, |
| 335 const google_apis::GetResourceEntryCallback& callback, |
335 google_apis::GDataErrorCode error, | 336 google_apis::GDataErrorCode error, |
336 scoped_ptr<google_apis::ResourceEntry> entry); | 337 scoped_ptr<google_apis::ResourceEntry> entry); |
337 | 338 |
338 // Callback for job finishing with a GetAboutResourceCallback. | 339 // Callback for job finishing with a GetAboutResourceCallback. |
339 void OnGetAboutResourceJobDone( | 340 void OnGetAboutResourceJobDone( |
340 scoped_ptr<QueueEntry> queue_entry, | 341 JobID job_id, |
| 342 const google_apis::GetAboutResourceCallback& callback, |
341 google_apis::GDataErrorCode error, | 343 google_apis::GDataErrorCode error, |
342 scoped_ptr<google_apis::AboutResource> about_resource); | 344 scoped_ptr<google_apis::AboutResource> about_resource); |
343 | 345 |
344 // Callback for job finishing with a GetAccountMetadataCallback. | 346 // Callback for job finishing with a GetAccountMetadataCallback. |
345 void OnGetAccountMetadataJobDone( | 347 void OnGetAccountMetadataJobDone( |
346 scoped_ptr<QueueEntry> queue_entry, | 348 JobID job_id, |
| 349 const google_apis::GetAccountMetadataCallback& callback, |
347 google_apis::GDataErrorCode error, | 350 google_apis::GDataErrorCode error, |
348 scoped_ptr<google_apis::AccountMetadata> account_metadata); | 351 scoped_ptr<google_apis::AccountMetadata> account_metadata); |
349 | 352 |
350 // Callback for job finishing with a GetAppListCallback. | 353 // Callback for job finishing with a GetAppListCallback. |
351 void OnGetAppListJobDone( | 354 void OnGetAppListJobDone( |
352 scoped_ptr<QueueEntry> queue_entry, | 355 JobID job_id, |
| 356 const google_apis::GetAppListCallback& callback, |
353 google_apis::GDataErrorCode error, | 357 google_apis::GDataErrorCode error, |
354 scoped_ptr<google_apis::AppList> app_list); | 358 scoped_ptr<google_apis::AppList> app_list); |
355 | 359 |
356 // Callback for job finishing with a EntryActionCallback. | 360 // Callback for job finishing with a EntryActionCallback. |
357 void OnEntryActionJobDone(scoped_ptr<QueueEntry> queue_entry, | 361 void OnEntryActionJobDone(JobID job_id, |
| 362 const google_apis::EntryActionCallback& callback, |
358 google_apis::GDataErrorCode error); | 363 google_apis::GDataErrorCode error); |
359 | 364 |
360 // Callback for job finishing with a DownloadActionCallback. | 365 // Callback for job finishing with a DownloadActionCallback. |
361 void OnDownloadActionJobDone(scoped_ptr<QueueEntry> queue_entry, | 366 void OnDownloadActionJobDone( |
362 google_apis::GDataErrorCode error, | 367 JobID job_id, |
363 const base::FilePath& temp_file); | 368 const google_apis::DownloadActionCallback& callback, |
| 369 google_apis::GDataErrorCode error, |
| 370 const base::FilePath& temp_file); |
364 | 371 |
365 // Callback for job finishing with a UploadCompletionCallback. | 372 // Callback for job finishing with a UploadCompletionCallback. |
366 void OnUploadCompletionJobDone( | 373 void OnUploadCompletionJobDone( |
367 scoped_ptr<QueueEntry> queue_entry, | 374 JobID job_id, |
| 375 const google_apis::UploadCompletionCallback& callback, |
368 google_apis::GDataErrorCode error, | 376 google_apis::GDataErrorCode error, |
369 const base::FilePath& drive_path, | 377 const base::FilePath& drive_path, |
370 const base::FilePath& file_path, | 378 const base::FilePath& file_path, |
371 scoped_ptr<google_apis::ResourceEntry> resource_entry); | 379 scoped_ptr<google_apis::ResourceEntry> resource_entry); |
372 | 380 |
373 // Updates the progress status of the specified job. | 381 // Updates the progress status of the specified job. |
374 void UpdateProgress(JobID job_id, int64 progress, int64 total); | 382 void UpdateProgress(JobID job_id, int64 progress, int64 total); |
375 | 383 |
376 // net::NetworkChangeNotifier::ConnectionTypeObserver override. | 384 // net::NetworkChangeNotifier::ConnectionTypeObserver override. |
377 virtual void OnConnectionTypeChanged( | 385 virtual void OnConnectionTypeChanged( |
(...skipping 21 matching lines...) Expand all Loading... |
399 | 407 |
400 // The number of times operations have failed in a row, capped at | 408 // The number of times operations have failed in a row, capped at |
401 // kMaxThrottleCount. This is used to calculate the delay before running the | 409 // kMaxThrottleCount. This is used to calculate the delay before running the |
402 // next task. | 410 // next task. |
403 int throttle_count_; | 411 int throttle_count_; |
404 | 412 |
405 // Disables throttling for testing. | 413 // Disables throttling for testing. |
406 bool disable_throttling_; | 414 bool disable_throttling_; |
407 | 415 |
408 // The queues of jobs. | 416 // The queues of jobs. |
409 std::list<QueueEntry*> queue_[NUM_QUEUES]; | 417 std::list<JobID> queue_[NUM_QUEUES]; |
410 | 418 |
411 // The list of unfinished (= queued or running) job info indexed by job IDs. | 419 // The list of unfinished (= queued or running) job info indexed by job IDs. |
412 typedef IDMap<JobInfo, IDMapOwnPointer> JobIDMap; | 420 typedef IDMap<JobEntry, IDMapOwnPointer> JobIDMap; |
413 JobIDMap job_map_; | 421 JobIDMap job_map_; |
414 | 422 |
415 // The list of observers for the scheduler. | 423 // The list of observers for the scheduler. |
416 ObserverList<JobListObserver> observer_list_; | 424 ObserverList<JobListObserver> observer_list_; |
417 | 425 |
418 google_apis::DriveServiceInterface* drive_service_; | 426 google_apis::DriveServiceInterface* drive_service_; |
419 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; | 427 scoped_ptr<google_apis::DriveUploaderInterface> uploader_; |
420 | 428 |
421 Profile* profile_; | 429 Profile* profile_; |
422 | 430 |
423 // Note: This should remain the last member so it'll be destroyed and | 431 // Note: This should remain the last member so it'll be destroyed and |
424 // invalidate its weak pointers before any other members are destroyed. | 432 // invalidate its weak pointers before any other members are destroyed. |
425 base::WeakPtrFactory<JobScheduler> weak_ptr_factory_; | 433 base::WeakPtrFactory<JobScheduler> weak_ptr_factory_; |
426 DISALLOW_COPY_AND_ASSIGN(JobScheduler); | 434 DISALLOW_COPY_AND_ASSIGN(JobScheduler); |
427 }; | 435 }; |
428 | 436 |
429 } // namespace drive | 437 } // namespace drive |
430 | 438 |
431 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_JOB_SCHEDULER_H_ | 439 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_JOB_SCHEDULER_H_ |
OLD | NEW |