Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: chrome/browser/chromeos/drive/drive_scheduler.cc

Issue 11142036: Add Move operation to the the scheduler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/drive_scheduler.h" 5 #include "chrome/browser/chromeos/drive/drive_scheduler.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/rand_util.h" 10 #include "base/rand_util.h"
(...skipping 16 matching lines...) Expand all
27 DriveScheduler::JobInfo::JobInfo(JobType in_job_type, FilePath in_file_path) 27 DriveScheduler::JobInfo::JobInfo(JobType in_job_type, FilePath in_file_path)
28 : job_type(in_job_type), 28 : job_type(in_job_type),
29 job_id(-1), 29 job_id(-1),
30 completed_bytes(0), 30 completed_bytes(0),
31 total_bytes(0), 31 total_bytes(0),
32 file_path(in_file_path), 32 file_path(in_file_path),
33 state(STATE_NONE) { 33 state(STATE_NONE) {
34 } 34 }
35 35
36 DriveScheduler::QueueEntry::QueueEntry(JobType in_job_type, 36 DriveScheduler::QueueEntry::QueueEntry(JobType in_job_type,
37 FilePath in_file_path) 37 FilePath in_file_path,
38 : job_info(TYPE_REMOVE, in_file_path) { 38 FileOperationCallback in_callback)
39 : job_info(in_job_type, in_file_path),
40 callback(in_callback),
41 is_recursive(false) {
39 } 42 }
40 43
41 DriveScheduler::QueueEntry::~QueueEntry() { 44 DriveScheduler::QueueEntry::~QueueEntry() {
42 } 45 }
43 46
44 DriveScheduler::RemoveJobPrivate::RemoveJobPrivate(
45 bool in_is_recursive,
46 FileOperationCallback in_callback)
47 : is_recursive(in_is_recursive),
48 callback(in_callback) {
49 }
50
51 DriveScheduler::RemoveJobPrivate::~RemoveJobPrivate() {
52 }
53
54 DriveScheduler::DriveScheduler(Profile* profile, 47 DriveScheduler::DriveScheduler(Profile* profile,
55 file_system::DriveOperations* drive_operations) 48 file_system::DriveOperations* drive_operations)
56 : job_loop_is_running_(false), 49 : job_loop_is_running_(false),
57 next_job_id_(0), 50 next_job_id_(0),
58 throttle_count_(0), 51 throttle_count_(0),
59 disable_throttling_(false), 52 disable_throttling_(false),
60 drive_operations_(drive_operations), 53 drive_operations_(drive_operations),
61 profile_(profile), 54 profile_(profile),
62 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 55 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
64 } 57 }
65 58
66 DriveScheduler::~DriveScheduler() { 59 DriveScheduler::~DriveScheduler() {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 61
69 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); 62 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
70 } 63 }
71 64
72 void DriveScheduler::Initialize() { 65 void DriveScheduler::Initialize() {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 67
75 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); 68 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
76 } 69 }
77 70
71 void DriveScheduler::Move(const FilePath& src_file_path,
72 const FilePath& dest_file_path,
73 const FileOperationCallback& callback) {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75
76 QueueEntry* new_job = new QueueEntry(TYPE_MOVE, src_file_path, callback);
satorux1 2012/10/16 04:34:01 This made me think how the object is deleted. This
Zachary Kuznia 2012/10/16 04:59:10 Done.
77 new_job->dest_file_path = dest_file_path;
78
79 QueueJob(new_job);
80
81 StartJobLoop();
82 }
78 83
79 void DriveScheduler::Remove(const FilePath& file_path, 84 void DriveScheduler::Remove(const FilePath& file_path,
80 bool is_recursive, 85 bool is_recursive,
81 const FileOperationCallback& callback) { 86 const FileOperationCallback& callback) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 88
84 QueueEntry* new_job = new QueueEntry(TYPE_REMOVE, file_path); 89 QueueEntry* new_job = new QueueEntry(TYPE_REMOVE, file_path, callback);
satorux1 2012/10/16 04:34:01 ditto.
Zachary Kuznia 2012/10/16 04:59:10 Done.
85 new_job->remove_private.reset(new RemoveJobPrivate(is_recursive, callback)); 90 new_job->is_recursive = is_recursive;
86 91
87 QueueJob(new_job); 92 QueueJob(new_job);
88 93
89 StartJobLoop(); 94 StartJobLoop();
90 } 95 }
91 96
92 int DriveScheduler::QueueJob(QueueEntry* job) { 97 int DriveScheduler::QueueJob(QueueEntry* job) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94 99
95 int job_id = next_job_id_; 100 int job_id = next_job_id_;
(...skipping 29 matching lines...) Expand all
125 int job_id = queue_.front(); 130 int job_id = queue_.front();
126 queue_.pop_front(); 131 queue_.pop_front();
127 132
128 JobMap::iterator job_iter = job_info_.find(job_id); 133 JobMap::iterator job_iter = job_info_.find(job_id);
129 DCHECK(job_iter != job_info_.end()); 134 DCHECK(job_iter != job_info_.end());
130 135
131 JobInfo& job_info = job_iter->second->job_info; 136 JobInfo& job_info = job_iter->second->job_info;
132 job_info.state = STATE_RUNNING; 137 job_info.state = STATE_RUNNING;
133 138
134 switch (job_info.job_type) { 139 switch (job_info.job_type) {
135 case TYPE_REMOVE: { 140 case TYPE_MOVE: {
136 DCHECK(job_iter->second->remove_private.get()); 141 drive_operations_->Move(
137
138 drive_operations_->Remove(
139 job_info.file_path, 142 job_info.file_path,
140 job_iter->second->remove_private->is_recursive, 143 job_iter->second->dest_file_path,
141 base::Bind(&DriveScheduler::OnRemoveDone, 144 base::Bind(&DriveScheduler::OnJobDone,
142 weak_ptr_factory_.GetWeakPtr(), 145 weak_ptr_factory_.GetWeakPtr(),
143 job_id)); 146 job_id));
144 } 147 }
148 break;
149
150 case TYPE_REMOVE: {
151 drive_operations_->Remove(
152 job_info.file_path,
153 job_iter->second->is_recursive,
154 base::Bind(&DriveScheduler::OnJobDone,
155 weak_ptr_factory_.GetWeakPtr(),
156 job_id));
157 }
145 break; 158 break;
satorux1 2012/10/16 04:34:01 Please add: default: NOTREACHED() << "Unknown j
Zachary Kuznia 2012/10/16 04:59:10 By leaving out a default case, the compiler will c
satorux1 2012/10/16 05:10:37 I see. Then please add some comment about it.
Zachary Kuznia 2012/11/07 05:46:17 Done.
146 } 159 }
147 160
148 } 161 }
149 162
150 bool DriveScheduler::ShouldStopJobLoop() { 163 bool DriveScheduler::ShouldStopJobLoop() {
151 // Should stop if the gdata feature was disabled while running the fetch 164 // Should stop if the gdata feature was disabled while running the fetch
152 // loop. 165 // loop.
153 if (profile_->GetPrefs()->GetBoolean(prefs::kDisableGData)) 166 if (profile_->GetPrefs()->GetBoolean(prefs::kDisableGData))
154 return true; 167 return true;
155 168
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 DCHECK(posted); 203 DCHECK(posted);
191 } 204 }
192 205
193 void DriveScheduler::ResetThrottleAndContinueJobLoop() { 206 void DriveScheduler::ResetThrottleAndContinueJobLoop() {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
195 208
196 throttle_count_ = 0; 209 throttle_count_ = 0;
197 DoJobLoop(); 210 DoJobLoop();
198 } 211 }
199 212
200 void DriveScheduler::OnRemoveDone(int job_id, DriveFileError error) { 213 void DriveScheduler::OnJobDone(int job_id, DriveFileError error) {
201 JobMap::iterator job_iter = job_info_.find(job_id); 214 JobMap::iterator job_iter = job_info_.find(job_id);
202 DCHECK(job_iter != job_info_.end()); 215 DCHECK(job_iter != job_info_.end());
203 216
204 // Retry, depending on the error. 217 // Retry, depending on the error.
205 if (error == DRIVE_FILE_ERROR_THROTTLED || 218 if (error == DRIVE_FILE_ERROR_THROTTLED ||
206 error == DRIVE_FILE_ERROR_NO_CONNECTION) { 219 error == DRIVE_FILE_ERROR_NO_CONNECTION) {
207 job_iter->second->job_info.state = STATE_RETRY; 220 job_iter->second->job_info.state = STATE_RETRY;
208 221
209 // Requeue the job. 222 // Requeue the job.
210 queue_.push_back(job_id); 223 queue_.push_back(job_id);
211 ThrottleAndContinueJobLoop(); 224 ThrottleAndContinueJobLoop();
212 } else { 225 } else {
213 DCHECK(job_iter->second->remove_private.get());
214
215 // Handle the callback. 226 // Handle the callback.
216 if (!job_iter->second->remove_private->callback.is_null()) { 227 if (!job_iter->second->callback.is_null()) {
217 MessageLoop::current()->PostTask(FROM_HERE, 228 MessageLoop::current()->PostTask(FROM_HERE,
218 base::Bind(job_iter->second->remove_private->callback, error)); 229 base::Bind(job_iter->second->callback, error));
219 } 230 }
220 231
221 // Delete the job. 232 // Delete the job.
222 job_info_.erase(job_id); 233 job_info_.erase(job_id);
223 ResetThrottleAndContinueJobLoop(); 234 ResetThrottleAndContinueJobLoop();
224 } 235 }
225 } 236 }
226 237
227 void DriveScheduler::OnConnectionTypeChanged( 238 void DriveScheduler::OnConnectionTypeChanged(
228 net::NetworkChangeNotifier::ConnectionType type) { 239 net::NetworkChangeNotifier::ConnectionType type) {
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 241
231 // Resume the job loop if the network is back online. Note that we don't 242 // Resume the job loop if the network is back online. Note that we don't
232 // need to check the type of the network as it will be checked in 243 // need to check the type of the network as it will be checked in
233 // ShouldStopJobLoop() as soon as the loop is resumed. 244 // ShouldStopJobLoop() as soon as the loop is resumed.
234 if (!net::NetworkChangeNotifier::IsOffline()) 245 if (!net::NetworkChangeNotifier::IsOffline())
235 StartJobLoop(); 246 StartJobLoop();
236 } 247 }
237 248
238 } // namespace drive 249 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698