| 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 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 5 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 10 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 11 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/drive/event_logger.h" | 14 #include "chrome/browser/drive/event_logger.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "google_apis/drive/drive_api_parser.h" | 17 #include "google_apis/drive/drive_api_parser.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 | 20 |
| 21 namespace drive { | 21 namespace drive { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| (...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 for (size_t i = 0; i < jobs.size(); ++i) { | 777 for (size_t i = 0; i < jobs.size(); ++i) { |
| 778 JobEntry* job = job_map_.Lookup(jobs[i]); | 778 JobEntry* job = job_map_.Lookup(jobs[i]); |
| 779 DCHECK(job); | 779 DCHECK(job); |
| 780 AbortNotRunningJob(job, google_apis::DRIVE_NO_CONNECTION); | 780 AbortNotRunningJob(job, google_apis::DRIVE_NO_CONNECTION); |
| 781 } | 781 } |
| 782 } | 782 } |
| 783 | 783 |
| 784 // Wait when throttled. | 784 // Wait when throttled. |
| 785 const base::Time now = base::Time::Now(); | 785 const base::Time now = base::Time::Now(); |
| 786 if (now < wait_until_) { | 786 if (now < wait_until_) { |
| 787 base::MessageLoopProxy::current()->PostDelayedTask( | 787 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 788 FROM_HERE, | 788 FROM_HERE, base::Bind(&JobScheduler::DoJobLoop, |
| 789 base::Bind(&JobScheduler::DoJobLoop, | 789 weak_ptr_factory_.GetWeakPtr(), queue_type), |
| 790 weak_ptr_factory_.GetWeakPtr(), | |
| 791 queue_type), | |
| 792 wait_until_ - now); | 790 wait_until_ - now); |
| 793 return; | 791 return; |
| 794 } | 792 } |
| 795 | 793 |
| 796 // Run the job with the highest priority in the queue. | 794 // Run the job with the highest priority in the queue. |
| 797 JobID job_id = -1; | 795 JobID job_id = -1; |
| 798 if (!queue_[queue_type]->PopForRun(accepted_priority, &job_id)) | 796 if (!queue_[queue_type]->PopForRun(accepted_priority, &job_id)) |
| 799 return; | 797 return; |
| 800 | 798 |
| 801 JobEntry* entry = job_map_.Lookup(job_id); | 799 JobEntry* entry = job_map_.Lookup(job_id); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 QueueJob(job_id); | 898 QueueJob(job_id); |
| 901 } else { | 899 } else { |
| 902 NotifyJobDone(*job_info, error); | 900 NotifyJobDone(*job_info, error); |
| 903 // The job has finished, no retry will happen in the scheduler. Now we can | 901 // The job has finished, no retry will happen in the scheduler. Now we can |
| 904 // remove the job info from the map. | 902 // remove the job info from the map. |
| 905 job_map_.Remove(job_id); | 903 job_map_.Remove(job_id); |
| 906 } | 904 } |
| 907 | 905 |
| 908 // Post a task to continue the job loop. This allows us to finish handling | 906 // Post a task to continue the job loop. This allows us to finish handling |
| 909 // the current job before starting the next one. | 907 // the current job before starting the next one. |
| 910 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 908 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 911 base::Bind(&JobScheduler::DoJobLoop, | 909 FROM_HERE, base::Bind(&JobScheduler::DoJobLoop, |
| 912 weak_ptr_factory_.GetWeakPtr(), | 910 weak_ptr_factory_.GetWeakPtr(), queue_type)); |
| 913 queue_type)); | |
| 914 return !should_retry; | 911 return !should_retry; |
| 915 } | 912 } |
| 916 | 913 |
| 917 void JobScheduler::OnGetFileListJobDone( | 914 void JobScheduler::OnGetFileListJobDone( |
| 918 JobID job_id, | 915 JobID job_id, |
| 919 const google_apis::FileListCallback& callback, | 916 const google_apis::FileListCallback& callback, |
| 920 google_apis::DriveApiErrorCode error, | 917 google_apis::DriveApiErrorCode error, |
| 921 scoped_ptr<google_apis::FileList> file_list) { | 918 scoped_ptr<google_apis::FileList> file_list) { |
| 922 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 919 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 923 DCHECK(!callback.is_null()); | 920 DCHECK(!callback.is_null()); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1131 job->job_info.ToString().c_str(), | 1128 job->job_info.ToString().c_str(), |
| 1132 DriveApiErrorCodeToString(error).c_str(), | 1129 DriveApiErrorCodeToString(error).c_str(), |
| 1133 base::Int64ToString(elapsed.InMilliseconds()).c_str(), | 1130 base::Int64ToString(elapsed.InMilliseconds()).c_str(), |
| 1134 GetQueueInfo(queue_type).c_str()); | 1131 GetQueueInfo(queue_type).c_str()); |
| 1135 | 1132 |
| 1136 base::Callback<void(google_apis::DriveApiErrorCode)> callback = | 1133 base::Callback<void(google_apis::DriveApiErrorCode)> callback = |
| 1137 job->abort_callback; | 1134 job->abort_callback; |
| 1138 queue_[GetJobQueueType(job->job_info.job_type)]->Remove(job->job_info.job_id); | 1135 queue_[GetJobQueueType(job->job_info.job_type)]->Remove(job->job_info.job_id); |
| 1139 NotifyJobDone(job->job_info, error); | 1136 NotifyJobDone(job->job_info, error); |
| 1140 job_map_.Remove(job->job_info.job_id); | 1137 job_map_.Remove(job->job_info.job_id); |
| 1141 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 1138 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1142 base::Bind(callback, error)); | 1139 base::Bind(callback, error)); |
| 1143 } | 1140 } |
| 1144 | 1141 |
| 1145 void JobScheduler::NotifyJobAdded(const JobInfo& job_info) { | 1142 void JobScheduler::NotifyJobAdded(const JobInfo& job_info) { |
| 1146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1147 FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobAdded(job_info)); | 1144 FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobAdded(job_info)); |
| 1148 } | 1145 } |
| 1149 | 1146 |
| 1150 void JobScheduler::NotifyJobDone(const JobInfo& job_info, | 1147 void JobScheduler::NotifyJobDone(const JobInfo& job_info, |
| 1151 google_apis::DriveApiErrorCode error) { | 1148 google_apis::DriveApiErrorCode error) { |
| 1152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1149 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1171 case FILE_QUEUE: | 1168 case FILE_QUEUE: |
| 1172 return "FILE_QUEUE"; | 1169 return "FILE_QUEUE"; |
| 1173 case NUM_QUEUES: | 1170 case NUM_QUEUES: |
| 1174 break; // This value is just a sentinel. Should never be used. | 1171 break; // This value is just a sentinel. Should never be used. |
| 1175 } | 1172 } |
| 1176 NOTREACHED(); | 1173 NOTREACHED(); |
| 1177 return ""; | 1174 return ""; |
| 1178 } | 1175 } |
| 1179 | 1176 |
| 1180 } // namespace drive | 1177 } // namespace drive |
| OLD | NEW |